KD Chart API Documentation  3.1
KDChartTernaryLineDiagram.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
12 #include "KDChartTernaryLineDiagram_p.h"
13 
14 #include <limits>
15 
16 #include <QPainter>
17 
18 #include <KDChartPaintContext.h>
19 
21 #include "KDChartLineAttributes.h"
23 #include "KDChartPainterSaver_p.h"
24 #include "TernaryConstants.h"
25 #include "TernaryPoint.h"
26 
27 using namespace KDChart;
28 
29 #define d d_func()
30 
31 TernaryLineDiagram::Private::Private()
32  : AbstractTernaryDiagram::Private()
33 {
34 }
35 
36 TernaryLineDiagram::TernaryLineDiagram(QWidget *parent,
38  : AbstractTernaryDiagram(new Private(), parent, plane)
39 {
40  init();
41  setDatasetDimensionInternal(3); // the third column is implicit
42 
45  MarkerAttributes markerAttributes;
47  markerAttributes.setVisible(true);
48  dataValueAttributes.setMarkerAttributes(markerAttributes);
51  QVariant::fromValue(dataValueAttributes));
52 }
53 
55 {
56 }
57 
58 void TernaryLineDiagram::init()
59 {
60 }
61 
62 void TernaryLineDiagram::resize(const QSizeF &area)
63 {
64  Q_UNUSED(area);
65 }
66 
68 {
69  d->reverseMapper.clear();
70 
71  d->paint(paintContext);
72  // sanity checks:
73  if (model() == nullptr)
74  return;
75 
76  QPainter *p = paintContext->painter();
77  PainterSaver s(p);
78 
79  auto *plane =
80  ( TernaryCoordinatePlane * )paintContext->coordinatePlane();
81  Q_ASSERT(plane);
82 
83  qreal x, y, z;
84 
85  // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
87 
88  d->forgetAlreadyPaintedDataValues();
89 
90  int columnCount = model()->columnCount(rootIndex());
91  QPointF start;
92  for (int column = 0; column < columnCount; column += datasetDimension()) {
93  int numrows = model()->rowCount(rootIndex());
94  for (int row = 0; row < numrows; row++) {
95  // see if there is data otherwise skip
96  QModelIndex base = model()->index(row, column); // checked
97  if (!model()->data(base).isNull()) {
98  p->setPen(PrintingParameters::scalePen(pen(base)));
99  p->setBrush(brush(base));
100 
101  // retrieve data
102  x = qMax(model()->data(model()->index(row, column, rootIndex())).toReal(), // checked
103  ( qreal )0.0);
104  y = qMax(model()->data(model()->index(row, column + 1, rootIndex())).toReal(), // checked
105  ( qreal )0.0);
106  z = qMax(model()->data(model()->index(row, column + 2, rootIndex())).toReal(), // checked
107  ( qreal )0.0);
108 
109  qreal total = x + y + z;
110  if (fabs(total) > 3 * std::numeric_limits<qreal>::epsilon()) {
111  TernaryPoint tPunkt(x / total, y / total);
112  QPointF diagramLocation = translate(tPunkt);
113  QPointF widgetLocation = plane->translate(diagramLocation);
114 
115  if (row > 0) {
116  p->drawLine(start, widgetLocation);
117  }
118  paintMarker(p, model()->index(row, column, rootIndex()), widgetLocation); // checked
119  start = widgetLocation;
120  // retrieve text and data value attributes
121  // FIXME use data model DisplayRole text
122  QString text = tr("(%1, %2, %3)")
123  .arg(x * 100, 0, 'f', 0)
124  .arg(y * 100, 0, 'f', 0)
125  .arg(z * 100, 0, 'f', 0);
126  d->paintDataValueText(p, attrs, widgetLocation, true, text, true);
127  } else {
128  // ignore and do not paint this point, garbage data
129  qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
130  << x << "/" << y << "/" << z << "ignored, unusable.";
131  }
132  }
133  }
134  }
135 }
136 
137 const QPair<QPointF, QPointF> TernaryLineDiagram::calculateDataBoundaries() const
138 {
139  // this is a constant, because we defined it to be one:
140  static QPair<QPointF, QPointF> Boundaries(
142  QPointF(TriangleBottomRight.x(), TriangleHeight));
143  return Boundaries;
144 }
Declaring the class KDChart::DataValueAttributes.
const qreal TriangleHeight
const QPointF TriangleBottomLeft
const QPointF TriangleBottomRight
QPointF translate(const TernaryPoint &point)
virtual AttributesModel * attributesModel() const
virtual void paintMarker(QPainter *painter, const MarkerAttributes &markerAttributes, const QBrush &brush, const QPen &, const QPointF &point, const QSizeF &size)
DataValueAttributes dataValueAttributes() const
void setDatasetDimensionInternal(int dimension)
Base class for diagrams based on a ternary coordinate plane.
void setDefaultForRole(int role, const QVariant &value)
Diagram attributes dealing with data value labels.
void setMarkerAttributes(const MarkerAttributes &a)
A set of attributes controlling the appearance of data set markers.
Stores information about painting diagrams.
AbstractCoordinatePlane * coordinatePlane() const
QPainter * painter() const
static QPen scalePen(const QPen &pen)
const QPair< QPointF, QPointF > calculateDataBoundaries() const override
void resize(const QSizeF &area) override
void paint(PaintContext *paintContext) override
TernaryPoint defines a point within a ternary coordinate plane.
Definition: TernaryPoint.h:22
@ DataValueLabelAttributesRole

© 2001 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/
Generated by doxygen 1.9.1