@@ -1,397 +1,408 | |||
|
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 | |
|
40 | 40 | /*! |
|
41 | 41 | \qmlclass ValueAxis QValueAxis |
|
42 | 42 | \brief The ValueAxis element is used for manipulating chart's axes |
|
43 | 43 | |
|
44 | 44 | ValueAxis can be setup to show axis line with tick marks, grid lines and shades. |
|
45 | 45 | Values of axis are drawn to position of ticks |
|
46 | 46 | |
|
47 | 47 | To access Axes you can use ChartView API. For example: |
|
48 | 48 | \code |
|
49 | 49 | ChartView { |
|
50 | 50 | ValueAxis { |
|
51 | 51 | id: xAxis |
|
52 | 52 | min: 0 |
|
53 | 53 | max: 10 |
|
54 | 54 | } |
|
55 | 55 | // Add a few series... |
|
56 | 56 | } |
|
57 | 57 | \endcode |
|
58 | 58 | */ |
|
59 | 59 | |
|
60 | 60 | /*! |
|
61 | 61 | \property QValueAxis::min |
|
62 | 62 | Defines the minimum value on the axis. |
|
63 | 63 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. |
|
64 | 64 | */ |
|
65 | 65 | /*! |
|
66 | 66 | \qmlproperty real ValueAxis::min |
|
67 | 67 | Defines the minimum value on the axis. |
|
68 | 68 | When setting this property the max is adjusted if necessary, to ensure that the range remains valid. |
|
69 | 69 | */ |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | \property QValueAxis::max |
|
73 | 73 | Defines the maximum value on the axis. |
|
74 | 74 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. |
|
75 | 75 | */ |
|
76 | 76 | /*! |
|
77 | 77 | \qmlproperty real ValueAxis::max |
|
78 | 78 | Defines the maximum value on the axis. |
|
79 | 79 | When setting this property the min is adjusted if necessary, to ensure that the range remains valid. |
|
80 | 80 | */ |
|
81 | 81 | |
|
82 | 82 | /*! |
|
83 | \property QValueAxis::labelFormat | |
|
84 | Defines the label format for the axis. | |
|
85 | See QString::sprintf() for the details. | |
|
86 | */ | |
|
87 | /*! | |
|
88 | \qmlproperty real ValueAxis::labelFormat | |
|
89 | Defines the label format for the axis. | |
|
90 | See QString::sprintf() for the details. | |
|
91 | */ | |
|
92 | ||
|
93 | /*! | |
|
83 | 94 | \fn void QValueAxis::minChanged(qreal min) |
|
84 | 95 | Axis emits signal when \a min of axis has changed. |
|
85 | 96 | */ |
|
86 | 97 | /*! |
|
87 | 98 | \qmlsignal ValueAxis::onMinChanged(real min) |
|
88 | 99 | Axis emits signal when \a min of axis has changed. |
|
89 | 100 | */ |
|
90 | 101 | |
|
91 | 102 | /*! |
|
92 | 103 | \fn void QValueAxis::maxChanged(qreal max) |
|
93 | 104 | Axis emits signal when \a max of axis has changed. |
|
94 | 105 | */ |
|
95 | 106 | /*! |
|
96 | 107 | \qmlsignal ValueAxis::onMaxChanged(real max) |
|
97 | 108 | Axis emits signal when \a max of axis has changed. |
|
98 | 109 | */ |
|
99 | 110 | |
|
100 | 111 | /*! |
|
101 | 112 | \fn void QValueAxis::rangeChanged(qreal min, qreal max) |
|
102 | 113 | Axis emits signal when \a min or \a max of axis has changed. |
|
103 | 114 | */ |
|
104 | 115 | |
|
105 | 116 | /*! |
|
106 | 117 | \property QValueAxis::tickCount |
|
107 | 118 | The number of tick marks for the axis. |
|
108 | 119 | */ |
|
109 | 120 | |
|
110 | 121 | /*! |
|
111 | 122 | \qmlproperty int ValueAxis::tickCount |
|
112 | 123 | The number of tick marks for the axis. |
|
113 | 124 | */ |
|
114 | 125 | |
|
115 | 126 | /*! |
|
116 | 127 | \property QValueAxis::niceNumbersEnabled |
|
117 | 128 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
118 | 129 | */ |
|
119 | 130 | |
|
120 | 131 | /*! |
|
121 | 132 | \qmlproperty bool ValueAxis::niceNumbersEnabled |
|
122 | 133 | Whether the nice numbers algorithm is enabled or not for the axis. |
|
123 | 134 | */ |
|
124 | 135 | |
|
125 | 136 | /*! |
|
126 | 137 | Constructs an axis object which is a child of \a parent. |
|
127 | 138 | */ |
|
128 | 139 | QValueAxis::QValueAxis(QObject *parent) : |
|
129 | 140 | QAbstractAxis(*new QValueAxisPrivate(this),parent) |
|
130 | 141 | { |
|
131 | 142 | |
|
132 | 143 | } |
|
133 | 144 | |
|
134 | 145 | /*! |
|
135 | 146 | \internal |
|
136 | 147 | */ |
|
137 | 148 | QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent) |
|
138 | 149 | { |
|
139 | 150 | |
|
140 | 151 | } |
|
141 | 152 | |
|
142 | 153 | /*! |
|
143 | 154 | Destroys the object |
|
144 | 155 | */ |
|
145 | 156 | QValueAxis::~QValueAxis() |
|
146 | 157 | { |
|
147 | 158 | Q_D(QValueAxis); |
|
148 | 159 | if(d->m_dataset) { |
|
149 | 160 | d->m_dataset->removeAxis(this); |
|
150 | 161 | } |
|
151 | 162 | } |
|
152 | 163 | |
|
153 | 164 | void QValueAxis::setMin(qreal min) |
|
154 | 165 | { |
|
155 | 166 | Q_D(QValueAxis); |
|
156 | 167 | setRange(min, qMax(d->m_max, min)); |
|
157 | 168 | } |
|
158 | 169 | |
|
159 | 170 | qreal QValueAxis::min() const |
|
160 | 171 | { |
|
161 | 172 | Q_D(const QValueAxis); |
|
162 | 173 | return d->m_min; |
|
163 | 174 | } |
|
164 | 175 | |
|
165 | 176 | void QValueAxis::setMax(qreal max) |
|
166 | 177 | { |
|
167 | 178 | Q_D(QValueAxis); |
|
168 | 179 | setRange(qMin(d->m_min, max), max); |
|
169 | 180 | } |
|
170 | 181 | |
|
171 | 182 | qreal QValueAxis::max() const |
|
172 | 183 | { |
|
173 | 184 | Q_D(const QValueAxis); |
|
174 | 185 | return d->m_max; |
|
175 | 186 | } |
|
176 | 187 | |
|
177 | 188 | /*! |
|
178 | 189 | Sets range from \a min to \a max on the axis. |
|
179 | 190 | If min is greater than max then this function returns without making any changes. |
|
180 | 191 | */ |
|
181 | 192 | void QValueAxis::setRange(qreal min, qreal max) |
|
182 | 193 | { |
|
183 | 194 | Q_D(QValueAxis); |
|
184 | 195 | bool changed = false; |
|
185 | 196 | |
|
186 | 197 | if (min > max) return; |
|
187 | 198 | |
|
188 | 199 | if(d->m_niceNumbers) { |
|
189 | 200 | int ticks = d->m_tickCount; |
|
190 | 201 | d->looseNiceNumbers(min, max, ticks); |
|
191 | 202 | if(ticks!=d->m_tickCount) setTickCount(ticks); |
|
192 | 203 | } |
|
193 | 204 | |
|
194 | 205 | if (!qFuzzyIsNull(d->m_min - min)) { |
|
195 | 206 | d->m_min = min; |
|
196 | 207 | changed = true; |
|
197 | 208 | emit minChanged(min); |
|
198 | 209 | } |
|
199 | 210 | |
|
200 | 211 | if (!qFuzzyIsNull(d->m_max - max)) { |
|
201 | 212 | d->m_max = max; |
|
202 | 213 | changed = true; |
|
203 | 214 | emit maxChanged(max); |
|
204 | 215 | } |
|
205 | 216 | |
|
206 | 217 | if (changed) { |
|
207 | 218 | emit rangeChanged(min,max); |
|
208 | 219 | d->emitUpdated(); |
|
209 | 220 | } |
|
210 | 221 | } |
|
211 | 222 | |
|
212 | 223 | /*! |
|
213 | 224 | Sets \a count for ticks on the axis. |
|
214 | 225 | */ |
|
215 | 226 | void QValueAxis::setTickCount(int count) |
|
216 | 227 | { |
|
217 | 228 | Q_D(QValueAxis); |
|
218 | 229 | if (d->m_tickCount != count && count >=2) { |
|
219 | 230 | d->m_tickCount = count; |
|
220 | 231 | d->emitUpdated(); |
|
221 | 232 | } |
|
222 | 233 | } |
|
223 | 234 | |
|
224 | 235 | /*! |
|
225 | 236 | \fn int QValueAxis::tickCount() const |
|
226 | 237 | Return number of ticks on the axis |
|
227 | 238 | */ |
|
228 | 239 | int QValueAxis::tickCount() const |
|
229 | 240 | { |
|
230 | 241 | Q_D(const QValueAxis); |
|
231 | 242 | return d->m_tickCount; |
|
232 | 243 | } |
|
233 | 244 | |
|
234 | 245 | void QValueAxis::setNiceNumbersEnabled(bool enable) |
|
235 | 246 | { |
|
236 | 247 | Q_D(QValueAxis); |
|
237 | 248 | if (d->m_niceNumbers != enable){ |
|
238 | 249 | d->m_niceNumbers = enable; |
|
239 | 250 | if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) { |
|
240 | 251 | setRange(d->m_min,d->m_max); |
|
241 | 252 | } |
|
242 | 253 | } |
|
243 | 254 | } |
|
244 | 255 | |
|
245 | 256 | bool QValueAxis::niceNumbersEnabled() const |
|
246 | 257 | { |
|
247 | 258 | Q_D(const QValueAxis); |
|
248 | 259 | return d->m_niceNumbers; |
|
249 | 260 | } |
|
250 | 261 | |
|
251 | 262 | void QValueAxis::setLabelFormat(const QString &format) |
|
252 | 263 | { |
|
253 | 264 | Q_D(QValueAxis); |
|
254 | 265 | d->m_format = format; |
|
255 | 266 | } |
|
256 | 267 | |
|
257 | 268 | QString QValueAxis::labelFormat() const |
|
258 | 269 | { |
|
259 | 270 | Q_D(const QValueAxis); |
|
260 | 271 | return d->m_format; |
|
261 | 272 | } |
|
262 | 273 | |
|
263 | 274 | /*! |
|
264 | 275 | Returns the type of the axis |
|
265 | 276 | */ |
|
266 | 277 | QAbstractAxis::AxisType QValueAxis::type() const |
|
267 | 278 | { |
|
268 | 279 | return AxisTypeValue; |
|
269 | 280 | } |
|
270 | 281 | |
|
271 | 282 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
272 | 283 | |
|
273 | 284 | QValueAxisPrivate::QValueAxisPrivate(QValueAxis* q): |
|
274 | 285 | QAbstractAxisPrivate(q), |
|
275 | 286 | m_min(0), |
|
276 | 287 | m_max(0), |
|
277 | 288 | m_tickCount(5), |
|
278 | 289 | m_niceNumbers(false), |
|
279 | 290 | m_format("%g") |
|
280 | 291 | { |
|
281 | 292 | |
|
282 | 293 | } |
|
283 | 294 | |
|
284 | 295 | QValueAxisPrivate::~QValueAxisPrivate() |
|
285 | 296 | { |
|
286 | 297 | |
|
287 | 298 | } |
|
288 | 299 | |
|
289 | 300 | void QValueAxisPrivate::handleDomainUpdated() |
|
290 | 301 | { |
|
291 | 302 | Q_Q(QValueAxis); |
|
292 | 303 | Domain* domain = qobject_cast<Domain*>(sender()); |
|
293 | 304 | Q_ASSERT(domain); |
|
294 | 305 | |
|
295 | 306 | if(orientation()==Qt::Horizontal){ |
|
296 | 307 | q->setRange(domain->minX(),domain->maxX()); |
|
297 | 308 | }else if(orientation()==Qt::Vertical){ |
|
298 | 309 | q->setRange(domain->minY(),domain->maxY()); |
|
299 | 310 | } |
|
300 | 311 | } |
|
301 | 312 | |
|
302 | 313 | |
|
303 | 314 | void QValueAxisPrivate::setMin(const QVariant &min) |
|
304 | 315 | { |
|
305 | 316 | Q_Q(QValueAxis); |
|
306 | 317 | bool ok; |
|
307 | 318 | qreal value = min.toReal(&ok); |
|
308 | 319 | if(ok) q->setMin(value); |
|
309 | 320 | } |
|
310 | 321 | |
|
311 | 322 | void QValueAxisPrivate::setMax(const QVariant &max) |
|
312 | 323 | { |
|
313 | 324 | |
|
314 | 325 | Q_Q(QValueAxis); |
|
315 | 326 | bool ok; |
|
316 | 327 | qreal value = max.toReal(&ok); |
|
317 | 328 | if(ok) q->setMax(value); |
|
318 | 329 | } |
|
319 | 330 | |
|
320 | 331 | void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) |
|
321 | 332 | { |
|
322 | 333 | Q_Q(QValueAxis); |
|
323 | 334 | bool ok1; |
|
324 | 335 | bool ok2; |
|
325 | 336 | qreal value1 = min.toReal(&ok1); |
|
326 | 337 | qreal value2 = max.toReal(&ok2); |
|
327 | 338 | if(ok1&&ok2) q->setRange(value1,value2); |
|
328 | 339 | } |
|
329 | 340 | |
|
330 | 341 | ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter) |
|
331 | 342 | { |
|
332 | 343 | Q_Q(QValueAxis); |
|
333 | 344 | if(m_orientation == Qt::Vertical){ |
|
334 | 345 | return new ChartValueAxisY(q,presenter); |
|
335 | 346 | }else{ |
|
336 | 347 | return new ChartValueAxisX(q,presenter); |
|
337 | 348 | } |
|
338 | 349 | |
|
339 | 350 | } |
|
340 | 351 | |
|
341 | 352 | void QValueAxisPrivate::intializeDomain(Domain* domain) |
|
342 | 353 | { |
|
343 | 354 | Q_Q(QValueAxis); |
|
344 | 355 | if(qFuzzyCompare(m_max,m_min)) { |
|
345 | 356 | if(m_orientation==Qt::Vertical){ |
|
346 | 357 | q->setRange(domain->minY(),domain->maxY()); |
|
347 | 358 | }else{ |
|
348 | 359 | q->setRange(domain->minX(), domain->maxX()); |
|
349 | 360 | } |
|
350 | 361 | } else { |
|
351 | 362 | if(m_orientation==Qt::Vertical){ |
|
352 | 363 | domain->setRangeY(m_min, m_max); |
|
353 | 364 | }else{ |
|
354 | 365 | domain->setRangeX(m_min, m_max); |
|
355 | 366 | } |
|
356 | 367 | } |
|
357 | 368 | } |
|
358 | 369 | |
|
359 | 370 | //algorithm defined by Paul S.Heckbert GraphicalGems I |
|
360 | 371 | |
|
361 | 372 | void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const |
|
362 | 373 | { |
|
363 | 374 | qreal range = niceNumber(max-min,true); //range with ceiling |
|
364 | 375 | qreal step = niceNumber(range/(ticksCount-1),false); |
|
365 | 376 | min = qFloor(min/step); |
|
366 | 377 | max = qCeil(max/step); |
|
367 | 378 | ticksCount = int(max-min) +1; |
|
368 | 379 | min*=step; |
|
369 | 380 | max*=step; |
|
370 | 381 | } |
|
371 | 382 | |
|
372 | 383 | //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n |
|
373 | 384 | |
|
374 | 385 | qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const |
|
375 | 386 | { |
|
376 | 387 | qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x |
|
377 | 388 | qreal q = x/z;//q<10 && q>=1; |
|
378 | 389 | |
|
379 | 390 | if(ceiling) { |
|
380 | 391 | if(q <= 1.0) q=1; |
|
381 | 392 | else if(q <= 2.0) q=2; |
|
382 | 393 | else if(q <= 5.0) q=5; |
|
383 | 394 | else q=10; |
|
384 | 395 | } |
|
385 | 396 | else { |
|
386 | 397 | if(q < 1.5) q=1; |
|
387 | 398 | else if(q < 3.0) q=2; |
|
388 | 399 | else if(q < 7.0) q=5; |
|
389 | 400 | else q=10; |
|
390 | 401 | } |
|
391 | 402 | return q*z; |
|
392 | 403 | } |
|
393 | 404 | |
|
394 | 405 | #include "moc_qvalueaxis.cpp" |
|
395 | 406 | #include "moc_qvalueaxis_p.cpp" |
|
396 | 407 | |
|
397 | 408 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,77 +1,78 | |||
|
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 | #ifndef QVALUESAXIS_H |
|
22 | 22 | #define QVALUESAXIS_H |
|
23 | 23 | |
|
24 | 24 | #include "qabstractaxis.h" |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | class QValueAxisPrivate; |
|
29 | 29 | |
|
30 | 30 | class QTCOMMERCIALCHART_EXPORT QValueAxis : public QAbstractAxis |
|
31 | 31 | { |
|
32 | 32 | Q_OBJECT |
|
33 | 33 | Q_PROPERTY(int tickCount READ tickCount WRITE setTickCount) |
|
34 | 34 | Q_PROPERTY(bool niceNumbersEnabled READ niceNumbersEnabled WRITE setNiceNumbersEnabled) |
|
35 | 35 | Q_PROPERTY(qreal min READ min WRITE setMin NOTIFY minChanged) |
|
36 | 36 | Q_PROPERTY(qreal max READ max WRITE setMax NOTIFY maxChanged) |
|
37 | Q_PROPERTY(QString labelFormat READ labelFormat WRITE setLabelFormat) | |
|
37 | 38 | |
|
38 | 39 | public: |
|
39 | 40 | explicit QValueAxis(QObject *parent = 0); |
|
40 | 41 | ~QValueAxis(); |
|
41 | 42 | |
|
42 | 43 | protected: |
|
43 | 44 | QValueAxis(QValueAxisPrivate &d,QObject *parent = 0); |
|
44 | 45 | |
|
45 | 46 | public: |
|
46 | 47 | AxisType type() const; |
|
47 | 48 | |
|
48 | 49 | //range handling |
|
49 | 50 | void setMin(qreal min); |
|
50 | 51 | qreal min() const; |
|
51 | 52 | void setMax(qreal max); |
|
52 | 53 | qreal max() const; |
|
53 | 54 | void setRange(qreal min, qreal max); |
|
54 | 55 | |
|
55 | 56 | //ticks handling |
|
56 | 57 | void setTickCount(int count); |
|
57 | 58 | int tickCount() const; |
|
58 | 59 | |
|
59 | 60 | void setLabelFormat(const QString &format); |
|
60 | 61 | QString labelFormat() const; |
|
61 | 62 | |
|
62 | 63 | void setNiceNumbersEnabled(bool enable = true); |
|
63 | 64 | bool niceNumbersEnabled() const; |
|
64 | 65 | |
|
65 | 66 | Q_SIGNALS: |
|
66 | 67 | void minChanged(qreal min); |
|
67 | 68 | void maxChanged(qreal max); |
|
68 | 69 | void rangeChanged(qreal min, qreal max); |
|
69 | 70 | |
|
70 | 71 | private: |
|
71 | 72 | Q_DECLARE_PRIVATE(QValueAxis) |
|
72 | 73 | Q_DISABLE_COPY(QValueAxis) |
|
73 | 74 | }; |
|
74 | 75 | |
|
75 | 76 | QTCOMMERCIALCHART_END_NAMESPACE |
|
76 | 77 | |
|
77 | 78 | #endif // QVALUESAXIS_H |
General Comments 0
You need to be logged in to leave comments.
Login now