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 "KDChartPercentLyingBarDiagram_p.h"
30
#include <QModelIndex>
32
#include "KDChartBarDiagram.h"
33
#include "KDChartTextAttributes.h"
34
#include "KDChartAttributesModel.h"
35
#include "KDChartAbstractCartesianDiagram.h"
37
using namespace KDChart;
39
PercentLyingBarDiagram::PercentLyingBarDiagram( BarDiagram* d )
44
BarDiagram::BarType PercentLyingBarDiagram::type() const
46
return BarDiagram::Percent;
49
const QPair<QPointF, QPointF> PercentLyingBarDiagram::calculateDataBoundaries() const
51
//const int rowCount = compressor().modelDataRows();
52
//const int colCount = compressor().modelDataColumns();
54
const double xMin = 0;
55
const double xMax = diagram()->model() ? diagram()->model()->rowCount( diagram()->rootIndex() ) : 0;
56
double yMin = 0.0, yMax = 100.0;
57
/*for( int col = 0; col < colCount; ++col )
59
for( int row = 0; row < rowCount; ++row )
61
// Ordinate should begin at 0 the max value being the 100% pos
62
const QModelIndex idx = diagram()->model()->index( row, col, diagram()->rootIndex() );
63
// only positive values are handled
64
double value = diagram()->model()->data( idx ).toDouble();
66
yMax = qMax( yMax, value );
72
yMax = 0.1; //we need at least a range
74
yMax = 0.0; // they are the same but negative
76
const QPointF bottomLeft( QPointF( yMin, xMin ) );
77
const QPointF topRight( QPointF( yMax, xMax ) );
79
//qDebug() << "BarDiagram::calculateDataBoundaries () returns ( " << bottomLeft << topRight <<")";
80
return QPair< QPointF, QPointF >( bottomLeft, topRight );
83
void PercentLyingBarDiagram::paint( PaintContext* ctx )
85
reverseMapper().clear();
87
const QPair<QPointF,QPointF> boundaries = diagram()->dataBoundaries(); // cached
89
const QPointF boundLeft = ctx->coordinatePlane()->translate( boundaries.first ) ;
90
const QPointF boundRight = ctx->coordinatePlane()->translate( boundaries.second );
92
const int rowCount = compressor().modelDataRows();
93
const int colCount = compressor().modelDataColumns();
95
BarAttributes ba = diagram()->barAttributes( diagram()->model()->index( 0, 0, diagram()->rootIndex() ) );
98
double width = boundLeft.y() - boundRight.y();
99
QPointF testVector = boundRight - boundLeft;
100
double groupWidth = width/ (rowCount + 2);
101
double spaceBetweenBars = 0;
102
double spaceBetweenGroups = 0;
104
if ( ba.useFixedBarWidth() ) {
105
barWidth = ba.fixedBarWidth();
106
groupWidth += barWidth;
108
// Pending Michel set a min and max value for the groupWidth
109
// related to the area.width
110
if ( groupWidth < 0 )
113
if ( groupWidth * rowCount > width )
114
groupWidth = width / rowCount;
117
// maxLimit: allow the space between bars to be larger until area.width()
118
// is covered by the groups.
119
double maxLimit = rowCount * (groupWidth + ((colCount-1) * ba.fixedDataValueGap()) );
122
//Pending Michel: FixMe
123
if ( ba.useFixedDataValueGap() ) {
124
if ( width > maxLimit )
125
spaceBetweenBars += ba.fixedDataValueGap();
127
spaceBetweenBars = ((ctx->rectangle().width()/rowCount) - groupWidth)/(colCount-1);
130
if ( ba.useFixedValueBlockGap() )
131
spaceBetweenGroups += ba.fixedValueBlockGap();
133
calculateValueAndGapWidths( rowCount, colCount,groupWidth,
134
barWidth, spaceBetweenBars, spaceBetweenGroups );
136
DataValueTextInfoList list;
137
const double maxValue = 100.0; // always 100 %
138
double sumValues = 0;
139
QVector <double > sumValuesVector;
141
//calculate sum of values for each column and store
142
for( int row = 0; row < rowCount; ++row )
144
for( int col = 0; col < colCount; ++col )
146
const CartesianDiagramDataCompressor::CachePosition position( row, col );
147
const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
148
//if ( point.value > 0 )
149
sumValues += qMax( point.value, -point.value );
150
if ( col == colCount - 1 ) {
151
sumValuesVector << sumValues ;
157
// calculate stacked percent value
158
for( int curRow = rowCount - 1; curRow >= 0; --curRow )
160
double offset = spaceBetweenGroups;
161
if( ba.useFixedBarWidth() )
162
offset -= ba.fixedBarWidth();
167
for( int col = 0; col < colCount ; ++col )
169
double threeDOffset = 0.0;
170
const CartesianDiagramDataCompressor::CachePosition position( curRow, col );
171
const CartesianDiagramDataCompressor::DataPoint p = compressor().data( position );
172
QModelIndex sourceIndex = attributesModel()->mapToSource( p.index );
173
ThreeDBarAttributes threeDAttrs = diagram()->threeDBarAttributes( sourceIndex );
175
if ( threeDAttrs.isEnabled() ){
176
if ( barWidth > 0 ) {
177
barWidth = (width - ((offset+(threeDAttrs.depth()))*rowCount))/ rowCount;
178
threeDOffset = threeDAttrs.depth();
180
if ( barWidth <= 0 ) {
182
threeDOffset = (width - (offset*rowCount))/ rowCount;
185
barWidth = (width - (offset*rowCount))/ rowCount;
188
const double value = qMax( p.value, -p.value );
189
double stackedValues = 0.0;
192
// calculate stacked percent value
193
// we only take in account positives values for now.
194
for( int k = col; k >= 0 ; --k )
196
const CartesianDiagramDataCompressor::CachePosition position( curRow, k );
197
const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
198
stackedValues += qMax( point.value, -point.value );
202
QPointF point, previousPoint;
203
if( sumValuesVector.at( curRow ) != 0 && value > 0 ) {
204
QPointF dataPoint( ( stackedValues / sumValuesVector.at( curRow ) * maxValue ), rowCount - key );
205
point = ctx->coordinatePlane()->translate( dataPoint );
206
point.ry() += offset / 2 + threeDOffset;
208
previousPoint = ctx->coordinatePlane()->translate( QPointF( ( ( stackedValues - value) / sumValuesVector.at( curRow ) * maxValue ), rowCount - key ) );
211
const double barHeight = point.x() - previousPoint.x();
213
point.setX ( point.x() - barHeight );
215
const QRectF rect( point, QSizeF( barHeight, barWidth ) );
216
appendDataValueTextInfoToList( diagram(), list, sourceIndex, PositionPoints( rect ),
217
Position::NorthEast, Position::SouthWest,
219
paintBars( ctx, sourceIndex, rect, maxDepth );
222
paintDataValueTextsAndMarkers( diagram(), ctx, list, false );