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