KD Chart 2  [rev.2.7]
KDChartTernaryPointDiagram.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 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 "KDChartTernaryPointDiagram_p.h"
25 
26 #include <limits>
27 
28 #include <QPainter>
29 
30 #include <KDChartPaintContext.h>
31 
32 #include "TernaryPoint.h"
33 #include "TernaryConstants.h"
34 
35 using namespace KDChart;
36 
37 #define d d_func()
38 
39 TernaryPointDiagram::Private::Private()
40  : AbstractTernaryDiagram::Private()
41 {
42 }
43 
44 TernaryPointDiagram::TernaryPointDiagram ( QWidget* parent,
45  TernaryCoordinatePlane* plane )
46  : AbstractTernaryDiagram( new Private(), parent, plane )
47 {
48  init();
49  setDatasetDimensionInternal( 3 ); // the third column is implicit
50 }
51 
53 {
54 }
55 
56 void TernaryPointDiagram::init()
57 {
58  d->reverseMapper.setDiagram( this );
59 }
60 
61 void TernaryPointDiagram::resize (const QSizeF& area)
62 {
63  Q_UNUSED( area );
64 }
65 
67 {
68  d->reverseMapper.clear();
69 
70  d->paint( paintContext );
71 
72  // sanity checks:
73  if ( model() == 0 ) return;
74 
75  QPainter* p = paintContext->painter();
76  PainterSaver s( p );
77 
78  TernaryCoordinatePlane* plane =
79  static_cast< TernaryCoordinatePlane* >( paintContext->coordinatePlane() );
80  Q_ASSERT( plane );
81 
82  qreal x, y, z;
83 
84 
85  // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
87 
88  d->forgetAlreadyPaintedDataValues();
89 
90  int columnCount = model()->columnCount( rootIndex() );
91  for (int column=0; column<columnCount; column+=datasetDimension() )
92  {
93  int numrows = model()->rowCount( rootIndex() );
94  for ( int row = 0; row < numrows; row++ )
95  {
96  QModelIndex base = model()->index( row, column, rootIndex() ); // checked
97  // see if there is data otherwise skip
98  if ( ! model()->data( base ).isNull() )
99  {
100  p->setPen( PrintingParameters::scalePen( pen( base ) ) );
101  p->setBrush( brush( base ) );
102 
103  // retrieve data
104  x = qMax( model()->data( model()->index( row, column+0, rootIndex() ) ).toReal(), // checked
105  (qreal)0.0 );
106  y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
107  (qreal)0.0 );
108  z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
109  (qreal)0.0 );
110 
111  // fix messed up data values (paint as much as possible)
112  qreal total = x + y + z;
113  if ( fabs( total ) > 3 * std::numeric_limits<qreal>::epsilon() ) {
114  TernaryPoint tPunkt( x / total, y / total );
115  QPointF diagramLocation = translate( tPunkt );
116  QPointF widgetLocation = plane->translate( diagramLocation );
117 
118  paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation ); // checked
119  QString text = tr( "(%1, %2, %3)" )
120  .arg( x * 100, 0, 'f', 0 )
121  .arg( y * 100, 0, 'f', 0 )
122  .arg( z * 100, 0, 'f', 0 );
123  d->paintDataValueText( p, attrs, widgetLocation, true, text, true );
124  } else {
125  // ignore and do not paint this point, garbage data
126  qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
127  << x << "/" << y << "/" << z << "ignored, unusable.";
128  }
129  }
130  }
131  }
132 }
133 
135 {
136  // this is a constant, because we defined it to be one:
137  static QPair<QPointF, QPointF> Boundaries(
139  QPointF( TriangleBottomRight.x(), TriangleHeight ) );
140  return Boundaries;
141 }
142 
DataValueAttributes dataValueAttributes() const
Retrieve the DataValueAttributes specified globally.
Diagram attributes dealing with data value labels.
const QPointF TriangleBottomRight
AbstractCoordinatePlane * coordinatePlane() const
TernaryPoint defines a point within a ternary coordinate plane.
Definition: TernaryPoint.h:33
const qreal TriangleHeight
void resize(const QSizeF &area) override
Called by the widget&#39;s sizeEvent.
QPainter * painter() const
void setDatasetDimensionInternal(int dimension)
QBrush brush() const
Retrieve the brush to be used for painting datapoints globally.
const QPointF TriangleBottomLeft
virtual void paintMarker(QPainter *painter, const MarkerAttributes &markerAttributes, const QBrush &brush, const QPen &, const QPointF &point, const QSizeF &size)
Stores information about painting diagrams.
void paint(PaintContext *paintContext) override
Draw the diagram contents to the rectangle and painter, that are passed in as part of the paint conte...
Base class for diagrams based on a ternary coordinate plane.
QPointF translate(const TernaryPoint &point)
QPen pen() const
Retrieve the pen to be used for painting datapoints globally.
static QPen scalePen(const QPen &pen)
Class only listed here to document inheritance of some KDChart classes.
const QPair< QPointF, QPointF > calculateDataBoundaries() const override
int datasetDimension() const
The dataset dimension of a diagram determines how many value dimensions it expects each datapoint to ...
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/