00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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 );
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
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
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
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
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
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
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
00144 static QPair<QPointF, QPointF> Boundaries(
00145 TriangleBottomLeft,
00146 QPointF( TriangleBottomRight.x(), TriangleHeight ) );
00147 return Boundaries;
00148 }
00149