00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "KDChartTernaryPointDiagram.h"
00024 #include "KDChartTernaryPointDiagram_p.h"
00025
00026 #include <limits>
00027
00028 #include <QPainter>
00029
00030 #include <KDChartPaintContext>
00031
00032 #include "TernaryPoint.h"
00033 #include "TernaryConstants.h"
00034
00035 using namespace KDChart;
00036
00037 #define d d_func()
00038
00039 TernaryPointDiagram::Private::Private()
00040 : AbstractTernaryDiagram::Private()
00041 {
00042 }
00043
00044 TernaryPointDiagram::TernaryPointDiagram ( QWidget* parent,
00045 TernaryCoordinatePlane* plane )
00046 : AbstractTernaryDiagram( new Private(), parent, plane )
00047 {
00048 init();
00049 setDatasetDimensionInternal( 3 );
00050 }
00051
00052 TernaryPointDiagram::~TernaryPointDiagram()
00053 {
00054 }
00055
00056 void TernaryPointDiagram::init()
00057 {
00058 d->reverseMapper.setDiagram( this );
00059 }
00060
00061 void TernaryPointDiagram::resize (const QSizeF& area)
00062 {
00063 Q_UNUSED( area );
00064 }
00065
00066 void TernaryPointDiagram::paint (PaintContext *paintContext)
00067 {
00068 d->reverseMapper.clear();
00069
00070 d->paint( paintContext );
00071
00072
00073 if ( model() == 0 ) return;
00074
00075 QPainter* p = paintContext->painter();
00076 PainterSaver s( p );
00077
00078 TernaryCoordinatePlane* plane =
00079 (TernaryCoordinatePlane*) paintContext->coordinatePlane();
00080 Q_ASSERT( plane );
00081
00082 double x, y, z;
00083
00084
00085
00086 const DataValueAttributes attrs( dataValueAttributes() );
00087
00088 d->clearListOfAlreadyDrawnDataValueTexts();
00089
00090 int columnCount = model()->columnCount( rootIndex() );
00091 for(int column=0; column<columnCount; column+=datasetDimension() )
00092 {
00093 int numrows = model()->rowCount( rootIndex() );
00094 for( int row = 0; row < numrows; row++ )
00095 {
00096 QModelIndex base = model()->index( row, column, rootIndex() );
00097
00098 if( ! model()->data( model()->index( row, column+0, rootIndex() ) ).isNull() )
00099 {
00100 p->setPen( PrintingParameters::scalePen( pen( base ) ) );
00101 p->setBrush( brush( base ) );
00102
00103
00104 x = qMax( model()->data( model()->index( row, column+0, rootIndex() ) ).toDouble(),
00105 0.0 );
00106 y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toDouble(),
00107 0.0 );
00108 z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toDouble(),
00109 0.0 );
00110
00111
00112 double total = x + y + z;
00113 if ( fabs( total ) > 3 * std::numeric_limits<double>::epsilon() ) {
00114 TernaryPoint tPunkt( x / total, y / total );
00115 QPointF diagramLocation = translate( tPunkt );
00116 QPointF widgetLocation = plane->translate( diagramLocation );
00117
00118 paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation );
00119 QString text = tr( "(%1, %2, %3)" )
00120 .arg( x * 100, 0, 'f', 0 )
00121 .arg( y * 100, 0, 'f', 0 )
00122 .arg( z * 100, 0, 'f', 0 );
00123 d->paintDataValueText( this, p, attrs, widgetLocation, text, true );
00124 } else {
00125
00126 qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
00127 << x << "/" << y << "/" << z << "ignored, unusable.";
00128 }
00129 }
00130 }
00131 }
00132 }
00133
00134 const QPair< QPointF, QPointF > TernaryPointDiagram::calculateDataBoundaries () const
00135 {
00136
00137 static QPair<QPointF, QPointF> Boundaries(
00138 TriangleBottomLeft,
00139 QPointF( TriangleBottomRight.x(), TriangleHeight ) );
00140 return Boundaries;
00141 }
00142