2
KDChart - a multi-platform charting engine
5
/****************************************************************************
6
** Copyright (C) 2005-2007 Klarälvdalens Datakonsult AB. All rights reserved.
8
** This file is part of the KD Chart library.
10
** This file may be used under the terms of the GNU General Public
11
** License versions 2.0 or 3.0 as published by the Free Software
12
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
13
** included in the packaging of this file. Alternatively you may (at
14
** your option) use any later version of the GNU General Public
15
** License if such license has been publicly approved by
16
** Klarälvdalens Datakonsult AB (or its successors, if any).
18
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
19
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
20
** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
21
** not expressly granted herein.
23
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26
**********************************************************************/
28
#include "KDChartPercentPlotter_p.h"
29
#include "KDChartPlotter.h"
33
using namespace KDChart;
36
PercentPlotter::PercentPlotter( Plotter* d )
41
Plotter::PlotType PercentPlotter::type() const
43
return Plotter::Percent;
46
const QPair< QPointF, QPointF > PercentPlotter::calculateDataBoundaries() const
48
const int rowCount = compressor().modelDataRows();
49
const int colCount = compressor().modelDataColumns();
50
double xMin = std::numeric_limits< double >::quiet_NaN();
51
double xMax = std::numeric_limits< double >::quiet_NaN();
52
const double yMin = 0.0;
53
const double yMax = 100.0;
55
for( int column = 0; column < colCount; ++column )
57
for ( int row = 0; row < rowCount; ++row )
59
const CartesianDiagramDataCompressor::CachePosition position( row, column );
60
const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
62
const double valueX = ISNAN( point.key ) ? 0.0 : point.key;
71
xMin = qMin( xMin, valueX );
72
xMax = qMax( xMax, valueX );
77
// NOTE: calculateDataBoundaries must return the *real* data boundaries!
78
// i.e. we may NOT fake yMin to be qMin( 0.0, yMin )
80
const QPointF bottomLeft( QPointF( xMin, yMin ) );
81
const QPointF topRight( QPointF( xMax, yMax ) );
82
return QPair< QPointF, QPointF >( bottomLeft, topRight );
89
: value( std::numeric_limits< double >::quiet_NaN() )
92
// allow implicit conversion
97
operator double() const
106
void PercentPlotter::paint( PaintContext* ctx )
108
reverseMapper().clear();
110
Q_ASSERT( dynamic_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() ) );
111
const CartesianCoordinatePlane* const plane = static_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() );
112
const int colCount = compressor().modelDataColumns();
113
const int rowCount = compressor().modelDataRows();
115
if( colCount == 0 || rowCount == 0 )
118
DataValueTextInfoList textInfoList;
119
LineAttributes::MissingValuesPolicy policy; // ???
121
// this map contains the y-values to each x-value
122
QMap< double, QVector< QPair< Value, QModelIndex > > > diagramValues;
124
for( int col = 0; col < colCount; ++col )
126
for( int row = 0; row < rowCount; ++row )
128
const CartesianDiagramDataCompressor::CachePosition position( row, col );
129
const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
130
diagramValues[ point.key ].resize( colCount );
131
diagramValues[ point.key ][ col ].first = point.value;
132
diagramValues[ point.key ][ col ].second = point.index;
136
// the sums of the y-values per x-value
137
QMap< double, double > yValueSums;
139
QList< double > xValues = diagramValues.keys();
140
// make sure it's sorted
142
Q_FOREACH( const double xValue, xValues )
144
// the y-values to the current x-value
145
QVector< QPair< Value, QModelIndex > >& yValues = diagramValues[ xValue ];
146
Q_ASSERT( yValues.count() == colCount );
148
for( int column = 0; column < colCount; ++column )
150
QPair< Value, QModelIndex >& data = yValues[ column ];
151
// if the index is invalid, there was no value. Let's interpolate.
152
if( !data.second.isValid() )
154
QPair< QPair< double, Value >, QModelIndex > left;
155
QPair< QPair< double, Value >, QModelIndex > right;
157
// let's find the next lower value
158
for( xIndex = xValues.indexOf( xValue ); xIndex >= 0; --xIndex )
160
if( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
162
left.first.first = xValues[ xIndex ];
163
left.first.second = diagramValues[ left.first.first ][ column ].first;
164
left.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
168
// let's find the next higher value
169
for( xIndex = xValues.indexOf( xValue ); xIndex < xValues.count(); ++xIndex )
171
if( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
173
right.first.first = xValues[ xIndex ];
174
right.first.second = diagramValues[ right.first.first ][ column ].first;
175
right.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
180
// interpolate out of them (left and/or right might be invalid, but this doesn't matter here)
181
const double leftX = left.first.first;
182
const double rightX = right.first.first;
183
const double leftY = left.first.second;
184
const double rightY = right.first.second;
186
data.first = leftY + ( rightY - leftY ) * ( xValue - leftX ) / ( rightX - leftX );
187
// if the result is a valid value, let's assign the index, too
188
if( !ISNAN( data.first ) )
189
data.second = left.second;
193
if( !ISNAN( yValues[ column ].first ) )
194
yValueSums[ xValue ] += yValues[ column ].first;
198
for( int column = 0; column < colCount; ++column )
200
LineAttributesInfoList lineList;
201
LineAttributes laPreviousCell;
202
CartesianDiagramDataCompressor::CachePosition previousCellPosition;
204
CartesianDiagramDataCompressor::DataPoint lastPoint;
206
qreal lastExtraY = 0.0;
207
qreal lastValue = 0.0;
209
QMapIterator< double, QVector< QPair< Value, QModelIndex > > > i( diagramValues );
213
CartesianDiagramDataCompressor::DataPoint point;
215
const QPair< Value, QModelIndex >& data = i.value().at( column );
216
point.value = data.first;
217
point.index = data.second;
219
if( ISNAN( point.key ) || ISNAN( point.value ) )
221
previousCellPosition = CartesianDiagramDataCompressor::CachePosition();
226
for( int col = column - 1; col >= 0; --col )
228
const double y = i.value().at( col ).first;
233
LineAttributes laCell;
235
const qreal value = ( point.value + extraY ) / yValueSums[ i.key() ] * 100;
237
const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
238
// area corners, a + b are the line ends:
239
const QPointF a( plane->translate( QPointF( lastPoint.key, lastValue ) ) );
240
const QPointF b( plane->translate( QPointF( point.key, value ) ) );
241
const QPointF c( plane->translate( QPointF( lastPoint.key, lastExtraY / yValueSums[ i.key() ] * 100 ) ) );
242
const QPointF d( plane->translate( QPointF( point.key, extraY / yValueSums[ i.key() ] * 100 ) ) );
243
// add the line to the list:
244
laCell = diagram()->lineAttributes( sourceIndex );
245
// add data point labels:
246
const PositionPoints pts = PositionPoints( b, a, d, c );
247
// if necessary, add the area to the area list:
248
QList<QPolygonF> areas;
249
if ( laCell.displayArea() ) {
251
polygon << a << b << d << c;
254
// add the pieces to painting if this is not hidden:
255
if ( !point.hidden /*&& !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) */) {
256
appendDataValueTextInfoToList( diagram(), textInfoList, sourceIndex, pts,
257
Position::NorthWest, Position::SouthWest,
259
if( !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) )
261
paintAreas( ctx, attributesModel()->mapToSource( lastPoint.index ), areas, laCell.transparency() );
262
lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
267
laPreviousCell = laCell;
272
paintElements( ctx, textInfoList, lineList, policy );