@@ -1,487 +1,487 | |||
|
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 "qbarcategoryaxis.h" |
|
22 | 22 | #include "qbarcategoryaxis_p.h" |
|
23 | 23 | #include "chartbarcategoryaxisx_p.h" |
|
24 | 24 | #include "chartbarcategoryaxisy_p.h" |
|
25 | 25 | #include "domain_p.h" |
|
26 | 26 | #include "chartdataset_p.h" |
|
27 | 27 | #include <qmath.h> |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 30 | /*! |
|
31 | 31 | \class QBarCategoryAxis |
|
32 | 32 | \brief The QBarCategoryAxis class is used for manipulating chart's axis. |
|
33 | 33 | \mainclass |
|
34 | 34 | |
|
35 | 35 | BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
36 | 36 | Categories are drawn between ticks. Note that you can use this also with lineseries too. |
|
37 | 37 | See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that. |
|
38 | 38 | |
|
39 | 39 | Example code on how to use QBarCategoryAxis. |
|
40 | 40 | \code |
|
41 | 41 | QChartView *chartView = new QChartView; |
|
42 | 42 | QBarSeries *series = new QBarSeries; |
|
43 | 43 | // ... |
|
44 | 44 | chartView->chart()->addSeries(series); |
|
45 | 45 | chartView->chart()->createDefaultAxes(); |
|
46 | 46 | |
|
47 | 47 | QBarCategoryAxis *axisX = new QBarCategoryAxis; |
|
48 | 48 | QStringList categories; |
|
49 | 49 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; |
|
50 | 50 | axisX->append(categories); |
|
51 | 51 | chartView->chart()->setAxisX(series, axisX); |
|
52 | 52 | \endcode |
|
53 | 53 | */ |
|
54 | 54 | |
|
55 | 55 | /*! |
|
56 | 56 | \qmlclass BarCategoryAxis QBarCategoryAxis |
|
57 | 57 | \inherits AbstractAxis |
|
58 | 58 | \brief The Axis element is used for manipulating chart's axes. |
|
59 | 59 | |
|
60 | 60 | Axis can be setup to show axis line with tick marks, grid lines and shades. |
|
61 | 61 | Categories are drawn between ticks. Note that you can use this also with lineseries too. |
|
62 | 62 | |
|
63 | 63 | To access BarCategoryAxis you can use ChartView API. For example: |
|
64 | 64 | \code |
|
65 | 65 | ChartView { |
|
66 | 66 | BarCategoryAxis { |
|
67 | 67 | id: categoryAxis |
|
68 | 68 | categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ] |
|
69 | 69 | } |
|
70 | 70 | // Add a few series... |
|
71 | 71 | } |
|
72 | 72 | \endcode |
|
73 | 73 | */ |
|
74 | 74 | |
|
75 | 75 | /*! |
|
76 | 76 | \property QBarCategoryAxis::categories |
|
77 | 77 | Defines the categories of axis |
|
78 | 78 | */ |
|
79 | 79 | /*! |
|
80 | 80 | \qmlproperty QStringList BarCategoryAxis::categories |
|
81 | 81 | Defines the categories of axis |
|
82 | 82 | */ |
|
83 | 83 | |
|
84 | 84 | /*! |
|
85 | 85 | \property QBarCategoryAxis::min |
|
86 | 86 | Defines the minimum value on the axis. |
|
87 | 87 | */ |
|
88 | 88 | /*! |
|
89 | 89 | \qmlproperty QString BarCategoryAxis::min |
|
90 | 90 | Defines the minimum value on the axis. |
|
91 | 91 | */ |
|
92 | 92 | |
|
93 | 93 | /*! |
|
94 | 94 | \property QBarCategoryAxis::max |
|
95 | 95 | Defines the maximum value on the axis. |
|
96 | 96 | */ |
|
97 | 97 | /*! |
|
98 | 98 | \qmlproperty QString BarCategoryAxis::max |
|
99 | 99 | Defines the maximum value on the axis. |
|
100 | 100 | */ |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | /*! |
|
104 | 104 | \fn void QBarCategoryAxis::categoriesChanged() |
|
105 | 105 | Axis emits signal when the categories of the axis has changed. |
|
106 | 106 | */ |
|
107 | 107 | /*! |
|
108 | 108 | \fn void QBarCategoryAxis::minChanged(const QString &min) |
|
109 | 109 | Axis emits signal when \a min of axis has changed. |
|
110 | 110 | */ |
|
111 | 111 | /*! |
|
112 | 112 | \qmlsignal BarCategoryAxis::onMinChanged(const QString &min) |
|
113 | 113 | Axis emits signal when \a min of axis has changed. |
|
114 | 114 | */ |
|
115 | 115 | |
|
116 | 116 | /*! |
|
117 | 117 | \fn void QBarCategoryAxis::maxChanged(const QString &max) |
|
118 | 118 | Axis emits signal when \a max of axis has changed. |
|
119 | 119 | */ |
|
120 | 120 | /*! |
|
121 | 121 | \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max) |
|
122 | 122 | Axis emits signal when \a max of axis has changed. |
|
123 | 123 | */ |
|
124 | 124 | |
|
125 | 125 | /*! |
|
126 | 126 | \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max) |
|
127 | 127 | Axis emits signal when \a min or \a max of axis has changed. |
|
128 | 128 | */ |
|
129 | 129 | |
|
130 | 130 | /*! |
|
131 | 131 | Constructs an axis object which is a child of \a parent. |
|
132 | 132 | */ |
|
133 | 133 | QBarCategoryAxis::QBarCategoryAxis(QObject *parent): |
|
134 | 134 | QAbstractAxis(*new QBarCategoryAxisPrivate(this),parent) |
|
135 | 135 | { |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | /*! |
|
139 | 139 | Destroys the object |
|
140 | 140 | */ |
|
141 | 141 | QBarCategoryAxis::~QBarCategoryAxis() |
|
142 | 142 | { |
|
143 | 143 | Q_D(QBarCategoryAxis); |
|
144 | 144 | if(d->m_dataset){ |
|
145 | 145 | d->m_dataset->removeAxis(this); |
|
146 | 146 | } |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | /*! |
|
150 | 150 | \internal |
|
151 | 151 | */ |
|
152 | 152 | QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent) |
|
153 | 153 | { |
|
154 | 154 | |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | /*! |
|
158 | 158 | Appends \a categories to axis |
|
159 | 159 | */ |
|
160 | 160 | void QBarCategoryAxis::append(const QStringList &categories) |
|
161 | 161 | { |
|
162 | 162 | if(categories.isEmpty()) return; |
|
163 | 163 | |
|
164 | 164 | Q_D(QBarCategoryAxis); |
|
165 | 165 | if (d->m_categories.isEmpty()) { |
|
166 | 166 | d->m_categories.append(categories); |
|
167 | 167 | setRange(categories.first(),categories.last()); |
|
168 | 168 | }else{ |
|
169 | 169 | d->m_categories.append(categories); |
|
170 | 170 | d->emitUpdated(); |
|
171 | 171 | } |
|
172 | 172 | emit categoriesChanged(); |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | /*! |
|
176 | 176 | Appends \a category to axis |
|
177 | 177 | */ |
|
178 | 178 | void QBarCategoryAxis::append(const QString &category) |
|
179 | 179 | { |
|
180 | 180 | Q_D(QBarCategoryAxis); |
|
181 | 181 | if (d->m_categories.isEmpty()) { |
|
182 | 182 | d->m_categories.append(category); |
|
183 | 183 | setRange(category,category); |
|
184 | 184 | }else{ |
|
185 | 185 | d->m_categories.append(category); |
|
186 | 186 | d->emitUpdated(); |
|
187 | 187 | } |
|
188 | 188 | emit categoriesChanged(); |
|
189 | 189 | } |
|
190 | 190 | |
|
191 | 191 | /*! |
|
192 | 192 | Removes \a category from axis |
|
193 | 193 | */ |
|
194 | 194 | void QBarCategoryAxis::remove(const QString &category) |
|
195 | 195 | { |
|
196 | 196 | Q_D(QBarCategoryAxis); |
|
197 | 197 | if (d->m_categories.contains(category)) { |
|
198 | 198 | d->m_categories.removeAt(d->m_categories.indexOf(category)); |
|
199 | 199 | if(!d->m_categories.isEmpty()) |
|
200 | 200 | setRange(d->m_categories.first(),d->m_categories.last()); |
|
201 | 201 | else |
|
202 |
setRange(QString |
|
|
202 | setRange(QString::null,QString::null); | |
|
203 | 203 | emit categoriesChanged(); |
|
204 | 204 | } |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | /*! |
|
208 | 208 | Inserts \a category to axis at \a index |
|
209 | 209 | */ |
|
210 | 210 | void QBarCategoryAxis::insert(int index, const QString &category) |
|
211 | 211 | { |
|
212 | 212 | Q_D(QBarCategoryAxis); |
|
213 | 213 | if (d->m_categories.isEmpty()) { |
|
214 | 214 | d->m_categories.insert(index,category); |
|
215 | 215 | setRange(category,category); |
|
216 | 216 | }else{ |
|
217 | 217 | d->m_categories.insert(index,category); |
|
218 | 218 | d->emitUpdated(); |
|
219 | 219 | } |
|
220 | 220 | emit categoriesChanged(); |
|
221 | 221 | } |
|
222 | 222 | |
|
223 | 223 | /*! |
|
224 | 224 | Replaces \a oldCategory with \a newCategory. |
|
225 | 225 | If \a oldCategory does not exits on the axis nothing is done. |
|
226 | 226 | */ |
|
227 | 227 | void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory) |
|
228 | 228 | { |
|
229 | 229 | Q_D(QBarCategoryAxis); |
|
230 | 230 | int pos = d->m_categories.indexOf(oldCategory); |
|
231 | 231 | if (pos != -1) { |
|
232 | 232 | d->m_categories.replace(pos, newCategory); |
|
233 | 233 | d->emitUpdated(); |
|
234 | 234 | emit categoriesChanged(); |
|
235 | 235 | } |
|
236 | 236 | } |
|
237 | 237 | |
|
238 | 238 | /*! |
|
239 | 239 | Removes all categories. |
|
240 | 240 | */ |
|
241 | 241 | void QBarCategoryAxis::clear() |
|
242 | 242 | { |
|
243 | 243 | Q_D(QBarCategoryAxis); |
|
244 | 244 | d->m_categories.clear(); |
|
245 |
setRange(QString |
|
|
245 | setRange(QString::null,QString::null); | |
|
246 | 246 | emit categoriesChanged(); |
|
247 | 247 | } |
|
248 | 248 | |
|
249 | 249 | void QBarCategoryAxis::setCategories(const QStringList &categories) |
|
250 | 250 | { |
|
251 | 251 | Q_D(QBarCategoryAxis); |
|
252 | 252 | if(d->m_categories!=categories) { |
|
253 | 253 | d->m_categories = categories; |
|
254 | 254 | setRange(categories.first(),categories.last()); |
|
255 | 255 | emit categoriesChanged(); |
|
256 | 256 | } |
|
257 | 257 | } |
|
258 | 258 | |
|
259 | 259 | QStringList QBarCategoryAxis::categories() |
|
260 | 260 | { |
|
261 | 261 | Q_D(QBarCategoryAxis); |
|
262 | 262 | return d->m_categories; |
|
263 | 263 | } |
|
264 | 264 | |
|
265 | 265 | /*! |
|
266 | 266 | Returns number of categories. |
|
267 | 267 | */ |
|
268 | 268 | int QBarCategoryAxis::count() const |
|
269 | 269 | { |
|
270 | 270 | Q_D(const QBarCategoryAxis); |
|
271 | 271 | return d->m_categories.count(); |
|
272 | 272 | } |
|
273 | 273 | |
|
274 | 274 | /*! |
|
275 | 275 | Returns category at \a index. Index must be valid. |
|
276 | 276 | */ |
|
277 | 277 | QString QBarCategoryAxis::at(int index) const |
|
278 | 278 | { |
|
279 | 279 | Q_D(const QBarCategoryAxis); |
|
280 | 280 | return d->m_categories.at(index); |
|
281 | 281 | } |
|
282 | 282 | |
|
283 | 283 | /*! |
|
284 | 284 | Sets minimum category to \a min. |
|
285 | 285 | */ |
|
286 | 286 | void QBarCategoryAxis::setMin(const QString& min) |
|
287 | 287 | { |
|
288 | 288 | Q_D(QBarCategoryAxis); |
|
289 | 289 | setRange(min,d->m_maxCategory); |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | /*! |
|
293 | 293 | Returns minimum category. |
|
294 | 294 | */ |
|
295 | 295 | QString QBarCategoryAxis::min() const |
|
296 | 296 | { |
|
297 | 297 | Q_D(const QBarCategoryAxis); |
|
298 | 298 | return d->m_minCategory; |
|
299 | 299 | } |
|
300 | 300 | |
|
301 | 301 | /*! |
|
302 | 302 | Sets maximum category to \a max. |
|
303 | 303 | */ |
|
304 | 304 | void QBarCategoryAxis::setMax(const QString& max) |
|
305 | 305 | { |
|
306 | 306 | Q_D(QBarCategoryAxis); |
|
307 | 307 | setRange(d->m_minCategory,max); |
|
308 | 308 | } |
|
309 | 309 | |
|
310 | 310 | /*! |
|
311 | 311 | Returns maximum category |
|
312 | 312 | */ |
|
313 | 313 | QString QBarCategoryAxis::max() const |
|
314 | 314 | { |
|
315 | 315 | Q_D(const QBarCategoryAxis); |
|
316 | 316 | return d->m_maxCategory; |
|
317 | 317 | } |
|
318 | 318 | |
|
319 | 319 | /*! |
|
320 | 320 | Sets range from \a minCategory to \a maxCategory |
|
321 | 321 | */ |
|
322 | 322 | void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCategory) |
|
323 | 323 | { |
|
324 | 324 | Q_D(QBarCategoryAxis); |
|
325 | 325 | |
|
326 | 326 | bool changed = false; |
|
327 | 327 | |
|
328 | 328 | //special case |
|
329 | 329 | if(minCategory.isNull() && maxCategory.isNull()){ |
|
330 | 330 | d->m_minCategory = minCategory; |
|
331 | 331 | d->m_maxCategory = maxCategory; |
|
332 | 332 | d->m_min = 0; |
|
333 | 333 | d->m_max = 0; |
|
334 | 334 | emit minChanged(minCategory); |
|
335 | 335 | emit maxChanged(maxCategory); |
|
336 | 336 | d->m_count=0; |
|
337 | 337 | emit rangeChanged(d->m_minCategory,d->m_maxCategory); |
|
338 | 338 | d->emitUpdated(); |
|
339 | 339 | } |
|
340 | 340 | |
|
341 | 341 | if(d->m_categories.indexOf(d->m_maxCategory)<d->m_categories.indexOf(d->m_minCategory)) return; |
|
342 | 342 | |
|
343 | 343 | if (!minCategory.isEmpty() && d->m_minCategory!=minCategory && d->m_categories.contains(minCategory)) { |
|
344 | 344 | d->m_minCategory = minCategory; |
|
345 | 345 | d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5; |
|
346 | 346 | changed = true; |
|
347 | 347 | emit minChanged(minCategory); |
|
348 | 348 | } |
|
349 | 349 | |
|
350 | 350 | if (!maxCategory.isEmpty() && d->m_maxCategory!=maxCategory && d->m_categories.contains(maxCategory)) { |
|
351 | 351 | d->m_maxCategory = maxCategory; |
|
352 | 352 | d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5; |
|
353 | 353 | changed = true; |
|
354 | 354 | emit maxChanged(maxCategory); |
|
355 | 355 | } |
|
356 | 356 | |
|
357 | 357 | if (changed) { |
|
358 | 358 | d->m_count=d->m_max - d->m_min; |
|
359 | 359 | emit rangeChanged(d->m_minCategory,d->m_maxCategory); |
|
360 | 360 | d->emitUpdated(); |
|
361 | 361 | } |
|
362 | 362 | } |
|
363 | 363 | |
|
364 | 364 | /*! |
|
365 | 365 | Returns the type of the axis |
|
366 | 366 | */ |
|
367 | 367 | QAbstractAxis::AxisType QBarCategoryAxis::type() const |
|
368 | 368 | { |
|
369 | 369 | return AxisTypeBarCategory; |
|
370 | 370 | } |
|
371 | 371 | |
|
372 | 372 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
373 | 373 | |
|
374 | 374 | QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis* q): |
|
375 | 375 | QAbstractAxisPrivate(q), |
|
376 | 376 | m_min(0.0), |
|
377 | 377 | m_max(0.0), |
|
378 | 378 | m_count(0) |
|
379 | 379 | { |
|
380 | 380 | |
|
381 | 381 | } |
|
382 | 382 | |
|
383 | 383 | QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate() |
|
384 | 384 | { |
|
385 | 385 | |
|
386 | 386 | } |
|
387 | 387 | |
|
388 | 388 | void QBarCategoryAxisPrivate::setMin(const QVariant &min) |
|
389 | 389 | { |
|
390 | 390 | setRange(min,m_maxCategory); |
|
391 | 391 | } |
|
392 | 392 | |
|
393 | 393 | void QBarCategoryAxisPrivate::setMax(const QVariant &max) |
|
394 | 394 | { |
|
395 | 395 | setRange(m_minCategory,max); |
|
396 | 396 | } |
|
397 | 397 | |
|
398 | 398 | void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max) |
|
399 | 399 | { |
|
400 | 400 | Q_Q(QBarCategoryAxis); |
|
401 | 401 | QString value1 = min.toString(); |
|
402 | 402 | QString value2 = max.toString(); |
|
403 | 403 | q->setRange(value1,value2); |
|
404 | 404 | } |
|
405 | 405 | |
|
406 | 406 | void QBarCategoryAxisPrivate::handleDomainUpdated() |
|
407 | 407 | { |
|
408 | 408 | Q_Q(QBarCategoryAxis); |
|
409 | 409 | Domain* domain = qobject_cast<Domain*>(sender()); |
|
410 | 410 | |
|
411 | 411 | if(m_orientation==Qt::Horizontal) { |
|
412 | 412 | m_min = domain->minX(); |
|
413 | 413 | m_max = domain->maxX(); |
|
414 | 414 | } |
|
415 | 415 | else if(m_orientation==Qt::Vertical) { |
|
416 | 416 | m_min = domain->minY(); |
|
417 | 417 | m_max = domain->maxY(); |
|
418 | 418 | } |
|
419 | 419 | |
|
420 | 420 | bool changed = false; |
|
421 | 421 | |
|
422 | 422 | int min = m_min + 0.5; |
|
423 | 423 | if(min>=0 && min<m_categories.count()) { |
|
424 | 424 | QString minCategory = m_categories.at(min); |
|
425 | 425 | if(m_minCategory!=minCategory && !minCategory.isEmpty()) { |
|
426 | 426 | m_minCategory=minCategory; |
|
427 | 427 | changed=true; |
|
428 | 428 | emit q->minChanged(minCategory); |
|
429 | 429 | } |
|
430 | 430 | } |
|
431 | 431 | int max = m_max - 0.5; |
|
432 | 432 | if(max>=0 && max<m_categories.count()) { |
|
433 | 433 | QString maxCategory = m_categories.at(max); |
|
434 | 434 | if(m_maxCategory!=maxCategory && !maxCategory.isEmpty()) { |
|
435 | 435 | m_maxCategory=maxCategory; |
|
436 | 436 | emit q->maxChanged(maxCategory); |
|
437 | 437 | } |
|
438 | 438 | } |
|
439 | 439 | |
|
440 | 440 | if (changed) { |
|
441 | 441 | emit q->rangeChanged(m_minCategory,m_maxCategory); |
|
442 | 442 | } |
|
443 | 443 | } |
|
444 | 444 | |
|
445 | 445 | ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter) |
|
446 | 446 | { |
|
447 | 447 | Q_Q(QBarCategoryAxis); |
|
448 | 448 | if(m_orientation == Qt::Vertical){ |
|
449 | 449 | return new ChartBarCategoryAxisY(q,presenter); |
|
450 | 450 | }else{ |
|
451 | 451 | return new ChartBarCategoryAxisX(q,presenter); |
|
452 | 452 | } |
|
453 | 453 | } |
|
454 | 454 | |
|
455 | 455 | void QBarCategoryAxisPrivate::intializeDomain(Domain* domain) |
|
456 | 456 | { |
|
457 | 457 | |
|
458 | 458 | Q_Q(QBarCategoryAxis); |
|
459 | 459 | if(m_max==m_min) { |
|
460 | 460 | int min; |
|
461 | 461 | int max; |
|
462 | 462 | if(m_orientation==Qt::Vertical) { |
|
463 | 463 | min = domain->minY() + 0.5; |
|
464 | 464 | max = domain->maxY() - 0.5; |
|
465 | 465 | } |
|
466 | 466 | else { |
|
467 | 467 | min = domain->minX() + 0.5; |
|
468 | 468 | max = domain->maxX() - 0.5; |
|
469 | 469 | } |
|
470 | 470 | |
|
471 | 471 | if(min>0 && min<m_categories.count() && max>0 && max<m_categories.count()) |
|
472 | 472 | q->setRange(m_categories.at(min),m_categories.at(max)); |
|
473 | 473 | } |
|
474 | 474 | else { |
|
475 | 475 | if(m_orientation==Qt::Vertical) { |
|
476 | 476 | domain->setRangeY(m_min, m_max); |
|
477 | 477 | } |
|
478 | 478 | else { |
|
479 | 479 | domain->setRangeX(m_min, m_max); |
|
480 | 480 | } |
|
481 | 481 | } |
|
482 | 482 | } |
|
483 | 483 | |
|
484 | 484 | #include "moc_qbarcategoryaxis.cpp" |
|
485 | 485 | #include "moc_qbarcategoryaxis_p.cpp" |
|
486 | 486 | |
|
487 | 487 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,422 +1,423 | |||
|
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 "qvalueaxis.h" |
|
22 | 22 | #include "qvalueaxis_p.h" |
|
23 | 23 | #include "chartvalueaxisx_p.h" |
|
24 | 24 | #include "chartvalueaxisy_p.h" |
|
25 | 25 | #include "domain_p.h" |
|
26 | 26 | #include "chartdataset_p.h" |
|
27 | 27 | #include <qmath.h> |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 31 | /*! |
|
32 | 32 | \class QValueAxis |
|
33 | 33 | \brief The QValueAxis class is used for manipulating chart's axis. |
|
34 | 34 | \mainclass |
|
35 | 35 | |
|
36 | 36 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
37 | 37 | Values of axis are drawn to position of ticks. |
|
38 | 38 | |
|
39 | 39 | Example code on how to use QValueAxis. |
|
40 | 40 | \code |
|
41 | 41 | QChartView *chartView = new QChartView; |
|
42 | 42 | QLineSeries *series = new QLineSeries; |
|
43 | 43 | // ... |
|
44 | 44 | chartView->chart()->addSeries(series); |
|
45 | 45 | |
|
46 | 46 | QValueAxis *axisX = new QValueAxis; |
|
47 | 47 | axisX->setRange(10, 20.5); |
|
48 | 48 | axisX->setTickCount(10); |
|
49 | 49 | axisX->setLabelFormat("%.2f"); |
|
50 | 50 | chartView->chart()->setAxisX(series, axisX); |
|
51 | 51 | \endcode |
|
52 | 52 | */ |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | \qmlclass ValueAxis QValueAxis |
|
56 | 56 | \inherits AbstractAxis |
|
57 | 57 | \brief The ValueAxis element is used for manipulating chart's axes |
|
58 | 58 | |
|
59 | 59 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
60 | 60 | Values of axis are drawn to position of ticks |
|
61 | 61 | |
|
62 | 62 | To access Axes you can use ChartView API. For example: |
|
63 | 63 | \code |
|
64 | 64 | ChartView { |
|
65 | 65 | ValueAxis { |
|
66 | 66 | id: xAxis |
|
67 | 67 | min: 0 |
|
68 | 68 | max: 10 |
|
69 | 69 | } |
|
70 | 70 | // Add a few series... |
|
71 | 71 | } |
|
72 | 72 | \endcode |
|
73 | 73 | */ |
|
74 | 74 | |
|
75 | 75 | /*! |
|
76 | 76 | \property QValueAxis::min |
|
77 | 77 | Defines the minimum value on the axis. |
|
78 | 78 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. |
|
79 | 79 | */ |
|
80 | 80 | /*! |
|
81 | 81 | \qmlproperty real ValueAxis::min |
|
82 | 82 | Defines the minimum value on the axis. |
|
83 | 83 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. |
|
84 | 84 | */ |
|
85 | 85 | |
|
86 | 86 | /*! |
|
87 | 87 | \property QValueAxis::max |
|
88 | 88 | Defines the maximum value on the axis. |
|
89 | 89 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. |
|
90 | 90 | */ |
|
91 | 91 | /*! |
|
92 | 92 | \qmlproperty real ValueAxis::max |
|
93 | 93 | Defines the maximum value on the axis. |
|
94 | 94 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. |
|
95 | 95 | */ |
|
96 | 96 | |
|
97 | 97 | /*! |
|
98 | 98 | \property QValueAxis::labelFormat |
|
99 | 99 | Defines the label format for the axis. |
|
100 | 100 | See QString::sprintf() for the details. |
|
101 | 101 | */ |
|
102 | 102 | /*! |
|
103 | 103 | \qmlproperty real ValueAxis::labelFormat |
|
104 | 104 | Defines the label format for the axis. |
|
105 | 105 | See QString::sprintf() for the details. |
|
106 | 106 | */ |
|
107 | 107 | |
|
108 | 108 | /*! |
|
109 | 109 | \fn void QValueAxis::minChanged(qreal min) |
|
110 | 110 | Axis emits signal when \a min of axis has changed. |
|
111 | 111 | */ |
|
112 | 112 | /*! |
|
113 | 113 | \qmlsignal ValueAxis::onMinChanged(real min) |
|
114 | 114 | Axis emits signal when \a min of axis has changed. |
|
115 | 115 | */ |
|
116 | 116 | |
|
117 | 117 | /*! |
|
118 | 118 | \fn void QValueAxis::maxChanged(qreal max) |
|
119 | 119 | Axis emits signal when \a max of axis has changed. |
|
120 | 120 | */ |
|
121 | 121 | /*! |
|
122 | 122 | \qmlsignal ValueAxis::onMaxChanged(real max) |
|
123 | 123 | Axis emits signal when \a max of axis has changed. |
|
124 | 124 | */ |
|
125 | 125 | |
|
126 | 126 | /*! |
|
127 | 127 | \fn void QValueAxis::rangeChanged(qreal min, qreal max) |
|
128 | 128 | Axis emits signal when \a min or \a max of axis has changed. |
|
129 | 129 | */ |
|
130 | 130 | |
|
131 | 131 | /*! |
|
132 | 132 | \property QValueAxis::tickCount |
|
133 | 133 | The number of tick marks for the axis. |
|
134 | 134 | */ |
|
135 | 135 | |
|
136 | 136 | /*! |
|
137 | 137 | \qmlproperty int ValueAxis::tickCount |
|
138 | 138 | The number of tick marks for the axis. |
|
139 | 139 | */ |
|
140 | 140 | |
|
141 | 141 | /*! |
|
142 | 142 | \property QValueAxis::niceNumbersEnabled |
|
143 | 143 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
144 | 144 | */ |
|
145 | 145 | |
|
146 | 146 | /*! |
|
147 | 147 | \qmlproperty bool ValueAxis::niceNumbersEnabled |
|
148 | 148 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
149 | 149 | */ |
|
150 | 150 | |
|
151 | 151 | /*! |
|
152 | 152 | Constructs an axis object which is a child of \a parent. |
|
153 | 153 | */ |
|
154 | 154 | QValueAxis::QValueAxis(QObject *parent) : |
|
155 | 155 | QAbstractAxis(*new QValueAxisPrivate(this),parent) |
|
156 | 156 | { |
|
157 | 157 | |
|
158 | 158 | } |
|
159 | 159 | |
|
160 | 160 | /*! |
|
161 | 161 | \internal |
|
162 | 162 | */ |
|
163 | 163 | QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent) |
|
164 | 164 | { |
|
165 | 165 | |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | /*! |
|
169 | 169 | Destroys the object |
|
170 | 170 | */ |
|
171 | 171 | QValueAxis::~QValueAxis() |
|
172 | 172 | { |
|
173 | 173 | Q_D(QValueAxis); |
|
174 | 174 | if(d->m_dataset) { |
|
175 | 175 | d->m_dataset->removeAxis(this); |
|
176 | 176 | } |
|
177 | 177 | } |
|
178 | 178 | |
|
179 | 179 | void QValueAxis::setMin(qreal min) |
|
180 | 180 | { |
|
181 | 181 | Q_D(QValueAxis); |
|
182 | 182 | setRange(min, qMax(d->m_max, min)); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | qreal QValueAxis::min() const |
|
186 | 186 | { |
|
187 | 187 | Q_D(const QValueAxis); |
|
188 | 188 | return d->m_min; |
|
189 | 189 | } |
|
190 | 190 | |
|
191 | 191 | void QValueAxis::setMax(qreal max) |
|
192 | 192 | { |
|
193 | 193 | Q_D(QValueAxis); |
|
194 | 194 | setRange(qMin(d->m_min, max), max); |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | qreal QValueAxis::max() const |
|
198 | 198 | { |
|
199 | 199 | Q_D(const QValueAxis); |
|
200 | 200 | return d->m_max; |
|
201 | 201 | } |
|
202 | 202 | |
|
203 | 203 | /*! |
|
204 | 204 | Sets range from \a min to \a max on the axis. |
|
205 | 205 | If min is greater than max then this function returns without making any changes. |
|
206 | 206 | */ |
|
207 | 207 | void QValueAxis::setRange(qreal min, qreal max) |
|
208 | 208 | { |
|
209 | 209 | Q_D(QValueAxis); |
|
210 | 210 | bool changed = false; |
|
211 | 211 | |
|
212 | 212 | if (min > max) return; |
|
213 | 213 | |
|
214 | 214 | if(d->m_niceNumbers) { |
|
215 | 215 | int ticks = d->m_tickCount; |
|
216 | 216 | d->looseNiceNumbers(min, max, ticks); |
|
217 | 217 | if(ticks!=d->m_tickCount) setTickCount(ticks); |
|
218 | 218 | } |
|
219 | 219 | |
|
220 | 220 | if (!qFuzzyIsNull(d->m_min - min)) { |
|
221 | 221 | d->m_min = min; |
|
222 | 222 | changed = true; |
|
223 | 223 | emit minChanged(min); |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | if (!qFuzzyIsNull(d->m_max - max)) { |
|
227 | 227 | d->m_max = max; |
|
228 | 228 | changed = true; |
|
229 | 229 | emit maxChanged(max); |
|
230 | 230 | } |
|
231 | 231 | |
|
232 | 232 | if (changed) { |
|
233 | 233 | emit rangeChanged(min,max); |
|
234 | 234 | d->emitUpdated(); |
|
235 | 235 | } |
|
236 | 236 | } |
|
237 | 237 | |
|
238 | 238 | /*! |
|
239 | 239 | Sets \a count for ticks on the axis. |
|
240 | 240 | */ |
|
241 | 241 | void QValueAxis::setTickCount(int count) |
|
242 | 242 | { |
|
243 | 243 | Q_D(QValueAxis); |
|
244 | 244 | if (d->m_tickCount != count && count >=2) { |
|
245 | 245 | d->m_tickCount = count; |
|
246 | 246 | d->emitUpdated(); |
|
247 | 247 | } |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | /*! |
|
251 | 251 | \fn int QValueAxis::tickCount() const |
|
252 | 252 | Return number of ticks on the axis |
|
253 | 253 | */ |
|
254 | 254 | int QValueAxis::tickCount() const |
|
255 | 255 | { |
|
256 | 256 | Q_D(const QValueAxis); |
|
257 | 257 | return d->m_tickCount; |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | void QValueAxis::setNiceNumbersEnabled(bool enable) |
|
261 | 261 | { |
|
262 | 262 | Q_D(QValueAxis); |
|
263 | 263 | if (d->m_niceNumbers != enable){ |
|
264 | 264 | d->m_niceNumbers = enable; |
|
265 | 265 | if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) { |
|
266 | 266 | setRange(d->m_min,d->m_max); |
|
267 | 267 | } |
|
268 | 268 | } |
|
269 | 269 | } |
|
270 | 270 | |
|
271 | 271 | bool QValueAxis::niceNumbersEnabled() const |
|
272 | 272 | { |
|
273 | 273 | Q_D(const QValueAxis); |
|
274 | 274 | return d->m_niceNumbers; |
|
275 | 275 | } |
|
276 | 276 | |
|
277 | 277 | void QValueAxis::setLabelFormat(const QString &format) |
|
278 | 278 | { |
|
279 | 279 | Q_D(QValueAxis); |
|
280 | 280 | d->m_format = format; |
|
281 | 281 | } |
|
282 | 282 | |
|
283 | 283 | QString QValueAxis::labelFormat() const |
|
284 | 284 | { |
|
285 | 285 | Q_D(const QValueAxis); |
|
286 | 286 | return d->m_format; |
|
287 | 287 | } |
|
288 | 288 | |
|
289 | 289 | /*! |
|
290 | 290 | Returns the type of the axis |
|
291 | 291 | */ |
|
292 | 292 | QAbstractAxis::AxisType QValueAxis::type() const |
|
293 | 293 | { |
|
294 | 294 | return AxisTypeValue; |
|
295 | 295 | } |
|
296 | 296 | |
|
297 | 297 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
298 | 298 | |
|
299 | 299 | QValueAxisPrivate::QValueAxisPrivate(QValueAxis* q): |
|
300 | 300 | QAbstractAxisPrivate(q), |
|
301 | 301 | m_min(0), |
|
302 | 302 | m_max(0), |
|
303 | 303 | m_tickCount(5), |
|
304 | m_niceNumbers(false) | |
|
304 | m_niceNumbers(false), | |
|
305 | m_format(QString::null) | |
|
305 | 306 | { |
|
306 | 307 | |
|
307 | 308 | } |
|
308 | 309 | |
|
309 | 310 | QValueAxisPrivate::~QValueAxisPrivate() |
|
310 | 311 | { |
|
311 | 312 | |
|
312 | 313 | } |
|
313 | 314 | |
|
314 | 315 | void QValueAxisPrivate::handleDomainUpdated() |
|
315 | 316 | { |
|
316 | 317 | Q_Q(QValueAxis); |
|
317 | 318 | Domain* domain = qobject_cast<Domain*>(sender()); |
|
318 | 319 | Q_ASSERT(domain); |
|
319 | 320 | |
|
320 | 321 | if(orientation()==Qt::Horizontal){ |
|
321 | 322 | q->setRange(domain->minX(),domain->maxX()); |
|
322 | 323 | }else if(orientation()==Qt::Vertical){ |
|
323 | 324 | q->setRange(domain->minY(),domain->maxY()); |
|
324 | 325 | } |
|
325 | 326 | } |
|
326 | 327 | |
|
327 | 328 | |
|
328 | 329 | void QValueAxisPrivate::setMin(const QVariant &min) |
|
329 | 330 | { |
|
330 | 331 | Q_Q(QValueAxis); |
|
331 | 332 | bool ok; |
|
332 | 333 | qreal value = min.toReal(&ok); |
|
333 | 334 | if(ok) q->setMin(value); |
|
334 | 335 | } |
|
335 | 336 | |
|
336 | 337 | void QValueAxisPrivate::setMax(const QVariant &max) |
|
337 | 338 | { |
|
338 | 339 | |
|
339 | 340 | Q_Q(QValueAxis); |
|
340 | 341 | bool ok; |
|
341 | 342 | qreal value = max.toReal(&ok); |
|
342 | 343 | if(ok) q->setMax(value); |
|
343 | 344 | } |
|
344 | 345 | |
|
345 | 346 | void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) |
|
346 | 347 | { |
|
347 | 348 | Q_Q(QValueAxis); |
|
348 | 349 | bool ok1; |
|
349 | 350 | bool ok2; |
|
350 | 351 | qreal value1 = min.toReal(&ok1); |
|
351 | 352 | qreal value2 = max.toReal(&ok2); |
|
352 | 353 | if(ok1&&ok2) q->setRange(value1,value2); |
|
353 | 354 | } |
|
354 | 355 | |
|
355 | 356 | ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter) |
|
356 | 357 | { |
|
357 | 358 | Q_Q(QValueAxis); |
|
358 | 359 | if(m_orientation == Qt::Vertical){ |
|
359 | 360 | return new ChartValueAxisY(q,presenter); |
|
360 | 361 | }else{ |
|
361 | 362 | return new ChartValueAxisX(q,presenter); |
|
362 | 363 | } |
|
363 | 364 | |
|
364 | 365 | } |
|
365 | 366 | |
|
366 | 367 | void QValueAxisPrivate::intializeDomain(Domain* domain) |
|
367 | 368 | { |
|
368 | 369 | Q_Q(QValueAxis); |
|
369 | 370 | if(qFuzzyCompare(m_max,m_min)) { |
|
370 | 371 | if(m_orientation==Qt::Vertical){ |
|
371 | 372 | q->setRange(domain->minY(),domain->maxY()); |
|
372 | 373 | }else{ |
|
373 | 374 | q->setRange(domain->minX(), domain->maxX()); |
|
374 | 375 | } |
|
375 | 376 | } else { |
|
376 | 377 | if(m_orientation==Qt::Vertical){ |
|
377 | 378 | domain->setRangeY(m_min, m_max); |
|
378 | 379 | }else{ |
|
379 | 380 | domain->setRangeX(m_min, m_max); |
|
380 | 381 | } |
|
381 | 382 | } |
|
382 | 383 | } |
|
383 | 384 | |
|
384 | 385 | //algorithm defined by Paul S.Heckbert GraphicalGems I |
|
385 | 386 | |
|
386 | 387 | void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const |
|
387 | 388 | { |
|
388 | 389 | qreal range = niceNumber(max-min,true); //range with ceiling |
|
389 | 390 | qreal step = niceNumber(range/(ticksCount-1),false); |
|
390 | 391 | min = qFloor(min/step); |
|
391 | 392 | max = qCeil(max/step); |
|
392 | 393 | ticksCount = int(max-min) +1; |
|
393 | 394 | min*=step; |
|
394 | 395 | max*=step; |
|
395 | 396 | } |
|
396 | 397 | |
|
397 | 398 | //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n |
|
398 | 399 | |
|
399 | 400 | qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const |
|
400 | 401 | { |
|
401 | 402 | qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x |
|
402 | 403 | qreal q = x/z;//q<10 && q>=1; |
|
403 | 404 | |
|
404 | 405 | if(ceiling) { |
|
405 | 406 | if(q <= 1.0) q=1; |
|
406 | 407 | else if(q <= 2.0) q=2; |
|
407 | 408 | else if(q <= 5.0) q=5; |
|
408 | 409 | else q=10; |
|
409 | 410 | } |
|
410 | 411 | else { |
|
411 | 412 | if(q < 1.5) q=1; |
|
412 | 413 | else if(q < 3.0) q=2; |
|
413 | 414 | else if(q < 7.0) q=5; |
|
414 | 415 | else q=10; |
|
415 | 416 | } |
|
416 | 417 | return q*z; |
|
417 | 418 | } |
|
418 | 419 | |
|
419 | 420 | #include "moc_qvalueaxis.cpp" |
|
420 | 421 | #include "moc_qvalueaxis_p.cpp" |
|
421 | 422 | |
|
422 | 423 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now