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