@@ -1,384 +1,381 | |||
|
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 "qbarcategoriesaxis.h" |
|
22 | 22 | #include "qbarcategoriesaxis_p.h" |
|
23 | 23 | #include "chartcategoriesaxisx_p.h" |
|
24 | 24 | #include "chartcategoriesaxisy_p.h" |
|
25 | 25 | #include <qmath.h> |
|
26 | 26 | #include <QDebug> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 29 | /*! |
|
30 | 30 | \class QBarCategoriesAxis |
|
31 | 31 | \brief The QBarCategoriesAxis class is used for manipulating chart's axis. |
|
32 | 32 | \mainclass |
|
33 | 33 | |
|
34 | 34 | BarCategoriesAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
35 | 35 | Categories are drawn between ticks. Note that you can use this also with lineseries too. |
|
36 | 36 | See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that. |
|
37 | 37 | */ |
|
38 | 38 | |
|
39 | 39 | /*! |
|
40 | 40 | \qmlclass BarCategoriesAxis QBarCategoriesAxis |
|
41 | 41 | \brief The Axis element is used for manipulating chart's axes. |
|
42 | 42 | |
|
43 | 43 | Axis can be setup to show axis line with tick marks, grid lines and shades. |
|
44 | 44 | Categories are drawn between ticks. Note that you can use this also with lineseries too. |
|
45 | 45 | |
|
46 | 46 | To access BarCategoriesAxis you can use ChartView API. For example: |
|
47 | 47 | \code |
|
48 | 48 | ChartView { |
|
49 | 49 | BarCategoriesAxis { |
|
50 | 50 | id: categoryAxis |
|
51 | 51 | categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ] |
|
52 | 52 | } |
|
53 | 53 | // Add a few series... |
|
54 | 54 | } |
|
55 | 55 | \endcode |
|
56 | 56 | */ |
|
57 | 57 | |
|
58 | 58 | /*! |
|
59 | 59 | \property QBarCategoriesAxis::categories |
|
60 | 60 | Defines the categories of axis |
|
61 | 61 | */ |
|
62 | 62 | /*! |
|
63 | 63 | \qmlproperty QStringList BarCategoriesAxis::categories |
|
64 | 64 | Defines the categories of axis |
|
65 | 65 | */ |
|
66 | 66 | |
|
67 | 67 | /*! |
|
68 | 68 | \property QBarCategoriesAxis::min |
|
69 | 69 | Defines the minimum value on the axis. |
|
70 | 70 | */ |
|
71 | 71 | /*! |
|
72 | 72 | \qmlproperty real BarCategoriesAxis::min |
|
73 | 73 | Defines the minimum value on the axis. |
|
74 | 74 | */ |
|
75 | 75 | |
|
76 | 76 | /*! |
|
77 | 77 | \property QBarCategoriesAxis::max |
|
78 | 78 | Defines the maximum value on the axis. |
|
79 | 79 | */ |
|
80 | 80 | /*! |
|
81 | 81 | \qmlproperty real BarCategoriesAxis::max |
|
82 | 82 | Defines the maximum value on the axis. |
|
83 | 83 | */ |
|
84 | 84 | |
|
85 | 85 | /*! |
|
86 | 86 | \fn void QBarCategoriesAxis::minChanged(const QString &min) |
|
87 | 87 | Axis emits signal when \a min of axis has changed. |
|
88 | 88 | */ |
|
89 | 89 | |
|
90 | 90 | /*! |
|
91 | 91 | \fn void QBarCategoriesAxis::maxChanged(const QString &max) |
|
92 | 92 | Axis emits signal when \a max of axis has changed. |
|
93 | 93 | */ |
|
94 | 94 | |
|
95 | 95 | /*! |
|
96 | 96 | \fn void QBarCategoriesAxis::rangeChanged(const QString &min, const QString &max) |
|
97 | 97 | Axis emits signal when \a min or \a max of axis has changed. |
|
98 | 98 | */ |
|
99 | 99 | |
|
100 | 100 | /*! |
|
101 | 101 | Constructs an axis object which is a child of \a parent. |
|
102 | 102 | */ |
|
103 | 103 | QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent): |
|
104 | 104 | QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent) |
|
105 | 105 | { |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | /*! |
|
109 | 109 | Destroys the object |
|
110 | 110 | */ |
|
111 | 111 | QBarCategoriesAxis::~QBarCategoriesAxis() |
|
112 | 112 | { |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | /*! |
|
116 | 116 | \internal |
|
117 | 117 | */ |
|
118 | 118 | QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent) |
|
119 | 119 | { |
|
120 | 120 | |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | /*! |
|
124 | 124 | Appends \a categories to axis |
|
125 | 125 | */ |
|
126 | 126 | void QBarCategoriesAxis::append(const QStringList &categories) |
|
127 | 127 | { |
|
128 | 128 | if(categories.isEmpty()) return; |
|
129 | 129 | |
|
130 | 130 | Q_D(QBarCategoriesAxis); |
|
131 | 131 | if (d->m_categories.isEmpty()) { |
|
132 | 132 | d->m_categories.append(categories); |
|
133 | 133 | setRange(categories.first(),categories.last()); |
|
134 | 134 | }else{ |
|
135 | 135 | d->m_categories.append(categories); |
|
136 | 136 | } |
|
137 | 137 | emit d->updated(); |
|
138 | 138 | emit categoriesChanged(); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | /*! |
|
142 | 142 | Appends \a category to axis |
|
143 | 143 | */ |
|
144 | 144 | void QBarCategoriesAxis::append(const QString &category) |
|
145 | 145 | { |
|
146 | 146 | Q_D(QBarCategoriesAxis); |
|
147 | 147 | if (d->m_categories.isEmpty()) { |
|
148 | 148 | d->m_categories.append(category); |
|
149 | 149 | setRange(category,category); |
|
150 | 150 | }else{ |
|
151 | 151 | d->m_categories.append(category); |
|
152 | 152 | } |
|
153 | 153 | emit d->updated(); |
|
154 | 154 | emit categoriesChanged(); |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | /*! |
|
158 | 158 | Removes \a category from axis |
|
159 | 159 | */ |
|
160 | 160 | void QBarCategoriesAxis::remove(const QString &category) |
|
161 | 161 | { |
|
162 | 162 | Q_D(QBarCategoriesAxis); |
|
163 | 163 | if (d->m_categories.contains(category)) { |
|
164 | 164 | d->m_categories.removeAt(d->m_categories.indexOf(category)); |
|
165 | 165 | setRange(d->m_categories.first(),d->m_categories.last()); |
|
166 | 166 | emit d->updated(); |
|
167 | 167 | emit categoriesChanged(); |
|
168 | 168 | } |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | /*! |
|
172 | 172 | Inserts \a category to axis at \a index |
|
173 | 173 | */ |
|
174 | 174 | void QBarCategoriesAxis::insert(int index, const QString &category) |
|
175 | 175 | { |
|
176 | 176 | Q_D(QBarCategoriesAxis); |
|
177 | 177 | if (d->m_categories.isEmpty()) { |
|
178 | 178 | d->m_categories.insert(index,category); |
|
179 | 179 | setRange(category,category); |
|
180 | 180 | }else{ |
|
181 | 181 | d->m_categories.insert(index,category); |
|
182 | 182 | } |
|
183 | 183 | emit d->updated(); |
|
184 | 184 | emit categoriesChanged(); |
|
185 | 185 | } |
|
186 | 186 | |
|
187 | 187 | /*! |
|
188 | 188 | Removes all categories. |
|
189 | 189 | */ |
|
190 | 190 | void QBarCategoriesAxis::clear() |
|
191 | 191 | { |
|
192 | 192 | Q_D(QBarCategoriesAxis); |
|
193 | 193 | d->m_categories.clear(); |
|
194 | 194 | setRange(QString::null,QString::null); |
|
195 | 195 | emit d->updated(); |
|
196 | 196 | emit categoriesChanged(); |
|
197 | 197 | } |
|
198 | 198 | |
|
199 | 199 | void QBarCategoriesAxis::setCategories(const QStringList &categories) |
|
200 | 200 | { |
|
201 | 201 | Q_D(QBarCategoriesAxis); |
|
202 | 202 | if(d->m_categories!=categories){ |
|
203 | 203 | d->m_categories = categories; |
|
204 | 204 | setRange(categories.first(),categories.last()); |
|
205 | 205 | emit d->updated(); |
|
206 | 206 | emit categoriesChanged(); |
|
207 | 207 | } |
|
208 | 208 | } |
|
209 | 209 | |
|
210 | 210 | QStringList QBarCategoriesAxis::categories() |
|
211 | 211 | { |
|
212 | 212 | Q_D(QBarCategoriesAxis); |
|
213 | 213 | return d->m_categories; |
|
214 | 214 | } |
|
215 | 215 | |
|
216 | 216 | /*! |
|
217 | 217 | Returns number of categories. |
|
218 | 218 | */ |
|
219 | 219 | int QBarCategoriesAxis::count() const |
|
220 | 220 | { |
|
221 | 221 | Q_D(const QBarCategoriesAxis); |
|
222 | 222 | return d->m_categories.count(); |
|
223 | 223 | } |
|
224 | 224 | |
|
225 | 225 | /*! |
|
226 | 226 | Returns category at \a index. Index must be valid. |
|
227 | 227 | */ |
|
228 | 228 | QString QBarCategoriesAxis::at(int index) const |
|
229 | 229 | { |
|
230 | 230 | Q_D(const QBarCategoriesAxis); |
|
231 | 231 | return d->m_categories.at(index); |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | /*! |
|
235 | 235 | Sets minimum category to \a min. |
|
236 | 236 | */ |
|
237 | 237 | void QBarCategoriesAxis::setMin(const QString& min) |
|
238 | 238 | { |
|
239 | 239 | Q_D(QBarCategoriesAxis); |
|
240 | 240 | setRange(min,d->m_maxCategory); |
|
241 | 241 | } |
|
242 | 242 | |
|
243 | 243 | /*! |
|
244 | 244 | Returns minimum category. |
|
245 | 245 | */ |
|
246 | 246 | QString QBarCategoriesAxis::min() const |
|
247 | 247 | { |
|
248 | 248 | Q_D(const QBarCategoriesAxis); |
|
249 | 249 | return d->m_minCategory; |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | /*! |
|
253 | 253 | Sets maximum category to \a max. |
|
254 | 254 | */ |
|
255 | 255 | void QBarCategoriesAxis::setMax(const QString& max) |
|
256 | 256 | { |
|
257 | 257 | Q_D(QBarCategoriesAxis); |
|
258 | 258 | setRange(d->m_minCategory,max); |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | /*! |
|
262 | 262 | Returns maximum category |
|
263 | 263 | */ |
|
264 | 264 | QString QBarCategoriesAxis::max() const |
|
265 | 265 | { |
|
266 | 266 | Q_D(const QBarCategoriesAxis); |
|
267 | 267 | return d->m_maxCategory; |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | /*! |
|
271 | 271 | Sets range from \a minCategory to \a maxCategory |
|
272 | 272 | */ |
|
273 | 273 | void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory) |
|
274 | 274 | { |
|
275 | 275 | Q_D(QBarCategoriesAxis); |
|
276 | 276 | |
|
277 | 277 | int minIndex = d->m_categories.indexOf(minCategory); |
|
278 | 278 | if (minIndex == -1) { |
|
279 | 279 | return; |
|
280 | 280 | } |
|
281 | 281 | int maxIndex = d->m_categories.indexOf(maxCategory); |
|
282 | 282 | if (maxIndex == -1) { |
|
283 | 283 | return; |
|
284 | 284 | } |
|
285 | 285 | |
|
286 | 286 | if (maxIndex <= minIndex) { |
|
287 | 287 | // max must be greater than min |
|
288 | 288 | return; |
|
289 | 289 | } |
|
290 | 290 | |
|
291 | 291 | bool changed = false; |
|
292 | 292 | if (!qFuzzyIsNull(d->m_min - (minIndex))||d->m_minCategory!=minCategory) { |
|
293 | 293 | d->m_minCategory = minCategory; |
|
294 | 294 | d->m_min = minIndex; |
|
295 | 295 | emit minChanged(minCategory); |
|
296 | 296 | changed = true; |
|
297 | 297 | } |
|
298 | 298 | |
|
299 | 299 | if (!qFuzzyIsNull(d->m_max - (maxIndex))||d->m_maxCategory!=maxCategory ) { |
|
300 | 300 | d->m_max = maxIndex; |
|
301 | 301 | d->m_maxCategory = maxCategory; |
|
302 | 302 | emit maxChanged(maxCategory); |
|
303 | 303 | changed = true; |
|
304 | 304 | } |
|
305 | 305 | |
|
306 | 306 | if (changed) { |
|
307 | 307 | d->emitRange(); |
|
308 | 308 | } |
|
309 | 309 | } |
|
310 | 310 | |
|
311 | 311 | /*! |
|
312 | 312 | Returns the type of the axis |
|
313 | 313 | */ |
|
314 | 314 | QAbstractAxis::AxisType QBarCategoriesAxis::type() const |
|
315 | 315 | { |
|
316 | 316 | return AxisTypeCategories; |
|
317 | 317 | } |
|
318 | 318 | |
|
319 | 319 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
320 | 320 | |
|
321 | 321 | QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q): |
|
322 | 322 | QAbstractAxisPrivate(q) |
|
323 | 323 | { |
|
324 | 324 | |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | 327 | QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate() |
|
328 | 328 | { |
|
329 | 329 | |
|
330 | 330 | } |
|
331 | 331 | |
|
332 | 332 | void QBarCategoriesAxisPrivate::setMin(const QVariant &min) |
|
333 | 333 | { |
|
334 | 334 | setRange(min,m_maxCategory); |
|
335 | 335 | } |
|
336 | 336 | |
|
337 | 337 | void QBarCategoriesAxisPrivate::setMax(const QVariant &max) |
|
338 | 338 | { |
|
339 | 339 | setRange(m_minCategory,max); |
|
340 | 340 | } |
|
341 | 341 | |
|
342 | 342 | void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max) |
|
343 | 343 | { |
|
344 | 344 | Q_Q(QBarCategoriesAxis); |
|
345 | 345 | QString value1 = min.toString(); |
|
346 | 346 | QString value2 = max.toString(); |
|
347 | 347 | q->setRange(value1,value2); |
|
348 | 348 | } |
|
349 | 349 | |
|
350 | 350 | int QBarCategoriesAxisPrivate::ticksCount() const |
|
351 | 351 | { |
|
352 | 352 | return m_categories.count()+1; |
|
353 | 353 | } |
|
354 | 354 | |
|
355 | 355 | void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count) |
|
356 | 356 | { |
|
357 | 357 | m_min = min; |
|
358 | 358 | m_max = max; |
|
359 | 359 | m_ticksCount = count; |
|
360 | 360 | } |
|
361 | 361 | |
|
362 | 362 | ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter) |
|
363 | 363 | { |
|
364 | 364 | Q_Q( QBarCategoriesAxis); |
|
365 | 365 | if(m_orientation == Qt::Vertical){ |
|
366 | 366 | return new ChartCategoriesAxisY(q,presenter); |
|
367 | 367 | }else{ |
|
368 | 368 | return new ChartCategoriesAxisX(q,presenter); |
|
369 | 369 | } |
|
370 | 370 | } |
|
371 | 371 | |
|
372 | 372 | void QBarCategoriesAxisPrivate::emitRange() |
|
373 | 373 | { |
|
374 | Q_Q( QBarCategoriesAxis); | |
|
375 | if(!q->signalsBlocked()) { | |
|
376 | emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false); | |
|
377 | } | |
|
374 | emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false); | |
|
378 | 375 | } |
|
379 | 376 | |
|
380 | 377 | |
|
381 | 378 | #include "moc_qbarcategoriesaxis.cpp" |
|
382 | 379 | #include "moc_qbarcategoriesaxis_p.cpp" |
|
383 | 380 | |
|
384 | 381 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,304 +1,303 | |||
|
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 "qvaluesaxis.h" |
|
22 | 22 | #include "qvaluesaxis_p.h" |
|
23 | 23 | #include "chartvaluesaxisx_p.h" |
|
24 | 24 | #include "chartvaluesaxisy_p.h" |
|
25 | 25 | #include <QDebug> |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | 28 | /*! |
|
29 | 29 | \class QValuesAxis |
|
30 | 30 | \brief The QValuesAxis class is used for manipulating chart's axis. |
|
31 | 31 | \mainclass |
|
32 | 32 | |
|
33 | 33 | ValuesAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
34 | 34 | Values of axis are drawn to position of ticks |
|
35 | 35 | */ |
|
36 | 36 | |
|
37 | 37 | /*! |
|
38 | 38 | \qmlclass ValuesAxis QValuesAxis |
|
39 | 39 | \brief The ValuesAxis element is used for manipulating chart's axes |
|
40 | 40 | |
|
41 | 41 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
42 | 42 | Values of axis are drawn to position of ticks |
|
43 | 43 | |
|
44 | 44 | To access Axes you can use ChartView API. For example: |
|
45 | 45 | \code |
|
46 | 46 | ChartView { |
|
47 | 47 | ValuesAxis { |
|
48 | 48 | id: xAxis |
|
49 | 49 | min: 0 |
|
50 | 50 | max: 10 |
|
51 | 51 | } |
|
52 | 52 | // Add a few series... |
|
53 | 53 | } |
|
54 | 54 | \endcode |
|
55 | 55 | */ |
|
56 | 56 | |
|
57 | 57 | /*! |
|
58 | 58 | \property QValuesAxis::min |
|
59 | 59 | Defines the minimum value on the axis. |
|
60 | 60 | */ |
|
61 | 61 | /*! |
|
62 | 62 | \qmlproperty real ValuesAxis::min |
|
63 | 63 | Defines the minimum value on the axis. |
|
64 | 64 | */ |
|
65 | 65 | |
|
66 | 66 | /*! |
|
67 | 67 | \property QValuesAxis::max |
|
68 | 68 | Defines the maximum value on the axis. |
|
69 | 69 | */ |
|
70 | 70 | /*! |
|
71 | 71 | \qmlproperty real ValuesAxis::max |
|
72 | 72 | Defines the maximum value on the axis. |
|
73 | 73 | */ |
|
74 | 74 | |
|
75 | 75 | /*! |
|
76 | 76 | \fn void QValuesAxis::minChanged(qreal min) |
|
77 | 77 | Axis emits signal when \a min of axis has changed. |
|
78 | 78 | */ |
|
79 | 79 | |
|
80 | 80 | /*! |
|
81 | 81 | \fn void QValuesAxis::maxChanged(qreal max) |
|
82 | 82 | Axis emits signal when \a max of axis has changed. |
|
83 | 83 | */ |
|
84 | 84 | |
|
85 | 85 | /*! |
|
86 | 86 | \fn void QValuesAxis::rangeChanged(qreal min, qreal max) |
|
87 | 87 | Axis emits signal when \a min or \a max of axis has changed. |
|
88 | 88 | */ |
|
89 | 89 | |
|
90 | 90 | /*! |
|
91 | 91 | \property QValuesAxis::ticksCount |
|
92 | 92 | The number of tick marks for the axis. |
|
93 | 93 | */ |
|
94 | 94 | |
|
95 | 95 | /*! |
|
96 | 96 | \qmlproperty int ValuesAxis::ticksCount |
|
97 | 97 | The number of tick marks for the axis. |
|
98 | 98 | */ |
|
99 | 99 | |
|
100 | 100 | /*! |
|
101 | 101 | \property QValuesAxis::niceNumbersEnabled |
|
102 | 102 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
103 | 103 | */ |
|
104 | 104 | |
|
105 | 105 | /*! |
|
106 | 106 | \qmlproperty bool ValuesAxis::niceNumbersEnabled |
|
107 | 107 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
108 | 108 | */ |
|
109 | 109 | |
|
110 | 110 | /*! |
|
111 | 111 | Constructs an axis object which is a child of \a parent. |
|
112 | 112 | */ |
|
113 | 113 | QValuesAxis::QValuesAxis(QObject *parent) : |
|
114 | 114 | QAbstractAxis(*new QValuesAxisPrivate(this),parent) |
|
115 | 115 | { |
|
116 | 116 | |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | /*! |
|
120 | 120 | \internal |
|
121 | 121 | */ |
|
122 | 122 | QValuesAxis::QValuesAxis(QValuesAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent) |
|
123 | 123 | { |
|
124 | 124 | |
|
125 | 125 | } |
|
126 | 126 | |
|
127 | 127 | /*! |
|
128 | 128 | Destroys the object |
|
129 | 129 | */ |
|
130 | 130 | QValuesAxis::~QValuesAxis() |
|
131 | 131 | { |
|
132 | 132 | |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | void QValuesAxis::setMin(qreal min) |
|
136 | 136 | { |
|
137 | 137 | Q_D(QValuesAxis); |
|
138 | 138 | setRange(min,d->m_max); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | qreal QValuesAxis::min() const |
|
142 | 142 | { |
|
143 | 143 | Q_D(const QValuesAxis); |
|
144 | 144 | return d->m_min; |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | 147 | void QValuesAxis::setMax(qreal max) |
|
148 | 148 | { |
|
149 | 149 | Q_D(QValuesAxis); |
|
150 | 150 | setRange(d->m_min,max); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | qreal QValuesAxis::max() const |
|
154 | 154 | { |
|
155 | 155 | Q_D(const QValuesAxis); |
|
156 | 156 | return d->m_max; |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | /*! |
|
160 | 160 | Sets range from \a min to \a max on the axis. |
|
161 | 161 | */ |
|
162 | 162 | void QValuesAxis::setRange(qreal min, qreal max) |
|
163 | 163 | { |
|
164 | 164 | Q_D(QValuesAxis); |
|
165 | ||
|
165 | 166 | bool changed = false; |
|
166 | 167 | if (!qFuzzyIsNull(d->m_min - min)) { |
|
167 | 168 | d->m_min = min; |
|
168 | 169 | changed = true; |
|
169 | 170 | emit minChanged(min); |
|
170 | 171 | } |
|
171 | 172 | |
|
172 | 173 | if (!qFuzzyIsNull(d->m_max - max)) { |
|
173 | 174 | d->m_max = max; |
|
174 | 175 | changed = true; |
|
175 | 176 | emit maxChanged(max); |
|
176 | 177 | } |
|
177 | 178 | |
|
178 | 179 | if (changed) { |
|
179 | 180 | d->emitRange(); |
|
180 | 181 | emit rangeChanged(d->m_min,d->m_max); |
|
181 | 182 | } |
|
182 | 183 | } |
|
183 | 184 | |
|
184 | 185 | /*! |
|
185 | 186 | Sets \a count for ticks on the axis. |
|
186 | 187 | */ |
|
187 | 188 | void QValuesAxis::setTicksCount(int count) |
|
188 | 189 | { |
|
189 | 190 | Q_D(QValuesAxis); |
|
190 | 191 | if (d->m_ticksCount != count) { |
|
191 | 192 | d->m_ticksCount = count; |
|
192 | 193 | emit d->changed(d->m_min, d->m_max, d->m_ticksCount, d->m_niceNumbers); |
|
193 | 194 | } |
|
194 | 195 | } |
|
195 | 196 | |
|
196 | 197 | /*! |
|
197 | 198 | \fn int QValuesAxis::ticksCount() const |
|
198 | 199 | Return number of ticks on the axis |
|
199 | 200 | */ |
|
200 | 201 | int QValuesAxis::ticksCount() const |
|
201 | 202 | { |
|
202 | 203 | Q_D(const QValuesAxis); |
|
203 | 204 | return d->m_ticksCount; |
|
204 | 205 | } |
|
205 | 206 | |
|
206 | 207 | void QValuesAxis::setNiceNumbersEnabled(bool enable) |
|
207 | 208 | { |
|
208 | 209 | Q_D(QValuesAxis); |
|
209 | 210 | if (d->m_niceNumbers != enable){ |
|
210 | 211 | d->m_niceNumbers = enable; |
|
211 | 212 | emit d->changed(d->m_min, d->m_max, d->m_ticksCount, d->m_niceNumbers); |
|
212 | 213 | } |
|
213 | 214 | } |
|
214 | 215 | |
|
215 | 216 | bool QValuesAxis::niceNumbersEnabled() const |
|
216 | 217 | { |
|
217 | 218 | Q_D(const QValuesAxis); |
|
218 | 219 | return d->m_niceNumbers; |
|
219 | 220 | } |
|
220 | 221 | |
|
221 | 222 | /*! |
|
222 | 223 | Returns the type of the axis |
|
223 | 224 | */ |
|
224 | 225 | QAbstractAxis::AxisType QValuesAxis::type() const |
|
225 | 226 | { |
|
226 | 227 | return AxisTypeValues; |
|
227 | 228 | } |
|
228 | 229 | |
|
229 | 230 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
230 | 231 | |
|
231 | 232 | QValuesAxisPrivate::QValuesAxisPrivate(QValuesAxis* q): |
|
232 | 233 | QAbstractAxisPrivate(q), |
|
233 | 234 | m_niceNumbers(false) |
|
234 | 235 | { |
|
235 | 236 | |
|
236 | 237 | } |
|
237 | 238 | |
|
238 | 239 | QValuesAxisPrivate::~QValuesAxisPrivate() |
|
239 | 240 | { |
|
240 | 241 | |
|
241 | 242 | } |
|
242 | 243 | |
|
243 | 244 | void QValuesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count) |
|
244 | 245 | { |
|
245 | 246 | Q_Q(QValuesAxis); |
|
246 | 247 | q->setRange(min,max); |
|
247 | 248 | q->setTicksCount(count); |
|
248 | 249 | } |
|
249 | 250 | |
|
250 | 251 | |
|
251 | 252 | void QValuesAxisPrivate::setMin(const QVariant &min) |
|
252 | 253 | { |
|
253 | 254 | Q_Q(QValuesAxis); |
|
254 | 255 | bool ok; |
|
255 | 256 | qreal value = min.toReal(&ok); |
|
256 | 257 | if(ok) q->setMin(value); |
|
257 | 258 | } |
|
258 | 259 | |
|
259 | 260 | void QValuesAxisPrivate::setMax(const QVariant &max) |
|
260 | 261 | { |
|
262 | ||
|
261 | 263 | Q_Q(QValuesAxis); |
|
262 | 264 | bool ok; |
|
263 | 265 | qreal value = max.toReal(&ok); |
|
264 | 266 | if(ok) q->setMax(value); |
|
265 | 267 | } |
|
266 | 268 | |
|
267 | 269 | void QValuesAxisPrivate::setRange(const QVariant &min, const QVariant &max) |
|
268 | 270 | { |
|
269 | 271 | Q_Q(QValuesAxis); |
|
270 | 272 | bool ok1; |
|
271 | 273 | bool ok2; |
|
272 | 274 | qreal value1 = min.toReal(&ok1); |
|
273 | 275 | qreal value2 = max.toReal(&ok2); |
|
274 | 276 | if(ok1&&ok2) q->setRange(value1,value2); |
|
275 | 277 | } |
|
276 | 278 | |
|
277 | 279 | int QValuesAxisPrivate::ticksCount() const |
|
278 | 280 | { |
|
279 | 281 | return m_ticksCount; |
|
280 | 282 | } |
|
281 | 283 | |
|
282 | 284 | ChartAxis* QValuesAxisPrivate::createGraphics(ChartPresenter* presenter) |
|
283 | 285 | { |
|
284 | 286 | Q_Q(QValuesAxis); |
|
285 | 287 | if(m_orientation == Qt::Vertical){ |
|
286 | 288 | return new ChartValuesAxisY(q,presenter); |
|
287 | 289 | }else{ |
|
288 | 290 | return new ChartValuesAxisX(q,presenter); |
|
289 | 291 | } |
|
290 | 292 | |
|
291 | 293 | } |
|
292 | 294 | |
|
293 | 295 | void QValuesAxisPrivate::emitRange() |
|
294 | 296 | { |
|
295 | Q_Q(QValuesAxis); | |
|
296 | if(!q->signalsBlocked()) { | |
|
297 | emit changed(m_min, m_max, m_ticksCount, m_niceNumbers); | |
|
298 | } | |
|
297 | emit changed(m_min, m_max, m_ticksCount, m_niceNumbers); | |
|
299 | 298 | } |
|
300 | 299 | |
|
301 | 300 | #include "moc_qvaluesaxis.cpp" |
|
302 | 301 | #include "moc_qvaluesaxis_p.cpp" |
|
303 | 302 | |
|
304 | 303 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,464 +1,465 | |||
|
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 "qchart.h" |
|
23 | 23 | #include "qvaluesaxis.h" |
|
24 | 24 | #include "qbarcategoriesaxis.h" |
|
25 | 25 | #include "qvaluesaxis_p.h" |
|
26 | 26 | #include "qabstractseries_p.h" |
|
27 | 27 | #include "qabstractbarseries.h" |
|
28 | 28 | #include "qstackedbarseries.h" |
|
29 | 29 | #include "qpercentbarseries.h" |
|
30 | 30 | #include "qpieseries.h" |
|
31 | 31 | |
|
32 | 32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
33 | 33 | |
|
34 | 34 | ChartDataSet::ChartDataSet(QChart *parent):QObject(parent), |
|
35 | 35 | m_domainIndex(0) |
|
36 | 36 | { |
|
37 | 37 | |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | ChartDataSet::~ChartDataSet() |
|
41 | 41 | { |
|
42 | 42 | removeAllSeries(); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | void ChartDataSet::addSeries(QAbstractSeries* series) |
|
46 | 46 | { |
|
47 | 47 | Domain* domain = m_seriesDomainMap.value(series); |
|
48 | 48 | |
|
49 | 49 | if(domain) { |
|
50 | 50 | qWarning() << "Can not add series. Series already on the chart"; |
|
51 | 51 | return; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | series->setParent(this); // take ownership |
|
55 | 55 | |
|
56 | 56 | domain = new Domain(series); |
|
57 | 57 | |
|
58 | 58 | m_seriesDomainMap.insert(series,domain); |
|
59 | 59 | |
|
60 | 60 | series->d_ptr->scaleDomain(*domain); |
|
61 | 61 | |
|
62 | 62 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); |
|
63 | 63 | |
|
64 | 64 | int key=0; |
|
65 | 65 | while (i.hasNext()) { |
|
66 | 66 | i.next(); |
|
67 | 67 | if(i.key()!=key) { |
|
68 | 68 | break; |
|
69 | 69 | } |
|
70 | 70 | key++; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | m_indexSeriesMap.insert(key,series); |
|
74 | 74 | |
|
75 | 75 | series->d_ptr->m_chart = qobject_cast<QChart*>(parent()); |
|
76 | 76 | series->d_ptr->m_dataset = this; |
|
77 | 77 | |
|
78 | 78 | emit seriesAdded(series,domain); |
|
79 | 79 | |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | void ChartDataSet::createDefaultAxes() |
|
83 | 83 | { |
|
84 | 84 | |
|
85 | 85 | if(m_seriesDomainMap.isEmpty()) return; |
|
86 | 86 | |
|
87 | 87 | QAbstractAxis::AxisTypes typeX(0); |
|
88 | 88 | QAbstractAxis::AxisTypes typeY(0); |
|
89 | 89 | |
|
90 | 90 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
91 | 91 | while (i.hasNext()) { |
|
92 | 92 | i.next(); |
|
93 | 93 | removeAxes(i.key()); |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | i.toFront(); |
|
97 | 97 | |
|
98 | 98 | while (i.hasNext()) { |
|
99 | 99 | i.next(); |
|
100 | 100 | QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); |
|
101 | 101 | QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); |
|
102 | 102 | if(axisX) typeX&=axisX->type(); |
|
103 | 103 | else typeX|=i.key()->d_ptr->defaultAxisXType(); |
|
104 | 104 | if(axisY) typeY&=axisY->type(); |
|
105 | 105 | else typeY|=i.key()->d_ptr->defaultAxisYType(); |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | |
|
109 | 109 | if(typeX.testFlag(QAbstractAxis::AxisTypeValues) && typeX.testFlag(QAbstractAxis::AxisTypeCategories)) |
|
110 | 110 | { |
|
111 | 111 | i.toFront(); |
|
112 | 112 | while (i.hasNext()) { |
|
113 | 113 | i.next(); |
|
114 | 114 | QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisXType()); |
|
115 | 115 | if(!axis) continue; |
|
116 | 116 | i.key()->d_ptr->initializeAxisX(axis); |
|
117 | 117 | addAxisX(axis,i.key()); |
|
118 | 118 | emit axisAdded(axis,i.value()); |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){ |
|
122 | 122 | QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeX))); |
|
123 | 123 | i.toFront(); |
|
124 | 124 | while (i.hasNext()) { |
|
125 | 125 | i.next(); |
|
126 | 126 | i.key()->d_ptr->initializeAxisX(axis); |
|
127 | 127 | addAxisX(axis,i.key()); |
|
128 | 128 | } |
|
129 | 129 | emit axisAdded(axis,i.value()); |
|
130 | 130 | |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | if(typeY.testFlag(QAbstractAxis::AxisTypeValues) && typeY.testFlag(QAbstractAxis::AxisTypeCategories)) |
|
134 | 134 | { |
|
135 | 135 | i.toFront(); |
|
136 | 136 | while (i.hasNext()) { |
|
137 | 137 | i.next(); |
|
138 | 138 | QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisYType()); |
|
139 | 139 | i.key()->d_ptr->initializeAxisY(axis); |
|
140 | 140 | addAxisY(axis,i.key()); |
|
141 | 141 | emit axisAdded(axis,i.value()); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){ |
|
145 | 145 | QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeY))); |
|
146 | 146 | i.toFront(); |
|
147 | 147 | while (i.hasNext()) { |
|
148 | 148 | i.next(); |
|
149 | 149 | i.key()->d_ptr->initializeAxisY(axis); |
|
150 | 150 | addAxisY(axis,i.key()); |
|
151 | 151 | } |
|
152 | 152 | emit axisAdded(axis,i.value()); |
|
153 | 153 | |
|
154 | 154 | } |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | |
|
158 | 158 | QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type) |
|
159 | 159 | { |
|
160 | 160 | QAbstractAxis* axis =0; |
|
161 | 161 | |
|
162 | 162 | switch(type) { |
|
163 | 163 | case QAbstractAxis::AxisTypeValues: |
|
164 | 164 | axis = new QValuesAxis(this); |
|
165 | 165 | break; |
|
166 | 166 | case QAbstractAxis::AxisTypeCategories: |
|
167 | 167 | axis = new QBarCategoriesAxis(this); |
|
168 | 168 | break; |
|
169 | 169 | default: |
|
170 | 170 | axis = 0; |
|
171 | 171 | break; |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | return axis; |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | void ChartDataSet::addAxisX(QAbstractAxis* axis,QAbstractSeries* series) { |
|
178 | 178 | Domain* domain = m_seriesDomainMap.value(series); |
|
179 | 179 | QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); |
|
180 | 180 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
181 | 181 | axis->d_ptr->m_orientation=Qt::Horizontal; |
|
182 | 182 | m_seriesAxisXMap.insert(series,axis); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | void ChartDataSet::addAxisY(QAbstractAxis* axis,QAbstractSeries* series) { |
|
186 | 186 | Domain* domain = m_seriesDomainMap.value(series); |
|
187 | 187 | QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); |
|
188 | 188 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
189 | 189 | axis->d_ptr->m_orientation=Qt::Vertical; |
|
190 | 190 | m_seriesAxisYMap.insert(series,axis); |
|
191 | 191 | } |
|
192 | 192 | |
|
193 | 193 | void ChartDataSet::removeSeries(QAbstractSeries* series) |
|
194 | 194 | { |
|
195 | 195 | Domain* domain = m_seriesDomainMap.take(series); |
|
196 | 196 | |
|
197 | 197 | if(!domain) { |
|
198 | 198 | qWarning()<<"Can not remove series. Series not found on the chart."; |
|
199 | 199 | } |
|
200 | 200 | |
|
201 | 201 | emit seriesRemoved(series); |
|
202 | 202 | |
|
203 | 203 | delete domain; |
|
204 | 204 | domain = 0; |
|
205 | 205 | |
|
206 | 206 | int key = seriesIndex(series); |
|
207 | 207 | Q_ASSERT(key!=-1); |
|
208 | 208 | |
|
209 | 209 | m_indexSeriesMap.remove(key); |
|
210 | 210 | |
|
211 | 211 | series->setParent(0); |
|
212 | 212 | series->d_ptr->m_chart = 0; |
|
213 | 213 | series->d_ptr->m_dataset = 0; |
|
214 | 214 | |
|
215 | 215 | removeAxes(series); |
|
216 | 216 | } |
|
217 | 217 | |
|
218 | 218 | void ChartDataSet::removeAxes(QAbstractSeries* series) |
|
219 | 219 | { |
|
220 | 220 | QAbstractAxis* axisX = m_seriesAxisXMap.take(series); |
|
221 | 221 | |
|
222 | 222 | if(axisX) { |
|
223 | 223 | QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values(); |
|
224 | 224 | int x = axesX.indexOf(axisX); |
|
225 | 225 | |
|
226 | 226 | if(x==-1) { |
|
227 | 227 | emit axisRemoved(axisX); |
|
228 | 228 | axisX->deleteLater(); |
|
229 | 229 | } |
|
230 | 230 | } |
|
231 | 231 | |
|
232 | 232 | QAbstractAxis* axisY = m_seriesAxisYMap.take(series); |
|
233 | 233 | |
|
234 | 234 | if(axisY) { |
|
235 | 235 | QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values(); |
|
236 | 236 | |
|
237 | 237 | int y = axesY.indexOf(axisY); |
|
238 | 238 | |
|
239 | 239 | if(y==-1) { |
|
240 | 240 | emit axisRemoved(axisY); |
|
241 | 241 | axisY->deleteLater(); |
|
242 | 242 | } |
|
243 | 243 | } |
|
244 | 244 | } |
|
245 | 245 | |
|
246 | 246 | void ChartDataSet::removeAllSeries() |
|
247 | 247 | { |
|
248 | 248 | QList<QAbstractSeries*> series = m_seriesDomainMap.keys(); |
|
249 | 249 | foreach(QAbstractSeries *s , series) { |
|
250 | 250 | removeSeries(s); |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | Q_ASSERT(m_seriesAxisXMap.count()==0); |
|
254 | 254 | Q_ASSERT(m_seriesAxisXMap.count()==0); |
|
255 | 255 | Q_ASSERT(m_seriesDomainMap.count()==0); |
|
256 | 256 | |
|
257 | 257 | qDeleteAll(series); |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) |
|
261 | 261 | { |
|
262 | 262 | //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates |
|
263 | 263 | |
|
264 | ||
|
264 | 265 | blockAxisSignals(true); |
|
265 | 266 | |
|
266 | 267 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
267 | 268 | |
|
268 | 269 | while (i.hasNext()) { |
|
269 | 270 | i.next(); |
|
270 | 271 | i.value()->zoomIn(rect,size); |
|
271 | 272 | } |
|
272 | 273 | |
|
273 | 274 | blockAxisSignals(false); |
|
274 | 275 | |
|
275 | 276 | } |
|
276 | 277 | |
|
277 | 278 | void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) |
|
278 | 279 | { |
|
279 | 280 | //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates |
|
280 | 281 | |
|
281 | 282 | blockAxisSignals(true); |
|
282 | 283 | |
|
283 | 284 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
284 | 285 | |
|
285 | 286 | while (i.hasNext()) { |
|
286 | 287 | i.next(); |
|
287 | 288 | i.value()->zoomOut(rect,size); |
|
288 | 289 | } |
|
289 | 290 | |
|
290 | 291 | blockAxisSignals(false); |
|
291 | 292 | } |
|
292 | 293 | |
|
293 | 294 | void ChartDataSet::blockAxisSignals(bool enabled) |
|
294 | 295 | { |
|
295 | 296 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
296 | ||
|
297 | Q_UNUSED(enabled); | |
|
297 | 298 | while (i.hasNext()) { |
|
298 | 299 | i.next(); |
|
299 | 300 | QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); |
|
300 | 301 | QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); |
|
301 |
if(axisX) axisX->blockSignals(e |
|
|
302 |
if(axisY) axisY->blockSignals(e |
|
|
302 | if(axisX) axisX->d_ptr->blockSignals(true); | |
|
303 | if(axisY) axisY->d_ptr->blockSignals(true); | |
|
303 | 304 | } |
|
304 | 305 | } |
|
305 | 306 | |
|
306 | 307 | int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) |
|
307 | 308 | { |
|
308 | 309 | int count=0; |
|
309 | 310 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
310 | 311 | while (i.hasNext()) { |
|
311 | 312 | i.next(); |
|
312 | 313 | if(i.key()->type()==type) count++; |
|
313 | 314 | } |
|
314 | 315 | return count; |
|
315 | 316 | } |
|
316 | 317 | |
|
317 | 318 | int ChartDataSet::seriesIndex(QAbstractSeries *series) |
|
318 | 319 | { |
|
319 | 320 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); |
|
320 | 321 | while (i.hasNext()) { |
|
321 | 322 | i.next(); |
|
322 | 323 | if (i.value() == series) |
|
323 | 324 | return i.key(); |
|
324 | 325 | } |
|
325 | 326 | return -1; |
|
326 | 327 | } |
|
327 | 328 | |
|
328 | 329 | QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const |
|
329 | 330 | { |
|
330 | 331 | if(series == 0) { |
|
331 | 332 | |
|
332 | 333 | QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisXMap); |
|
333 | 334 | |
|
334 | 335 | while (i.hasNext()) { |
|
335 | 336 | i.next(); |
|
336 | 337 | if(i.value()->isVisible()) return i.value(); |
|
337 | 338 | } |
|
338 | 339 | return 0; |
|
339 | 340 | } |
|
340 | 341 | return m_seriesAxisXMap.value(series); |
|
341 | 342 | } |
|
342 | 343 | |
|
343 | 344 | QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const |
|
344 | 345 | { |
|
345 | 346 | if(series == 0) { |
|
346 | 347 | QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisYMap); |
|
347 | 348 | |
|
348 | 349 | while (i.hasNext()) { |
|
349 | 350 | i.next(); |
|
350 | 351 | if(i.value()->isVisible()) return i.value(); |
|
351 | 352 | } |
|
352 | 353 | return 0; |
|
353 | 354 | } |
|
354 | 355 | return m_seriesAxisYMap.value(series); |
|
355 | 356 | } |
|
356 | 357 | |
|
357 | 358 | void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis) |
|
358 | 359 | { |
|
359 | 360 | Q_ASSERT(axis); |
|
360 | 361 | Domain* domain = m_seriesDomainMap.value(series); |
|
361 | 362 | |
|
362 | 363 | if(!domain) { |
|
363 | 364 | qWarning() << "Series not found on the chart."; |
|
364 | 365 | return; |
|
365 | 366 | } |
|
366 | 367 | |
|
367 | 368 | if(axis->d_ptr->m_orientation==Qt::Vertical) { |
|
368 | 369 | qWarning()<<"Axis already defined as axis Y"; |
|
369 | 370 | return; |
|
370 | 371 | } |
|
371 | 372 | |
|
372 | 373 | QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series); |
|
373 | 374 | QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values(); |
|
374 | 375 | |
|
375 | 376 | if(oldAxis) { |
|
376 | 377 | |
|
377 | 378 | int x = axesX.indexOf(oldAxis); |
|
378 | 379 | if(x==-1) { |
|
379 | 380 | emit axisRemoved(oldAxis); |
|
380 | 381 | oldAxis->deleteLater(); |
|
381 | 382 | } |
|
382 | 383 | } |
|
383 | 384 | |
|
384 | 385 | QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); |
|
385 | 386 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
386 | 387 | |
|
387 | 388 | int x = axesX.indexOf(axis); |
|
388 | 389 | if(x==-1) { |
|
389 | 390 | axis->d_ptr->m_orientation=Qt::Horizontal; |
|
390 | 391 | emit axisAdded(axis,domain); |
|
391 | 392 | } |
|
392 | 393 | |
|
393 | 394 | m_seriesAxisXMap.insert(series,axis); |
|
394 | 395 | axis->d_ptr->emitRange(); |
|
395 | 396 | } |
|
396 | 397 | |
|
397 | 398 | void ChartDataSet::setAxisY(QAbstractSeries *series, QAbstractAxis *axis) |
|
398 | 399 | { |
|
399 | 400 | Q_ASSERT(axis); |
|
400 | 401 | Domain* domain = m_seriesDomainMap.value(series); |
|
401 | 402 | |
|
402 | 403 | if(!domain) { |
|
403 | 404 | qWarning() << "Series not found on the chart."; |
|
404 | 405 | return; |
|
405 | 406 | } |
|
406 | 407 | |
|
407 | 408 | if(axis->d_ptr->m_orientation==Qt::Horizontal) { |
|
408 | 409 | qWarning()<<"Axis already defined as axis X"; |
|
409 | 410 | return; |
|
410 | 411 | } |
|
411 | 412 | |
|
412 | 413 | QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series); |
|
413 | 414 | QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values(); |
|
414 | 415 | |
|
415 | 416 | if(oldAxis) { |
|
416 | 417 | int y = axesY.indexOf(oldAxis); |
|
417 | 418 | if(y==-1) { |
|
418 | 419 | emit axisRemoved(oldAxis); |
|
419 | 420 | oldAxis->deleteLater(); |
|
420 | 421 | } |
|
421 | 422 | } |
|
422 | 423 | |
|
423 | 424 | QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); |
|
424 | 425 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); |
|
425 | 426 | |
|
426 | 427 | int y = axesY.indexOf(axis); |
|
427 | 428 | if(y==-1) { |
|
428 | 429 | axis->d_ptr->m_orientation=Qt::Vertical; |
|
429 | 430 | emit axisAdded(axis,domain); |
|
430 | 431 | } |
|
431 | 432 | |
|
432 | 433 | m_seriesAxisYMap.insert(series,axis); |
|
433 | 434 | axis->d_ptr->emitRange(); |
|
434 | 435 | } |
|
435 | 436 | |
|
436 | 437 | Domain* ChartDataSet::domain(QAbstractSeries *series) const |
|
437 | 438 | { |
|
438 | 439 | return m_seriesDomainMap.value(series); |
|
439 | 440 | } |
|
440 | 441 | |
|
441 | 442 | void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size) |
|
442 | 443 | { |
|
443 | 444 | blockAxisSignals(true); |
|
444 | 445 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); |
|
445 | 446 | while (i.hasNext()) { |
|
446 | 447 | i.next(); |
|
447 | 448 | i.value()->move(dx,dy,size); |
|
448 | 449 | } |
|
449 | 450 | blockAxisSignals(false); |
|
450 | 451 | } |
|
451 | 452 | |
|
452 | 453 | QList<QAbstractSeries*> ChartDataSet::series() const |
|
453 | 454 | { |
|
454 | 455 | return m_seriesAxisXMap.keys(); |
|
455 | 456 | } |
|
456 | 457 | |
|
457 | 458 | void ChartDataSet::updateSeries(QAbstractSeries *series) |
|
458 | 459 | { |
|
459 | 460 | emit seriesUpdated(series); |
|
460 | 461 | } |
|
461 | 462 | |
|
462 | 463 | #include "moc_chartdataset_p.cpp" |
|
463 | 464 | |
|
464 | 465 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,178 +1,183 | |||
|
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 <QtTest/QtTest> |
|
22 | 22 | #include <qchartview.h> |
|
23 | 23 | #include <qlineseries.h> |
|
24 | 24 | #include <cmath> |
|
25 | 25 | #include <tst_definitions.h> |
|
26 | #include <qvaluesaxis.h> | |
|
26 | 27 | |
|
27 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | 29 | |
|
29 | 30 | |
|
30 | 31 | Q_DECLARE_METATYPE(QChart*) |
|
31 | 32 | Q_DECLARE_METATYPE(QChartView::RubberBands) |
|
32 | 33 | Q_DECLARE_METATYPE(Qt::Key) |
|
33 | 34 | |
|
34 | 35 | class tst_QChartView : public QObject |
|
35 | 36 | { |
|
36 | 37 | Q_OBJECT |
|
37 | 38 | |
|
38 | 39 | public Q_SLOTS: |
|
39 | 40 | void initTestCase(); |
|
40 | 41 | void cleanupTestCase(); |
|
41 | 42 | void init(); |
|
42 | 43 | void cleanup(); |
|
43 | 44 | |
|
44 | 45 | private Q_SLOTS: |
|
45 | 46 | void qchartview_data(); |
|
46 | 47 | void qchartview(); |
|
47 | 48 | void chart_data(); |
|
48 | 49 | void chart(); |
|
49 | 50 | void rubberBand_data(); |
|
50 | 51 | void rubberBand(); |
|
51 | 52 | |
|
52 | 53 | private: |
|
53 | 54 | QChartView* m_view; |
|
54 | 55 | }; |
|
55 | 56 | |
|
56 | 57 | void tst_QChartView::initTestCase() |
|
57 | 58 | { |
|
58 | 59 | //test tracks mouse, give a while to user to relese it |
|
59 | 60 | QTest::qWait(1000); |
|
60 | 61 | } |
|
61 | 62 | |
|
62 | 63 | void tst_QChartView::cleanupTestCase() |
|
63 | 64 | { |
|
64 | 65 | } |
|
65 | 66 | |
|
66 | 67 | void tst_QChartView::init() |
|
67 | 68 | { |
|
68 | 69 | m_view = new QChartView(new QChart()); |
|
69 | 70 | m_view->chart()->legend()->setVisible(false); |
|
70 | 71 | } |
|
71 | 72 | |
|
72 | 73 | void tst_QChartView::cleanup() |
|
73 | 74 | { |
|
74 | 75 | delete m_view; |
|
75 | 76 | m_view =0; |
|
76 | 77 | } |
|
77 | 78 | |
|
78 | 79 | void tst_QChartView::qchartview_data() |
|
79 | 80 | { |
|
80 | 81 | |
|
81 | 82 | } |
|
82 | 83 | |
|
83 | 84 | void tst_QChartView::qchartview() |
|
84 | 85 | { |
|
85 | 86 | QVERIFY(m_view->chart()); |
|
86 | 87 | QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand); |
|
87 | 88 | m_view->show(); |
|
88 | 89 | QTest::qWaitForWindowShown(m_view); |
|
89 | 90 | |
|
90 | 91 | delete(new QChartView()); |
|
91 | 92 | |
|
92 | 93 | QChartView view; |
|
93 | 94 | QVERIFY(view.chart()); |
|
94 | 95 | |
|
95 | 96 | } |
|
96 | 97 | |
|
97 | 98 | void tst_QChartView::chart_data() |
|
98 | 99 | { |
|
99 | 100 | |
|
100 | 101 | QTest::addColumn<QChart*>("chart"); |
|
101 | 102 | QTest::newRow("qchart") << new QChart(); |
|
102 | 103 | } |
|
103 | 104 | |
|
104 | 105 | void tst_QChartView::chart() |
|
105 | 106 | { |
|
106 | 107 | QFETCH(QChart*, chart); |
|
107 | 108 | QChartView* view = new QChartView(chart); |
|
108 | 109 | QCOMPARE(view->chart(), chart); |
|
109 | 110 | delete view; |
|
110 | 111 | } |
|
111 | 112 | |
|
112 | 113 | void tst_QChartView::rubberBand_data() |
|
113 | 114 | { |
|
114 | 115 | QTest::addColumn<QChartView::RubberBands>("rubberBand"); |
|
115 | 116 | QTest::addColumn<int>("Xcount"); |
|
116 | 117 | QTest::addColumn<int>("Ycount"); |
|
117 | 118 | |
|
118 | 119 | QTest::addColumn<int>("minX"); |
|
119 | 120 | QTest::addColumn<int>("maxX"); |
|
120 | 121 | QTest::addColumn<int>("minY"); |
|
121 | 122 | QTest::addColumn<int>("maxY"); |
|
122 | 123 | |
|
123 | 124 | QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << 20 << 180 << 0<< 200; |
|
124 | 125 | QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 0 << 200 << 20<< 180; |
|
125 | 126 | QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<20 << 180 << 20<< 180; |
|
126 | 127 | } |
|
127 | 128 | |
|
128 | 129 | void tst_QChartView::rubberBand() |
|
129 | 130 | { |
|
130 | 131 | QFETCH(QChartView::RubberBands, rubberBand); |
|
131 | 132 | QFETCH(int, Xcount); |
|
132 | 133 | QFETCH(int, Ycount); |
|
133 | 134 | QFETCH(int, minX); |
|
134 | 135 | QFETCH(int, maxX); |
|
135 | 136 | QFETCH(int, minY); |
|
136 | 137 | QFETCH(int, maxY); |
|
137 | 138 | |
|
138 | 139 | m_view->setRubberBand(rubberBand); |
|
139 | 140 | QRectF padding = m_view->chart()->margins(); |
|
140 | 141 | QCOMPARE(m_view->rubberBand(), rubberBand); |
|
141 | 142 | |
|
142 | 143 | QLineSeries* line = new QLineSeries(); |
|
143 | 144 | *line << QPointF(0, 0) << QPointF(200, 200); |
|
144 | 145 | |
|
145 | 146 | m_view->chart()->addSeries(line); |
|
147 | m_view->chart()->createDefaultAxes(); | |
|
146 | 148 | m_view->resize(200 + padding.left() + padding.right(), 200 + padding.top()+ padding.bottom()); |
|
147 | 149 | m_view->show(); |
|
148 | 150 | |
|
149 | 151 | //this is hack since view does not get events otherwise |
|
150 | 152 | m_view->setMouseTracking(true); |
|
151 | 153 | |
|
152 | 154 | QAbstractAxis* axisY = m_view->chart()->axisY(); |
|
153 | 155 | QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal))); |
|
154 | 156 | QAbstractAxis* axisX = m_view->chart()->axisX(); |
|
155 | 157 | QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal))); |
|
156 | 158 | |
|
157 | 159 | QTest::qWaitForWindowShown(m_view); |
|
158 | 160 | QTest::mouseMove(m_view->viewport(), QPoint(minX, minY) + padding.topLeft().toPoint()); |
|
159 | 161 | QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(minX, minY) + padding.topLeft().toPoint()); |
|
160 | 162 | QTest::mouseMove(m_view->viewport(), QPoint(maxX, maxY) + padding.topLeft().toPoint()); |
|
161 | 163 | QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(maxX, maxY)+ padding.topLeft().toPoint()); |
|
162 | 164 | |
|
163 | 165 | TRY_COMPARE(spy0.count(), Xcount); |
|
164 | 166 | TRY_COMPARE(spy1.count(), Ycount); |
|
165 | 167 | |
|
166 | 168 | //this is hack since view does not get events otherwise |
|
167 | 169 | m_view->setMouseTracking(false); |
|
168 | 170 | |
|
169 | //TODO: QVERIFY(axisX->min() - minX < 1); | |
|
170 | //TODO: QVERIFY(axisX->max() - maxX < 1); | |
|
171 | //TODO: QVERIFY(axisY->min() - minY < 1); | |
|
172 |
|
|
|
173 | qFatal("implement TODO"); | |
|
171 | QValuesAxis* vaxisX = qobject_cast<QValuesAxis*>(axisX); | |
|
172 | QValuesAxis* vaxisY = qobject_cast<QValuesAxis*>(axisY); | |
|
173 | ||
|
174 | QVERIFY(vaxisX->min() - minX < 1); | |
|
175 | QVERIFY(vaxisX->max() - maxX < 1); | |
|
176 | QVERIFY(vaxisY->min() - minY < 1); | |
|
177 | QVERIFY(vaxisY->max() - maxY < 1); | |
|
178 | ||
|
174 | 179 | } |
|
175 | 180 | |
|
176 | 181 | QTEST_MAIN(tst_QChartView) |
|
177 | 182 | #include "tst_qchartview.moc" |
|
178 | 183 |
General Comments 0
You need to be logged in to leave comments.
Login now