Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
00104 int maxFound = 0;
00105
00106
00107
00108
00109
00110
00111
00112 maxFound = columnCount;
00113
00114
00115 DataValueTextInfoList list;
00116 LineAttributesInfoList lineList;
00117 LineAttributes::MissingValuesPolicy policy = LineAttributes::MissingValuesAreBridged;
00118
00119
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
00131 LineAttributes laPreviousCell;
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
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
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
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 }