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 "KDChartLineDiagram.h"
00031 #include "KDChartDataValueAttributes.h"
00032
00033 #include "KDChartLineDiagram_p.h"
00034
00035 using namespace KDChart;
00036 using namespace std;
00037
00038 LineDiagram::Private::Private( const Private& rhs )
00039 : AbstractCartesianDiagram::Private( rhs )
00040 {
00041 }
00042
00043 void LineDiagram::Private::paintPolyline(
00044 PaintContext* ctx,
00045 const QBrush& brush, const QPen& pen,
00046 const QPolygonF& points ) const
00047 {
00048 ctx->painter()->setBrush( brush );
00049 ctx->painter()->setPen( PrintingParameters::scalePen(
00050 QPen( pen.color(),
00051 pen.width(),
00052 pen.style(),
00053 Qt::FlatCap,
00054 Qt::MiterJoin ) ) );
00055 #if QT_VERSION > 0x040299
00056 ctx->painter()->drawPolyline( points );
00057 #else
00058
00059
00060
00061 for (int i = 0; i < points.size()-1; ++i)
00062 ctx->painter()->drawLine( points.at(i), points.at(i+1) );
00063 #endif
00064 }
00065
00071 const QPointF LineDiagram::LineDiagramType::project(
00072 QPointF point, QPointF maxLimits,
00073 double z, const QModelIndex& index ) const
00074 {
00075 Q_UNUSED( maxLimits );
00076 ThreeDLineAttributes td = diagram()->threeDLineAttributes( index );
00077
00078
00079 double xrad = DEGTORAD( td.lineXRotation() );
00080 double yrad = DEGTORAD( td.lineYRotation() );
00081 QPointF ret = QPointF(point.x()*cos( yrad ) + z * sin( yrad ) , point.y()*cos( xrad ) - z * sin( xrad ) );
00082 return ret;
00083 }
00084
00085 void LineDiagram::LineDiagramType::paintThreeDLines(
00086 PaintContext* ctx, const QModelIndex& index,
00087 const QPointF& from, const QPointF& to, const double depth )
00088 {
00089
00090 const QPair< QPointF, QPointF > boundaries = diagram()->dataBoundaries();
00091 const QPointF& maxLimits = boundaries.second;
00092 const QPointF topLeft = project( from, maxLimits, depth, index );
00093 const QPointF topRight = project ( to, maxLimits, depth, index );
00094
00095 const QPolygonF segment = QPolygonF() << from << topLeft << topRight << to;
00096 const QBrush indexBrush ( diagram()->brush( index ) );
00097 const PainterSaver painterSaver( ctx->painter() );
00098
00099 if( diagram()->antiAliasing() )
00100 ctx->painter()->setRenderHint( QPainter::Antialiasing );
00101
00102 ctx->painter()->setBrush( indexBrush );
00103 ctx->painter()->setPen( PrintingParameters::scalePen( diagram()->pen( index ) ) );
00104
00105 reverseMapper().addPolygon( index.row(), index.column(), segment );
00106 ctx->painter()->drawPolygon( segment );
00107 }
00108
00109
00110
00111
00112 void LineDiagram::LineDiagramType::paintElements(
00113 PaintContext* ctx,
00114 DataValueTextInfoList& list,
00115 LineAttributesInfoList& lineList,
00116 LineAttributes::MissingValuesPolicy policy )
00117 {
00118 Q_UNUSED( policy );
00119
00120 const PainterSaver painterSaver( ctx->painter() );
00121 if ( diagram()->antiAliasing() )
00122 ctx->painter()->setRenderHint ( QPainter::Antialiasing );
00123 LineAttributesInfoListIterator itline ( lineList );
00124
00125 QBrush curBrush;
00126 QPen curPen;
00127 QPolygonF points;
00128 while ( itline.hasNext() ) {
00129 const LineAttributesInfo& lineInfo = itline.next();
00130 const QModelIndex& index = lineInfo.index;
00131 const ThreeDLineAttributes td = diagram()->threeDLineAttributes( index );
00132 const ValueTrackerAttributes vt = diagram()->valueTrackerAttributes( index );
00133
00134 if( td.isEnabled() ){
00135 paintThreeDLines( ctx, index, lineInfo.value, lineInfo.nextValue, td.depth() );
00136 } else {
00137 const QBrush br( diagram()->brush( index ) );
00138 const QPen pn( diagram()->pen( index ) );
00139 if( points.count() && points.last() == lineInfo.value && curBrush == br && curPen == pn ) {
00140
00141 reverseMapper().addLine( lineInfo.index.row(), lineInfo.index.column(), points.last(), lineInfo.nextValue );
00142 points << lineInfo.nextValue;
00143 } else {
00144 if( points.count() )
00145 paintPolyline( ctx, curBrush, curPen, points );
00146 curBrush = br;
00147 curPen = pn;
00148 points.clear();
00149
00150 reverseMapper().addLine( lineInfo.index.row(), lineInfo.index.column(), lineInfo.value, lineInfo.nextValue );
00151 points << lineInfo.value << lineInfo.nextValue;
00152 }
00153 }
00154
00155 if( vt.isEnabled() )
00156 paintValueTracker( ctx, vt, lineInfo.value );
00157 }
00158 if( points.count() )
00159 paintPolyline( ctx, curBrush, curPen, points );
00160
00161 paintDataValueTextsAndMarkers( diagram(), ctx, list, true );
00162 }
00163
00164 AttributesModel* LineDiagram::LineDiagramType::attributesModel() const
00165 {
00166 return m_private->attributesModel;
00167 }
00168
00169 QModelIndex LineDiagram::LineDiagramType::attributesModelRootIndex() const
00170 {
00171 return m_private->diagram->attributesModelRootIndex();
00172 }
00173
00174 int LineDiagram::LineDiagramType::datasetDimension() const
00175 {
00176 return m_private->datasetDimension;
00177 }
00178
00179 ReverseMapper& LineDiagram::LineDiagramType::reverseMapper()
00180 {
00181 return m_private->reverseMapper;
00182 }
00183
00184 LineAttributes::MissingValuesPolicy LineDiagram::LineDiagramType::getCellValues(
00185 int row, int column,
00186 bool shiftCountedXValuesByHalfSection,
00187 double& valueX, double& valueY ) const
00188 {
00189 return m_private->diagram->getCellValues( row, column, shiftCountedXValuesByHalfSection,
00190 valueX, valueY );
00191 }
00192
00193 double LineDiagram::LineDiagramType::valueForCellTesting(
00194 int row, int column,
00195 bool& bOK,
00196 bool showHiddenCellsAsInvalid) const
00197 {
00198 return m_private->diagram->valueForCellTesting( row, column, bOK, showHiddenCellsAsInvalid );
00199 }
00200
00201 LineDiagram* LineDiagram::LineDiagramType::diagram() const
00202 {
00203 return m_private->diagram;
00204 }
00205
00206 void LineDiagram::LineDiagramType::paintAreas(
00207 PaintContext* ctx,
00208 const QModelIndex& index, const QList< QPolygonF >& areas,
00209 const uint transparency )
00210 {
00211 QColor trans = diagram()->brush( index ).color();
00212 trans.setAlpha( transparency );
00213 QPen indexPen = diagram()->pen(index);
00214 indexPen.setColor( trans );
00215 const PainterSaver painterSaver( ctx->painter() );
00216
00217 if( diagram()->antiAliasing() )
00218 ctx->painter()->setRenderHint( QPainter::Antialiasing );
00219
00220 ctx->painter()->setPen( PrintingParameters::scalePen( indexPen ) );
00221 ctx->painter()->setBrush( trans );
00222
00223 QPainterPath path;
00224 for( int i = 0; i < areas.count(); ++i )
00225 {
00226 const QPolygonF& p = areas[ i ];
00227 path.addPolygon( p );
00228 reverseMapper().addPolygon( index.row(), index.column(), p );
00229 path.closeSubpath();
00230 }
00231 ctx->painter()->drawPath( path );
00232 }
00233
00234 double LineDiagram::LineDiagramType::valueForCell( int row, int column )
00235 {
00236 return diagram()->valueForCell( row, column );
00237 }
00238
00239 void LineDiagram::LineDiagramType::appendDataValueTextInfoToList(
00240 AbstractDiagram * diagram,
00241 DataValueTextInfoList & list,
00242 const QModelIndex & index,
00243 const CartesianDiagramDataCompressor::CachePosition * position,
00244 const PositionPoints& points,
00245 const Position& autoPositionPositive,
00246 const Position& autoPositionNegative,
00247 const qreal value )
00248 {
00249 Q_UNUSED( autoPositionNegative );
00250 m_private->appendDataValueTextInfoToList( diagram, list, index, position, points,
00251 autoPositionPositive, autoPositionPositive, value );
00252 }
00253
00254 void LineDiagram::LineDiagramType::paintValueTracker( PaintContext* ctx, const ValueTrackerAttributes& vt, const QPointF& at )
00255 {
00256 CartesianCoordinatePlane* plane = qobject_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() );
00257 if( !plane )
00258 return;
00259
00260 DataDimensionsList gridDimensions = ctx->coordinatePlane()->gridDimensionsList();
00261 const QPointF bottomLeft( ctx->coordinatePlane()->translate(
00262 QPointF( plane->isHorizontalRangeReversed() ?
00263 gridDimensions.at( 0 ).end :
00264 gridDimensions.at( 0 ).start,
00265 plane->isVerticalRangeReversed() ?
00266 gridDimensions.at( 1 ).end :
00267 gridDimensions.at( 1 ).start ) ) );
00268 const QPointF markerPoint = at;
00269 const QPointF ordinatePoint( bottomLeft.x(), at.y() );
00270 const QPointF abscissaPoint( at.x(), bottomLeft.y() );
00271
00272 const QSizeF markerSize = vt.markerSize();
00273 const QRectF ellipseMarker = QRectF( at.x() - markerSize.width() / 2,
00274 at.y() - markerSize.height() / 2,
00275 markerSize.width(), markerSize.height() );
00276
00277 const QPointF ordinateMarker[3] = {
00278 QPointF( ordinatePoint.x(), at.y() + markerSize.height() / 2 ),
00279 QPointF( ordinatePoint.x() + markerSize.width() / 2, at.y() ),
00280 QPointF( ordinatePoint.x(), at.y() - markerSize.height() / 2 )
00281 };
00282
00283 const QPointF abscissaMarker[3] = {
00284 QPointF( at.x() + markerSize.width() / 2, abscissaPoint.y() ),
00285 QPointF( at.x(), abscissaPoint.y() - markerSize.height() / 2 ),
00286 QPointF( at.x() - markerSize.width() / 2, abscissaPoint.y() )
00287 };
00288
00289 QPointF topLeft = ordinatePoint;
00290 QPointF bottomRightOffset = abscissaPoint - topLeft;
00291 QSizeF size( bottomRightOffset.x(), bottomRightOffset.y() );
00292 QRectF area( topLeft, size );
00293
00294 PainterSaver painterSaver( ctx->painter() );
00295 ctx->painter()->setPen( PrintingParameters::scalePen( vt.pen() ) );
00296 ctx->painter()->setBrush( QBrush() );
00297
00298 ctx->painter()->drawLine( markerPoint, ordinatePoint );
00299 ctx->painter()->drawLine( markerPoint, abscissaPoint );
00300
00301 ctx->painter()->fillRect( area, vt.areaBrush() );
00302
00303 ctx->painter()->drawEllipse( ellipseMarker );
00304
00305 ctx->painter()->setBrush( vt.pen().color() );
00306 ctx->painter()->drawPolygon( ordinateMarker, 3 );
00307 ctx->painter()->drawPolygon( abscissaMarker, 3 );
00308 }
00309
00310 CartesianDiagramDataCompressor& LineDiagram::LineDiagramType::compressor() const
00311 {
00312 return m_private->compressor;
00313 }
00314
00315 double LineDiagram::LineDiagramType::interpolateMissingValue( const CartesianDiagramDataCompressor::CachePosition& pos ) const
00316 {
00317 double leftValue = std::numeric_limits< double >::quiet_NaN();
00318 double rightValue = std::numeric_limits< double >::quiet_NaN();
00319 int missingCount = 1;
00320
00321 const int column = pos.second;
00322 const int row = pos.first;
00323 const int rowCount = compressor().modelDataRows();
00324
00325
00326 for( int r1 = row - 1; r1 > 0; --r1 )
00327 {
00328 const CartesianDiagramDataCompressor::CachePosition position( r1, column );
00329 const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00330 leftValue = point.value;
00331 if( !ISNAN( point.value ) )
00332 break;
00333 ++missingCount;
00334 }
00335 for( int r2 = row + 1; r2 < rowCount; ++r2 )
00336 {
00337 const CartesianDiagramDataCompressor::CachePosition position( r2, column );
00338 const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00339 rightValue = point.value;
00340 if( !ISNAN( point.value ) )
00341 break;
00342 ++missingCount;
00343 }
00344 if( !ISNAN( leftValue ) && !ISNAN( rightValue ) )
00345 return leftValue + ( rightValue - leftValue ) / ( missingCount + 1 );
00346 else
00347 return std::numeric_limits< double >::quiet_NaN();
00348 }