KD Chart 2  [rev.2.5.1]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
KDChartTernaryLineDiagram.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
24 #include "KDChartTernaryLineDiagram_p.h"
25 
26 #include <limits>
27 
28 #include <QPainter>
29 
30 #include <KDChartPaintContext.h>
31 
32 #include "KDChartLineAttributes.h"
35 #include "TernaryPoint.h"
36 #include "TernaryConstants.h"
37 #include "KDChartPainterSaver_p.h"
38 
39 using namespace KDChart;
40 
41 #define d d_func()
42 
43 TernaryLineDiagram::Private::Private()
44  : AbstractTernaryDiagram::Private()
45 {
46 }
47 
48 TernaryLineDiagram::TernaryLineDiagram ( QWidget* parent,
49  TernaryCoordinatePlane* plane )
50  : AbstractTernaryDiagram( new Private(), parent, plane )
51 {
52  init();
53  setDatasetDimensionInternal( 3 ); // the third column is implicit
54 
56  dataValueAttributes.setVisible( true );
57  MarkerAttributes markerAttributes;
59  markerAttributes.setVisible( true );
60  dataValueAttributes.setMarkerAttributes( markerAttributes );
63  qVariantFromValue( dataValueAttributes ) );
64 }
65 
67 {
68 }
69 
70 void TernaryLineDiagram::init()
71 {
72 }
73 
74 void TernaryLineDiagram::resize (const QSizeF& area)
75 {
76  Q_UNUSED( area );
77 }
78 
80 {
81  d->reverseMapper.clear();
82 
83  d->paint( paintContext );
84  // sanity checks:
85  if ( model() == 0 ) return;
86 
87  QPainter* p = paintContext->painter();
88  PainterSaver s( p );
89 
90  TernaryCoordinatePlane* plane =
91  (TernaryCoordinatePlane*) paintContext->coordinatePlane();
92  Q_ASSERT( plane );
93 
94  qreal x, y, z;
95 
96  // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
98 
99  d->forgetAlreadyPaintedDataValues();
100 
101  int columnCount = model()->columnCount( rootIndex() );
102  QPointF start;
103  for (int column=0; column<columnCount; column+=datasetDimension() )
104  {
105  int numrows = model()->rowCount( rootIndex() );
106  for ( int row = 0; row < numrows; row++ )
107  {
108  // see if there is data otherwise skip
109  QModelIndex base = model()->index( row, column ); // checked
110  if ( ! model()->data( base ).isNull() )
111  {
112  p->setPen( PrintingParameters::scalePen( pen( base ) ) );
113  p->setBrush( brush( base ) );
114 
115  // retrieve data
116  x = qMax( model()->data( model()->index( row, column, rootIndex() ) ).toReal(), // checked
117  0.0 );
118  y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
119  0.0 );
120  z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
121  0.0 );
122 
123  qreal total = x + y + z;
124  if ( fabs( total ) > 3 * std::numeric_limits<qreal>::epsilon() ) {
125  TernaryPoint tPunkt( x / total, y / total );
126  QPointF diagramLocation = translate( tPunkt );
127  QPointF widgetLocation = plane->translate( diagramLocation );
128 
129  if ( row > 0 ) {
130  p->drawLine( start, widgetLocation );
131  }
132  paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation ); // checked
133  start = widgetLocation;
134  // retrieve text and data value attributes
135  // FIXME use data model DisplayRole text
136  QString text = tr( "(%1, %2, %3)" )
137  .arg( x * 100, 0, 'f', 0 )
138  .arg( y * 100, 0, 'f', 0 )
139  .arg( z * 100, 0, 'f', 0 );
140  d->paintDataValueText( p, attrs, widgetLocation, true, text, true );
141  } else {
142  // ignore and do not paint this point, garbage data
143  qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
144  << x << "/" << y << "/" << z << "ignored, unusable.";
145  }
146  }
147  }
148  }
149 }
150 
152 {
153  // this is a constant, because we defined it to be one:
154  static QPair<QPointF, QPointF> Boundaries(
156  QPointF( TriangleBottomRight.x(), TriangleHeight ) );
157  return Boundaries;
158 }

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/