KChart

KChartTernaryLineDiagram.cpp
1 /*
2  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "KChartTernaryLineDiagram.h"
10 #include "KChartTernaryLineDiagram_p.h"
11 
12 #include <limits>
13 
14 #include <QPainter>
15 
16 #include <KChartPaintContext.h>
17 
18 #include "KChartLineAttributes.h"
20 #include "KChartMarkerAttributes.h"
21 #include "TernaryPoint.h"
22 #include "TernaryConstants.h"
23 #include "KChartPainterSaver_p.h"
24 
25 using namespace KChart;
26 
27 #define d d_func()
28 
29 TernaryLineDiagram::Private::Private()
30  : AbstractTernaryDiagram::Private()
31 {
32 }
33 
34 TernaryLineDiagram::TernaryLineDiagram ( QWidget* parent,
35  TernaryCoordinatePlane* plane )
36  : AbstractTernaryDiagram( new Private(), parent, plane )
37 {
38  init();
39  setDatasetDimensionInternal( 3 ); // the third column is implicit
40 
41  DataValueAttributes dataValueAttributes;
42  dataValueAttributes.setVisible( true );
43  MarkerAttributes markerAttributes;
44  markerAttributes.setMarkerStyle( MarkerAttributes::MarkerCircle );
45  markerAttributes.setVisible( true );
46  dataValueAttributes.setMarkerAttributes( markerAttributes );
47  attributesModel()->setDefaultForRole(
48  KChart::DataValueLabelAttributesRole,
49  QVariant::fromValue( dataValueAttributes ) );
50 }
51 
52 TernaryLineDiagram::~TernaryLineDiagram()
53 {
54 }
55 
56 void TernaryLineDiagram::init()
57 {
58 }
59 
61 {
63 }
64 
66 {
67  d->reverseMapper.clear();
68 
69  d->paint( paintContext );
70  // sanity checks:
71  if ( model() == nullptr ) return;
72 
73  QPainter* p = paintContext->painter();
74  PainterSaver s( p );
75 
76  TernaryCoordinatePlane* plane =
77  (TernaryCoordinatePlane*) paintContext->coordinatePlane();
78  Q_ASSERT( plane );
79 
80  qreal x, y, z;
81 
82  // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
84 
85  d->forgetAlreadyPaintedDataValues();
86 
87  int columnCount = model()->columnCount( rootIndex() );
88  QPointF start;
89  for (int column=0; column<columnCount; column+=datasetDimension() )
90  {
91  int numrows = model()->rowCount( rootIndex() );
92  for ( int row = 0; row < numrows; row++ )
93  {
94  // see if there is data otherwise skip
95  QModelIndex base = model()->index( row, column ); // checked
96  if ( ! model()->data( base ).isNull() )
97  {
98  p->setPen( PrintingParameters::scalePen( pen( base ) ) );
99  p->setBrush( brush( base ) );
100 
101  // retrieve data
102  x = qMax<qreal>( model()->data( model()->index( row, column, rootIndex() ) ).toReal(), // checked
103  0.0 );
104  y = qMax<qreal>( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
105  0.0 );
106  z = qMax<qreal>( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
107  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)", "(x, y, z) values of the data point" )
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(
141  TriangleBottomLeft,
142  QPointF( TriangleBottomRight.x(), TriangleHeight ) );
143  return Boundaries;
144 }
void setMarkerAttributes(const MarkerAttributes &a)
Set the marker attributes to use for the data values.
void setPen(const QColor &color)
virtual int rowCount(const QModelIndex &parent) const const=0
QVariant fromValue(const T &value)
QAbstractItemModel * model() const const
Q_SCRIPTABLE Q_NOREPLY void start()
Declaring the class KChart::DataValueAttributes.
Class only listed here to document inheritance of some KChart classes.
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.
Stores information about painting diagrams.
void paint(PaintContext *paintContext) override
Draw the diagram contents to the rectangle and painter, that are passed in as part of the paint conte...
Diagram attributes dealing with data value labels.
void init(KXmlGuiWindow *window, KgDifficulty *difficulty=nullptr)
virtual int columnCount(const QModelIndex &parent) const const=0
DataValueAttributes dataValueAttributes() const
Retrieve the DataValueAttributes specified globally.
QModelIndex rootIndex() const const
int datasetDimension() const
The dataset dimension of a diagram determines how many value dimensions it expects each datapoint to ...
void setBrush(const QBrush &brush)
A set of attributes controlling the appearance of data set markers.
qreal x() const const
void setMarkerStyle(uint style)
Set the marker-style to use.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
TernaryPoint defines a point within a ternary coordinate plane.
Definition: TernaryPoint.h:19
void drawLine(const QLineF &line)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual void resize(const QSizeF &area)
Called by the widget's sizeEvent.
void setVisible(bool visible)
Set whether data value labels should be displayed.
QPen pen() const
Retrieve the pen to be used for painting datapoints globally.
QBrush brush() const
Retrieve the brush to be used for painting datapoints globally.
QString tr(const char *sourceText, const char *disambiguation, int n)
Base class for diagrams based on a ternary coordinate plane.
void resize(const QSizeF &area) override
Called by the widget's sizeEvent.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Jul 1 2022 05:09:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.