~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kchart/kdchart/src/KDChartPercentPlotter_p.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++ -*-
2
 
   KDChart - a multi-platform charting engine
3
 
   */
4
 
 
5
 
/****************************************************************************
6
 
 ** Copyright (C) 2005-2007 Klarälvdalens Datakonsult AB.  All rights reserved.
7
 
 **
8
 
 ** This file is part of the KD Chart library.
9
 
 **
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).
17
 
 ** 
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.
22
 
 ** 
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.
25
 
 **
26
 
 **********************************************************************/
27
 
 
28
 
#include "KDChartPercentPlotter_p.h"
29
 
#include "KDChartPlotter.h"
30
 
 
31
 
#include <limits>
32
 
 
33
 
using namespace KDChart;
34
 
using namespace std;
35
 
 
36
 
PercentPlotter::PercentPlotter( Plotter* d )
37
 
    : PlotterType( d )
38
 
{
39
 
}
40
 
 
41
 
Plotter::PlotType PercentPlotter::type() const
42
 
{
43
 
    return Plotter::Percent;
44
 
}
45
 
 
46
 
const QPair< QPointF, QPointF > PercentPlotter::calculateDataBoundaries() const
47
 
{
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;
54
 
 
55
 
    for( int column = 0; column < colCount; ++column )
56
 
    {
57
 
        for ( int row = 0; row < rowCount; ++row )
58
 
        {
59
 
            const CartesianDiagramDataCompressor::CachePosition position( row, column );
60
 
            const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
61
 
 
62
 
            const double valueX = ISNAN( point.key ) ? 0.0 : point.key;
63
 
 
64
 
            if( ISNAN( xMin ) )
65
 
            {
66
 
                xMin = valueX;
67
 
                xMax = valueX;
68
 
            }
69
 
            else
70
 
            {
71
 
                xMin = qMin( xMin, valueX );
72
 
                xMax = qMax( xMax, valueX );
73
 
            }
74
 
        }
75
 
    }
76
 
 
77
 
    // NOTE: calculateDataBoundaries must return the *real* data boundaries!
78
 
    //       i.e. we may NOT fake yMin to be qMin( 0.0, yMin )
79
 
    //       (khz, 2008-01-24)
80
 
    const QPointF bottomLeft( QPointF( xMin, yMin ) );
81
 
    const QPointF topRight( QPointF( xMax, yMax ) );
82
 
    return QPair< QPointF, QPointF >( bottomLeft, topRight );
83
 
}
84
 
 
85
 
class Value
86
 
{
87
 
public:
88
 
    Value()
89
 
        : value( std::numeric_limits< double >::quiet_NaN() )
90
 
    {
91
 
    }
92
 
    // allow implicit conversion
93
 
    Value( double value )
94
 
        : value( value )
95
 
    {
96
 
    }
97
 
    operator double() const
98
 
    {
99
 
        return value;
100
 
    }
101
 
 
102
 
private:
103
 
    double value;
104
 
};
105
 
 
106
 
void PercentPlotter::paint( PaintContext* ctx )
107
 
