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 auto mainSplineDirection = plane->isHorizontalRangeReversed() ? ReverseSplineDirection : NormalSplineDirection;
183 
184  const int step = rev ? -1 : 1;
185  const int end = rev ? -1 : columnCount;
186  for ( int column = rev ? columnCount - 1 : 0; column != end; column += step ) {
187  LineAttributes laPreviousCell;
188  CartesianDiagramDataCompressor::DataPoint lastPoint;
189  qreal lastAreaBoundingValue = 0;
190 
191  // Get min. y value, used as lower or upper bounding for area highlighting
192  const qreal minYValue = qMin(plane->visibleDataRange().bottom(), plane->visibleDataRange().top());
193 
194  CartesianDiagramDataCompressor::CachePosition previousCellPosition;
195  for ( int row = 0; row < rowCount; ++row ) {
196  const CartesianDiagramDataCompressor::CachePosition position( row, column );
197  // get where to draw the line from:
198  CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
199  if ( point.hidden ) {
200  continue;
201  }
202 
203  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
204 
205  const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
207 
208  // lower or upper bounding for the highlighted area
209  qreal areaBoundingValue;
210  if ( laCell.areaBoundingDataset() != -1 ) {
211  const CartesianDiagramDataCompressor::CachePosition areaBoundingCachePosition( row, laCell.areaBoundingDataset() );
212  areaBoundingValue = compressor().data( areaBoundingCachePosition ).value;
213  } else {
214  // Use min. y value (i.e. zero line in most cases) if no bounding dataset is set
215  areaBoundingValue = minYValue;
216  }
217 
218  if ( ISNAN( point.value ) )
219  {
220  switch ( policy )
221  {
223  // we just bridge both values
224  continue;
226  // set it to zero
227  point.value = 0.0;
228  break;
230  // they're just hidden
231  break;
232  default:
233  break;
234  // hm....
235  }
236  }
237 
238  if ( !ISNAN( point.value ) ) {
239  // area corners, a + b are the line ends:
240  const qreal offset = diagram()->centerDataPoints() ? 0.5 : 0;
241  const QPointF a( plane->translate( QPointF( lastPoint.key + offset, lastPoint.value ) ) );
242  const QPointF b( plane->translate( QPointF( point.key + offset, point.value ) ) );
243  const QPointF c( plane->translate( QPointF( lastPoint.key + offset, lastAreaBoundingValue ) ) );
244  const QPointF d( plane->translate( QPointF( point.key + offset, areaBoundingValue ) ) );
245  const PositionPoints pts = PositionPoints( b, a, d, c );
246 
247  // add label
248  m_private->addLabel( &lpc, sourceIndex, &position, pts, Position::NorthWest,
249  Position::NorthWest, point.value );
250 
251  // add line and area, if switched on and we have a current and previous value
252  if ( !ISNAN( lastPoint.value ) ) {
253  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
254 
255  if ( laCell.displayArea() ) {
256  QPainterPath path;
257 
258  // TODO: revert back to lambdas when we stop caring about pre-C++11
259  // auto dataAt = [&] ( int i ) {
260  // if ( i < 0 || i >= rowCount ) {
261  // return QPointF( NAN, NAN );
262  // } else {
263  // const auto data = compressor().data( { i, column } );
264  // return QPointF( plane->translate( QPointF( data.key + offset, data.value ) ) );
265  // }
266  // };
267  struct dataAtLambda {
268  dataAtLambda( int rowCount, int column, qreal offset, CartesianCoordinatePlane* plane, NormalLineDiagram* _this )
269  : rowCount( rowCount )
270  , column( column )
271  , offset( offset )
272  , plane( plane )
273  , _this( _this )
274  {
275  }
276 
277  int rowCount;
278  int column;
279  qreal offset;
281  NormalLineDiagram* _this;
282 
283  QPointF operator() ( int i ) const
284  {
285  if ( i < 0 || i >= rowCount ) {
286  return QPointF( NAN, NAN );
287  } else {
288  const KDChart::CartesianDiagramDataCompressor::DataPoint data = _this->compressor().data( CartesianDiagramDataCompressor::CachePosition( i, column ) );
289  return QPointF( plane->translate( QPointF( data.key + offset, data.value ) ) );
290  }
291  }
292  };
293  dataAtLambda dataAt( rowCount, column, offset, plane, this );
294 
295  path.moveTo( a );
296 
297  addSplineChunkTo( path, tension, dataAt( row - 2 ), a, b, dataAt( row + 1 ), mainSplineDirection );
298 
299  path.lineTo( d );
300  path.lineTo( c );
301  path.lineTo( a );
302  PaintingHelpers::paintAreas( m_private, ctx, attributesModel()->mapToSource( lastPoint.index ),
303  QList<QPainterPath>() << path, laCell.transparency() ); // TODO: change to {path} in C++11
304  }
305  }
306  }
307 
308  previousCellPosition = position;
309  laPreviousCell = laCell;
310  lastAreaBoundingValue = areaBoundingValue;
311  lastPoint = point;
312  }
313  }
314 
315  // paint the lines
316  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
317 }
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/