00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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
00111 int maxFound = 0;
00112
00113
00114
00115
00116
00117
00118
00119 maxFound = columnCount;
00120
00121
00122 DataValueTextInfoList list;
00123 LineAttributesInfoList lineList;
00124 LineAttributes::MissingValuesPolicy policy;
00125
00126
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
00138 LineAttributes laPreviousCell;
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
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
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
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 }