23 #include "KDChartPercentPlotter_p.h"
25 #include "PaintingHelpers_p.h"
29 using namespace KDChart;
32 PercentPlotter::PercentPlotter(
Plotter*
d )
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;
51 for (
int column = 0; column < colCount; ++column )
53 for (
int row = 0; row < rowCount; ++row )
55 const CartesianDiagramDataCompressor::CachePosition position( row, column );
56 const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
58 const qreal valueX = ISNAN( point.key ) ? 0.0 : point.key;
67 xMin = qMin( xMin, valueX );
68 xMax = qMax( xMax, valueX );
76 const QPointF bottomLeft( QPointF( xMin, yMin ) );
77 const QPointF topRight( QPointF( xMax, yMax ) );
85 : value( std::numeric_limits< qreal >::quiet_NaN() )
93 operator qreal()
const
104 reverseMapper().clear();
106 Q_ASSERT( dynamic_cast< CartesianCoordinatePlane* >( ctx->
coordinatePlane() ) );
108 const int colCount = compressor().modelDataColumns();
109 const int rowCount = compressor().modelDataRows();
111 if ( colCount == 0 || rowCount == 0 )
119 for (
int col = 0; col < colCount; ++col )
121 for (
int row = 0; row < rowCount; ++row )
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;
137 Q_FOREACH(
const qreal xValue, xValues )
141 Q_ASSERT( yValues.count() == colCount );
143 for (
int column = 0; column < colCount; ++column )
147 if ( !data.second.isValid() )
153 for ( xIndex = xValues.indexOf( xValue ); xIndex >= 0; --xIndex )
155 if ( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
157 left.first.first = xValues[ xIndex ];
158 left.first.second = diagramValues[ left.first.first ][ column ].first;
159 left.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
164 for ( xIndex = xValues.indexOf( xValue ); xIndex < xValues.count(); ++xIndex )
166 if ( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
168 right.first.first = xValues[ xIndex ];
169 right.first.second = diagramValues[ right.first.first ][ column ].first;
170 right.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
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;
181 data.first = leftY + ( rightY - leftY ) * ( xValue - leftX ) / ( rightX - leftX );
183 if ( !ISNAN( data.first.operator qreal() ) )
184 data.second = left.second;
188 if ( !ISNAN( yValues[ column ].first.operator qreal() ) )
189 yValueSums[ xValue ] += yValues[ column ].first;
193 for (
int column = 0; column < colCount; ++column )
195 LineAttributesInfoList lineList;
197 CartesianDiagramDataCompressor::CachePosition previousCellPosition;
199 CartesianDiagramDataCompressor::DataPoint lastPoint;
201 qreal lastExtraY = 0.0;
202 qreal lastValue = 0.0;
204 QMapIterator< qreal, QVector< QPair< Value, QModelIndex > > > i( diagramValues );
205 while ( i.hasNext() )
208 CartesianDiagramDataCompressor::DataPoint point;
211 point.value = data.first;
212 point.index = data.second;
214 if ( ISNAN( point.key ) || ISNAN( point.value ) )
216 previousCellPosition = CartesianDiagramDataCompressor::CachePosition();
221 for (
int col = column - 1; col >= 0; --col )
223 const qreal y = i.value().at( col ).first;
230 const qreal value = ( point.value + extraY ) / yValueSums[ i.key() ] * 100;
232 const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
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 ) ) );
239 laCell = diagram()->lineAttributes( sourceIndex );
244 if ( laCell.displayArea() ) {
246 polygon << a << b <<
d << c;
250 if ( !point.hidden ) {
253 if ( !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) )
256 attributesModel()->mapToSource( lastPoint.index ),
257 areas, laCell.transparency() );
258 lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
263 laPreviousCell = laCell;