##// END OF EJS Templates
Fix QValueAxis range setting...
Titta Heikkala -
r2699:9a409f001e2a
parent child
Show More
@@ -1,431 +1,443
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "abstractdomain_p.h"
26 26 #include "polarchartvalueaxisangular_p.h"
27 27 #include "polarchartvalueaxisradial_p.h"
28 28 #include "chartdataset_p.h"
29 29 #include "chartpresenter_p.h"
30 30 #include "charttheme_p.h"
31 31
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34 /*!
35 35 \class QValueAxis
36 36 \inmodule Qt Charts
37 37 \brief The QValueAxis class is used for manipulating chart's axis.
38 38 \mainclass
39 39
40 40 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
41 41 Values of axis are drawn to position of ticks.
42 42
43 43 Example code on how to use QValueAxis.
44 44 \code
45 45 QChartView *chartView = new QChartView;
46 46 QLineSeries *series = new QLineSeries;
47 47 // ...
48 48 chartView->chart()->addSeries(series);
49 49
50 50 QValueAxis *axisX = new QValueAxis;
51 51 axisX->setRange(10, 20.5);
52 52 axisX->setTickCount(10);
53 53 axisX->setLabelFormat("%.2f");
54 54 chartView->chart()->setAxisX(axisX, series);
55 55 \endcode
56 56 */
57 57 #ifdef QDOC_QT5
58 58 /*!
59 59 \qmltype ValueAxis
60 60 \instantiates QValueAxis
61 61 \inqmlmodule QtCommercial.Chart
62 62
63 63 \include doc/src/valueaxis.qdocinc
64 64 */
65 65 #else
66 66 /*!
67 67 \qmlclass ValueAxis QValueAxis
68 68
69 69 \include ../doc/src/valueaxis.qdocinc
70 70 */
71 71 #endif
72 72
73 73 /*!
74 74 \property QValueAxis::min
75 75 Defines the minimum value on the axis.
76 76 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
77 77 */
78 78 /*!
79 79 \qmlproperty real ValueAxis::min
80 80 Defines the minimum value on the axis.
81 81 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
82 82 */
83 83
84 84 /*!
85 85 \property QValueAxis::max
86 86 Defines the maximum value on the axis.
87 87 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
88 88 */
89 89 /*!
90 90 \qmlproperty real ValueAxis::max
91 91 Defines the maximum value on the axis.
92 92 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
93 93 */
94 94
95 95 /*!
96 96 \property QValueAxis::tickCount
97 97 Defines the number of ticks on the axis. This indicates how many grid lines are draw on the chart.
98 98 The default value is 5, and it can not be below 2.
99 99 */
100 100 /*!
101 101 \qmlproperty real ValueAxis::tickCount
102 102 Defines the number of ticks on the axis. This indicates how many grid lines are draw on the chart.
103 103 The default value is 5, and it can not be below 2.
104 104 */
105 105
106 106 /*!
107 107 \property QValueAxis::labelFormat
108 108 Defines the label format of the axis.
109 109 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c
110 110 See QString::sprintf() for additional details.
111 111 */
112 112 /*!
113 113 \qmlproperty real ValueAxis::labelFormat
114 114 Defines the label format of the axis.
115 115 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c
116 116 See QString::sprintf() for additional details.
117 117 */
118 118
119 119 /*!
120 120 \fn void QValueAxis::minChanged(qreal min)
121 121 Axis emits signal when \a min of axis has changed.
122 122 */
123 123 /*!
124 124 \qmlsignal ValueAxis::onMinChanged(real min)
125 125 Axis emits signal when \a min of axis has changed.
126 126 */
127 127
128 128 /*!
129 129 \fn void QValueAxis::maxChanged(qreal max)
130 130 Axis emits signal when \a max of axis has changed.
131 131 */
132 132 /*!
133 133 \qmlsignal ValueAxis::onMaxChanged(real max)
134 134 Axis emits signal when \a max of axis has changed.
135 135 */
136 136
137 137 /*!
138 138 \fn void QValueAxis::tickCountChanged(int tickCount)
139 139 Axis emits signal when \a tickCount of axis has changed.
140 140 */
141 141 /*!
142 142 \qmlsignal ValueAxis::tickCountChanged(int tickCount)
143 143 Axis emits signal when \a tickCount of axis has changed.
144 144 */
145 145
146 146 /*!
147 147 \fn void QValueAxis::rangeChanged(qreal min, qreal max)
148 148 Axis emits signal when \a min or \a max of axis has changed.
149 149 */
150 150
151 151 /*!
152 152 \fn void QValueAxis::labelFormatChanged(const QString &format)
153 153 Axis emits signal when \a format of axis labels has changed.
154 154 */
155 155 /*!
156 156 \qmlsignal ValueAxis::labelFormatChanged(const QString &format)
157 157 Axis emits signal when \a format of axis labels has changed.
158 158 */
159 159
160 160 /*!
161 161 \property QValueAxis::niceNumbersEnabled
162 162 \obsolete
163 163 Using this function can lead to unexpected behavior. Use applyNiceNumbers() instead.
164 164 */
165 165
166 166 /*!
167 167 \qmlproperty bool ValueAxis::niceNumbersEnabled
168 168 Deprecated; Using this function can lead to unexpected behavior. Use applyNiceNumbers() instead.
169 169 */
170 170
171 171 /*!
172 172 Constructs an axis object which is a child of \a parent.
173 173 */
174 174 QValueAxis::QValueAxis(QObject *parent) :
175 175 QAbstractAxis(*new QValueAxisPrivate(this), parent)
176 176 {
177 177
178 178 }
179 179
180 180 /*!
181 181 \internal
182 182 */
183 183 QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent)
184 184 : QAbstractAxis(d, parent)
185 185 {
186 186
187 187 }
188 188
189 189 /*!
190 190 Destroys the object
191 191 */
192 192 QValueAxis::~QValueAxis()
193 193 {
194 194 Q_D(QValueAxis);
195 195 if (d->m_chart)
196 196 d->m_chart->removeAxis(this);
197 197 }
198 198
199 199 void QValueAxis::setMin(qreal min)
200 200 {
201 201 Q_D(QValueAxis);
202 202 setRange(min, qMax(d->m_max, min));
203 203 }
204 204
205 205 qreal QValueAxis::min() const
206 206 {
207 207 Q_D(const QValueAxis);
208 208 return d->m_min;
209 209 }
210 210
211 211 void QValueAxis::setMax(qreal max)
212 212 {
213 213 Q_D(QValueAxis);
214 214 setRange(qMin(d->m_min, max), max);
215 215 }
216 216
217 217 qreal QValueAxis::max() const
218 218 {
219 219 Q_D(const QValueAxis);
220 220 return d->m_max;
221 221 }
222 222
223 223 /*!
224 224 Sets range from \a min to \a max on the axis.
225 225 If min is greater than max then this function returns without making any changes.
226 226 */
227 227 void QValueAxis::setRange(qreal min, qreal max)
228 228 {
229 229 Q_D(QValueAxis);
230 230 d->setRange(min,max);
231 231 }
232 232
233 233 void QValueAxis::setTickCount(int count)
234 234 {
235 235 Q_D(QValueAxis);
236 236 if (d->m_tickCount != count && count >= 2) {
237 237 d->m_tickCount = count;
238 238 emit tickCountChanged(count);
239 239 }
240 240 }
241 241
242 242 int QValueAxis::tickCount() const
243 243 {
244 244 Q_D(const QValueAxis);
245 245 return d->m_tickCount;
246 246 }
247 247
248 248 void QValueAxis::setNiceNumbersEnabled(bool enable)
249 249 {
250 250 Q_D(QValueAxis);
251 251 qWarning() << "Deprecated; Using this function can lead to unexpected behavior. " \
252 252 "Use applyNiceNumbers() instead.";
253 253 if(enable) {
254 254 QObject::connect(this,SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(applyNiceNumbers()));
255 255 QObject::connect(this,SIGNAL(tickCountChanged(int)),this,SLOT(applyNiceNumbers()));
256 256 applyNiceNumbers();
257 257 }
258 258 else {
259 259 QObject::disconnect(this,SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(applyNiceNumbers()));
260 260 QObject::disconnect(this,SIGNAL(tickCountChanged(int)),this,SLOT(applyNiceNumbers()));
261 261 }
262 262 d->m_niceNumbersEnabled=enable;
263 263 }
264 264
265 265 bool QValueAxis::niceNumbersEnabled() const
266 266 {
267 267 Q_D(const QValueAxis);
268 268 qWarning() << "Deprecated; Using this function can lead to unexpected behavior. " \
269 269 "Use applyNiceNumbers() instead.";
270 270 return d->m_niceNumbersEnabled;
271 271 }
272 272
273 273 void QValueAxis::setLabelFormat(const QString &format)
274 274 {
275 275 Q_D(QValueAxis);
276 276 d->m_format = format;
277 277 emit labelFormatChanged(format);
278 278 }
279 279
280 280 QString QValueAxis::labelFormat() const
281 281 {
282 282 Q_D(const QValueAxis);
283 283 return d->m_format;
284 284 }
285 285
286 286 /*!
287 287 Returns the type of the axis
288 288 */
289 289 QAbstractAxis::AxisType QValueAxis::type() const
290 290 {
291 291 return AxisTypeValue;
292 292 }
293 293
294 294 /*!
295 295 This method modifies range and number of ticks on the axis to look "nice". Algorithm considers numbers that
296 296 can be expressed as form of 1*10^n, 2* 10^n or 5*10^n as a nice numbers. These numbers are used for spacing the ticks.
297 297 This method will modify the current range and number of ticks.
298 298 \sa setRange(), setTickCount()
299 299 */
300 300 void QValueAxis::applyNiceNumbers()
301 301 {
302 302 Q_D(QValueAxis);
303 303 if(d->m_applying) return;
304 304 qreal min = d->m_min;
305 305 qreal max = d->m_max;
306 306 int ticks = d->m_tickCount;
307 307 AbstractDomain::looseNiceNumbers(min,max,ticks);
308 308 d->m_applying=true;
309 309 d->setRange(min,max);
310 310 setTickCount(ticks);
311 311 d->m_applying=false;
312 312 }
313 313
314 314 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
315 315
316 316 QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q)
317 317 : QAbstractAxisPrivate(q),
318 318 m_min(0),
319 319 m_max(0),
320 320 m_tickCount(5),
321 321 m_format(QString::null),
322 322 m_applying(false),
323 323 m_niceNumbersEnabled(false)
324 324 {
325 325
326 326 }
327 327
328 328 QValueAxisPrivate::~QValueAxisPrivate()
329 329 {
330 330
331 331 }
332 332
333 333 void QValueAxisPrivate::setMin(const QVariant &min)
334 334 {
335 335 Q_Q(QValueAxis);
336 336 bool ok;
337 337 qreal value = min.toReal(&ok);
338 338 if (ok)
339 339 q->setMin(value);
340 340 }
341 341
342 342 void QValueAxisPrivate::setMax(const QVariant &max)
343 343 {
344 344 Q_Q(QValueAxis);
345 345 bool ok;
346 346 qreal value = max.toReal(&ok);
347 347 if (ok)
348 348 q->setMax(value);
349 349 }
350 350
351 351 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
352 352 {
353 353 Q_Q(QValueAxis);
354 354 bool ok1;
355 355 bool ok2;
356 356 qreal value1 = min.toReal(&ok1);
357 357 qreal value2 = max.toReal(&ok2);
358 358 if (ok1 && ok2)
359 359 q->setRange(value1, value2);
360 360 }
361 361
362 362 void QValueAxisPrivate::setRange(qreal min, qreal max)
363 363 {
364 364 Q_Q(QValueAxis);
365 365 bool changed = false;
366 366
367 367 if (min > max)
368 368 return;
369 369
370 if (!qFuzzyCompare(m_min,min)) {
370 bool changeMin = false;
371 if (m_min == 0 || min == 0)
372 changeMin = !qFuzzyCompare(1 + m_min, 1 + min);
373 else
374 changeMin = !qFuzzyCompare(m_min, min);
375
376 bool changeMax = false;
377 if (m_max == 0 || max == 0)
378 changeMax = !qFuzzyCompare(1 + m_max, 1 + max);
379 else
380 changeMax = !qFuzzyCompare(m_max, max);
381
382 if (changeMin) {
371 383 m_min = min;
372 384 changed = true;
373 385 emit q->minChanged(min);
374 386 }
375 387
376 if (!qFuzzyCompare(m_max,max)) {
388 if (changeMax) {
377 389 m_max = max;
378 390 changed = true;
379 391 emit q->maxChanged(max);
380 392 }
381 393
382 394 if (changed) {
383 395 emit rangeChanged(min,max);
384 396 emit q->rangeChanged(min, max);
385 397 }
386 398 }
387 399
388 400 void QValueAxisPrivate::initializeGraphics(QGraphicsItem *parent)
389 401 {
390 402 Q_Q(QValueAxis);
391 403 ChartAxisElement *axis(0);
392 404
393 405 if (m_chart->chartType() == QChart::ChartTypeCartesian) {
394 406 if (orientation() == Qt::Vertical)
395 407 axis = new ChartValueAxisY(q,parent);
396 408 if (orientation() == Qt::Horizontal)
397 409 axis = new ChartValueAxisX(q,parent);
398 410 }
399 411
400 412 if (m_chart->chartType() == QChart::ChartTypePolar) {
401 413 if (orientation() == Qt::Vertical)
402 414 axis = new PolarChartValueAxisRadial(q, parent);
403 415 if (orientation() == Qt::Horizontal)
404 416 axis = new PolarChartValueAxisAngular(q, parent);
405 417 }
406 418
407 419 m_item.reset(axis);
408 420 QAbstractAxisPrivate::initializeGraphics(parent);
409 421 }
410 422
411 423
412 424 void QValueAxisPrivate::initializeDomain(AbstractDomain *domain)
413 425 {
414 426 if (orientation() == Qt::Vertical) {
415 427 if (!qFuzzyIsNull(m_max - m_min))
416 428 domain->setRangeY(m_min, m_max);
417 429 else
418 430 setRange(domain->minY(), domain->maxY());
419 431 }
420 432 if (orientation() == Qt::Horizontal) {
421 433 if (!qFuzzyIsNull(m_max - m_min))
422 434 domain->setRangeX(m_min, m_max);
423 435 else
424 436 setRange(domain->minX(), domain->maxX());
425 437 }
426 438 }
427 439
428 440 #include "moc_qvalueaxis.cpp"
429 441 #include "moc_qvalueaxis_p.cpp"
430 442
431 443 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now