# KDChartNormalPlotter_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 "KDChartPlotter.h"
    00031 #include "KDChartNormalPlotter_p.h"
    00032
    00033 using namespace KDChart;
    00034
    00035 NormalPlotter::NormalPlotter( Plotter* d )
    00036     : PlotterType( d )
    00037 {
    00038 }
    00039
    00040 Plotter::PlotType NormalPlotter::type() const
    00041 {
    00042     return Plotter::Normal;
    00043 }
    00044
    00045 const QPair< QPointF, QPointF > NormalPlotter::calculateDataBoundaries() const
    00046 {
    00047     const int rowCount = compressor().modelDataRows();
    00048     const int colCount = compressor().modelDataColumns();
    00049     double xMin = 0;
    00050     double xMax = 0;
    00051     double yMin = 0;
    00052     double yMax = 0;
    00053
    00054     bool first = true;
    00055     for( int column = 0; column < colCount; ++column )
    00056     {
    00057         for ( int row = 0; row < rowCount; ++row )
    00058         {
    00059             const CartesianDiagramDataCompressor::CachePosition position( row, column );
    00060             const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
    00061
    00062             const double valueX = point.key;
    00063             const double valueY = point.value;
    00064
    00065             if( first )
    00066             {
    00067                 xMin = valueX;
    00068                 xMax = valueX;
    00069                 yMin = valueY;
    00070                 yMax = valueY;
    00071             }
    00072             else
    00073             {
    00074                 xMin = qMin( xMin, valueX );
    00075                 xMax = qMax( xMax, valueX );
    00076                 yMin = qMin( yMin, valueY );
    00077                 yMax = qMax( yMax, valueY );
    00078             }
    00079
    00080             first = false;
    00081         }
    00082     }
    00083
    00084     const QPointF bottomLeft( QPointF( xMin, qMin( 0.0, yMin ) ) );
    00085     const QPointF topRight( QPointF( xMax, yMax ) );
    00086     return QPair< QPointF, QPointF >( bottomLeft, topRight );
    00087 }
    00088
    00089 void NormalPlotter::paint( PaintContext* ctx )
    00090 {
    00091     reverseMapper().clear();
    00092
    00093     Q_ASSERT( dynamic_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() ) );
    00094     const CartesianCoordinatePlane* const plane = static_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() );
    00095     const int colCount = compressor().modelDataColumns();
    00096     const int rowCount = compressor().modelDataRows();
    00097
    00098     if( colCount == 0 || rowCount == 0 )
    00099         return;
    00100
    00101     DataValueTextInfoList textInfoList;
    00102     LineAttributesInfoList lineList;
    00103     LineAttributes::MissingValuesPolicy policy; // ???
    00104
    00105     for( int column = 0; column < colCount; ++column )
    00106     {
    00107         LineAttributes laPreviousCell;
    00108         CartesianDiagramDataCompressor::CachePosition previousCellPosition;
    00109
    00110         for( int row = 0; row < rowCount; ++row )
    00111         {
    00112             const CartesianDiagramDataCompressor::CachePosition position( row, column );
    00113             const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
    00114             LineAttributes laCell;
    00115             if( row > 0 )
    00116             {
    00117                 const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
    00118                 const CartesianDiagramDataCompressor::DataPoint lastPoint = compressor().data( previousCellPosition );
    00119                 // area corners, a + b are the line ends:
    00120                 const QPointF a( plane->translate( QPointF( lastPoint.key, lastPoint.value ) ) );
    00121                 const QPointF b( plane->translate( QPointF( point.key, point.value ) ) );
    00122                 const QPointF c( plane->translate( QPointF( lastPoint.key, 0.0 ) ) );
    00123                 const QPointF d( plane->translate( QPointF( point.key, 0.0 ) ) );
    00124                 // add the line to the list:
    00125                 laCell = diagram()->lineAttributes( sourceIndex );
    00126                 // add data point labels:
    00127                 const PositionPoints pts = PositionPoints( b, a, d, c );
    00128                 // if necessary, add the area to the area list:
    00129                 QList<QPolygonF> areas;
    00130                 if ( laCell.displayArea() ) {
    00131                     QPolygonF polygon;
    00132                     polygon << a << b << d << c;
    00133                     areas << polygon;
    00134                 }
    00135                 // add the pieces to painting if this is not hidden:
    00136                 if ( ! point.hidden ) {
    00137                     appendDataValueTextInfoToList( diagram(), textInfoList, sourceIndex, pts,
    00138                                                    Position::NorthWest, Position::SouthWest,
    00139                                                    point.value );
    00140                     paintAreas( ctx, attributesModel()->mapToSource( lastPoint.index ), areas, laCell.transparency() );
    00141                     lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
    00142                 }
    00143             }
    00144             // wrap it up:
    00145             previousCellPosition = position;
    00146             laPreviousCell = laCell;
    00147         }
    00148     }
    00149     paintElements( ctx, textInfoList, lineList, policy );
    00150 }