KChart

KChartTernaryLineDiagram.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 "KChartTernaryLineDiagram.h"
21 #include "KChartTernaryLineDiagram_p.h"
22 
23 #include <limits>
24 
25 #include <QPainter>
26 
27 #include <KChartPaintContext.h>
28 
29 #include "KChartLineAttributes.h"
31 #include "KChartMarkerAttributes.h"
32 #include "TernaryPoint.h"
33 #include "TernaryConstants.h"
34 #include "KChartPainterSaver_p.h"
35 
36 using namespace KChart;
37 
38 #define d d_func()
39 
40 TernaryLineDiagram::Private::Private()
41  : AbstractTernaryDiagram::Private()
42 {
43 }
44 
45 TernaryLineDiagram::TernaryLineDiagram ( QWidget* parent,
46  TernaryCoordinatePlane* plane )
47  : AbstractTernaryDiagram( new Private(), parent, plane )
48 {
49  init();
50  setDatasetDimensionInternal( 3 ); // the third column is implicit
51 
52  DataValueAttributes dataValueAttributes;
53  dataValueAttributes.setVisible( true );
54  MarkerAttributes markerAttributes;
55  markerAttributes.setMarkerStyle( MarkerAttributes::MarkerCircle );
56  markerAttributes.setVisible( true );
57  dataValueAttributes.setMarkerAttributes( markerAttributes );
58  attributesModel()->setDefaultForRole(
59  KChart::DataValueLabelAttributesRole,
60  QVariant::fromValue( dataValueAttributes ) );
61 }
62 
63 TernaryLineDiagram::~TernaryLineDiagram()
64 {
65 }
66 
67 void TernaryLineDiagram::init()
68 {
69 }
70 
72 {
74 }
75 
77 {
78  d->reverseMapper.clear();
79 
80  d->paint( paintContext );
81  // sanity checks:
82  if ( model() == nullptr ) return;
83 
84  QPainter* p = paintContext->painter();
85  PainterSaver s( p );
86 
87  TernaryCoordinatePlane* plane =
88  (TernaryCoordinatePlane*) paintContext->coordinatePlane();
89  Q_ASSERT( plane );
90 
91  qreal x, y, z;
92 
93  // for some reason(?) TernaryPointDiagram is using per-diagram DVAs only:
94  const DataValueAttributes attrs( dataValueAttributes() );
95 
96  d->forgetAlreadyPaintedDataValues();
97 
98  int columnCount = model()->columnCount( rootIndex() );
99  QPointF start;
100  for (int column=0; column<columnCount; column+=datasetDimension() )
101  {
102  int numrows = model()->rowCount( rootIndex() );
103  for ( int row = 0; row < numrows; row++ )
104  {
105  // see if there is data otherwise skip
106  QModelIndex base = model()->index( row, column ); // checked
107  if ( ! model()->data( base ).isNull() )
108  {
109  p->setPen( PrintingParameters::scalePen( pen( base ) ) );
110  p->setBrush( brush( base ) );
111 
112  // retrieve data
113  x = qMax<qreal>( model()->data( model()->index( row, column, rootIndex() ) ).toReal(), // checked
114  0.0 );
115  y = qMax<qreal>( model()->data( model()->index( row, column+1, rootIndex() ) ).toReal(), // checked
116  0.0 );
117  z = qMax<qreal>( model()->data( model()->index( row, column+2, rootIndex() ) ).toReal(), // checked
118  0.0 );
119 
120  qreal total = x + y + z;
121  if ( fabs( total ) > 3 * std::numeric_limits<qreal>::epsilon() ) {
122  TernaryPoint tPunkt( x / total, y / total );
123  QPointF diagramLocation = translate( tPunkt );
124  QPointF widgetLocation = plane->translate( diagramLocation );
125 
126  if ( row > 0 ) {
127  p->drawLine( start, widgetLocation );
128  }
129  paintMarker( p, model()->index( row, column, rootIndex() ), widgetLocation ); // checked
130  start = widgetLocation;
131  // retrieve text and data value attributes
132  // FIXME use data model DisplayRole text
133  QString text = tr( "(%1, %2, %3)", "(x, y, z) values of the data point" )
134  .arg( x * 100, 0, 'f', 0 )
135  .arg( y * 100, 0, 'f', 0 )
136  .arg( z * 100, 0, 'f', 0 );
137  d->paintDataValueText( p, attrs, widgetLocation, true, text, true );
138  } else {
139  // ignore and do not paint this point, garbage data
140  qDebug() << "TernaryPointDiagram::paint: data point x/y/z:"
141  << x << "/" << y << "/" << z << "ignored, unusable.";
142  }
143  }
144  }
145  }
146 }
147 
148 const QPair< QPointF, QPointF > TernaryLineDiagram::calculateDataBoundaries () const
149 {
150  // this is a constant, because we defined it to be one:
151  static QPair<QPointF, QPointF> Boundaries(
152  TriangleBottomLeft,
153  QPointF( TriangleBottomRight.x(), TriangleHeight ) );
154  return Boundaries;
155 }
Class only listed here to document inheritance of some KChart classes.
A set of attributes controlling the appearance of data set markers.
Diagram attributes dealing with data value labels.
void drawLine(const QLineF &line)
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)
Declaring the class KChart::DataValueAttributes.
qreal x() const const
Stores information about painting diagrams.
void setPen(const QColor &color)
void setBrush(const QBrush &brush)
Base class for diagrams based on a ternary coordinate plane.
void setMarkerStyle(uint style)
Set the marker-style to use.
TernaryPoint defines a point within a ternary coordinate plane.
Definition: TernaryPoint.h:30
void resize(const QSizeF &area) override
Called by the widget&#39;s sizeEvent.
QVariant fromValue(const T &value)
QCA_EXPORT void init()
virtual void resize(const QSizeF &area)
Called by the widget&#39;s sizeEvent.
void paint(PaintContext *paintContext) override
Draw the diagram contents to the rectangle and painter, that are passed in as part of the paint conte...
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void setMarkerAttributes(const MarkerAttributes &a)
Set the marker attributes to use for the data values.
Global namespace.
void setVisible(bool visible)
Set whether data value labels should be displayed.
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.