KD Chart 2  [rev.2.7]
KDChartPercentPlotter_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 "KDChartPercentPlotter_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 PercentPlotter::PercentPlotter( Plotter* d )
33  : PlotterType( d )
34 {
35 }
36 
37 Plotter::PlotType PercentPlotter::type() const
38 {
39  return Plotter::Percent;
40 }
41 
42 const QPair< QPointF, QPointF > PercentPlotter::calculateDataBoundaries() const
43 {
44  const int rowCount = compressor().modelDataRows();
45  const int colCount = compressor().modelDataColumns();
46  qreal xMin = std::numeric_limits< qreal >::quiet_NaN();
47  qreal xMax = std::numeric_limits< qreal >::quiet_NaN();
48  const qreal yMin = 0.0;
49  const qreal yMax = 100.0;
50 
51  for ( int column = 0; column < colCount; ++column )
52  {
53  for ( int row = 0; row < rowCount; ++row )
54  {
55  const CartesianDiagramDataCompressor::CachePosition position( row, column );
56  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
57 
58  const qreal valueX = ISNAN( point.key ) ? 0.0 : point.key;
59 
60  if ( ISNAN( xMin ) )
61  {
62  xMin = valueX;
63  xMax = valueX;
64  }
65  else
66  {
67  xMin = qMin( xMin, valueX );
68  xMax = qMax( xMax, valueX );
69  }
70  }
71  }
72 
73  const QPointF bottomLeft( QPointF( xMin, yMin ) );
74  const QPointF topRight( QPointF( xMax, yMax ) );
75  return QPair< QPointF, QPointF >( bottomLeft, topRight );
76 }
77 
78 class Value
79 {
80 public:
81  Value()
82  : value( std::numeric_limits< qreal >::quiet_NaN() )
83  {
84  }
85  // allow implicit conversion
86  Value( qreal value )
87  : value( value )
88  {
89  }
90  operator qreal() const
91  {
92  return value;
93  }
94 
95 private:
96  qreal value;
97 };
98 
99 void PercentPlotter::paint( PaintContext* ctx )
100 {
101  reverseMapper().clear();
102 
103  Q_ASSERT( dynamic_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() ) );
104  const CartesianCoordinatePlane* const plane = static_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() );
105  const int colCount = compressor().modelDataColumns();
106  const int rowCount = compressor().modelDataRows();
107 
108  if ( colCount == 0 || rowCount == 0 )
109  return;
110 
111  LabelPaintCache lpc;
112 
113  // this map contains the y-values to each x-value
115 
116  for ( int col = 0; col < colCount; ++col )
117  {
118  for ( int row = 0; row < rowCount; ++row )
119  {
120  const CartesianDiagramDataCompressor::CachePosition position( row, col );
121  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
122  diagramValues[ point.key ].resize( colCount );
123  diagramValues[ point.key ][ col ].first = point.value;
124  diagramValues[ point.key ][ col ].second = point.index;
125  }
126  }
127 
128  // the sums of the y-values per x-value
129  QMap< qreal, qreal > yValueSums;
130  // the x-values
131  QList< qreal > xValues = diagramValues.keys();
132  // make sure it's sorted
133  qSort( xValues );
134  Q_FOREACH( const qreal xValue, xValues )
135  {
136  // the y-values to the current x-value
137  QVector< QPair< Value, QModelIndex > >& yValues = diagramValues[ xValue ];
138  Q_ASSERT( yValues.count() == colCount );
139 
140  for ( int column = 0; column < colCount; ++column )
141  {
142  QPair< Value, QModelIndex >& data = yValues[ column ];
143  // if the index is invalid, there was no value. Let's interpolate.
144  if ( !data.second.isValid() )
145  {
146  QPair< QPair< qreal, Value >, QModelIndex > left;
147  QPair< QPair< qreal, Value >, QModelIndex > right;
148  int xIndex = 0;
149  // let's find the next lower value
150  for ( xIndex = xValues.indexOf( xValue ); xIndex >= 0; --xIndex )
151  {
152  if ( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
153  {
154  left.first.first = xValues[ xIndex ];
155  left.first.second = diagramValues[ left.first.first ][ column ].first;
156  left.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
157  break;
158  }
159  }
160  // let's find the next higher value
161  for ( xIndex = xValues.indexOf( xValue ); xIndex < xValues.count(); ++xIndex )
162  {
163  if ( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
164  {
165  right.first.first = xValues[ xIndex ];
166  right.first.second = diagramValues[ right.first.first ][ column ].first;
167  right.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
168  break;
169  }
170  }
171 
172  // interpolate out of them (left and/or right might be invalid, but this doesn't matter here)
173  const qreal leftX = left.first.first;
174  const qreal rightX = right.first.first;
175  const qreal leftY = left.first.second;
176  const qreal rightY = right.first.second;
177 
178  data.first = leftY + ( rightY - leftY ) * ( xValue - leftX ) / ( rightX - leftX );
179  // if the result is a valid value, let's assign the index, too
180  if ( !ISNAN( data.first.operator qreal() ) )
181  data.second = left.second;
182  }
183 
184  // sum it up
185  if ( !ISNAN( yValues[ column ].first.operator qreal() ) )
186  yValueSums[ xValue ] += yValues[ column ].first;
187  }
188  }
189 
190  for ( int column = 0; column < colCount; ++column )
191  {
192  LineAttributesInfoList lineList;
193  LineAttributes laPreviousCell;
194  CartesianDiagramDataCompressor::CachePosition previousCellPosition;
195 
196  CartesianDiagramDataCompressor::DataPoint lastPoint;
197 
198  qreal lastExtraY = 0.0;
199  qreal lastValue = 0.0;
200 
201  QMapIterator< qreal, QVector< QPair< Value, QModelIndex > > > i( diagramValues );
202  while ( i.hasNext() )
203  {
204  i.next();
205  CartesianDiagramDataCompressor::DataPoint point;
206  point.key = i.key();
207  const QPair< Value, QModelIndex >& data = i.value().at( column );
208  point.value = data.first;
209  point.index = data.second;
210 
211  if ( ISNAN( point.key ) || ISNAN( point.value ) )
212  {
213  previousCellPosition = CartesianDiagramDataCompressor::CachePosition();
214  continue;
215  }
216 
217  qreal extraY = 0.0;
218  for ( int col = column - 1; col >= 0; --col )
219  {
220  const qreal y = i.value().at( col ).first;
221  if ( !ISNAN( y ) )
222  extraY += y;
223  }
224 
225  LineAttributes laCell;
226 
227  const qreal scalingFactor =
228  qFuzzyIsNull( yValueSums[ i.key() ] ) ? 0.0 :
229  100.0 / yValueSums[ i.key() ];
230 
231  const qreal value = ( point.value + extraY ) * scalingFactor;
232 
233  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
234  // area corners, a + b are the line ends:
235  const QPointF a( plane->translate( QPointF( lastPoint.key, lastValue ) ) );
236  const QPointF b( plane->translate( QPointF( point.key, value ) ) );
237  const QPointF c( plane->translate( QPointF( lastPoint.key, lastExtraY * scalingFactor ) ) );
238  const QPointF d( plane->translate( QPointF( point.key, extraY * scalingFactor ) ) );
239  // add the line to the list:
240  laCell = diagram()->lineAttributes( sourceIndex );
241  // add data point labels:
242  const PositionPoints pts = PositionPoints( b, a, d, c );
243  // if necessary, add the area to the area list:
244  QList<QPolygonF> areas;
245  if ( laCell.displayArea() ) {
246  QPolygonF polygon;
247  polygon << a << b << d << c;
248  areas << polygon;
249  }
250  // add the pieces to painting if this is not hidden:
251  if ( !point.hidden /*&& !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) */) {
252  m_private->addLabel( &lpc, sourceIndex, 0, pts, Position::NorthWest,
253  Position::NorthWest, value );
254  if ( !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) )
255  {
256  PaintingHelpers::paintAreas( m_private, ctx,
257  attributesModel()->mapToSource( lastPoint.index ),
258  areas, laCell.transparency() );
259  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
260  }
261  }
262 
263  // wrap it up:
264  laPreviousCell = laCell;
265  lastPoint = point;
266  lastExtraY = extraY;
267  lastValue = value;
268  }
269  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
270  }
271 }
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.

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/