@@ -1,345 +1,345 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartdataset_p.h" |
|
22 | 22 | #include "qchartaxis.h" |
|
23 | 23 | //series |
|
24 | 24 | #include "qlineseries.h" |
|
25 | 25 | #include "qareaseries.h" |
|
26 | 26 | #include "qbarseries.h" |
|
27 | 27 | #include "qstackedbarseries.h" |
|
28 | 28 | #include "qpercentbarseries.h" |
|
29 | 29 | #include "qpieseries.h" |
|
30 | 30 | #include "qscatterseries.h" |
|
31 | 31 | #include "qsplineseries.h" |
|
32 | 32 | |
|
33 | 33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
34 | 34 | |
|
35 | 35 | ChartDataSet::ChartDataSet(QObject *parent):QObject(parent), |
|
36 | 36 | m_axisX(new QChartAxis(this)), |
|
37 | 37 | m_axisY(new QChartAxis(this)), |
|
38 | 38 | m_domainIndex(0), |
|
39 | 39 | m_axisXInitialized(false) |
|
40 | 40 | { |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | ChartDataSet::~ChartDataSet() |
|
44 | 44 | { |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY) |
|
48 | 48 | { |
|
49 | 49 | if(axisY==0) axisY = m_axisY; |
|
50 | 50 | |
|
51 | 51 | QChartAxis* axis = m_seriesAxisMap.value(series); |
|
52 | 52 | |
|
53 | 53 | if(axis) { |
|
54 | 54 | qWarning() << "Can not add series. Series already on the chart"; |
|
55 | 55 | return; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | if(!series->parent()){ |
|
59 | 59 | series->setParent(this); // take ownership |
|
60 | 60 | }; |
|
61 | 61 | |
|
62 | 62 | if(!axisY->parent()){ |
|
63 | 63 | axisY->setParent(this); // take ownership |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | Domain* domain = m_axisDomainMap.value(axisY); |
|
67 | 67 | |
|
68 | 68 | if(!domain) { |
|
69 | domain = new Domain(); | |
|
69 | domain = new Domain(axisY); | |
|
70 | 70 | QObject::connect(axisY,SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); |
|
71 | 71 | QObject::connect(axisX(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); |
|
72 | 72 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY,SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
73 | 73 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
74 | 74 | //initialize |
|
75 | 75 | m_axisDomainMap.insert(axisY,domain); |
|
76 | 76 | emit axisAdded(axisY,domain); |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | if(!m_axisXInitialized){ |
|
80 | 80 | emit axisAdded(axisX(),domain); |
|
81 | 81 | m_axisXInitialized=true; |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | calculateDomain(series,domain); |
|
85 | 85 | |
|
86 | 86 | m_seriesAxisMap.insert(series,axisY); |
|
87 | 87 | emit seriesAdded(series,domain); |
|
88 | 88 | |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | void ChartDataSet::removeSeries(QSeries* series) |
|
92 | 92 | { |
|
93 | 93 | |
|
94 | 94 | QChartAxis* axis = m_seriesAxisMap.value(series); |
|
95 | 95 | |
|
96 | 96 | if(!axis){ |
|
97 | 97 | qWarning()<<"Can not remove series. Series not found on the chart."; |
|
98 | 98 | return; |
|
99 | 99 | } |
|
100 | 100 | emit seriesRemoved(series); |
|
101 | 101 | m_seriesAxisMap.remove(series); |
|
102 | 102 | |
|
103 | 103 | if(series->parent()==this){ |
|
104 | 104 | delete series; |
|
105 | 105 | series=0; |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | QList<QChartAxis*> axes = m_seriesAxisMap.values(); |
|
109 | 109 | |
|
110 | 110 | int i = axes.indexOf(axis); |
|
111 | 111 | |
|
112 | 112 | if(i==-1){ |
|
113 | 113 | Domain* domain = m_axisDomainMap.take(axis); |
|
114 | 114 | emit axisRemoved(axis); |
|
115 | 115 | if(axis!=axisY()){ |
|
116 | 116 | if(axis->parent()==this){ |
|
117 | 117 | delete axis; |
|
118 | 118 | axis=0; |
|
119 | 119 | } |
|
120 | 120 | } |
|
121 | 121 | delete domain; |
|
122 | 122 | } |
|
123 | 123 | |
|
124 | 124 | if(m_seriesAxisMap.values().size()==0) |
|
125 | 125 | { |
|
126 | 126 | m_axisXInitialized=false; |
|
127 | 127 | emit axisRemoved(axisX()); |
|
128 | 128 | } |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | void ChartDataSet::removeAllSeries() |
|
132 | 132 | { |
|
133 | 133 | |
|
134 | 134 | QList<QSeries*> series = m_seriesAxisMap.keys(); |
|
135 | 135 | |
|
136 | 136 | foreach(QSeries* s , series) { |
|
137 | 137 | removeSeries(s); |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | Q_ASSERT(m_seriesAxisMap.count()==0); |
|
141 | 141 | Q_ASSERT(m_axisDomainMap.count()==0); |
|
142 | 142 | |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | //to be removed with PIMPL |
|
146 | 146 | void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) |
|
147 | 147 | { |
|
148 | 148 | qreal minX(domain->minX()); |
|
149 | 149 | qreal minY(domain->minY()); |
|
150 | 150 | qreal maxX(domain->maxX()); |
|
151 | 151 | qreal maxY(domain->maxY()); |
|
152 | 152 | int tickXCount(domain->tickXCount()); |
|
153 | 153 | int tickYCount(domain->tickYCount()); |
|
154 | 154 | |
|
155 | 155 | |
|
156 | 156 | switch(series->type()) |
|
157 | 157 | { |
|
158 | 158 | case QSeries::SeriesTypeLine: |
|
159 | 159 | case QSeries::SeriesTypeSpline: |
|
160 | 160 | case QSeries::SeriesTypeScatter: |
|
161 | 161 | { |
|
162 | 162 | |
|
163 | 163 | QXYSeries* xySeries = static_cast<QXYSeries*>(series); |
|
164 | 164 | |
|
165 | 165 | for (int i = 0; i < xySeries->count(); i++) |
|
166 | 166 | { |
|
167 | 167 | qreal x = xySeries->x(i); |
|
168 | 168 | qreal y = xySeries->y(i); |
|
169 | 169 | minX = qMin(minX, x); |
|
170 | 170 | minY = qMin(minY, y); |
|
171 | 171 | maxX = qMax(maxX, x); |
|
172 | 172 | maxY = qMax(maxY, y); |
|
173 | 173 | } |
|
174 | 174 | break; |
|
175 | 175 | } |
|
176 | 176 | case QSeries::SeriesTypeArea: { |
|
177 | 177 | |
|
178 | 178 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
179 | 179 | QLineSeries* upperSeries = areaSeries->upperSeries(); |
|
180 | 180 | QLineSeries* lowerSeries = areaSeries->lowerSeries(); |
|
181 | 181 | |
|
182 | 182 | for (int i = 0; i < upperSeries->count(); i++) |
|
183 | 183 | { |
|
184 | 184 | qreal x = upperSeries->x(i); |
|
185 | 185 | qreal y = upperSeries->y(i); |
|
186 | 186 | minX = qMin(minX, x); |
|
187 | 187 | minY = qMin(minY, y); |
|
188 | 188 | maxX = qMax(maxX, x); |
|
189 | 189 | maxY = qMax(maxY, y); |
|
190 | 190 | } |
|
191 | 191 | if(lowerSeries) { |
|
192 | 192 | for (int i = 0; i < lowerSeries->count(); i++) |
|
193 | 193 | { |
|
194 | 194 | qreal x = lowerSeries->x(i); |
|
195 | 195 | qreal y = lowerSeries->y(i); |
|
196 | 196 | minX = qMin(minX, x); |
|
197 | 197 | minY = qMin(minY, y); |
|
198 | 198 | maxX = qMax(maxX, x); |
|
199 | 199 | maxY = qMax(maxY, y); |
|
200 | 200 | }} |
|
201 | 201 | break; |
|
202 | 202 | } |
|
203 | 203 | case QSeries::SeriesTypeBar: { |
|
204 | 204 | |
|
205 | 205 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
206 | 206 | qreal x = barSeries->categoryCount(); |
|
207 | 207 | qreal y = barSeries->max(); |
|
208 | 208 | minX = qMin(minX, x); |
|
209 | 209 | minY = qMin(minY, y); |
|
210 | 210 | maxX = qMax(maxX, x); |
|
211 | 211 | maxY = qMax(maxY, y); |
|
212 | 212 | tickXCount = x+1; |
|
213 | 213 | setupCategories(barSeries); |
|
214 | 214 | break; |
|
215 | 215 | } |
|
216 | 216 | case QSeries::SeriesTypeStackedBar: { |
|
217 | 217 | |
|
218 | 218 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
219 | 219 | qreal x = stackedBarSeries->categoryCount(); |
|
220 | 220 | qreal y = stackedBarSeries->maxCategorySum(); |
|
221 | 221 | minX = qMin(minX, x); |
|
222 | 222 | minY = qMin(minY, y); |
|
223 | 223 | maxX = qMax(maxX, x); |
|
224 | 224 | maxY = qMax(maxY, y); |
|
225 | 225 | tickXCount = x+1; |
|
226 | 226 | setupCategories(stackedBarSeries); |
|
227 | 227 | break; |
|
228 | 228 | } |
|
229 | 229 | case QSeries::SeriesTypePercentBar: { |
|
230 | 230 | |
|
231 | 231 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
232 | 232 | qreal x = percentBarSeries->categoryCount(); |
|
233 | 233 | minX = qMin(minX, x); |
|
234 | 234 | maxX = qMax(maxX, x); |
|
235 | 235 | minY = 0; |
|
236 | 236 | maxY = 100; |
|
237 | 237 | setupCategories(percentBarSeries); |
|
238 | 238 | break; |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | case QSeries::SeriesTypePie: { |
|
242 | 242 | //QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
243 | 243 | // TODO: domain stuff |
|
244 | 244 | break; |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | |
|
248 | 248 | default: { |
|
249 | 249 | qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported"; |
|
250 | 250 | return; |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | } |
|
254 | 254 | |
|
255 | 255 | domain->setRangeX(minX,maxX,tickXCount); |
|
256 | 256 | domain->setRangeY(minY,maxY,tickYCount); |
|
257 | 257 | } |
|
258 | 258 | |
|
259 | 259 | |
|
260 | 260 | void ChartDataSet::setupCategories(QBarSeries* series) |
|
261 | 261 | { |
|
262 | 262 | QChartAxisCategories* categories = axisX()->categories(); |
|
263 | 263 | categories->clear(); |
|
264 | 264 | categories->insert(series->categories()); |
|
265 | 265 | } |
|
266 | 266 | |
|
267 | 267 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) |
|
268 | 268 | { |
|
269 | 269 | QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap); |
|
270 | 270 | while (i.hasNext()) { |
|
271 | 271 | i.next(); |
|
272 | 272 | i.value()->zoomIn(rect,size); |
|
273 | 273 | } |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) |
|
277 | 277 | { |
|
278 | 278 | QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap); |
|
279 | 279 | while (i.hasNext()) { |
|
280 | 280 | i.next(); |
|
281 | 281 | i.value()->zoomOut(rect,size); |
|
282 | 282 | } |
|
283 | 283 | } |
|
284 | 284 | |
|
285 | 285 | int ChartDataSet::seriesCount(QSeries::QSeriesType type) |
|
286 | 286 | { |
|
287 | 287 | int count=0; |
|
288 | 288 | QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap); |
|
289 | 289 | while (i.hasNext()) { |
|
290 | 290 | i.next(); |
|
291 | 291 | if(i.key()->type()==type) count++; |
|
292 | 292 | } |
|
293 | 293 | return count; |
|
294 | 294 | } |
|
295 | 295 | |
|
296 | 296 | int ChartDataSet::seriesIndex(QSeries *series) |
|
297 | 297 | { |
|
298 | 298 | int count=-1; |
|
299 | 299 | QMapIterator<QSeries*, QChartAxis*> i(m_seriesAxisMap); |
|
300 | 300 | while (i.hasNext()) { |
|
301 | 301 | i.next(); |
|
302 | 302 | count++; |
|
303 | 303 | if (i.key() == series) |
|
304 | 304 | return count; |
|
305 | 305 | } |
|
306 | 306 | return count; |
|
307 | 307 | } |
|
308 | 308 | |
|
309 | 309 | QChartAxis* ChartDataSet::axisY(QSeries* series) const |
|
310 | 310 | { |
|
311 | 311 | if(series == 0) return m_axisY; |
|
312 | 312 | return m_seriesAxisMap.value(series); |
|
313 | 313 | } |
|
314 | 314 | |
|
315 | 315 | Domain* ChartDataSet::domain(QSeries* series) const |
|
316 | 316 | { |
|
317 | 317 | QChartAxis* axis = m_seriesAxisMap.value(series); |
|
318 | 318 | if(axis){ |
|
319 | 319 | return m_axisDomainMap.value(axis); |
|
320 | 320 | }else |
|
321 | 321 | return 0; |
|
322 | 322 | } |
|
323 | 323 | |
|
324 | 324 | Domain* ChartDataSet::domain(QChartAxis* axis) const |
|
325 | 325 | { |
|
326 | 326 | if(!axis || axis==axisX()) { |
|
327 | 327 | return m_axisDomainMap.value(axisY()); |
|
328 | 328 | } |
|
329 | 329 | else { |
|
330 | 330 | return m_axisDomainMap.value(axis); |
|
331 | 331 | } |
|
332 | 332 | } |
|
333 | 333 | |
|
334 | 334 | void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size) |
|
335 | 335 | { |
|
336 | 336 | QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap); |
|
337 | 337 | while (i.hasNext()) { |
|
338 | 338 | i.next(); |
|
339 | 339 | i.value()->move(dx,dy,size); |
|
340 | 340 | } |
|
341 | 341 | } |
|
342 | 342 | |
|
343 | 343 | #include "moc_chartdataset_p.cpp" |
|
344 | 344 | |
|
345 | 345 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now