KD Chart 2  [rev.2.7]
KDChartTernaryLineDiagram.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 "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  (qreal)0.0 );
118  y = qMax( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
119  (qreal)0.0 );
120  z = qMax( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
121  (qreal)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 }
void paint(PaintContext *paintContext) override
Draw the diagram contents to the rectangle and painter, that are passed in as part of the paint conte...
DataValueAttributes dataValueAttributes() const
Retrieve the DataValueAttributes specified globally.
const QPair< QPointF, QPointF > calculateDataBoundaries() const override
void setVisible(bool visible)
Set whether data value labels should be displayed.
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
Declaring the class KDChart::DataValueAttributes.
void resize(const QSizeF &area) override
Called by the widget&#39;s sizeEvent.
QPainter * painter() const
virtual AttributesModel * attributesModel() const
Returns the AttributesModel, that is used by this diagram.
void setMarkerAttributes(const MarkerAttributes &a)
Set the marker attributes to use for the data values.
void setDatasetDimensionInternal(int dimension)
void setDefaultForRole(int role, const QVariant &value)
Define the default value for a certain role.
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)
void setMarkerStyle(uint style)
Set the marker-style to use.
Stores information about painting diagrams.
Base class for diagrams based on a ternary coordinate plane.
QPointF translate(const TernaryPoint &point)
A set of attributes controlling the appearance of data set markers.
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.
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/