KD Chart 2  [rev.2.7]
KDChartNormalLineDiagram_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 <limits>
24 
25 #include <QAbstractItemModel>
26 
27 #include "KDChartBarDiagram.h"
28 #include "KDChartLineDiagram.h"
29 #include "KDChartTextAttributes.h"
30 #include "KDChartAttributesModel.h"
32 #include "KDChartNormalLineDiagram_p.h"
33 #include "PaintingHelpers_p.h"
34 
35 using namespace KDChart;
36 using namespace std;
37 
38 NormalLineDiagram::NormalLineDiagram( LineDiagram* d )
39  : LineDiagramType( d )
40 {
41 }
42 
43 LineDiagram::LineType NormalLineDiagram::type() const
44 {
45  return LineDiagram::Normal;
46 }
47 
48 const QPair< QPointF, QPointF > NormalLineDiagram::calculateDataBoundaries() const
49 {
50  return compressor().dataBoundaries();
51 }
52 
53 void NormalLineDiagram::paint( PaintContext* ctx )
54 {
55  if ( qFuzzyIsNull( m_private->tension ) ) {
56  paintWithLines( ctx );
57 
58  } else {
59  paintWithSplines( ctx, m_private->tension );
60  }
61 }
62 
63 void NormalLineDiagram::paintWithLines( PaintContext* ctx )
64 {
65  reverseMapper().clear();
66  Q_ASSERT( dynamic_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() ) );
67  CartesianCoordinatePlane* plane = static_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() );
68  const int columnCount = compressor().modelDataColumns();
69  const int rowCount = compressor().modelDataRows();
70  if ( columnCount == 0 || rowCount == 0 ) return; // maybe blank out the area?
71 
72  // Reverse order of data sets?
73  bool rev = diagram()->reverseDatasetOrder();
74  LabelPaintCache lpc;
75  LineAttributesInfoList lineList;
76 
77  const int step = rev ? -1 : 1;
78  const int end = rev ? -1 : columnCount;
79  for ( int column = rev ? columnCount - 1 : 0; column != end; column += step ) {
80  LineAttributes laPreviousCell;
81  CartesianDiagramDataCompressor::DataPoint lastPoint;
82  qreal lastAreaBoundingValue = 0;
83 
84  // Get min. y value, used as lower or upper bounding for area highlighting
85  const qreal minYValue = qMin(plane->visibleDataRange().bottom(), plane->visibleDataRange().top());
86 
87  CartesianDiagramDataCompressor::CachePosition previousCellPosition;
88  for ( int row = 0; row < rowCount; ++row ) {
89  const CartesianDiagramDataCompressor::CachePosition position( row, column );
90  // get where to draw the line from:
91  CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
92  if ( point.hidden ) {
93  continue;
94  }
95 
96  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
97 
98  const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
100 
101  // lower or upper bounding for the highlighted area
102  qreal areaBoundingValue;
103  if ( laCell.areaBoundingDataset() != -1 ) {
104  const CartesianDiagramDataCompressor::CachePosition areaBoundingCachePosition( row, laCell.areaBoundingDataset() );
105  areaBoundingValue = compressor().data( areaBoundingCachePosition ).value;
106  } else {
107  // Use min. y value (i.e. zero line in most cases) if no bounding dataset is set
108  areaBoundingValue = minYValue;
109  }
110 
111  if ( ISNAN( point.value ) )
112  {
113  switch ( policy )
114  {
116  // we just bridge both values
117  continue;
119  // set it to zero
120  point.value = 0.0;
121  break;
123  // they're just hidden
124  break;
125  default:
126  break;
127  // hm....
128  }
129  }
130 
131  if ( !ISNAN( point.value ) ) {
132  // area corners, a + b are the line ends:
133  const qreal offset = diagram()->centerDataPoints() ? 0.5 : 0;
134  const QPointF a( plane->translate( QPointF( lastPoint.key + offset, lastPoint.value ) ) );
135  const QPointF b( plane->translate( QPointF( point.key + offset, point.value ) ) );
136  const QPointF c( plane->translate( QPointF( lastPoint.key + offset, lastAreaBoundingValue ) ) );
137  const QPointF d( plane->translate( QPointF( point.key + offset, areaBoundingValue ) ) );
138  const PositionPoints pts = PositionPoints( b, a, d, c );
139 
140  // add label
141  m_private->addLabel( &lpc, sourceIndex, &position, pts, Position::NorthWest,
142  Position::NorthWest, point.value );
143 
144  // add line and area, if switched on and we have a current and previous value
145  if ( !ISNAN( lastPoint.value ) ) {
146  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
147 
148  if ( laCell.displayArea() ) {
149  QList<QPolygonF> areas;
150  areas << ( QPolygonF() << a << b << d << c );
151  PaintingHelpers::paintAreas( m_private, ctx, attributesModel()->mapToSource( lastPoint.index ),
152  areas, laCell.transparency() );
153  }
154  }
155  }
156 
157  previousCellPosition = position;
158  laPreviousCell = laCell;
159  lastAreaBoundingValue = areaBoundingValue;
160  lastPoint = point;
161  }
162  }
163 
164  // paint the lines
165  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
166 }
167 
168 void NormalLineDiagram::paintWithSplines( PaintContext* ctx, qreal tension )
169 {
170  reverseMapper().clear();
171  Q_ASSERT( dynamic_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() ) );
172  CartesianCoordinatePlane* plane = static_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() );
173  const int columnCount = compressor().modelDataColumns();
174  const int rowCount = compressor().modelDataRows();
175  if ( columnCount == 0 || rowCount == 0 ) return; // maybe blank out the area?
176 
177  // Reverse order of data sets?
178  bool rev = diagram()->reverseDatasetOrder();
179  LabelPaintCache lpc;
180  LineAttributesInfoList lineList;
181 
182  const int step = rev ? -1 : 1;
183  const int end = rev ? -1 : columnCount;
184  for ( int column = rev ? columnCount - 1 : 0; column != end; column += step ) {
185  LineAttributes laPreviousCell;
186  CartesianDiagramDataCompressor::DataPoint lastPoint;
187  qreal lastAreaBoundingValue = 0;
188 
189  // Get min. y value, used as lower or upper bounding for area highlighting
190  const qreal minYValue = qMin(plane->visibleDataRange().bottom(), plane->visibleDataRange().top());
191 
192  CartesianDiagramDataCompressor::CachePosition previousCellPosition;
193  for ( int row = 0; row < rowCount; ++row ) {
194  const CartesianDiagramDataCompressor::CachePosition position( row, column );
195  // get where to draw the line from:
196  CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
197  if ( point.hidden ) {
198  continue;
199  }
200 
201  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
202 
203  const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
205 
206  // lower or upper bounding for the highlighted area
207  qreal areaBoundingValue;
208  if ( laCell.areaBoundingDataset() != -1 ) {
209  const CartesianDiagramDataCompressor::CachePosition areaBoundingCachePosition( row, laCell.areaBoundingDataset() );
210  areaBoundingValue = compressor().data( areaBoundingCachePosition ).value;
211  } else {
212  // Use min. y value (i.e. zero line in most cases) if no bounding dataset is set
213  areaBoundingValue = minYValue;
214  }
215 
216  if ( ISNAN( point.value ) )
217  {
218  switch ( policy )
219  {
221  // we just bridge both values
222  continue;
224  // set it to zero
225  point.value = 0.0;
226  break;
228  // they're just hidden
229  break;
230  default:
231  break;
232  // hm....
233  }
234  }
235 
236  if ( !ISNAN( point.value ) ) {
237  // area corners, a + b are the line ends:
238  const qreal offset = diagram()->centerDataPoints() ? 0.5 : 0;
239  const QPointF a( plane->translate( QPointF( lastPoint.key + offset, lastPoint.value ) ) );
240  const QPointF b( plane->translate( QPointF( point.key + offset, point.value ) ) );
241  const QPointF c( plane->translate( QPointF( lastPoint.key + offset, lastAreaBoundingValue ) ) );
242  const QPointF d( plane->translate( QPointF( point.key + offset, areaBoundingValue ) ) );
243  const PositionPoints pts = PositionPoints( b, a, d, c );
244 
245  // add label
246  m_private->addLabel( &lpc, sourceIndex, &position, pts, Position::NorthWest,
247  Position::NorthWest, point.value );
248 
249  // add line and area, if switched on and we have a current and previous value
250  if ( !ISNAN( lastPoint.value ) ) {
251  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
252 
253  if ( laCell.displayArea() ) {
254  QPainterPath path;
255 
256  // TODO: revert back to lambdas when we stop caring about pre-C++11
257  // auto dataAt = [&] ( int i ) {
258  // if ( i < 0 || i >= rowCount ) {
259  // return QPointF( NAN, NAN );
260  // } else {
261  // const auto data = compressor().data( { i, column } );
262  // return QPointF( plane->translate( QPointF( data.key + offset, data.value ) ) );
263  // }
264  // };
265  struct dataAtLambda {
266  dataAtLambda( int rowCount, int column, int offset, CartesianCoordinatePlane* plane, NormalLineDiagram* _this )
267  : rowCount( rowCount )
268  , column( column )
269  , offset( offset )
270  , plane( plane )
271  , _this( _this )
272  {
273  }
274 
275  int rowCount;
276  int column;
277  int offset;
279  NormalLineDiagram* _this;
280 
281  QPointF operator() ( int i ) const
282  {
283  if ( i < 0 || i >= rowCount ) {
284  return QPointF( NAN, NAN );
285  } else {
286  const KDChart::CartesianDiagramDataCompressor::DataPoint data = _this->compressor().data( CartesianDiagramDataCompressor::CachePosition( i, column ) );
287  return QPointF( plane->translate( QPointF( data.key + offset, data.value ) ) );
288  }
289  }
290  };
291  dataAtLambda dataAt( rowCount, column, offset, plane, this );
292 
293  path.moveTo( a );
294 
295  addSplineChunkTo( path, tension, dataAt( row - 2 ), a, b, dataAt( row + 1 ) );
296 
297  path.lineTo( d );
298  path.lineTo( c );
299  path.lineTo( a );
300  PaintingHelpers::paintAreas( m_private, ctx, attributesModel()->mapToSource( lastPoint.index ),
301  QList<QPainterPath>() << path, laCell.transparency() ); // TODO: change to {path} in C++11
302  }
303  }
304  }
305 
306  previousCellPosition = position;
307  laPreviousCell = laCell;
308  lastAreaBoundingValue = areaBoundingValue;
309  lastPoint = point;
310  }
311  }
312 
313  // paint the lines
314  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
315 }
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.
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
LineDiagram defines a common line diagram.
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.
QRectF visibleDataRange() const
Returns the currently visible data range.
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/