KDChartTernaryPointDiagram.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002    KDChart - a multi-platform charting engine
00003    */
00004 
00005 /****************************************************************************
00006  ** Copyright (C) 2005-2007 Klarälvdalens Datakonsult AB.  All rights reserved.
00007  **
00008  ** This file is part of the KD Chart library.
00009  **
00010  ** This file may be distributed and/or modified under the terms of the
00011  ** GNU General Public License version 2 as published by the Free Software
00012  ** Foundation and appearing in the file LICENSE.GPL included in the
00013  ** packaging of this file.
00014  **
00015  ** Licensees holding valid commercial KD Chart licenses may use this file in
00016  ** accordance with the KD Chart Commercial License Agreement provided with
00017  ** the Software.
00018  **
00019  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021  **
00022  ** See http://www.kdab.net/kdchart for
00023  **   information about KD Chart Commercial License Agreements.
00024  **
00025  ** Contact info@kdab.net if any conditions of this
00026  ** licensing are not clear to you.
00027  **
00028  **********************************************************************/
00029 
00030 #include "KDChartTernaryPointDiagram.h"
00031 #include "KDChartTernaryPointDiagram_p.h"
00032 
00033 #include <limits>
00034 
00035 #include <QPainter>
00036 
00037 #include <KDChartPaintContext>
00038 
00039 #include "TernaryPoint.h"
00040 #include "TernaryConstants.h"
00041 
00042 using namespace KDChart;
00043 
00044 #define d d_func()
00045 
00046 TernaryPointDiagram::Private::Private()
00047     : AbstractTernaryDiagram::Private()
00048 {
00049 }
00050 
00051 TernaryPointDiagram::TernaryPointDiagram ( QWidget* parent,
00052                                            TernaryCoordinatePlane* plane )
00053     : AbstractTernaryDiagram( new Private(), parent, plane )
00054 {
00055     init();
00056     setDatasetDimensionInternal( 3 ); // the third column is implicit
00057 }
00058 
00059 TernaryPointDiagram::~TernaryPointDiagram()
00060 {
00061 }
00062 
00063 void TernaryPointDiagram::init()
00064 {
00065     d->reverseMapper.setDiagram( this );
00066 }
00067 
00068 void  TernaryPointDiagram::resize (const QSizeF& area)
00069 {
00070     Q_UNUSED( area );
00071 }
00072 
00073 void  TernaryPointDiagram::paint (PaintContext *paintContext)
00074 {
00075     d->reverseMapper.clear();
00076 
00077     d->paint( paintContext );
00078 
00079     // sanity checks:
00080     if ( model() == 0 ) return;
00081 
00082     QPainter* p = paintContext->painter();
00083     PainterSaver s( p );
00084 
00085     TernaryCoordinatePlane* plane =
00086         (TernaryCoordinatePlane*) paintContext->coordinatePlane();
00087     Q_ASSERT( plane );
00088 
00089     double x, y, z;
00090 
00091 
00092     // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
00093     const DataValueAttributes attrs( dataValueAttributes() );
00094 
00095     d->clearListOfAlreadyDrawnDataValueTexts();
00096 
00097     int columnCount = model()->columnCount( rootIndex() );
00098     for(int column=0; column<columnCount; column+=datasetDimension() )
00099     {
00100         int numrows = model()->rowCount( rootIndex() );
00101         for( int row = 0; row < numrows; row++ )
00102         {
00103             QModelIndex base = model()->index( row, column, rootIndex() );
00104             // see if there is data otherwise skip
00105             if( ! model()->data( model()->index( row, column+0, rootIndex() ) ).isNull() )
00106             {
00107                 p->setPen( PrintingParameters::scalePen( pen( base ) ) );
00108                 p->setBrush( brush( base ) );
00109 
00110                 // retrieve data
00111                 x = qMax( model()->data( model()->index( row, column+0, rootIndex() ) ).toDouble(),
00112                           0.0 );
00113                 y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toDouble(),
00114                           0.0 );
00115                 z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toDouble(),
00116                           0.0 );
00117 
00118                 // fix messed up data values (paint as much as possible)
00119                 double total = x + y + z;
00120                 if ( fabs( total ) > 3 * std::numeric_limits<double>::epsilon() ) {
00121                     TernaryPoint tPunkt( x / total, y / total );
00122                     QPointF diagramLocation = translate( tPunkt );
00123                     QPointF widgetLocation = plane->translate( diagramLocation );
00124 
00125                     paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation );
00126                     QString text = tr( "(%1, %2, %3)" )
00127                                    .arg( x * 100, 0, 'f', 0 )
00128                                    .arg( y * 100, 0, 'f', 0 )
00129                                    .arg( z * 100, 0, 'f', 0 );
00130                     d->paintDataValueText( this, p, attrs, widgetLocation, text, true );
00131                 } else {
00132                     // ignore and do not paint this point, garbage data
00133                     qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
00134                              << x << "/" << y << "/" << z << "ignored, unusable.";
00135                 }
00136             }
00137         }
00138     }
00139 }
00140 
00141 const QPair< QPointF, QPointF >  TernaryPointDiagram::calculateDataBoundaries () const
00142 {
00143     // this is a constant, because we defined it to be one:
00144     static QPair<QPointF, QPointF> Boundaries(
00145         TriangleBottomLeft,
00146         QPointF( TriangleBottomRight.x(), TriangleHeight ) );
00147     return Boundaries;
00148 }
00149 

Generated on Thu Mar 4 23:19:12 2010 for KD Chart 2 by  doxygen 1.5.4