{
108
 
    reverseMapper().clear();
109
 
 
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();
114
 
 
115
 
    if( colCount == 0 || rowCount == 0 )
116
 
        return;
117
 
 
118
 
    DataValueTextInfoList textInfoList;
119
 
    LineAttributes::MissingValuesPolicy policy; // ???
120
 
 
121
 
    // this map contains the y-values to each x-value
122
 
    QMap< double, QVector< QPair< Value, QModelIndex > > > diagramValues;
123
 
 
124
 
    for( int col = 0; col < colCount; ++col )
125
 
    {
126
 
        for( int row = 0; row < rowCount; ++row )
127
 
        {
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;
133
 
        }
134
 
    }
135
 
 
136
 
    // the sums of the y-values per x-value
137
 
    QMap< double, double > yValueSums;
138
 
    // the x-values
139
 
    QList< double > xValues = diagramValues.keys();
140
 
    // make sure it's sorted
141
 
    qSort( xValues );
142
 
    Q_FOREACH( const double xValue, xValues )
143
 
    {
144
 
        // the y-values to the current x-value
145
 
        QVector< QPair< Value, QModelIndex > >& yValues = diagramValues[ xValue ];
146
 
        Q_ASSERT( yValues.count() == colCount );
147
 
 
148
 
        for( int column = 0; column < colCount; ++column )
149
 
        {
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() )
153
 
            {
154
 
                QPair< QPair< double, Value >, QModelIndex > left;
155
 
                QPair< QPair< double, Value >, QModelIndex > right;
156
 
                int xIndex = 0;
157
 
                // let's find the next lower value
158
 
                for( xIndex = xValues.indexOf( xValue ); xIndex >= 0; --xIndex )
159
 
                {
160
 
                    if( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
161
 
                    {
162
 
                        left.first.first = xValues[ xIndex ];
163
 
                        left.first.second = diagramValues[ left.first.first ][ column ].first;
164
 
                        left.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
165
 
                        break;
166
 
                    }
167
 
                }
168
 
                // let's find the next higher value
169
 
                for( xIndex = xValues.indexOf( xValue ); xIndex < xValues.count(); ++xIndex )
170
 
                {
171
 
                    if( diagramValues[ xValues[ xIndex ] ][ column ].second.isValid() )
172
 
                    {
173
 
                        right.first.first = xValues[ xIndex ];
174
 
                        right.first.second = diagramValues[ right.first.first ][ column ].first;
175
 
                        right.second = diagramValues[ xValues[ xIndex ] ][ column ].second;
176
 
                        break;
177
 
                    }
178
 
                }
179
 
 
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;
185
 
 
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;
190
 
            }
191
 
 
192
 
            // sum it up
193
 
            if( !ISNAN( yValues[ column ].first ) )
194
 
                yValueSums[ xValue ] += yValues[ column ].first;
195
 
        }
196
 
    }
197
 
 
198
 
    for( int column = 0; column < colCount; ++column )
199
 
    {
200
 
        LineAttributesInfoList lineList;
201
 
        LineAttributes laPreviousCell;
202
 
        CartesianDiagramDataCompressor::CachePosition previousCellPosition;
203
 
 
204
 
        CartesianDiagramDataCompressor::DataPoint lastPoint;
205
 
 
206
 
        qreal lastExtraY = 0.0;
207
 
        qreal lastValue = 0.0;
208
 
 
209
 
        QMapIterator< double, QVector< QPair< Value, QModelIndex > > >  i( diagramValues );
210
 
        while( i.hasNext() )
211
 
        {
212
 
            i.next();
213
 
            CartesianDiagramDataCompressor::DataPoint point;
214
 
            point.key = i.key();
215
 
            const QPair< Value, QModelIndex >& data = i.value().at( column );
216
 
            point.value = data.first;
217
 
            point.index = data.second;
218
 
 
219
 
            if( ISNAN( point.key ) || ISNAN( point.value ) )
220
 
            {
221
 
                previousCellPosition = CartesianDiagramDataCompressor::CachePosition();
222
 
                continue;
223
 
            }
224
 
 
225
 
            double extraY = 0.0;
226
 
            for( int col = column - 1; col >= 0; --col )
227
 
            {
228
 
                const double y = i.value().at( col ).first;
229
 
                if( !ISNAN( y ) )
230
 
                    extraY += y;
231
 
            }
232
 
 
233
 
            LineAttributes laCell;
234
 
 
235
 
            const qreal value = ( point.value + extraY ) / yValueSums[ i.key() ] * 100;
236
 
 
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() ) {
250
 
                QPolygonF polygon;
251
 
                polygon << a << b << d << c;
252
 
                areas << polygon;
253
 
            }
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,
258
 
                                               value );
259
 
                if( !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) )
260
 
                {
261
 
                    paintAreas( ctx, attributesModel()->mapToSource( lastPoint.index ), areas, laCell.transparency() );
262
 
                    lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
263
 
                }
264
 
            }
265
 
 
266
 
            // wrap it up:
267
 
            laPreviousCell = laCell;
268
 
            lastPoint = point;
269
 
            lastExtraY = extraY;
270
 
            lastValue = value;
271
 
        }
272
 
        paintElements( ctx, textInfoList, lineList, policy );
273
 
    }
274
 
}