@@ -1,632 +1,625 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2013 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 "chartpresenter_p.h" |
|
23 | 23 | #include "qchart.h" |
|
24 | 24 | #include "qchart_p.h" |
|
25 | 25 | #include "qvalueaxis.h" |
|
26 | 26 | #include "qbarcategoryaxis.h" |
|
27 | 27 | #include "qvalueaxis_p.h" |
|
28 | 28 | #include "qcategoryaxis.h" |
|
29 | 29 | #include "qabstractseries_p.h" |
|
30 | 30 | #include "qabstractbarseries.h" |
|
31 | 31 | #include "qstackedbarseries.h" |
|
32 | 32 | #include "qpercentbarseries.h" |
|
33 | 33 | #include "qpieseries.h" |
|
34 | 34 | #include "chartitem_p.h" |
|
35 | 35 | #include "xydomain_p.h" |
|
36 | 36 | #include "xypolardomain_p.h" |
|
37 | 37 | #include "xlogydomain_p.h" |
|
38 | 38 | #include "logxydomain_p.h" |
|
39 | 39 | #include "logxlogydomain_p.h" |
|
40 | 40 | #include "xlogypolardomain_p.h" |
|
41 | 41 | #include "logxypolardomain_p.h" |
|
42 | 42 | #include "logxlogypolardomain_p.h" |
|
43 | 43 | |
|
44 | 44 | #ifndef QT_ON_ARM |
|
45 | 45 | #include "qdatetimeaxis.h" |
|
46 | 46 | #endif |
|
47 | 47 | |
|
48 | 48 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
49 | 49 | |
|
50 | 50 | ChartDataSet::ChartDataSet(QChart *chart) |
|
51 | 51 | : QObject(chart), |
|
52 | 52 | m_chart(chart) |
|
53 | 53 | { |
|
54 | 54 | |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | ChartDataSet::~ChartDataSet() |
|
58 | 58 | { |
|
59 | 59 | deleteAllSeries(); |
|
60 | 60 | deleteAllAxes(); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | /* |
|
64 | 64 | * This method adds series to chartdataset, series ownership is taken from caller. |
|
65 | 65 | */ |
|
66 | 66 | void ChartDataSet::addSeries(QAbstractSeries *series) |
|
67 | 67 | { |
|
68 | 68 | if (m_seriesList.contains(series)) { |
|
69 | 69 | qWarning() << QObject::tr("Can not add series. Series already on the chart."); |
|
70 | 70 | return; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | // Ignore unsupported series added to polar chart |
|
74 | 74 | if (m_chart && m_chart->chartType() == QChart::ChartTypePolar) { |
|
75 | 75 | if (!(series->type() == QAbstractSeries::SeriesTypeArea |
|
76 | 76 | || series->type() == QAbstractSeries::SeriesTypeLine |
|
77 | 77 | || series->type() == QAbstractSeries::SeriesTypeScatter |
|
78 | 78 | || series->type() == QAbstractSeries::SeriesTypeSpline)) { |
|
79 | 79 | qWarning() << QObject::tr("Can not add series. Series type is not supported by a polar chart."); |
|
80 | 80 | return; |
|
81 | 81 | } |
|
82 | 82 | series->d_ptr->setDomain(new XYPolarDomain()); |
|
83 | 83 | } else { |
|
84 | 84 | series->d_ptr->setDomain(new XYDomain()); |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | series->d_ptr->initializeDomain(); |
|
88 | 88 | m_seriesList.append(series); |
|
89 | 89 | |
|
90 | 90 | series->setParent(this); // take ownership |
|
91 | 91 | series->d_ptr->m_chart = m_chart; |
|
92 | 92 | |
|
93 | 93 | emit seriesAdded(series); |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | /* |
|
97 | 97 | * This method adds axis to chartdataset, axis ownership is taken from caller. |
|
98 | 98 | */ |
|
99 | 99 | void ChartDataSet::addAxis(QAbstractAxis *axis, Qt::Alignment aligment) |
|
100 | 100 | { |
|
101 | 101 | if (m_axisList.contains(axis)) { |
|
102 | 102 | qWarning() << QObject::tr("Can not add axis. Axis already on the chart."); |
|
103 | 103 | return; |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | axis->d_ptr->setAlignment(aligment); |
|
107 | 107 | |
|
108 | 108 | if (!axis->alignment()) { |
|
109 | 109 | qWarning() << QObject::tr("No alignment specified !"); |
|
110 | 110 | return; |
|
111 | 111 | }; |
|
112 | 112 | |
|
113 | 113 | AbstractDomain *newDomain; |
|
114 |
if (m_chart && m_chart->chartType() == QChart::ChartTypePolar) |
|
|
115 | foreach (QAbstractAxis *existingAxis, axes()) { | |
|
116 | if (existingAxis->orientation() == axis->orientation()) { | |
|
117 | qWarning() << QObject::tr("Cannot add multiple axes of same orientation to a polar chart!"); | |
|
118 | return; | |
|
119 | } | |
|
120 | } | |
|
114 | if (m_chart && m_chart->chartType() == QChart::ChartTypePolar) | |
|
121 | 115 | newDomain = new XYPolarDomain(); |
|
122 |
|
|
|
116 | else | |
|
123 | 117 | newDomain = new XYDomain(); |
|
124 | } | |
|
125 | 118 | |
|
126 | 119 | QSharedPointer<AbstractDomain> domain(newDomain); |
|
127 | 120 | axis->d_ptr->initializeDomain(domain.data()); |
|
128 | 121 | |
|
129 | 122 | axis->setParent(this); |
|
130 | 123 | axis->d_ptr->m_chart = m_chart; |
|
131 | 124 | m_axisList.append(axis); |
|
132 | 125 | |
|
133 | 126 | emit axisAdded(axis); |
|
134 | 127 | } |
|
135 | 128 | |
|
136 | 129 | /* |
|
137 | 130 | * This method removes series form chartdataset, series ownership is passed back to caller. |
|
138 | 131 | */ |
|
139 | 132 | void ChartDataSet::removeSeries(QAbstractSeries *series) |
|
140 | 133 | { |
|
141 | 134 | |
|
142 | 135 | if (! m_seriesList.contains(series)) { |
|
143 | 136 | qWarning() << QObject::tr("Can not remove series. Series not found on the chart."); |
|
144 | 137 | return; |
|
145 | 138 | } |
|
146 | 139 | |
|
147 | 140 | QList<QAbstractAxis*> axes = series->d_ptr->m_axes; |
|
148 | 141 | |
|
149 | 142 | foreach(QAbstractAxis* axis, axes) { |
|
150 | 143 | detachAxis(series,axis); |
|
151 | 144 | } |
|
152 | 145 | |
|
153 | 146 | emit seriesRemoved(series); |
|
154 | 147 | m_seriesList.removeAll(series); |
|
155 | 148 | |
|
156 | 149 | // Reset domain to default |
|
157 | 150 | series->d_ptr->setDomain(new XYDomain()); |
|
158 | 151 | series->setParent(0); |
|
159 | 152 | series->d_ptr->m_chart = 0; |
|
160 | 153 | } |
|
161 | 154 | |
|
162 | 155 | /* |
|
163 | 156 | * This method removes axis form chartdataset, series ownership is passed back to caller. |
|
164 | 157 | */ |
|
165 | 158 | void ChartDataSet::removeAxis(QAbstractAxis *axis) |
|
166 | 159 | { |
|
167 | 160 | if (! m_axisList.contains(axis)) { |
|
168 | 161 | qWarning() << QObject::tr("Can not remove axis. Axis not found on the chart."); |
|
169 | 162 | return; |
|
170 | 163 | } |
|
171 | 164 | |
|
172 | 165 | QList<QAbstractSeries*> series = axis->d_ptr->m_series; |
|
173 | 166 | |
|
174 | 167 | foreach(QAbstractSeries* s, series) { |
|
175 | 168 | detachAxis(s,axis); |
|
176 | 169 | } |
|
177 | 170 | |
|
178 | 171 | emit axisRemoved(axis); |
|
179 | 172 | m_axisList.removeAll(axis); |
|
180 | 173 | |
|
181 | 174 | axis->setParent(0); |
|
182 | 175 | axis->d_ptr->m_chart = 0; |
|
183 | 176 | } |
|
184 | 177 | |
|
185 | 178 | /* |
|
186 | 179 | * This method attaches axis to series, return true if success. |
|
187 | 180 | */ |
|
188 | 181 | bool ChartDataSet::attachAxis(QAbstractSeries *series,QAbstractAxis *axis) |
|
189 | 182 | { |
|
190 | 183 | Q_ASSERT(series); |
|
191 | 184 | Q_ASSERT(axis); |
|
192 | 185 | |
|
193 | 186 | QList<QAbstractSeries *> attachedSeriesList = axis->d_ptr->m_series; |
|
194 | 187 | QList<QAbstractAxis *> attachedAxisList = series->d_ptr->m_axes; |
|
195 | 188 | |
|
196 | 189 | if (!m_seriesList.contains(series)) { |
|
197 | 190 | qWarning() << QObject::tr("Can not find series on the chart."); |
|
198 | 191 | return false; |
|
199 | 192 | } |
|
200 | 193 | |
|
201 | 194 | if (axis && !m_axisList.contains(axis)) { |
|
202 | 195 | qWarning() << QObject::tr("Can not find axis on the chart."); |
|
203 | 196 | return false; |
|
204 | 197 | } |
|
205 | 198 | |
|
206 | 199 | if (attachedAxisList.contains(axis)) { |
|
207 | 200 | qWarning() << QObject::tr("Axis already attached to series."); |
|
208 | 201 | return false; |
|
209 | 202 | } |
|
210 | 203 | |
|
211 | 204 | if (attachedSeriesList.contains(series)) { |
|
212 | 205 | qWarning() << QObject::tr("Axis already attached to series."); |
|
213 | 206 | return false; |
|
214 | 207 | } |
|
215 | 208 | |
|
216 | 209 | AbstractDomain *domain = series->d_ptr->domain(); |
|
217 | 210 | AbstractDomain::DomainType type = selectDomain(attachedAxisList<<axis); |
|
218 | 211 | |
|
219 | 212 | if (type == AbstractDomain::UndefinedDomain) return false; |
|
220 | 213 | |
|
221 | 214 | if (domain->type() != type) { |
|
222 | 215 | AbstractDomain *old = domain; |
|
223 | 216 | domain = createDomain(type); |
|
224 | 217 | domain->setRange(old->minX(), old->maxX(), old->minY(), old->maxY()); |
|
225 | 218 | // Initialize domain size to old domain size, as it won't get updated |
|
226 | 219 | // unless geometry changes. |
|
227 | 220 | domain->setSize(old->size()); |
|
228 | 221 | } |
|
229 | 222 | |
|
230 | 223 | if (!domain) |
|
231 | 224 | return false; |
|
232 | 225 | |
|
233 | 226 | if (!domain->attachAxis(axis)) |
|
234 | 227 | return false; |
|
235 | 228 | |
|
236 | 229 | QList<AbstractDomain *> blockedDomains; |
|
237 | 230 | domain->blockRangeSignals(true); |
|
238 | 231 | blockedDomains << domain; |
|
239 | 232 | |
|
240 | 233 | if (domain != series->d_ptr->domain()) { |
|
241 | 234 | foreach (QAbstractAxis *axis, series->d_ptr->m_axes) { |
|
242 | 235 | series->d_ptr->domain()->detachAxis(axis); |
|
243 | 236 | domain->attachAxis(axis); |
|
244 | 237 | foreach (QAbstractSeries *otherSeries, axis->d_ptr->m_series) { |
|
245 | 238 | if (otherSeries != series && otherSeries->d_ptr->domain()) { |
|
246 | 239 | if (!otherSeries->d_ptr->domain()->rangeSignalsBlocked()) { |
|
247 | 240 | otherSeries->d_ptr->domain()->blockRangeSignals(true); |
|
248 | 241 | blockedDomains << otherSeries->d_ptr->domain(); |
|
249 | 242 | } |
|
250 | 243 | } |
|
251 | 244 | } |
|
252 | 245 | } |
|
253 | 246 | series->d_ptr->setDomain(domain); |
|
254 | 247 | series->d_ptr->initializeDomain(); |
|
255 | 248 | } |
|
256 | 249 | |
|
257 | 250 | series->d_ptr->m_axes<<axis; |
|
258 | 251 | axis->d_ptr->m_series<<series; |
|
259 | 252 | |
|
260 | 253 | series->d_ptr->initializeAxes(); |
|
261 | 254 | axis->d_ptr->initializeDomain(domain); |
|
262 | 255 | |
|
263 | 256 | foreach (AbstractDomain *blockedDomain, blockedDomains) |
|
264 | 257 | blockedDomain->blockRangeSignals(false); |
|
265 | 258 | |
|
266 | 259 | return true; |
|
267 | 260 | } |
|
268 | 261 | |
|
269 | 262 | /* |
|
270 | 263 | * This method detaches axis to series, return true if success. |
|
271 | 264 | */ |
|
272 | 265 | bool ChartDataSet::detachAxis(QAbstractSeries* series,QAbstractAxis *axis) |
|
273 | 266 | { |
|
274 | 267 | Q_ASSERT(series); |
|
275 | 268 | Q_ASSERT(axis); |
|
276 | 269 | |
|
277 | 270 | QList<QAbstractSeries* > attachedSeriesList = axis->d_ptr->m_series; |
|
278 | 271 | QList<QAbstractAxis* > attachedAxisList = series->d_ptr->m_axes; |
|
279 | 272 | AbstractDomain* domain = series->d_ptr->domain(); |
|
280 | 273 | |
|
281 | 274 | if (!m_seriesList.contains(series)) { |
|
282 | 275 | qWarning() << QObject::tr("Can not find series on the chart."); |
|
283 | 276 | return false; |
|
284 | 277 | } |
|
285 | 278 | |
|
286 | 279 | if (axis && !m_axisList.contains(axis)) { |
|
287 | 280 | qWarning() << QObject::tr("Can not find axis on the chart."); |
|
288 | 281 | return false; |
|
289 | 282 | } |
|
290 | 283 | |
|
291 | 284 | if (!attachedAxisList.contains(axis)) { |
|
292 | 285 | qWarning() << QObject::tr("Axis not attached to series."); |
|
293 | 286 | return false; |
|
294 | 287 | } |
|
295 | 288 | |
|
296 | 289 | Q_ASSERT(axis->d_ptr->m_series.contains(series)); |
|
297 | 290 | |
|
298 | 291 | domain->detachAxis(axis); |
|
299 | 292 | series->d_ptr->m_axes.removeAll(axis); |
|
300 | 293 | axis->d_ptr->m_series.removeAll(series); |
|
301 | 294 | |
|
302 | 295 | return true; |
|
303 | 296 | } |
|
304 | 297 | |
|
305 | 298 | void ChartDataSet::createDefaultAxes() |
|
306 | 299 | { |
|
307 | 300 | if (m_seriesList.isEmpty()) |
|
308 | 301 | return; |
|
309 | 302 | |
|
310 | 303 | QAbstractAxis::AxisTypes typeX(0); |
|
311 | 304 | QAbstractAxis::AxisTypes typeY(0); |
|
312 | 305 | |
|
313 | 306 | // Remove possibly existing axes |
|
314 | 307 | deleteAllAxes(); |
|
315 | 308 | |
|
316 | 309 | Q_ASSERT(m_axisList.isEmpty()); |
|
317 | 310 | |
|
318 | 311 | // Select the required axis x and axis y types based on the types of the current series |
|
319 | 312 | foreach(QAbstractSeries* s, m_seriesList) { |
|
320 | 313 | typeX |= s->d_ptr->defaultAxisType(Qt::Horizontal); |
|
321 | 314 | typeY |= s->d_ptr->defaultAxisType(Qt::Vertical); |
|
322 | 315 | } |
|
323 | 316 | |
|
324 | 317 | // Create the axes of the types selected |
|
325 | 318 | createAxes(typeX, Qt::Horizontal); |
|
326 | 319 | createAxes(typeY, Qt::Vertical); |
|
327 | 320 | |
|
328 | 321 | } |
|
329 | 322 | |
|
330 | 323 | void ChartDataSet::createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation orientation) |
|
331 | 324 | { |
|
332 | 325 | QAbstractAxis *axis = 0; |
|
333 | 326 | //decide what axis should be created |
|
334 | 327 | |
|
335 | 328 | switch (type) { |
|
336 | 329 | case QAbstractAxis::AxisTypeValue: |
|
337 | 330 | axis = new QValueAxis(this); |
|
338 | 331 | break; |
|
339 | 332 | case QAbstractAxis::AxisTypeBarCategory: |
|
340 | 333 | axis = new QBarCategoryAxis(this); |
|
341 | 334 | break; |
|
342 | 335 | case QAbstractAxis::AxisTypeCategory: |
|
343 | 336 | axis = new QCategoryAxis(this); |
|
344 | 337 | break; |
|
345 | 338 | #ifndef Q_WS_QWS |
|
346 | 339 | case QAbstractAxis::AxisTypeDateTime: |
|
347 | 340 | axis = new QDateTimeAxis(this); |
|
348 | 341 | break; |
|
349 | 342 | #endif |
|
350 | 343 | default: |
|
351 | 344 | axis = 0; |
|
352 | 345 | break; |
|
353 | 346 | } |
|
354 | 347 | |
|
355 | 348 | if (axis) { |
|
356 | 349 | //create one axis for all |
|
357 | 350 | |
|
358 | 351 | addAxis(axis,orientation==Qt::Horizontal?Qt::AlignBottom:Qt::AlignLeft); |
|
359 | 352 | qreal min = 0; |
|
360 | 353 | qreal max = 0; |
|
361 | 354 | findMinMaxForSeries(m_seriesList,orientation,min,max); |
|
362 | 355 | foreach(QAbstractSeries *s, m_seriesList) { |
|
363 | 356 | attachAxis(s,axis); |
|
364 | 357 | } |
|
365 | 358 | axis->setRange(min,max); |
|
366 | 359 | } |
|
367 | 360 | else if (!type.testFlag(QAbstractAxis::AxisTypeNoAxis)) { |
|
368 | 361 | //create separate axis |
|
369 | 362 | foreach(QAbstractSeries *s, m_seriesList) { |
|
370 | 363 | QAbstractAxis *axis = s->d_ptr->createDefaultAxis(orientation); |
|
371 | 364 | if(axis) { |
|
372 | 365 | addAxis(axis,orientation==Qt::Horizontal?Qt::AlignBottom:Qt::AlignLeft); |
|
373 | 366 | attachAxis(s,axis); |
|
374 | 367 | } |
|
375 | 368 | } |
|
376 | 369 | } |
|
377 | 370 | } |
|
378 | 371 | |
|
379 | 372 | void ChartDataSet::findMinMaxForSeries(QList<QAbstractSeries *> series,Qt::Orientations orientation, qreal &min, qreal &max) |
|
380 | 373 | { |
|
381 | 374 | Q_ASSERT(!series.isEmpty()); |
|
382 | 375 | |
|
383 | 376 | AbstractDomain *domain = series.first()->d_ptr->domain(); |
|
384 | 377 | min = (orientation == Qt::Vertical) ? domain->minY() : domain->minX(); |
|
385 | 378 | max = (orientation == Qt::Vertical) ? domain->maxY() : domain->maxX(); |
|
386 | 379 | |
|
387 | 380 | for (int i = 1; i< series.size(); i++) { |
|
388 | 381 | AbstractDomain *domain = series[i]->d_ptr->domain(); |
|
389 | 382 | min = qMin((orientation == Qt::Vertical) ? domain->minY() : domain->minX(), min); |
|
390 | 383 | max = qMax((orientation == Qt::Vertical) ? domain->maxY() : domain->maxX(), max); |
|
391 | 384 | } |
|
392 | 385 | if (min == max) { |
|
393 | 386 | min -= 0.5; |
|
394 | 387 | max += 0.5; |
|
395 | 388 | } |
|
396 | 389 | } |
|
397 | 390 | |
|
398 | 391 | void ChartDataSet::deleteAllSeries() |
|
399 | 392 | { |
|
400 | 393 | foreach (QAbstractSeries *s , m_seriesList){ |
|
401 | 394 | removeSeries(s); |
|
402 | 395 | s->deleteLater(); |
|
403 | 396 | } |
|
404 | 397 | Q_ASSERT(m_seriesList.count() == 0); |
|
405 | 398 | } |
|
406 | 399 | |
|
407 | 400 | void ChartDataSet::deleteAllAxes() |
|
408 | 401 | { |
|
409 | 402 | foreach (QAbstractAxis *a , m_axisList){ |
|
410 | 403 | removeAxis(a); |
|
411 | 404 | a->deleteLater(); |
|
412 | 405 | } |
|
413 | 406 | Q_ASSERT(m_axisList.count() == 0); |
|
414 | 407 | } |
|
415 | 408 | |
|
416 | 409 | void ChartDataSet::zoomInDomain(const QRectF &rect) |
|
417 | 410 | { |
|
418 | 411 | QList<AbstractDomain*> domains; |
|
419 | 412 | foreach(QAbstractSeries *s, m_seriesList) { |
|
420 | 413 | AbstractDomain* domain = s->d_ptr->domain(); |
|
421 | 414 | s->d_ptr->m_domain->blockRangeSignals(true); |
|
422 | 415 | domains<<domain; |
|
423 | 416 | } |
|
424 | 417 | |
|
425 | 418 | foreach(AbstractDomain *domain, domains) |
|
426 | 419 | domain->zoomIn(rect); |
|
427 | 420 | |
|
428 | 421 | foreach(AbstractDomain *domain, domains) |
|
429 | 422 | domain->blockRangeSignals(false); |
|
430 | 423 | } |
|
431 | 424 | |
|
432 | 425 | void ChartDataSet::zoomOutDomain(const QRectF &rect) |
|
433 | 426 | { |
|
434 | 427 | QList<AbstractDomain*> domains; |
|
435 | 428 | foreach(QAbstractSeries *s, m_seriesList) { |
|
436 | 429 | AbstractDomain* domain = s->d_ptr->domain(); |
|
437 | 430 | s->d_ptr->m_domain->blockRangeSignals(true); |
|
438 | 431 | domains<<domain; |
|
439 | 432 | } |
|
440 | 433 | |
|
441 | 434 | foreach(AbstractDomain *domain, domains) |
|
442 | 435 | domain->zoomOut(rect); |
|
443 | 436 | |
|
444 | 437 | foreach(AbstractDomain *domain, domains) |
|
445 | 438 | domain->blockRangeSignals(false); |
|
446 | 439 | } |
|
447 | 440 | |
|
448 | 441 | void ChartDataSet::zoomResetDomain() |
|
449 | 442 | { |
|
450 | 443 | QList<AbstractDomain*> domains; |
|
451 | 444 | foreach (QAbstractSeries *s, m_seriesList) { |
|
452 | 445 | AbstractDomain *domain = s->d_ptr->domain(); |
|
453 | 446 | s->d_ptr->m_domain->blockRangeSignals(true); |
|
454 | 447 | domains << domain; |
|
455 | 448 | } |
|
456 | 449 | |
|
457 | 450 | foreach (AbstractDomain *domain, domains) |
|
458 | 451 | domain->zoomReset(); |
|
459 | 452 | |
|
460 | 453 | foreach (AbstractDomain *domain, domains) |
|
461 | 454 | domain->blockRangeSignals(false); |
|
462 | 455 | } |
|
463 | 456 | |
|
464 | 457 | bool ChartDataSet::isZoomedDomain() |
|
465 | 458 | { |
|
466 | 459 | foreach (QAbstractSeries *s, m_seriesList) { |
|
467 | 460 | if (s->d_ptr->domain()->isZoomed()) |
|
468 | 461 | return true; |
|
469 | 462 | } |
|
470 | 463 | return false; |
|
471 | 464 | } |
|
472 | 465 | |
|
473 | 466 | void ChartDataSet::scrollDomain(qreal dx, qreal dy) |
|
474 | 467 | { |
|
475 | 468 | QList<AbstractDomain*> domains; |
|
476 | 469 | foreach(QAbstractSeries *s, m_seriesList) { |
|
477 | 470 | AbstractDomain* domain = s->d_ptr->domain(); |
|
478 | 471 | s->d_ptr->m_domain->blockRangeSignals(true); |
|
479 | 472 | domains<<domain; |
|
480 | 473 | } |
|
481 | 474 | |
|
482 | 475 | foreach(AbstractDomain *domain, domains) |
|
483 | 476 | domain->move(dx, dy); |
|
484 | 477 | |
|
485 | 478 | foreach(AbstractDomain *domain, domains) |
|
486 | 479 | domain->blockRangeSignals(false); |
|
487 | 480 | } |
|
488 | 481 | |
|
489 | 482 | QPointF ChartDataSet::mapToValue(const QPointF &position, QAbstractSeries *series) |
|
490 | 483 | { |
|
491 | 484 | QPointF point; |
|
492 | 485 | if (series == 0 && !m_seriesList.isEmpty()) |
|
493 | 486 | series = m_seriesList.first(); |
|
494 | 487 | |
|
495 | 488 | if (series && series->type() == QAbstractSeries::SeriesTypePie) |
|
496 | 489 | return point; |
|
497 | 490 | |
|
498 | 491 | if (series && m_seriesList.contains(series)) |
|
499 | 492 | point = series->d_ptr->m_domain->calculateDomainPoint(position - m_chart->plotArea().topLeft()); |
|
500 | 493 | return point; |
|
501 | 494 | } |
|
502 | 495 | |
|
503 | 496 | QPointF ChartDataSet::mapToPosition(const QPointF &value, QAbstractSeries *series) |
|
504 | 497 | { |
|
505 | 498 | QPointF point = m_chart->plotArea().topLeft(); |
|
506 | 499 | if (series == 0 && !m_seriesList.isEmpty()) |
|
507 | 500 | series = m_seriesList.first(); |
|
508 | 501 | |
|
509 | 502 | if (series && series->type() == QAbstractSeries::SeriesTypePie) |
|
510 | 503 | return QPoint(0, 0); |
|
511 | 504 | |
|
512 | 505 | bool ok; |
|
513 | 506 | if (series && m_seriesList.contains(series)) |
|
514 | 507 | point += series->d_ptr->m_domain->calculateGeometryPoint(value, ok); |
|
515 | 508 | return point; |
|
516 | 509 | } |
|
517 | 510 | |
|
518 | 511 | QList<QAbstractAxis *> ChartDataSet::axes() const |
|
519 | 512 | { |
|
520 | 513 | return m_axisList; |
|
521 | 514 | } |
|
522 | 515 | |
|
523 | 516 | QList<QAbstractSeries *> ChartDataSet::series() const |
|
524 | 517 | { |
|
525 | 518 | return m_seriesList; |
|
526 | 519 | } |
|
527 | 520 | |
|
528 | 521 | AbstractDomain::DomainType ChartDataSet::selectDomain(QList<QAbstractAxis *> axes) |
|
529 | 522 | { |
|
530 | 523 | enum Type { |
|
531 | 524 | Undefined = 0, |
|
532 | 525 | LogType = 0x1, |
|
533 | 526 | ValueType = 0x2 |
|
534 | 527 | }; |
|
535 | 528 | |
|
536 | 529 | int horizontal(Undefined); |
|
537 | 530 | int vertical(Undefined); |
|
538 | 531 | |
|
539 | 532 | // Assume cartesian chart type, unless chart is set |
|
540 | 533 | QChart::ChartType chartType(QChart::ChartTypeCartesian); |
|
541 | 534 | if (m_chart) |
|
542 | 535 | chartType = m_chart->chartType(); |
|
543 | 536 | |
|
544 | 537 | foreach (QAbstractAxis *axis, axes) |
|
545 | 538 | { |
|
546 | 539 | switch (axis->type()) { |
|
547 | 540 | case QAbstractAxis::AxisTypeLogValue: |
|
548 | 541 | if (axis->orientation() == Qt::Horizontal) |
|
549 | 542 | horizontal |= LogType; |
|
550 | 543 | if (axis->orientation() == Qt::Vertical) |
|
551 | 544 | vertical |= LogType; |
|
552 | 545 | break; |
|
553 | 546 | case QAbstractAxis::AxisTypeValue: |
|
554 | 547 | case QAbstractAxis::AxisTypeBarCategory: |
|
555 | 548 | case QAbstractAxis::AxisTypeCategory: |
|
556 | 549 | case QAbstractAxis::AxisTypeDateTime: |
|
557 | 550 | if (axis->orientation() == Qt::Horizontal) |
|
558 | 551 | horizontal |= ValueType; |
|
559 | 552 | if (axis->orientation() == Qt::Vertical) |
|
560 | 553 | vertical |= ValueType; |
|
561 | 554 | break; |
|
562 | 555 | default: |
|
563 | 556 | qWarning() << "Undefined type"; |
|
564 | 557 | break; |
|
565 | 558 | } |
|
566 | 559 | } |
|
567 | 560 | |
|
568 | 561 | if (vertical == Undefined) |
|
569 | 562 | vertical = ValueType; |
|
570 | 563 | if (horizontal == Undefined) |
|
571 | 564 | horizontal = ValueType; |
|
572 | 565 | |
|
573 | 566 | if (vertical == ValueType && horizontal == ValueType) { |
|
574 | 567 | if (chartType == QChart::ChartTypeCartesian) |
|
575 | 568 | return AbstractDomain::XYDomain; |
|
576 | 569 | else if (chartType == QChart::ChartTypePolar) |
|
577 | 570 | return AbstractDomain::XYPolarDomain; |
|
578 | 571 | } |
|
579 | 572 | |
|
580 | 573 | if (vertical == LogType && horizontal == ValueType) { |
|
581 | 574 | if (chartType == QChart::ChartTypeCartesian) |
|
582 | 575 | return AbstractDomain::XLogYDomain; |
|
583 | 576 | if (chartType == QChart::ChartTypePolar) |
|
584 | 577 | return AbstractDomain::XLogYPolarDomain; |
|
585 | 578 | } |
|
586 | 579 | |
|
587 | 580 | if (vertical == ValueType && horizontal == LogType) { |
|
588 | 581 | if (chartType == QChart::ChartTypeCartesian) |
|
589 | 582 | return AbstractDomain::LogXYDomain; |
|
590 | 583 | else if (chartType == QChart::ChartTypePolar) |
|
591 | 584 | return AbstractDomain::LogXYPolarDomain; |
|
592 | 585 | } |
|
593 | 586 | |
|
594 | 587 | if (vertical == LogType && horizontal == LogType) { |
|
595 | 588 | if (chartType == QChart::ChartTypeCartesian) |
|
596 | 589 | return AbstractDomain::LogXLogYDomain; |
|
597 | 590 | else if (chartType == QChart::ChartTypePolar) |
|
598 | 591 | return AbstractDomain::LogXLogYPolarDomain; |
|
599 | 592 | } |
|
600 | 593 | |
|
601 | 594 | return AbstractDomain::UndefinedDomain; |
|
602 | 595 | } |
|
603 | 596 | |
|
604 | 597 | //refactor create factory |
|
605 | 598 | AbstractDomain* ChartDataSet::createDomain(AbstractDomain::DomainType type) |
|
606 | 599 | { |
|
607 | 600 | switch (type) |
|
608 | 601 | { |
|
609 | 602 | case AbstractDomain::LogXLogYDomain: |
|
610 | 603 | return new LogXLogYDomain(); |
|
611 | 604 | case AbstractDomain::XYDomain: |
|
612 | 605 | return new XYDomain(); |
|
613 | 606 | case AbstractDomain::XLogYDomain: |
|
614 | 607 | return new XLogYDomain(); |
|
615 | 608 | case AbstractDomain::LogXYDomain: |
|
616 | 609 | return new LogXYDomain(); |
|
617 | 610 | case AbstractDomain::XYPolarDomain: |
|
618 | 611 | return new XYPolarDomain(); |
|
619 | 612 | case AbstractDomain::XLogYPolarDomain: |
|
620 | 613 | return new XLogYPolarDomain(); |
|
621 | 614 | case AbstractDomain::LogXYPolarDomain: |
|
622 | 615 | return new LogXYPolarDomain(); |
|
623 | 616 | case AbstractDomain::LogXLogYPolarDomain: |
|
624 | 617 | return new LogXLogYPolarDomain(); |
|
625 | 618 | default: |
|
626 | 619 | return 0; |
|
627 | 620 | } |
|
628 | 621 | } |
|
629 | 622 | |
|
630 | 623 | #include "moc_chartdataset_p.cpp" |
|
631 | 624 | |
|
632 | 625 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,129 +1,132 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2013 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 "qpolarchart.h" |
|
22 | 22 | #include "qabstractaxis.h" |
|
23 | 23 | |
|
24 | 24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
25 | 25 | |
|
26 | 26 | /*! |
|
27 | 27 | \enum QPolarChart::PolarOrientation |
|
28 | 28 | |
|
29 | 29 | This type is used to specify the polar orientation of an axis. |
|
30 | 30 | |
|
31 | 31 | \value PolarOrientationRadial |
|
32 | 32 | \value PolarOrientationAngular |
|
33 | 33 | */ |
|
34 | 34 | |
|
35 | 35 | /*! |
|
36 | 36 | \class QPolarChart |
|
37 | 37 | \brief QtCommercial chart API. |
|
38 | 38 | |
|
39 | 39 | QPolarChart is a specialization of QChart to show a polar chart. |
|
40 | 40 | |
|
41 | 41 | Polar charts support line, spline, area, and scatter series, and all axis types |
|
42 | 42 | supported by those series. |
|
43 | 43 | |
|
44 | 44 | \note When setting ticks to an angular QValueAxis, keep in mind that the first and last tick |
|
45 | 45 | are co-located at 0/360 degree angle. |
|
46 | 46 | |
|
47 | 47 | \note If the angular distance between two consecutive points in a series is more than 180 degrees, |
|
48 | 48 | any line connecting the two points becomes meaningless, so choose the axis ranges accordingly |
|
49 | 49 | when displaying line, spline, or area series. In such case series don't draw a direct line between |
|
50 | 50 | the two points, but instead draw a line to and from the center of the chart. |
|
51 | 51 | |
|
52 |
\note Polar charts d |
|
|
52 | \note Polar charts draw all axes of same orientation in the same position, so using multiple | |
|
53 | axes of same orientation can be confusing, unless the extra axes are only used to customize the | |
|
54 | grid (e.g. you can display a highlighted range with a secondary shaded QCategoryAxis or provide | |
|
55 | unlabeled subticks with a secondary QValueAxis that has its labels hidden). | |
|
53 | 56 | |
|
54 | 57 | \sa QChart |
|
55 | 58 | */ |
|
56 | 59 | |
|
57 | 60 | /*! |
|
58 | 61 | Constructs a polar chart as a child of the \a parent. |
|
59 | 62 | Parameter \a wFlags is passed to the QChart constructor. |
|
60 | 63 | */ |
|
61 | 64 | QPolarChart::QPolarChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) |
|
62 | 65 | : QChart(QChart::ChartTypePolar, parent, wFlags) |
|
63 | 66 | { |
|
64 | 67 | } |
|
65 | 68 | |
|
66 | 69 | /*! |
|
67 | 70 | Destroys the polar chart object and its children, like series and axis objects added to it. |
|
68 | 71 | */ |
|
69 | 72 | QPolarChart::~QPolarChart() |
|
70 | 73 | { |
|
71 | 74 | } |
|
72 | 75 | |
|
73 | 76 | /*! |
|
74 | 77 | Returns the axes added for the \a series with \a polarOrientation. If no series is provided, then any axis with the |
|
75 | 78 | specified polar orientation is returned. |
|
76 | 79 | |
|
77 | 80 | \sa addAxis() |
|
78 | 81 | */ |
|
79 | 82 | QList<QAbstractAxis *> QPolarChart::axes(PolarOrientations polarOrientation, QAbstractSeries *series) const |
|
80 | 83 | { |
|
81 | 84 | Qt::Orientations orientation(0); |
|
82 | 85 | if (polarOrientation.testFlag(PolarOrientationAngular)) |
|
83 | 86 | orientation |= Qt::Horizontal; |
|
84 | 87 | if (polarOrientation.testFlag(PolarOrientationRadial)) |
|
85 | 88 | orientation |= Qt::Vertical; |
|
86 | 89 | |
|
87 | 90 | return QChart::axes(orientation, series); |
|
88 | 91 | } |
|
89 | 92 | |
|
90 | 93 | /*! |
|
91 | 94 | This convenience method adds \a axis to the polar chart with \a polarOrientation. |
|
92 | 95 | The chart takes the ownership of the axis. |
|
93 | 96 | |
|
94 | 97 | \note Axes can be added to a polar chart also with QChart::addAxis() instead of this method. |
|
95 | 98 | The specified alignment determines the polar orientation: horizontal alignments indicate angular |
|
96 | 99 | axis and vertical alignments indicate radial axis. |
|
97 | 100 | |
|
98 | 101 | \sa QChart::removeAxis(), QChart::createDefaultAxes(), QAbstractSeries::attachAxis(), QChart::addAxis() |
|
99 | 102 | */ |
|
100 | 103 | void QPolarChart::addAxis(QAbstractAxis *axis, PolarOrientation polarOrientation) |
|
101 | 104 | { |
|
102 | 105 | if (!axis || axis->type() == QAbstractAxis::AxisTypeBarCategory) { |
|
103 | 106 | qWarning("QAbstractAxis::AxisTypeBarCategory is not a supported axis type for polar charts."); |
|
104 | 107 | } else { |
|
105 | 108 | Qt::Alignment alignment = Qt::AlignLeft; |
|
106 | 109 | if (polarOrientation == PolarOrientationAngular) |
|
107 | 110 | alignment = Qt::AlignBottom; |
|
108 | 111 | QChart::addAxis(axis, alignment); |
|
109 | 112 | } |
|
110 | 113 | } |
|
111 | 114 | |
|
112 | 115 | /*! |
|
113 | 116 | Angular axes of a polar chart report horizontal orientation and radial axes report |
|
114 | 117 | vertical orientation. |
|
115 | 118 | This function is a convenience function for converting the orientation of an \a axis to |
|
116 | 119 | corresponding polar orientation. If the \a axis is NULL or not added to a polar chart, |
|
117 | 120 | the return value is meaningless. |
|
118 | 121 | */ |
|
119 | 122 | QPolarChart::PolarOrientation QPolarChart::axisPolarOrientation(QAbstractAxis *axis) |
|
120 | 123 | { |
|
121 | 124 | if (axis && axis->orientation() == Qt::Horizontal) |
|
122 | 125 | return PolarOrientationAngular; |
|
123 | 126 | else |
|
124 | 127 | return PolarOrientationRadial; |
|
125 | 128 | } |
|
126 | 129 | |
|
127 | 130 | #include "moc_qpolarchart.cpp" |
|
128 | 131 | |
|
129 | 132 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now