KChart

KChartTernaryPointDiagram.cpp
1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "KChartTernaryPointDiagram.h"
21 #include "KChartTernaryPointDiagram_p.h"
22 
23 #include <limits>
24 
25 #include <QPainter>
26 
27 #include <KChartPaintContext.h>
28 
29 #include "TernaryPoint.h"
30 #include "TernaryConstants.h"
31 
32 using namespace KChart;
33 
34 #define d d_func()
35 
36 TernaryPointDiagram::Private::Private()
37  : AbstractTernaryDiagram::Private()
38 {
39 }
40 
41 TernaryPointDiagram::TernaryPointDiagram ( QWidget* parent,
42  TernaryCoordinatePlane* plane )
43  : AbstractTernaryDiagram( new Private(), parent, plane )
44 {
45  init();
46  setDatasetDimensionInternal( 3 ); // the third column is implicit
47 }
48 
49 TernaryPointDiagram::~TernaryPointDiagram()
50 {
51 }
52 
53 void TernaryPointDiagram::init()
54 {
55  d->reverseMapper.setDiagram( this );
56 }
57 
59 {
61 }
62 
64 {
65  d->reverseMapper.clear();
66 
67  d->paint( paintContext );
68 
69  // sanity checks:
70  if ( model() == nullptr ) return;
71 
72  QPainter* p = paintContext->painter();
73  PainterSaver s( p );
74 
75  TernaryCoordinatePlane* plane =
76  static_cast< TernaryCoordinatePlane* >( paintContext->coordinatePlane() );
77  Q_ASSERT( plane );
78 
79  qreal x, y, z;
80 
81 
82  // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
84 
85  d->forgetAlreadyPaintedDataValues();
86 
87  int columnCount = model()->columnCount( rootIndex() );
88  for (int column=0; column<columnCount; column+=datasetDimension() )
89  {
90  int numrows = model()->rowCount( rootIndex() );
91  for ( int row = 0; row < numrows; row++ )
92  {
93  QModelIndex base = model()->index( row, column, rootIndex() ); // checked
94  // see if there is data otherwise skip
95  if ( ! model()->data( base ).isNull() )
96  {
97  p->setPen( PrintingParameters::scalePen( pen( base ) ) );
98  p->setBrush( brush( base ) );
99 
100  // retrieve data
101  x = qMax<qreal>( model()->data( model()->index( row, column+0, rootIndex() ) ).toReal(), // checked
102  0.0 );
103  y = qMax<qreal>( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
104  0.0 );
105  z = qMax<qreal>( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
106  0.0 );
107 
108  // fix messed up data values (paint as much as possible)
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  paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation ); // checked
116  QString text = tr( "(%1, %2, %3)", "(x, y, z) values of the data point" )
117  .arg( x * 100, 0, 'f', 0 )
118  .arg( y * 100, 0, 'f', 0 )
119  .arg( z * 100, 0, 'f', 0 );
120  d->paintDataValueText( p, attrs, widgetLocation, true, text, true );
121  } else {
122  // ignore and do not paint this point, garbage data
123  qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
124  << x << "/" << y << "/" << z << "ignored, unusable.";
125  }
126  }
127  }
128  }
129 }
130 
131 const QPair< QPointF, QPointF > TernaryPointDiagram::calculateDataBoundaries () const
132 {
133  // this is a constant, because we defined it to be one:
134  static QPair<QPointF, QPointF> Boundaries(
135  TriangleBottomLeft,
136  QPointF( TriangleBottomRight.x(), TriangleHeight ) );
137  return Boundaries;
138 }
139 
Class only listed here to document inheritance of some KChart classes.
virtual int rowCount(const QModelIndex &parent) const const =0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const =0
void resize(const QSizeF &area) override
Called by the widget&#39;s sizeEvent.
int y() const const
Diagram attributes dealing with data value labels.
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.
QString tr(const char *sourceText, const char *disambiguation, int n)
qreal x() const const
Stores information about painting diagrams.
int x() const const
void setPen(const QColor &color)
QPen pen() const
Retrieve the pen to be used for painting datapoints globally.
void setBrush(const QBrush &brush)
Base class for diagrams based on a ternary coordinate plane.
virtual QVariant data(const QModelIndex &index, int role) const const =0
TernaryPoint defines a point within a ternary coordinate plane.
Definition: TernaryPoint.h:30
QCA_EXPORT void init()
QModelIndex rootIndex() const const
QBrush brush() const
Retrieve the brush to be used for painting datapoints globally.
virtual void resize(const QSizeF &area)
Called by the widget&#39;s sizeEvent.
DataValueAttributes dataValueAttributes() const
Retrieve the DataValueAttributes specified globally.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
virtual int columnCount(const QModelIndex &parent) const const =0
qreal toReal(bool *ok) const const
QAbstractItemModel * model() const const
Global namespace.
QObject * parent() const const
int datasetDimension() const
The dataset dimension of a diagram determines how many value dimensions it expects each datapoint to ...
void paint(PaintContext *paintContext) override
Draw the diagram contents to the rectangle and painter, that are passed in as part of the paint conte...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Sep 22 2020 22:36:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.