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

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/