KDChartStackedLineDiagram_p.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "KDChartStackedLineDiagram_p.h"
00024 
00025 #include <QAbstractItemModel>
00026 
00027 #include "KDChartBarDiagram.h"
00028 #include "KDChartLineDiagram.h"
00029 #include "KDChartTextAttributes.h"
00030 #include "KDChartAttributesModel.h"
00031 #include "KDChartAbstractCartesianDiagram.h"
00032 
00033 using namespace KDChart;
00034 using namespace std;
00035 
00036 StackedLineDiagram::StackedLineDiagram( LineDiagram* d )
00037     : LineDiagramType( d )
00038 {
00039 }
00040 
00041 LineDiagram::LineType StackedLineDiagram::type() const
00042 {
00043     return LineDiagram::Stacked;
00044 }
00045 
00046 const QPair<QPointF, QPointF> StackedLineDiagram::calculateDataBoundaries() const
00047 {
00048     const int rowCount = compressor().modelDataRows();
00049     const int colCount = compressor().modelDataColumns();
00050     const double xMin = 0;
00051     double xMax = diagram()->model() ? diagram()->model()->rowCount( diagram()->rootIndex() ) : 0;
00052     if ( !diagram()->centerDataPoints() && diagram()->model() )
00053         xMax -= 1;
00054     double yMin = 0, yMax = 0;
00055 
00056     bool bStarting = true;
00057     for( int row = 0; row < rowCount; ++row )
00058     {
00059         // calculate sum of values per column - Find out stacked Min/Max
00060         double stackedValues = 0.0;
00061         double negativeStackedValues = 0.0;
00062         for( int col = datasetDimension() - 1; col < colCount; col += datasetDimension() ) {
00063             const CartesianDiagramDataCompressor::CachePosition position( row, col );
00064             const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00065 
00066             if( ISNAN( point.value ) )
00067                 continue;
00068 
00069             if( point.value >= 0.0 )
00070                 stackedValues += point.value;
00071             else
00072                 negativeStackedValues += point.value;
00073         }
00074 
00075         if( bStarting ){
00076             yMin = stackedValues;
00077             yMax = stackedValues;
00078             bStarting = false;
00079         }else{
00080             // take in account all stacked values
00081             yMin = qMin( qMin( yMin, negativeStackedValues ), stackedValues );
00082             yMax = qMax( qMax( yMax, negativeStackedValues ), stackedValues );
00083         }
00084     }
00085 
00086     const QPointF bottomLeft( xMin, yMin );
00087     const QPointF topRight( xMax, yMax );
00088 
00089     return QPair<QPointF, QPointF> ( bottomLeft, topRight );
00090 }
00091 
00092 void StackedLineDiagram::paint(  PaintContext* ctx )
00093 {
00094     reverseMapper().clear();
00095 
00096     const QPair<QPointF, QPointF> boundaries = diagram()->dataBoundaries();
00097     const QPointF bottomLeft = boundaries.first;
00098     const QPointF topRight = boundaries.second;
00099 
00100     const int columnCount = compressor().modelDataColumns();
00101     const int rowCount = compressor().modelDataRows();
00102 
00103 // FIXME integrate column index retrieval to compressor:
00104     int maxFound = 0;
00105 //    {   // find the last column number that is not hidden
00106 //        for( int iColumn =  datasetDimension() - 1;
00107 //             iColumn <  columnCount;
00108 //             iColumn += datasetDimension() )
00109 //            if( ! diagram()->isHidden( iColumn ) )
00110 //                maxFound = iColumn;
00111 //    }
00112     maxFound = columnCount;
00113     // ^^^ temp
00114 
00115     DataValueTextInfoList list;
00116     LineAttributesInfoList lineList;
00117     LineAttributes::MissingValuesPolicy policy = LineAttributes::MissingValuesAreBridged;
00118 
00119     //FIXME(khz): add LineAttributes::MissingValuesPolicy support for LineDiagram::Stacked and ::Percent
00120 
00121     QVector <double > percentSumValues;
00122 
00123     QList<QPointF> bottomPoints;
00124     bool bFirstDataset = true;
00125 
00126     for( int column = 0; column < columnCount; ++column )
00127     {
00128         CartesianDiagramDataCompressor::CachePosition previousCellPosition;
00129 
00130         //display area can be set by dataset ( == column) and/or by cell
00131         LineAttributes laPreviousCell; // by default no area is drawn
00132         QModelIndex indexPreviousCell;
00133         QList<QPolygonF> areas;
00134         QList<QPointF> points;
00135 
00136         for ( int row = 0; row < rowCount; ++row ) {
00137             const CartesianDiagramDataCompressor::CachePosition position( row, column );
00138             CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00139             const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
00140 
00141             const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
00142             const bool bDisplayCellArea = laCell.displayArea();
00143 
00144             const LineAttributes::MissingValuesPolicy policy = laCell.missingValuesPolicy();
00145 
00146             if( ISNAN( point.value ) && policy == LineAttributes::MissingValuesShownAsZero )
00147                 point.value = 0.0;
00148 
00149             double stackedValues = 0, nextValues = 0, nextKey = 0;
00150             for ( int column2 = column; column2 >= 0; --column2 )
00151             {
00152                 const CartesianDiagramDataCompressor::CachePosition position( row, column2 );
00153                 const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00154                 const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
00155                 if( !ISNAN( point.value ) )
00156                 {
00157                     stackedValues += point.value;
00158                 }
00159                 else if( policy == LineAttributes::MissingValuesAreBridged )
00160                 {
00161                     const double interpolation = interpolateMissingValue( position );
00162                     if( !ISNAN( interpolation ) )
00163                         stackedValues += interpolation;
00164                 }
00165 
00166                 //qDebug() << valueForCell( iRow, iColumn2 );
00167                 if ( row + 1 < rowCount ){
00168                     const CartesianDiagramDataCompressor::CachePosition position( row + 1, column2 );
00169                     const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00170                     if( !ISNAN( point.value ) )
00171                     {
00172                         nextValues += point.value;
00173                     }
00174                     else if( policy == LineAttributes::MissingValuesAreBridged )
00175                     {
00176                         const double interpolation = interpolateMissingValue( position );
00177                         if( !ISNAN( interpolation ) )
00178                             nextValues += interpolation;
00179                     }
00180                     nextKey = point.key;
00181                 }
00182             }
00183             //qDebug() << stackedValues << endl;
00184             const QPointF nextPoint = ctx->coordinatePlane()->translate( QPointF( diagram()->centerDataPoints() ? point.key + 0.5 : point.key, stackedValues ) );
00185             points << nextPoint;
00186 
00187             const QPointF ptNorthWest( nextPoint );
00188             const QPointF ptSouthWest(
00189                 bDisplayCellArea
00190                 ? ( bFirstDataset
00191                     ? ctx->coordinatePlane()->translate( QPointF( diagram()->centerDataPoints() ? point.key + 0.5 : point.key, 0.0 ) )
00192                     : bottomPoints.at( row )
00193                     )
00194                 : nextPoint );
00195             QPointF ptNorthEast;
00196             QPointF ptSouthEast;
00197 
00198             if ( row + 1 < rowCount ){
00199                 QPointF toPoint = ctx->coordinatePlane()->translate( QPointF( diagram()->centerDataPoints() ? nextKey + 0.5 : nextKey, nextValues ) );
00200                 lineList.append( LineAttributesInfo( sourceIndex, nextPoint, toPoint ) );
00201                 ptNorthEast = toPoint;
00202                 ptSouthEast =
00203                     bDisplayCellArea
00204                     ? ( bFirstDataset
00205                         ? ctx->coordinatePlane()->translate( QPointF( diagram()->centerDataPoints() ? nextKey + 0.5 : nextKey, 0.0 ) )
00206                         : bottomPoints.at( row + 1 )
00207                         )
00208                     : toPoint;
00209                 if( areas.count() && laCell != laPreviousCell ){
00210                     paintAreas( ctx, indexPreviousCell, areas, laPreviousCell.transparency() );
00211                     areas.clear();
00212                 }
00213                 if( bDisplayCellArea ){
00214                     QPolygonF poly;
00215                     poly << ptNorthWest << ptNorthEast << ptSouthEast << ptSouthWest;
00216                     areas << poly;
00217                     laPreviousCell = laCell;
00218                     indexPreviousCell = sourceIndex;
00219                 }else{
00220                     //qDebug() << "no area shown for row"<<iRow<<"  column"<<iColumn;
00221                 }
00222             }else{
00223                 ptNorthEast = ptNorthWest;
00224                 ptSouthEast = ptSouthWest;
00225             }
00226 
00227             const PositionPoints pts( ptNorthWest, ptNorthEast, ptSouthEast, ptSouthWest );
00228             if( !ISNAN( point.value ) )
00229                 appendDataValueTextInfoToList( diagram(), list, sourceIndex, &position,
00230                                                pts, Position::NorthWest, Position::SouthWest,
00231                                                point.value );
00232         }
00233         if( areas.count() ){
00234             paintAreas( ctx, indexPreviousCell, areas, laPreviousCell.transparency() );
00235             areas.clear();
00236         }
00237         bottomPoints = points;
00238         bFirstDataset = false;
00239     }
00240     paintElements( ctx, list, lineList, policy );
00241 }