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 
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 }
KDChart::AbstractDiagram::datasetDimension
int datasetDimension() const
The dataset dimension of a diagram determines how many value dimensions it expects each datapoint to ...
Definition: KDChartAbstractDiagram.cpp:961
QWidget
Class only listed here to document inheritance of some KDChart classes.
translate
QPointF translate(const TernaryPoint &point)
Definition: TernaryPoint.cpp:86
KDChart::TernaryLineDiagram::~TernaryLineDiagram
~TernaryLineDiagram() override
Definition: KDChartTernaryLineDiagram.cpp:66
KDChart::MarkerAttributes
A set of attributes controlling the appearance of data set markers.
Definition: KDChartMarkerAttributes.h:44
TriangleBottomLeft
const QPointF TriangleBottomLeft
KDChart::DataValueLabelAttributesRole
@ DataValueLabelAttributesRole
Definition: KDChartGlobal.h:192
KDChart::MarkerAttributes::MarkerCircle
@ MarkerCircle
Definition: KDChartMarkerAttributes.h:52
KDChart::PrintingParameters::scalePen
static QPen scalePen(const QPen &pen)
Definition: KDChartPrintingParameters.cpp:48
KDChart::AbstractDiagram::pen
QPen pen() const
Retrieve the pen to be used for painting datapoints globally.
Definition: KDChartAbstractDiagram.cpp:668
KDChart
Definition: KDChartAbstractCartesianDiagram.h:30
KDChart::PaintContext::coordinatePlane
AbstractCoordinatePlane * coordinatePlane() const
Definition: KDChartPaintContext.cpp:78
TriangleBottomRight
const QPointF TriangleBottomRight
KDChartPaintContext.h
KDChart::MarkerAttributes::setMarkerStyle
void setMarkerStyle(uint style)
Set the marker-style to use.
Definition: KDChartMarkerAttributes.cpp:142
KDChart::AbstractDiagram::setDatasetDimensionInternal
void setDatasetDimensionInternal(int dimension)
Definition: KDChartAbstractDiagram.cpp:973
KDChart::PaintContext
Stores information about painting diagrams.
Definition: KDChartPaintContext.h:42
KDChart::TernaryLineDiagram::calculateDataBoundaries
const QPair< QPointF, QPointF > calculateDataBoundaries() const override
Definition: KDChartTernaryLineDiagram.cpp:151
KDChart::TernaryLineDiagram::paint
void paint(PaintContext *paintContext) override
Draw the diagram contents to the rectangle and painter, that are passed in as part of the paint conte...
Definition: KDChartTernaryLineDiagram.cpp:79
KDChart::PaintContext::painter
QPainter * painter() const
Definition: KDChartPaintContext.cpp:68
KDChartMarkerAttributes.h
KDChart::AbstractTernaryDiagram
Base class for diagrams based on a ternary coordinate plane.
Definition: KDChartAbstractTernaryDiagram.h:38
KDChart::AbstractDiagram::paintMarker
virtual void paintMarker(QPainter *painter, const MarkerAttributes &markerAttributes, const QBrush &brush, const QPen &, const QPointF &point, const QSizeF &size)
Definition: KDChartAbstractDiagram.cpp:472
KDChart::TernaryLineDiagram::resize
void resize(const QSizeF &area) override
Called by the widget's sizeEvent.
Definition: KDChartTernaryLineDiagram.cpp:74
KDChartTernaryLineDiagram.h
KDChartLineAttributes.h
KDChart::AbstractDiagram::attributesModel
virtual AttributesModel * attributesModel() const
Returns the AttributesModel, that is used by this diagram.
Definition: KDChartAbstractDiagram.cpp:198
TernaryConstants.h
KDChart::AbstractDiagram::dataValueAttributes
DataValueAttributes dataValueAttributes() const
Retrieve the DataValueAttributes specified globally.
Definition: KDChartAbstractDiagram.cpp:325
TernaryPoint
TernaryPoint defines a point within a ternary coordinate plane.
Definition: TernaryPoint.h:34
KDChart::MarkerAttributes::setVisible
void setVisible(bool visible)
Definition: KDChartMarkerAttributes.cpp:112
KDChart::DataValueAttributes::setMarkerAttributes
void setMarkerAttributes(const MarkerAttributes &a)
Set the marker attributes to use for the data values.
Definition: KDChartDataValueAttributes.cpp:199
KDChartDataValueAttributes.h
Declaring the class KDChart::DataValueAttributes.
TernaryPoint.h
QPair
Definition: KDChartWidget.h:35
KDChart::AttributesModel::setDefaultForRole
void setDefaultForRole(int role, const QVariant &value)
Define the default value for a certain role.
Definition: KDChartAttributesModel.cpp:691
KDChart::TernaryCoordinatePlane::translate
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.
Definition: KDChartTernaryCoordinatePlane.cpp:135
KDChart::DataValueAttributes::setVisible
void setVisible(bool visible)
Set whether data value labels should be displayed.
Definition: KDChartDataValueAttributes.cpp:159
KDChart::DataValueAttributes
Diagram attributes dealing with data value labels.
Definition: KDChartDataValueAttributes.h:56
d
#define d
Definition: KDChartTernaryLineDiagram.cpp:41
TriangleHeight
const qreal TriangleHeight
KDChart::TernaryCoordinatePlane
Ternary coordinate plane.
Definition: KDChartTernaryCoordinatePlane.h:37
KDChart::AbstractDiagram::brush
QBrush brush() const
Retrieve the brush to be used for painting datapoints globally.
Definition: KDChartAbstractDiagram.cpp:709

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/