KD Chart 2  [rev.2.6]
KDChartNormalPlotter_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2019 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  else
126  {
127  if ( colCount == 0 || rowCount == 0 )
128  return;
129  for ( int column = 0; column < colCount; ++column )
130  {
131  LineAttributesInfoList lineList;
132  CartesianDiagramDataCompressor::DataPoint lastPoint;
133 
134  for ( int row = 0; row < rowCount; ++row )
135  {
136  const CartesianDiagramDataCompressor::CachePosition position( row, column );
137  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
138 
139  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
140  LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
142 
143  if ( ISNAN( point.key ) || ISNAN( point.value ) )
144  {
145  switch ( policy )
146  {
147  case LineAttributes::MissingValuesAreBridged: // we just bridge both values
148  continue;
149  case LineAttributes::MissingValuesShownAsZero: // fall-through since that attribute makes no sense for the plotter
150  case LineAttributes::MissingValuesHideSegments: // fall-through since they're just hidden
151  default:
152  lastPoint = CartesianDiagramDataCompressor::DataPoint();
153  continue;
154  }
155  }
156 
157  // data area painting: a and b are prev / current data points, c and d are on the null line
158  const QPointF b( plane->translate( QPointF( point.key, point.value ) ) );
159 
160  if ( !point.hidden && PaintingHelpers::isFinite( b ) ) {
161  const QPointF a( plane->translate( QPointF( lastPoint.key, lastPoint.value ) ) );
162  const QPointF c( plane->translate( QPointF( lastPoint.key, 0.0 ) ) );
163  const QPointF d( plane->translate( QPointF( point.key, 0.0 ) ) );
164 
165  // data point label
166  const PositionPoints pts = PositionPoints( b, a, d, c );
167  m_private->addLabel( &lpc, sourceIndex, 0, pts, Position::NorthWest,
168  Position::NorthWest, point.value );
169 
170  const bool lineValid = a.toPoint() != b.toPoint() && PaintingHelpers::isFinite( a );
171  if ( lineValid ) {
172  // data line
173  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
174 
175  if ( laCell.displayArea() ) {
176  // data area
177  QList<QPolygonF> areas;
178  QPolygonF polygon;
179  polygon << a << b << d << c;
180  areas << polygon;
181  PaintingHelpers::paintAreas( m_private, ctx,
182  attributesModel()->mapToSource( lastPoint.index ),
183  areas, laCell.transparency() );
184  }
185  }
186  }
187 
188  lastPoint = point;
189  }
190  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
191  }
192  }
193 }
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)
const QPointF translate(const QPointF &diagramPoint) const
Translate the given point in value space coordinates to a position in pixel space.
AbstractCoordinatePlane * coordinatePlane() const
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)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/