KD Chart API Documentation  3.1
KDChartTernaryPointDiagram.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 "KDChartTernaryPointDiagram_p.h"
13 
14 #include <limits>
15 
16 #include <QPainter>
17 
18 #include <KDChartPaintContext.h>
19 
20 #include "TernaryConstants.h"
21 #include "TernaryPoint.h"
22 
23 using namespace KDChart;
24 
25 #define d d_func()
26 
27 TernaryPointDiagram::Private::Private()
28  : AbstractTernaryDiagram::Private()
29 {
30 }
31 
32 TernaryPointDiagram::TernaryPointDiagram(QWidget *parent,
34  : AbstractTernaryDiagram(new Private(), parent, plane)
35 {
36  init();
37  setDatasetDimensionInternal(3); // the third column is implicit
38 }
39 
41 {
42 }
43 
44 void TernaryPointDiagram::init()
45 {
46  d->reverseMapper.setDiagram(this);
47 }
48 
49 void TernaryPointDiagram::resize(const QSizeF &area)
50 {
51  Q_UNUSED(area);
52 }
53 
55 {
56  d->reverseMapper.clear();
57 
58  d->paint(paintContext);
59 
60  // sanity checks:
61  if (model() == nullptr)
62  return;
63 
64  QPainter *p = paintContext->painter();
65  PainterSaver s(p);
66 
67  auto *plane =
68  static_cast<TernaryCoordinatePlane *>(paintContext->coordinatePlane());
69  Q_ASSERT(plane);
70 
71  qreal x, y, z;
72 
73  // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
75 
76  d->forgetAlreadyPaintedDataValues();
77 
78  int columnCount = model()->columnCount(rootIndex());
79  for (int column = 0; column < columnCount; column += datasetDimension()) {
80  int numrows = model()->rowCount(rootIndex());
81  for (int row = 0; row < numrows; row++) {
82  QModelIndex base = model()->index(row, column, rootIndex()); // checked
83  // see if there is data otherwise skip
84  if (!model()->data(base).isNull()) {
85  p->setPen(PrintingParameters::scalePen(pen(base)));
86  p->setBrush(brush(base));
87 
88  // retrieve data
89  x = qMax(model()->data(model()->index(row, column + 0, rootIndex())).toReal(), // checked
90  ( qreal )0.0);
91  y = qMax(model()->data(model()->index(row, column + 1, rootIndex())).toReal(), // checked
92  ( qreal )0.0);
93  z = qMax(model()->data(model()->index(row, column + 2, rootIndex())).toReal(), // checked
94  ( qreal )0.0);
95 
96  // fix messed up data values (paint as much as possible)
97  qreal total = x + y + z;
98  if (fabs(total) > 3 * std::numeric_limits<qreal>::epsilon()) {
99  TernaryPoint tPunkt(x / total, y / total);
100  QPointF diagramLocation = translate(tPunkt);
101  QPointF widgetLocation = plane->translate(diagramLocation);
102 
103  paintMarker(p, model()->index(row, column, rootIndex()), widgetLocation); // checked
104  QString text = tr("(%1, %2, %3)")
105  .arg(x * 100, 0, 'f', 0)
106  .arg(y * 100, 0, 'f', 0)
107  .arg(z * 100, 0, 'f', 0);
108  d->paintDataValueText(p, attrs, widgetLocation, true, text, true);
109  } else {
110  // ignore and do not paint this point, garbage data
111  qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
112  << x << "/" << y << "/" << z << "ignored, unusable.";
113  }
114  }
115  }
116  }
117 }
118 
119 const QPair<QPointF, QPointF> TernaryPointDiagram::calculateDataBoundaries() const
120 {
121  // this is a constant, because we defined it to be one:
122  static QPair<QPointF, QPointF> Boundaries(
124  QPointF(TriangleBottomRight.x(), TriangleHeight));
125  return Boundaries;
126 }
const qreal TriangleHeight
const QPointF TriangleBottomLeft
const QPointF TriangleBottomRight
QPointF translate(const TernaryPoint &point)
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.
Diagram attributes dealing with data value labels.
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

© 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