kchart

KDChartSeriesCollection.cpp
1/*
2 KDChart - a multi-platform charting engine
3 */
4
5/****************************************************************************
6 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB. All rights reserved.
7 **
8 ** This file is part of the KDChart library.
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.GPL included in the
13 ** packaging of this file.
14 **
15 ** Licensees holding valid commercial KDChart licenses may use this file in
16 ** accordance with the KDChart Commercial License Agreement provided with
17 ** the Software.
18 **
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 **
22 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
23 ** information about KDChart Commercial License Agreements.
24 **
25 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
26 ** licensing are not clear to you.
27 **
28 **********************************************************************/
29
30
31#include "KDChartSeriesCollection.h"
32#include "KDChartParams.h"
33
34
35KDChartSeriesCollection::KDChartSeriesCollection( KDChartParams *params )
36{
37 _params = params;
38}
39
40
41KDChartSeriesCollection::~KDChartSeriesCollection()
42{
43 for ( int i = 0; i < static_cast < int > ( (*this).size() ); i ++ )
44 delete (*this)[i];
45}
46
47
48// both rows return the same amount
49uint KDChartSeriesCollection::rows() const
50{
51 return usedRows();
52}
53uint KDChartSeriesCollection::usedRows() const
54{
55 return (*this).size();
56}
57
58
59uint KDChartSeriesCollection::cols() const
60{
61 return usedCols();
62}
63
64
65uint KDChartSeriesCollection::usedCols() const
66{
67 uint result = 0;
68
69 // find the maximum number of rows in all the visible series
70
71 for ( int i = 0; i < static_cast < int > ( (*this).size() ); i ++ )
72 if ( result < (*this)[i]->rows())
73 result = (*this)[i]->rows();
74
75 return result;
76}
77
78// Don't really know how to handle these yet, I have no need yet.
79// It appears to be only used to load TQTables.
80// ASSERT if used.
81void KDChartSeriesCollection::setUsedRows( uint )
82{
83 Q_ASSERT(0);
84}
85void KDChartSeriesCollection::setUsedCols( uint )
86{
87 Q_ASSERT(0);
88}
89
90
91void KDChartSeriesCollection::setCell( uint row, uint col,
92 const KDChartData& element )
93{
94 Q_ASSERT( row < (*this).size() );
95 this->at(row)->setCell(col, element);
96}
97
98const KDChartData& KDChartSeriesCollection::cell( uint row, uint col ) const
99{
100 Q_ASSERT( row < (*this).size() );
101 // Put this back in if/when KHZ adds the performance improvements
102 // re usedCols( uint row ) Q_ASSERT( col < (*this)[row]->rows() );
103
104 if ( col < this->at(row)->rows() )
105 return this->at(row)->cell(col);
106 else
107 return _blank;
108}
109
110void KDChartSeriesCollection::expand( uint cols, uint rows )
111{
112 // first expand ourselves - cols-wise
113 (*this).resize(rows);
114
115 // now expand our babies
116 for ( int i = 0; i < static_cast < int > ( (*this).size() ); i ++ )
117 (*this)[i]->expand(cols);
118}
119
120
121
122// coordinate is the first or second value in a data point
123double KDChartSeriesCollection::maxValue( int coordinate ) const
124{
125 // IF there are no series to read from, then just return zero.
126 // KHZ: perhaps this should assert?
127
128 bool ok; // the ok is required in case we check a PlaneSeries, which
129 // cannot possibly have a min or max on one of the axis.
130
131 double result = 0; // if no valid min/max, then this is the default
132 bool first_max = true;
133
134 // find the first max
135 TQValueVector<KDChartBaseSeries *>::const_iterator i;
136 for ( i = (*this).begin(); i != (*this).end(); i ++ )
137 {
138 double temp = (*i)->maxValue(coordinate, ok);
139 if ( ok && (first_max || temp > result) )
140 {
141 first_max = false;
142 result = temp;
143 }
144 }
145
146 return result;
147}
148
149
150
151double KDChartSeriesCollection::minValue( int coordinate ) const
152{
153 // IF there are no series to read from, then just return zero.
154 // KHZ: perhaps this should assert?
155
156 bool ok = false; // the ok is required in case we check a PlaneSeries, which
157 // cannot possibly have a min or max on one of the axis.
158
159 double result = 0; // if no valid min/max, then this is the default
160
161 // find the first min
162 TQValueVector<KDChartBaseSeries *>::const_iterator i;
163 for ( i = (*this).begin(); !ok && i != (*this).end(); i ++ )
164 result = (*i)->minValue(coordinate, ok);
165
166 if ( ok )
167 for ( ; i != (*this).end(); i ++ )
168 {
169 double temp = (*i)->minValue(coordinate, ok);
170 if (ok)
171 result = TQMIN( result, temp );
172 }
173
174 return result;
175}
176
177
178unsigned int KDChartSeriesCollection::indexOf( KDChartBaseSeries *series )
179{
180 unsigned int index = 0;
181 TQValueVector<KDChartBaseSeries *>::const_iterator i;
182 for ( i = (*this).begin(); i != (*this).end(); i ++, index ++ )
183 if ( *i == series )
184 break;
185
186 // must find it
187 Q_ASSERT( index < (*this).size() );
188
189 return index;
190}
191
192
193void KDChartSeriesCollection::setLegendText( KDChartBaseSeries *series, TQString text )
194{
195 _params->setLegendText( indexOf( series ), text );
196}
197
198
199
200void KDChartSeriesCollection::setYaxis( KDChartBaseSeries *series,
201 KDChartAxisParams::AxisPos axis )
202{
203 unsigned int index = indexOf( series );
204 _params->setAxisDatasets( axis, index, index, 0 );
205}
206
207
208
209
210TQString KDChartSeriesCollection::legendText( KDChartBaseSeries *series )
211{
212 return _params->legendText( indexOf(series) );
213}
214
215
216KDChartAxisParams::AxisPos KDChartSeriesCollection::yAxis( KDChartBaseSeries *series )
217{
218 unsigned int index = indexOf( series );
219 unsigned int tempchart = 0; // needed cause for some reason KHZ wants a reference.
220
221 // now we have to look through
222 for ( int i = 0; i < KDCHART_MAX_AXES; i ++ )
223 if ( _params->axisDatasets( i, index, index, tempchart ) )
224 return (KDChartAxisParams::AxisPos) i;
225
226 Q_ASSERT(0); // should find it
227 return (KDChartAxisParams::AxisPos) 0;
228}
Header for all common chart parameters.
#define KDCHART_MAX_AXES
Charts have up to 12 axes: up to two axes on both sides for X, Y and Z (if data is 3-dimensional).
Encapsulates one data cell in a chart.
Bundles the display parameters of a chart.
virtual void setCell(uint _row, uint _col, const TQVariant &_value1, const TQVariant &_value2=TQVariant())=0
Stores data in a cell.