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 <QAbstractItemModel>
00031
00032 #include "KDChartBarDiagram.h"
00033 #include "KDChartLineDiagram.h"
00034 #include "KDChartTextAttributes.h"
00035 #include "KDChartAttributesModel.h"
00036 #include "KDChartAbstractCartesianDiagram.h"
00037 #include "KDChartStackedLineDiagram_p.h"
00038
00039 using namespace KDChart;
00040
00041 StackedLineDiagram::StackedLineDiagram( LineDiagram* d )
00042 : LineDiagramType( d )
00043 {
00044 }
00045
00046 LineDiagram::LineType StackedLineDiagram::type() const
00047 {
00048 return LineDiagram::Stacked;
00049 }
00050
00051 const QPair<QPointF, QPointF> StackedLineDiagram::calculateDataBoundaries() const
00052 {
00053 const int rowCount = compressor().modelDataRows();
00054 const int colCount = compressor().modelDataColumns();
00055 double xMin = 0;
00056 double xMax = diagram()->model() ? diagram()->model()->rowCount( diagram()->rootIndex() ) - 1 : 0;
00057 double yMin = 0, yMax = 0;
00058
00059 bool bStarting = true;
00060 for( int row = 0; row < rowCount; ++row )
00061 {
00062
00063 double stackedValues = 0;
00064 for( int col = datasetDimension() - 1; col < colCount; col += datasetDimension() ) {
00065 CartesianDiagramDataCompressor::CachePosition position( row, col );
00066 CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00067
00068 stackedValues += point.value;
00069 }
00070
00071 if( bStarting ){
00072 yMin = stackedValues;
00073 yMax = stackedValues;
00074 bStarting = false;
00075 }else{
00076
00077
00078
00079 yMin = qMin( qMin( yMin, 0.0 ), stackedValues );
00080 yMax = qMax( yMax, stackedValues );
00081 }
00082 }
00083
00084 QPointF bottomLeft( QPointF( xMin, yMin ) );
00085 QPointF topRight( QPointF( xMax, yMax ) );
00086 return QPair<QPointF, QPointF> ( bottomLeft, topRight );
00087 }
00088
00089 void StackedLineDiagram::paint( PaintContext* ctx )
00090 {
00091 reverseMapper().clear();
00092
00093 const QPair<QPointF, QPointF> boundaries = diagram()->dataBoundaries();
00094 const QPointF bottomLeft = boundaries.first;
00095 const QPointF topRight = boundaries.second;
00096
00097 const int columnCount = compressor().modelDataColumns();
00098 const int rowCount = compressor().modelDataRows();
00099
00100
00101 int maxFound = 0;
00102
00103
00104
00105
00106
00107
00108
00109 maxFound = columnCount;
00110
00111
00112 DataValueTextInfoList list;
00113 LineAttributesInfoList lineList;
00114 LineAttributes::MissingValuesPolicy policy;
00115
00116
00117
00118 QVector <double > percentSumValues;
00119
00120 QList<QPointF> bottomPoints;
00121 bool bFirstDataset = true;
00122
00123 for( int column = 0; column < columnCount; ++column )
00124 {
00125 CartesianDiagramDataCompressor::CachePosition previousCellPosition;
00126
00127
00128 LineAttributes laPreviousCell;
00129 QModelIndex indexPreviousCell;
00130 QList<QPolygonF> areas;
00131 QList<QPointF> points;
00132
00133 for ( int row = 0; row < rowCount; ++row ) {
00134 const CartesianDiagramDataCompressor::CachePosition position( row, column );
00135 const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00136 const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
00137
00138 const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
00139 const bool bDisplayCellArea = laCell.displayArea();
00140
00141 double stackedValues = 0, nextValues = 0, nextKey = 0;
00142 for ( int column2 = column; column2 >= 0; --column2 )
00143 {
00144 const CartesianDiagramDataCompressor::CachePosition position( row, column2 );
00145 const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00146 const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
00147 stackedValues += point.value;
00148
00149 if ( row + 1 < rowCount ){
00150 CartesianDiagramDataCompressor::CachePosition position( row + 1, column2 );
00151 CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00152 nextValues += point.value;
00153 nextKey = point.key;
00154 }
00155 }
00156
00157 const QPointF nextPoint = ctx->coordinatePlane()->translate( QPointF( point.key, stackedValues ) );
00158 points << nextPoint;
00159
00160 const QPointF ptNorthWest( nextPoint );
00161 const QPointF ptSouthWest(
00162 bDisplayCellArea
00163 ? ( bFirstDataset
00164 ? ctx->coordinatePlane()->translate( QPointF( point.key, 0.0 ) )
00165 : bottomPoints.at( row )
00166 )
00167 : nextPoint );
00168 QPointF ptNorthEast;
00169 QPointF ptSouthEast;
00170
00171 if ( row + 1 < rowCount ){
00172 QPointF toPoint = ctx->coordinatePlane()->translate( QPointF( nextKey, nextValues ) );
00173 lineList.append( LineAttributesInfo( sourceIndex, nextPoint, toPoint ) );
00174 ptNorthEast = toPoint;
00175 ptSouthEast =
00176 bDisplayCellArea
00177 ? ( bFirstDataset
00178 ? ctx->coordinatePlane()->translate( QPointF( nextKey, 0.0 ) )
00179 : bottomPoints.at( row + 1 )
00180 )
00181 : toPoint;
00182 if( areas.count() && laCell != laPreviousCell ){
00183 paintAreas( ctx, indexPreviousCell, areas, laPreviousCell.transparency() );
00184 areas.clear();
00185 }
00186 if( bDisplayCellArea ){
00187 QPolygonF poly;
00188 poly << ptNorthWest << ptNorthEast << ptSouthEast << ptSouthWest;
00189 areas << poly;
00190 laPreviousCell = laCell;
00191 indexPreviousCell = sourceIndex;
00192 }else{
00193
00194 }
00195 }else{
00196 ptNorthEast = ptNorthWest;
00197 ptSouthEast = ptSouthWest;
00198 }
00199
00200 const PositionPoints pts( ptNorthWest, ptNorthEast, ptSouthEast, ptSouthWest );
00201 appendDataValueTextInfoToList( diagram(), list, sourceIndex, pts,
00202 Position::NorthWest, Position::SouthWest,
00203 point.value );
00204 }
00205 if( areas.count() ){
00206 paintAreas( ctx, indexPreviousCell, areas, laPreviousCell.transparency() );
00207 areas.clear();
00208 }
00209 bottomPoints = points;
00210 bFirstDataset = false;
00211 }
00212 paintElements( ctx, list, lineList, policy );
00213 }