KDChartStackedLineDiagram_p.cpp

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

Generated on Thu Mar 4 23:19:12 2010 for KD Chart 2 by  doxygen 1.5.4