KD Chart 2  [rev.2.7]
KDChartNormalPlotter_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "KDChartNormalPlotter_p.h"
24 #include "KDChartPlotter.h"
25 #include "PaintingHelpers_p.h"
26 
27 #include <limits>
28 
29 using namespace KDChart;
30 using namespace std;
31 
32 NormalPlotter::NormalPlotter( Plotter* d )
33  : PlotterType( d )
34 {
35 }
36 
37 Plotter::PlotType NormalPlotter::type() const
38 {
39  return Plotter::Normal;
40 }
41 
42 const QPair< QPointF, QPointF > NormalPlotter::calculateDataBoundaries() const
43 {
44  if ( diagram()->useDataCompression() != Plotter::NONE )
45  return plotterCompressor().dataBoundaries();
46  else
47  return compressor().dataBoundaries();
48 }
49 
50 void NormalPlotter::paint( PaintContext* ctx )
51 {
52  reverseMapper().clear();
53 
54  Q_ASSERT( dynamic_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() ) );
55  const CartesianCoordinatePlane* const plane = static_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() );
56  const int colCount = compressor().modelDataColumns();
57  const int rowCount = compressor().modelDataRows();
58 
59  LabelPaintCache lpc;
60 
61  if ( diagram()->useDataCompression() != Plotter::NONE )
62  {
63  for ( int dataset = 0; dataset < plotterCompressor().datasetCount(); ++dataset )
64  {
65  LineAttributesInfoList lineList;
67  for ( PlotterDiagramCompressor::Iterator it = plotterCompressor().begin( dataset ); it != plotterCompressor().end( dataset ); ++ it )
68  {
69  const PlotterDiagramCompressor::DataPoint point = *it;
70 
71  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
72  LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
74 
75  if ( ISNAN( point.key ) || ISNAN( point.value ) )
76  {
77  switch ( policy )
78  {
79  case LineAttributes::MissingValuesAreBridged: // we just bridge both values
80  continue;
81  case LineAttributes::MissingValuesShownAsZero: // fall-through since that attribute makes no sense for the plotter
82  case LineAttributes::MissingValuesHideSegments: // fall-through since they're just hidden
83  default:
85  continue;
86  }
87  }
88 
89  // data area painting: a and b are prev / current data points, c and d are on the null line
90  const QPointF b( plane->translate( QPointF( point.key, point.value ) ) );
91 
92  if ( !point.hidden && PaintingHelpers::isFinite( b ) ) {
93  const QPointF a( plane->translate( QPointF( lastPoint.key, lastPoint.value ) ) );
94  const QPointF c( plane->translate( QPointF( lastPoint.key, 0.0 ) ) );
95  const QPointF d( plane->translate( QPointF( point.key, 0.0 ) ) );
96 
97  // data point label
98  const PositionPoints pts = PositionPoints( b, a, d, c );
99  m_private->addLabel( &lpc, sourceIndex, 0, pts, Position::NorthWest,
100  Position::NorthWest, point.value );
101 
102  const bool lineValid = a.toPoint() != b.toPoint() && PaintingHelpers::isFinite( a );
103  if ( lineValid ) {
104  // data line
105  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
106 
107  if ( laCell.displayArea() ) {
108  // data area
109  QList<QPolygonF> areas;
110  QPolygonF polygon;
111  polygon << a << b << d << c;
112  areas << polygon;
113  PaintingHelpers::paintAreas( m_private, ctx,
114  attributesModel()->mapToSource( lastPoint.index ),
115  areas, laCell.transparency() );
116  }
117  }
118  }
119 
120  lastPoint = point;
121  }
122  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
123  }
124 
125  }
126  else
127  {
128  if ( colCount == 0 || rowCount == 0 )
129  return;
130  for ( int column = 0; column < colCount; ++column )
131  {
132  LineAttributesInfoList lineList;
133  CartesianDiagramDataCompressor::DataPoint lastPoint;
134 
135  for ( int row = 0; row < rowCount; ++row )
136  {
137  const CartesianDiagramDataCompressor::CachePosition position( row, column );
138  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
139 
140  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
141  LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
143 
144  if ( ISNAN( point.key ) || ISNAN( point.value ) )
145  {
146  switch ( policy )
147  {
148  case LineAttributes::MissingValuesAreBridged: // we just bridge both values
149  continue;
150  case LineAttributes::MissingValuesShownAsZero: // fall-through since that attribute makes no sense for the plotter
151  case LineAttributes::MissingValuesHideSegments: // fall-through since they're just hidden
152  default:
153  lastPoint = CartesianDiagramDataCompressor::DataPoint();
154  continue;
155  }
156  }
157 
158  // data area painting: a and b are prev / current data points, c and d are on the null line
159  const QPointF b( plane->translate( QPointF( point.key, point.value ) ) );
160 
161  if ( !point.hidden && PaintingHelpers::isFinite( b ) ) {
162  const QPointF a( plane->translate( QPointF( lastPoint.key, lastPoint.value ) ) );
163  const QPointF c( plane->translate( QPointF( lastPoint.key, 0.0 ) ) );
164  const QPointF d( plane->translate( QPointF( point.key, 0.0 ) ) );
165 
166  // data point label
167  const PositionPoints pts = PositionPoints( b, a, d, c );
168  m_private->addLabel( &lpc, sourceIndex, 0, pts, Position::NorthWest,
169  Position::NorthWest, point.value );
170 
171  const bool lineValid = a.toPoint() != b.toPoint() && PaintingHelpers::isFinite( a );
172  if ( lineValid ) {
173  // data line
174  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
175 
176  if ( laCell.displayArea() ) {
177  // data area
178  QList<QPolygonF> areas;
179  QPolygonF polygon;
180  polygon << a << b << d << c;
181  areas << polygon;
182  PaintingHelpers::paintAreas( m_private, ctx,
183  attributesModel()->mapToSource( lastPoint.index ),
184  areas, laCell.transparency() );
185  }
186  }
187  }
188 
189  lastPoint = point;
190  }
191  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
192  }
193  }
194 }
KDChartEnums::PositionValue value() const
Returns an integer value corresponding to this Position.
void paintElements(AbstractDiagram::Private *diagramPrivate, PaintContext *ctx, const LabelPaintCache &lpc, const LineAttributesInfoList &lineList)
AbstractCoordinatePlane * coordinatePlane() const
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.
Plotter defines a diagram type plotting two-dimensional data.
Set of attributes for changing the appearance of line charts.
void paintAreas(AbstractDiagram::Private *diagramPrivate, PaintContext *ctx, const QModelIndex &index, const QList< QPolygonF > &areas, uint opacity)
static const Position & NorthWest
Stores information about painting diagrams.
Stores the absolute target points of a Position.
MissingValuesPolicy
MissingValuesPolicy specifies how a missing value will be shown in a line diagram.
MissingValuesPolicy missingValuesPolicy() const

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/