KDChartTernaryLineDiagram.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 "KDChartTernaryLineDiagram.h"
00031 #include "KDChartTernaryLineDiagram_p.h"
00032 
00033 #include <limits>
00034 
00035 #include <QPainter>
00036 
00037 #include <KDChartPaintContext>
00038 
00039 #include "KDChartLineAttributes.h"
00040 #include "KDChartDataValueAttributes.h"
00041 #include "KDChartMarkerAttributes.h"
00042 #include "TernaryPoint.h"
00043 #include "TernaryConstants.h"
00044 #include "KDChartPainterSaver_p.h"
00045 
00046 using namespace KDChart;
00047 
00048 #define d d_func()
00049 
00050 TernaryLineDiagram::Private::Private()
00051     : AbstractTernaryDiagram::Private()
00052 {
00053 }
00054 
00055 TernaryLineDiagram::TernaryLineDiagram ( QWidget* parent,
00056                                          TernaryCoordinatePlane* plane )
00057     : AbstractTernaryDiagram( new Private(), parent, plane )
00058 {
00059     init();
00060     setDatasetDimensionInternal( 3 ); // the third column is implicit
00061 
00062     DataValueAttributes dataValueAttributes;
00063     dataValueAttributes.setVisible( true );
00064     MarkerAttributes markerAttributes;
00065     markerAttributes.setMarkerStyle( MarkerAttributes::MarkerCircle );
00066     markerAttributes.setVisible( true );
00067     dataValueAttributes.setMarkerAttributes( markerAttributes );
00068     attributesModel()->setDefaultForRole(
00069         KDChart::DataValueLabelAttributesRole,
00070         qVariantFromValue( dataValueAttributes ) );
00071 }
00072 
00073 TernaryLineDiagram::~TernaryLineDiagram()
00074 {
00075 }
00076 
00077 void TernaryLineDiagram::init()
00078 {
00079 }
00080 
00081 void  TernaryLineDiagram::resize (const QSizeF& area)
00082 {
00083     Q_UNUSED( area );
00084 }
00085 
00086 void  TernaryLineDiagram::paint (PaintContext *paintContext)
00087 {
00088     d->reverseMapper.clear();
00089 
00090     d->paint( paintContext );
00091     // sanity checks:
00092     if ( model() == 0 ) return;
00093 
00094     QPainter* p = paintContext->painter();
00095     PainterSaver s( p );
00096 
00097     TernaryCoordinatePlane* plane =
00098         (TernaryCoordinatePlane*) paintContext->coordinatePlane();
00099     Q_ASSERT( plane );
00100 
00101     double x, y, z;
00102 
00103 
00104     // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
00105     const DataValueAttributes attrs( dataValueAttributes() );
00106 
00107 
00108     d->clearListOfAlreadyDrawnDataValueTexts();
00109 
00110     int columnCount = model()->columnCount( rootIndex() );
00111     QPointF start;
00112     for(int column=0; column<columnCount; column+=datasetDimension() )
00113     {
00114         int numrows = model()->rowCount( rootIndex() );
00115         for( int row = 0; row < numrows; row++ )
00116         {
00117             // see if there is data otherwise skip
00118             QModelIndex base = model()->index( row, column );
00119             if( ! model()->data( base ).isNull() )
00120             {
00121                 p->setPen( PrintingParameters::scalePen( pen( base ) ) );
00122                 p->setBrush( brush( base ) );
00123 
00124                 // retrieve data
00125                 x = qMax( model()->data( model()->index( row, column, rootIndex() ) ).toDouble(),
00126                           0.0 );
00127                 y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toDouble(),
00128                           0.0 );
00129                 z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toDouble(),
00130                           0.0 );
00131 
00132                 double total = x + y + z;
00133                 if ( fabs( total ) > 3 * std::numeric_limits<double>::epsilon() ) {
00134                     TernaryPoint tPunkt( x / total, y / total );
00135                     QPointF diagramLocation = translate( tPunkt );
00136                     QPointF widgetLocation = plane->translate( diagramLocation );
00137 
00138                     if ( row > 0 ) {
00139                         p->drawLine( start, widgetLocation );
00140                     }
00141                     paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation );
00142                     start = widgetLocation;
00143                     // retrieve text and data value attributes
00144                     // FIXME use data model DisplayRole text
00145                     QString text = tr( "(%1, %2, %3)" )
00146                                    .arg( x * 100, 0, 'f', 0 )
00147                                    .arg( y * 100, 0, 'f', 0 )
00148                                    .arg( z * 100, 0, 'f', 0 );
00149                     d->paintDataValueText( this, p, attrs, widgetLocation, text, true );
00150                 } else {
00151                     // ignore and do not paint this point, garbage data
00152                     qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
00153                              << x << "/" << y << "/" << z << "ignored, unusable.";
00154                 }
00155             }
00156         }
00157     }
00158 }
00159 
00160 const QPair< QPointF, QPointF >  TernaryLineDiagram::calculateDataBoundaries () const
00161 {
00162     // this is a constant, because we defined it to be one:
00163     static QPair<QPointF, QPointF> Boundaries(
00164         TriangleBottomLeft,
00165         QPointF( TriangleBottomRight.x(), TriangleHeight ) );
00166     return Boundaries;
00167 }

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