KDChartNormalLineDiagram_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 <limits>
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 #include "KDChartNormalLineDiagram_p.h"
00040 
00041 using namespace KDChart;
00042 using namespace std;
00043 
00044 NormalLineDiagram::NormalLineDiagram( LineDiagram* d )
00045     : LineDiagramType( d )
00046 {
00047 }
00048 
00049 LineDiagram::LineType NormalLineDiagram::type() const
00050 {
00051     return LineDiagram::Normal;
00052 }
00053 
00054 const QPair< QPointF, QPointF > NormalLineDiagram::calculateDataBoundaries() const
00055 {
00056     const int rowCount = compressor().modelDataRows();
00057     const int colCount = compressor().modelDataColumns();
00058     const double xMin = 0.0;
00059     double xMax = diagram()->model() ? diagram()->model()->rowCount( diagram()->rootIndex() ) : 0;
00060     if ( !diagram()->centerDataPoints() && diagram()->model() )
00061         xMax -= 1;
00062     double yMin = std::numeric_limits< double >::quiet_NaN();
00063     double yMax = std::numeric_limits< double >::quiet_NaN();
00064 
00065     for( int column = 0; column < colCount; ++column )
00066     {
00067         for ( int row = 0; row < rowCount; ++row )
00068         {
00069             const CartesianDiagramDataCompressor::CachePosition position( row, column );
00070             const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00071             const double value = ISNAN( point.value ) ? 0.0 : point.value;
00072 
00073             if ( ISNAN( yMin ) ) {
00074                     yMin = value;
00075                     yMax = value;
00076             } else {
00077                 yMin = qMin( yMin, value );
00078                 yMax = qMax( yMax, value );
00079             }
00080         }
00081     }
00082 
00083     // NOTE: calculateDataBoundaries must return the *real* data boundaries!
00084     //       i.e. we may NOT fake yMin to be qMin( 0.0, yMin )
00085     //       (khz, 2008-01-24)
00086     const QPointF bottomLeft( QPointF( xMin, yMin ) );
00087     const QPointF topRight( QPointF( xMax, yMax ) );
00088     return QPair< QPointF, QPointF >( bottomLeft, topRight );
00089 }
00090 
00091 void NormalLineDiagram::paint( PaintContext* ctx )
00092 {
00093     reverseMapper().clear();
00094     Q_ASSERT( dynamic_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() ) );
00095     CartesianCoordinatePlane* plane = static_cast<CartesianCoordinatePlane*>( ctx->coordinatePlane() );
00096     const int columnCount = compressor().modelDataColumns();
00097     const int rowCount = compressor().modelDataRows();
00098     if ( columnCount == 0 || rowCount == 0 ) return; // maybe blank out the area?
00099 
00100 // FIXME integrate column index retrieval to compressor:
00101 // the compressor should only pass through visiblel columns
00102     int maxFound = 0;
00103 //     {   // find the last column number that is not hidden
00104 //         for( int column =  datasetDimension() - 1;
00105 //              column <  columnCount;
00106 //              column += datasetDimension() )
00107 //             if( ! diagram()->isHidden( column ) )
00108 //                 maxFound = column;
00109 //     }
00110     maxFound = columnCount;
00111     // ^^^ temp
00112 
00113     for( int column = 0; column < columnCount; ++column ) {
00114         DataValueTextInfoList textInfoList;
00115         LineAttributesInfoList lineList;
00116         LineAttributes laPreviousCell;
00117         CartesianDiagramDataCompressor::DataPoint lastPoint;
00118         qreal lastAreaBoundingValue;
00119 
00120         // Get min. y value, used as lower or upper bounding for area highlighting
00121         const qreal minYValue = plane->visibleDataRange().bottom();
00122 
00123         CartesianDiagramDataCompressor::CachePosition previousCellPosition;
00124         for ( int row = 0; row < rowCount; ++row ) {
00125             const CartesianDiagramDataCompressor::CachePosition position( row, column );
00126             // get where to draw the line from:
00127             CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
00128 
00129             const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
00130 
00131             const LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
00132             const LineAttributes::MissingValuesPolicy policy = laCell.missingValuesPolicy();
00133 
00134             // lower or upper bounding for the highlighted area
00135             qreal areaBoundingValue;
00136             if ( laCell.areaBoundingDataset() != -1 ) {
00137                 const CartesianDiagramDataCompressor::CachePosition areaBoundingCachePosition( row, laCell.areaBoundingDataset() );
00138                 areaBoundingValue = compressor().data( areaBoundingCachePosition ).value;
00139             } else
00140                 // Use min. y value (i.e. zero line in most cases) if no bounding dataset is set
00141                 areaBoundingValue = minYValue;
00142 
00143             if( ISNAN( point.value ) )
00144             {
00145                 switch( policy )
00146                 {
00147                 case LineAttributes::MissingValuesAreBridged:
00148                     // we just bridge both values
00149                     continue;
00150                 case LineAttributes::MissingValuesShownAsZero:
00151                     // set it to zero
00152                     point.value = 0.0;
00153                     break;
00154                 case LineAttributes::MissingValuesHideSegments:
00155                     // they're just hidden
00156                     break;
00157                 default:
00158                     break;
00159                     // hm....
00160                 }
00161             }
00162 
00163             // area corners, a + b are the line ends:
00164             const QPointF a( plane->translate( QPointF( diagram()->centerDataPoints() ? lastPoint.key + 0.5 : lastPoint.key, lastPoint.value ) ) );
00165             const QPointF b( plane->translate( QPointF( diagram()->centerDataPoints() ? point.key + 0.5 : point.key, point.value ) ) );
00166             const QPointF c( plane->translate( QPointF( diagram()->centerDataPoints() ? lastPoint.key + 0.5 : lastPoint.key, lastAreaBoundingValue ) ) );
00167             const QPointF d( plane->translate( QPointF( diagram()->centerDataPoints() ? point.key + 0.5 : point.key, areaBoundingValue ) ) );
00168             // add the line to the list:
00169            // add data point labels:
00170             const PositionPoints pts = PositionPoints( b, a, d, c );
00171             // if necessary, add the area to the area list:
00172             QList<QPolygonF> areas;
00173             if ( !ISNAN( point.value ) && !ISNAN( lastPoint.value ) && laCell.displayArea() ) {
00174                 areas << ( QPolygonF() << a << b << d << c );//polygon;
00175             }
00176             // add the pieces to painting if this is not hidden:
00177             if ( ! point.hidden && !ISNAN( point.value ) )
00178             {
00179                 appendDataValueTextInfoToList( diagram(), textInfoList, sourceIndex, &position,
00180                                                pts, Position::NorthWest, Position::SouthWest,
00181                                                point.value );
00182                 paintAreas( ctx, attributesModel()->mapToSource( lastPoint.index ), areas, laCell.transparency() );
00183                 // position 0 is not really painted, since it takes two points to make a line :-)
00184                 if( row > 0 && !ISNAN( lastPoint.value ) )
00185                     lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
00186             }
00187 
00188             // wrap it up:
00189             previousCellPosition = position;
00190             laPreviousCell = laCell;
00191             lastAreaBoundingValue = areaBoundingValue;
00192             lastPoint = point;
00193         }
00194 
00195         LineAttributes::MissingValuesPolicy policy = LineAttributes::MissingValuesAreBridged; //unused
00196         paintElements( ctx, textInfoList, lineList, policy );
00197     }
00198 }

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