kchart

KDChartSeriesCollection.cpp

00001 /* -*- Mode: C++ -*-
00002    KDChart - a multi-platform charting engine
00003    */
00004 
00005 /****************************************************************************
00006  ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
00007  **
00008  ** This file is part of the KDChart 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 KDChart licenses may use this file in
00016  ** accordance with the KDChart 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.klaralvdalens-datakonsult.se/?page=products for
00023  **   information about KDChart Commercial License Agreements.
00024  **
00025  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
00026  ** licensing are not clear to you.
00027  **
00028  **********************************************************************/
00029 
00030 
00031 #include "KDChartSeriesCollection.h"
00032 #include "KDChartParams.h"
00033 
00034 
00035 KDChartSeriesCollection::KDChartSeriesCollection( KDChartParams *params )
00036 {
00037     _params = params;
00038 }
00039 
00040 
00041 KDChartSeriesCollection::~KDChartSeriesCollection()
00042 {
00043     for ( int i = 0; i < static_cast < int > ( (*this).size() ); i ++ )
00044         delete (*this)[i];
00045 }
00046 
00047 
00048 // both rows return the same amount
00049 uint KDChartSeriesCollection::rows() const
00050 {
00051     return usedRows();
00052 }
00053 uint KDChartSeriesCollection::usedRows() const
00054 {
00055     return (*this).size();
00056 }
00057 
00058 
00059 uint KDChartSeriesCollection::cols() const
00060 {
00061     return usedCols();
00062 }
00063 
00064 
00065 uint KDChartSeriesCollection::usedCols() const
00066 {
00067     uint result = 0;
00068 
00069     // find the maximum number of rows in all the visible series
00070 
00071     for ( int i = 0; i < static_cast < int > ( (*this).size() ); i ++ )
00072         if ( result < (*this)[i]->rows())
00073             result = (*this)[i]->rows();
00074 
00075     return result;
00076 }
00077 
00078 // Don't really know how to handle these yet, I have no need yet.
00079 // It appears to be only used to load QTables.
00080 // ASSERT if used.
00081 void KDChartSeriesCollection::setUsedRows( uint )
00082 {
00083     Q_ASSERT(0);
00084 }
00085 void KDChartSeriesCollection::setUsedCols( uint )
00086 {
00087     Q_ASSERT(0);
00088 }
00089 
00090 
00091 void KDChartSeriesCollection::setCell( uint row, uint col,
00092         const KDChartData& element )
00093 {
00094     Q_ASSERT( row < (*this).size() );
00095     this->at(row)->setCell(col, element);
00096 }
00097 
00098 const KDChartData& KDChartSeriesCollection::cell( uint row, uint col ) const
00099 {
00100     Q_ASSERT( row < (*this).size() );
00101     // Put this back in if/when KHZ adds the performance improvements
00102     // re usedCols( uint row )   Q_ASSERT( col < (*this)[row]->rows() );
00103 
00104     if ( col < this->at(row)->rows() )
00105         return this->at(row)->cell(col);
00106     else
00107         return _blank;
00108 }
00109 
00110 void KDChartSeriesCollection::expand( uint cols, uint rows )
00111 {
00112     // first expand ourselves - cols-wise
00113     (*this).resize(rows);
00114 
00115     // now expand our babies
00116     for ( int i = 0; i < static_cast < int > ( (*this).size() ); i ++ )
00117         (*this)[i]->expand(cols);
00118 }
00119 
00120 
00121 
00122 // coordinate is the first or second value in a data point
00123 double KDChartSeriesCollection::maxValue( int coordinate ) const
00124 {
00125     // IF there are no series to read from, then just return zero.
00126     // KHZ: perhaps this should assert?
00127 
00128     bool ok;    // the ok is required in case we check a PlaneSeries, which
00129     // cannot possibly have a min or max on one of the axis.
00130 
00131     double result = 0;      // if no valid min/max, then this is the default
00132     bool first_max = true;
00133 
00134     // find the first max
00135 #if COMPAT_QT_VERSION >= 0x030000
00136     QValueVector<KDChartBaseSeries *>::const_iterator i;
00137 #else
00138     QArray<KDChartBaseSeries *>::ConstIterator i;
00139 #endif
00140     for ( i = (*this).begin(); i != (*this).end(); i ++ )
00141     {
00142         double temp = (*i)->maxValue(coordinate, ok);
00143         if ( ok && (first_max || temp > result) )
00144         {
00145             first_max = false;
00146             result = temp;
00147         }
00148     }
00149 
00150     return result;
00151 }
00152 
00153 
00154 
00155 double KDChartSeriesCollection::minValue( int coordinate ) const
00156 {
00157     // IF there are no series to read from, then just return zero.
00158     // KHZ: perhaps this should assert?
00159 
00160     bool ok = false;    // the ok is required in case we check a PlaneSeries, which
00161     // cannot possibly have a min or max on one of the axis.
00162 
00163     double result = 0;      // if no valid min/max, then this is the default
00164 
00165     // find the first min
00166 #if COMPAT_QT_VERSION >= 0x030000
00167     QValueVector<KDChartBaseSeries *>::const_iterator i;
00168 #else
00169     QArray<KDChartBaseSeries *>::ConstIterator i;
00170 #endif
00171     for ( i = (*this).begin(); !ok && i != (*this).end(); i ++ )
00172         result = (*i)->minValue(coordinate, ok);
00173 
00174     if ( ok )
00175         for ( ; i != (*this).end(); i ++ )
00176         {
00177             double temp = (*i)->minValue(coordinate, ok);
00178             if (ok)
00179                 result = QMIN( result, temp );
00180         }
00181 
00182     return result;
00183 }
00184 
00185 
00186 unsigned int KDChartSeriesCollection::indexOf( KDChartBaseSeries *series )
00187 {
00188     unsigned int index = 0;
00189 #if COMPAT_QT_VERSION >= 0x030000
00190     QValueVector<KDChartBaseSeries *>::const_iterator i;
00191 #else
00192     QArray<KDChartBaseSeries *>::ConstIterator i;
00193 #endif
00194     for ( i = (*this).begin(); i != (*this).end(); i ++, index ++ )
00195         if ( *i == series )
00196             break;
00197 
00198     // must find it
00199     Q_ASSERT( index < (*this).size() );
00200 
00201     return index;
00202 }
00203 
00204 
00205 void KDChartSeriesCollection::setLegendText( KDChartBaseSeries *series, QString text )
00206 {
00207     _params->setLegendText( indexOf( series ), text );
00208 }
00209 
00210 
00211 
00212 void KDChartSeriesCollection::setYaxis( KDChartBaseSeries *series, 
00213         KDChartAxisParams::AxisPos axis )
00214 {
00215     unsigned int index = indexOf( series );
00216     _params->setAxisDatasets( axis, index, index, 0 );
00217 }
00218 
00219 
00220 
00221 
00222 QString KDChartSeriesCollection::legendText( KDChartBaseSeries *series )
00223 {
00224     return _params->legendText( indexOf(series) );
00225 }
00226 
00227 
00228 KDChartAxisParams::AxisPos KDChartSeriesCollection::yAxis( KDChartBaseSeries *series )
00229 {
00230     unsigned int index = indexOf( series );
00231     unsigned int tempchart = 0; // needed cause for some reason KHZ wants a reference.
00232 
00233     // now we have to look through
00234     for ( int i = 0; i < KDCHART_MAX_AXES; i ++ )
00235         if ( _params->axisDatasets( i, index, index, tempchart ) )
00236             return (KDChartAxisParams::AxisPos) i;
00237 
00238     Q_ASSERT(0);    // should find it
00239     return (KDChartAxisParams::AxisPos) 0;
00240 }
KDE Home | KDE Accessibility Home | Description of Access Keys