##// END OF EJS Templates
Fix bar category rendering for empty category...
Titta Heikkala -
r2677:9d5038c13301
parent child
Show More
@@ -1,604 +1,607
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise 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 "abstractdomain_p.h"
25 #include "abstractdomain_p.h"
26 #include "qchart.h"
26 #include "qchart.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 \inmodule Qt Charts
32 \inmodule Qt Charts
33 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
33 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
34 \mainclass
34 \mainclass
35
35
36 BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades.
36 BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades.
37 Categories are drawn between ticks. Note that you can use this also with lineseries too.
37 Categories are drawn between ticks. Note that you can use this also with lineseries too.
38 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
38 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
39
39
40 Example code on how to use QBarCategoryAxis.
40 Example code on how to use QBarCategoryAxis.
41 \code
41 \code
42 QChartView *chartView = new QChartView;
42 QChartView *chartView = new QChartView;
43 QBarSeries *series = new QBarSeries;
43 QBarSeries *series = new QBarSeries;
44 // ...
44 // ...
45 chartView->chart()->addSeries(series);
45 chartView->chart()->addSeries(series);
46 chartView->chart()->createDefaultAxes();
46 chartView->chart()->createDefaultAxes();
47
47
48 QBarCategoryAxis *axisX = new QBarCategoryAxis;
48 QBarCategoryAxis *axisX = new QBarCategoryAxis;
49 QStringList categories;
49 QStringList categories;
50 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
50 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
51 axisX->append(categories);
51 axisX->append(categories);
52 axisX->setRange("Feb", "May");
52 axisX->setRange("Feb", "May");
53 chartView->chart()->setAxisX(axisX, series);
53 chartView->chart()->setAxisX(axisX, series);
54 \endcode
54 \endcode
55 */
55 */
56
56
57 #ifdef QDOC_QT5
57 #ifdef QDOC_QT5
58 /*!
58 /*!
59 \qmltype BarCategoryAxis
59 \qmltype BarCategoryAxis
60 \instantiates QBarCategoryAxis
60 \instantiates QBarCategoryAxis
61 \inqmlmodule QtCommercial.Chart
61 \inqmlmodule QtCommercial.Chart
62
62
63 \include doc/src/barcategoryaxis.qdocinc
63 \include doc/src/barcategoryaxis.qdocinc
64 */
64 */
65 #else
65 #else
66 /*!
66 /*!
67 \qmlclass BarCategoryAxis QBarCategoryAxis
67 \qmlclass BarCategoryAxis QBarCategoryAxis
68
68
69 \include ../doc/src/barcategoryaxis.qdocinc
69 \include ../doc/src/barcategoryaxis.qdocinc
70 */
70 */
71 #endif
71 #endif
72
72
73 /*!
73 /*!
74 \property QBarCategoryAxis::categories
74 \property QBarCategoryAxis::categories
75 Defines the categories of axis
75 Defines the categories of axis
76 */
76 */
77 /*!
77 /*!
78 \qmlproperty QStringList BarCategoryAxis::categories
78 \qmlproperty QStringList BarCategoryAxis::categories
79 Defines the categories of axis
79 Defines the categories of axis
80 */
80 */
81
81
82 /*!
82 /*!
83 \property QBarCategoryAxis::min
83 \property QBarCategoryAxis::min
84 Defines the minimum value on the axis.
84 Defines the minimum value on the axis.
85 */
85 */
86 /*!
86 /*!
87 \qmlproperty string BarCategoryAxis::min
87 \qmlproperty string BarCategoryAxis::min
88 Defines the minimum value on the axis.
88 Defines the minimum value on the axis.
89 */
89 */
90
90
91 /*!
91 /*!
92 \property QBarCategoryAxis::max
92 \property QBarCategoryAxis::max
93 Defines the maximum value on the axis.
93 Defines the maximum value on the axis.
94 */
94 */
95 /*!
95 /*!
96 \qmlproperty string BarCategoryAxis::max
96 \qmlproperty string BarCategoryAxis::max
97 Defines the maximum value on the axis.
97 Defines the maximum value on the axis.
98 */
98 */
99
99
100 /*!
100 /*!
101 \property QBarCategoryAxis::count
101 \property QBarCategoryAxis::count
102 The count of categories.
102 The count of categories.
103 */
103 */
104 /*!
104 /*!
105 \qmlproperty int BarCategoryAxis::count
105 \qmlproperty int BarCategoryAxis::count
106 The count of categories.
106 The count of categories.
107 */
107 */
108
108
109 /*!
109 /*!
110 \fn void QBarCategoryAxis::categoriesChanged()
110 \fn void QBarCategoryAxis::categoriesChanged()
111 Axis emits signal when the categories of the axis have changed.
111 Axis emits signal when the categories of the axis have changed.
112 */
112 */
113
113
114 /*!
114 /*!
115 \fn void QBarCategoryAxis::minChanged(const QString &min)
115 \fn void QBarCategoryAxis::minChanged(const QString &min)
116 Axis emits signal when \a min of axis has changed.
116 Axis emits signal when \a min of axis has changed.
117 */
117 */
118 /*!
118 /*!
119 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
119 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
120 Axis emits signal when \a min of axis has changed.
120 Axis emits signal when \a min of axis has changed.
121 */
121 */
122
122
123 /*!
123 /*!
124 \fn void QBarCategoryAxis::maxChanged(const QString &max)
124 \fn void QBarCategoryAxis::maxChanged(const QString &max)
125 Axis emits signal when \a max of axis has changed.
125 Axis emits signal when \a max of axis has changed.
126 */
126 */
127 /*!
127 /*!
128 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
128 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
129 Axis emits signal when \a max of axis has changed.
129 Axis emits signal when \a max of axis has changed.
130 */
130 */
131
131
132 /*!
132 /*!
133 \fn void QBarCategoryAxis::countChanged()
133 \fn void QBarCategoryAxis::countChanged()
134 Axis emits signal when the count of categories has changed.
134 Axis emits signal when the count of categories has changed.
135 */
135 */
136 /*!
136 /*!
137 \qmlsignal BarCategoryAxis::onCountChanged()
137 \qmlsignal BarCategoryAxis::onCountChanged()
138 Axis emits signal when the count of categories has changed.
138 Axis emits signal when the count of categories has changed.
139 */
139 */
140
140
141 /*!
141 /*!
142 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
142 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
143 Axis emits signal when \a min or \a max of axis has changed.
143 Axis emits signal when \a min or \a max of axis has changed.
144 */
144 */
145
145
146 /*!
146 /*!
147 Constructs an axis object which is a child of \a parent.
147 Constructs an axis object which is a child of \a parent.
148 */
148 */
149 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
149 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
150 QAbstractAxis(*new QBarCategoryAxisPrivate(this), parent)
150 QAbstractAxis(*new QBarCategoryAxisPrivate(this), parent)
151 {
151 {
152 }
152 }
153
153
154 /*!
154 /*!
155 Destroys the object
155 Destroys the object
156 */
156 */
157 QBarCategoryAxis::~QBarCategoryAxis()
157 QBarCategoryAxis::~QBarCategoryAxis()
158 {
158 {
159 Q_D(QBarCategoryAxis);
159 Q_D(QBarCategoryAxis);
160 if (d->m_chart)
160 if (d->m_chart)
161 d->m_chart->removeAxis(this);
161 d->m_chart->removeAxis(this);
162 }
162 }
163
163
164 /*!
164 /*!
165 \internal
165 \internal
166 */
166 */
167 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent)
167 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent)
168 : QAbstractAxis(d, parent)
168 : QAbstractAxis(d, parent)
169 {
169 {
170
170
171 }
171 }
172
172
173 /*!
173 /*!
174 Appends \a categories to axis. A maximum of the axis will be changed to last category in \a categories.
174 Appends \a categories to axis. A maximum of the axis will be changed to last category in \a categories.
175 If there were no categories previously defined, minimum of axis will be also changed to first category in \a categories.
175 If there were no categories previously defined, minimum of axis will be also changed to first category in \a categories.
176 A category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
176 A category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
177 */
177 */
178 void QBarCategoryAxis::append(const QStringList &categories)
178 void QBarCategoryAxis::append(const QStringList &categories)
179 {
179 {
180 if (categories.isEmpty())
180 if (categories.isEmpty())
181 return;
181 return;
182
182
183 Q_D(QBarCategoryAxis);
183 Q_D(QBarCategoryAxis);
184
184
185 int count = d->m_categories.count();
185 int count = d->m_categories.count();
186
186
187 foreach(QString category, categories) {
187 foreach(QString category, categories) {
188 if (!d->m_categories.contains(category) && !category.isNull()) {
188 if (!d->m_categories.contains(category) && !category.isNull()) {
189 d->m_categories.append(category);
189 d->m_categories.append(category);
190 }
190 }
191 }
191 }
192
192
193 if (d->m_categories.count() == count)
193 if (d->m_categories.count() == count)
194 return;
194 return;
195
195
196 if (count == 0)
196 if (count == 0)
197 setRange(d->m_categories.first(), d->m_categories.last());
197 setRange(d->m_categories.first(), d->m_categories.last());
198 else
198 else
199 setRange(d->m_minCategory, d->m_categories.last());
199 setRange(d->m_minCategory, d->m_categories.last());
200
200
201 emit categoriesChanged();
201 emit categoriesChanged();
202 emit countChanged();
202 emit countChanged();
203 }
203 }
204
204
205 /*!
205 /*!
206 Appends \a category to axis. A maximum of the axis will be changed to last \a category.
206 Appends \a category to axis. A maximum of the axis will be changed to last \a category.
207 If there were no categories previously defined, minimum of axis will be also changed to \a category.
207 If there were no categories previously defined, minimum of axis will be also changed to \a category.
208 A \a category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
208 A \a category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
209 */
209 */
210 void QBarCategoryAxis::append(const QString &category)
210 void QBarCategoryAxis::append(const QString &category)
211 {
211 {
212 Q_D(QBarCategoryAxis);
212 Q_D(QBarCategoryAxis);
213
213
214 int count = d->m_categories.count();
214 int count = d->m_categories.count();
215
215
216 if (!d->m_categories.contains(category) && !category.isNull())
216 if (!d->m_categories.contains(category) && !category.isNull())
217 d->m_categories.append(category);
217 d->m_categories.append(category);
218
218
219 if (d->m_categories.count() == count)
219 if (d->m_categories.count() == count)
220 return;
220 return;
221
221
222 if (count == 0)
222 if (count == 0)
223 setRange(d->m_categories.last(), d->m_categories.last());
223 setRange(d->m_categories.last(), d->m_categories.last());
224 else
224 else
225 setRange(d->m_minCategory, d->m_categories.last());
225 setRange(d->m_minCategory, d->m_categories.last());
226
226
227 emit categoriesChanged();
227 emit categoriesChanged();
228 emit countChanged();
228 emit countChanged();
229 }
229 }
230
230
231 /*!
231 /*!
232 Removes \a category from axis. Removing category which is currently maximum or minimum
232 Removes \a category from axis. Removing category which is currently maximum or minimum
233 will affect the axis range.
233 will affect the axis range.
234 */
234 */
235 void QBarCategoryAxis::remove(const QString &category)
235 void QBarCategoryAxis::remove(const QString &category)
236 {
236 {
237 Q_D(QBarCategoryAxis);
237 Q_D(QBarCategoryAxis);
238
238
239 if (d->m_categories.contains(category)) {
239 if (d->m_categories.contains(category)) {
240 d->m_categories.removeAt(d->m_categories.indexOf(category));
240 d->m_categories.removeAt(d->m_categories.indexOf(category));
241 if (!d->m_categories.isEmpty()) {
241 if (!d->m_categories.isEmpty()) {
242 if (d->m_minCategory == category) {
242 if (d->m_minCategory == category) {
243 setRange(d->m_categories.first(), d->m_maxCategory);
243 setRange(d->m_categories.first(), d->m_maxCategory);
244 } else if (d->m_maxCategory == category) {
244 } else if (d->m_maxCategory == category) {
245 setRange(d->m_minCategory, d->m_categories.last());
245 setRange(d->m_minCategory, d->m_categories.last());
246 } else {
246 } else {
247 d->updateCategoryDomain();
247 d->updateCategoryDomain();
248 }
248 }
249 } else {
249 } else {
250 setRange(QString::null, QString::null);
250 setRange(QString::null, QString::null);
251 }
251 }
252 emit categoriesChanged();
252 emit categoriesChanged();
253 emit countChanged();
253 emit countChanged();
254 }
254 }
255 }
255 }
256
256
257 /*!
257 /*!
258 Inserts \a category to axis at \a index. A \a category has to be valid QStrings and can not be duplicated.
258 Inserts \a category to axis at \a index. A \a category has to be valid QStrings and can not be duplicated.
259 If \a category is prepended or appended to categories, minimum and maximum of axis is updated accordingly.
259 If \a category is prepended or appended to categories, minimum and maximum of axis is updated accordingly.
260 */
260 */
261 void QBarCategoryAxis::insert(int index, const QString &category)
261 void QBarCategoryAxis::insert(int index, const QString &category)
262 {
262 {
263 Q_D(QBarCategoryAxis);
263 Q_D(QBarCategoryAxis);
264
264
265 int count = d->m_categories.count();
265 int count = d->m_categories.count();
266
266
267 if (!d->m_categories.contains(category) && !category.isNull())
267 if (!d->m_categories.contains(category) && !category.isNull())
268 d->m_categories.insert(index, category);
268 d->m_categories.insert(index, category);
269
269
270 if (d->m_categories.count() == count)
270 if (d->m_categories.count() == count)
271 return;
271 return;
272
272
273 if (count == 0) {
273 if (count == 0) {
274 setRange(d->m_categories.first(), d->m_categories.first());
274 setRange(d->m_categories.first(), d->m_categories.first());
275 } else if (index == 0) {
275 } else if (index == 0) {
276 setRange(d->m_categories.first(), d->m_maxCategory);
276 setRange(d->m_categories.first(), d->m_maxCategory);
277 } else if (index == count) {
277 } else if (index == count) {
278 setRange(d->m_minCategory, d->m_categories.last());
278 setRange(d->m_minCategory, d->m_categories.last());
279 } else {
279 } else {
280 d->updateCategoryDomain();
280 d->updateCategoryDomain();
281 }
281 }
282
282
283 emit categoriesChanged();
283 emit categoriesChanged();
284 emit countChanged();
284 emit countChanged();
285 }
285 }
286
286
287 /*!
287 /*!
288 Replaces \a oldCategory with \a newCategory. If \a oldCategory does not exist on the axis nothing is done.
288 Replaces \a oldCategory with \a newCategory. If \a oldCategory does not exist on the axis nothing is done.
289 A \a newCategory has to be valid QStrings and can not be duplicated. In case of replacing minimum or maximum category,
289 A \a newCategory has to be valid QStrings and can not be duplicated. In case of replacing minimum or maximum category,
290 minimum and maximum of axis is updated accordingly.
290 minimum and maximum of axis is updated accordingly.
291 */
291 */
292 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
292 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
293 {
293 {
294 Q_D(QBarCategoryAxis);
294 Q_D(QBarCategoryAxis);
295
295
296 int pos = d->m_categories.indexOf(oldCategory);
296 int pos = d->m_categories.indexOf(oldCategory);
297
297
298 if (pos != -1 && !d->m_categories.contains(newCategory) && !newCategory.isNull()) {
298 if (pos != -1 && !d->m_categories.contains(newCategory) && !newCategory.isNull()) {
299 d->m_categories.replace(pos, newCategory);
299 d->m_categories.replace(pos, newCategory);
300 if (d->m_minCategory == oldCategory)
300 if (d->m_minCategory == oldCategory)
301 setRange(newCategory, d->m_maxCategory);
301 setRange(newCategory, d->m_maxCategory);
302 else if (d->m_maxCategory == oldCategory)
302 else if (d->m_maxCategory == oldCategory)
303 setRange(d->m_minCategory, newCategory);
303 setRange(d->m_minCategory, newCategory);
304
304
305 emit categoriesChanged();
305 emit categoriesChanged();
306 emit countChanged();
306 emit countChanged();
307 }
307 }
308 }
308 }
309
309
310 /*!
310 /*!
311 Removes all categories. Sets the maximum and minimum of the axis's range to QString::null.
311 Removes all categories. Sets the maximum and minimum of the axis's range to QString::null.
312 */
312 */
313 void QBarCategoryAxis::clear()
313 void QBarCategoryAxis::clear()
314 {
314 {
315 Q_D(QBarCategoryAxis);
315 Q_D(QBarCategoryAxis);
316 d->m_categories.clear();
316 d->m_categories.clear();
317 setRange(QString::null, QString::null);
317 setRange(QString::null, QString::null);
318 emit categoriesChanged();
318 emit categoriesChanged();
319 emit countChanged();
319 emit countChanged();
320 }
320 }
321
321
322 /*!
322 /*!
323 Set \a categories and discards the old ones, range of axis is adjusted to match first and last category in \a categories.
323 Set \a categories and discards the old ones, range of axis is adjusted to match first and last category in \a categories.
324 A category has to be valid QStrings and can not be duplicated.
324 A category has to be valid QStrings and can not be duplicated.
325 */
325 */
326 void QBarCategoryAxis::setCategories(const QStringList &categories)
326 void QBarCategoryAxis::setCategories(const QStringList &categories)
327 {
327 {
328 Q_D(QBarCategoryAxis);
328 Q_D(QBarCategoryAxis);
329 d->m_categories.clear();
329 d->m_categories.clear();
330 d->m_minCategory = QString::null;
330 d->m_minCategory = QString::null;
331 d->m_maxCategory = QString::null;
331 d->m_maxCategory = QString::null;
332 d->m_min = 0;
332 d->m_min = 0;
333 d->m_max = 0;
333 d->m_max = 0;
334 d->m_count = 0;
334 d->m_count = 0;
335 append(categories);
335 append(categories);
336 }
336 }
337
337
338 /*!
338 /*!
339 Returns categories
339 Returns categories
340 */
340 */
341 QStringList QBarCategoryAxis::categories()
341 QStringList QBarCategoryAxis::categories()
342 {
342 {
343 Q_D(QBarCategoryAxis);
343 Q_D(QBarCategoryAxis);
344 return d->m_categories;
344 return d->m_categories;
345 }
345 }
346
346
347 /*!
347 /*!
348 Returns number of categories.
348 Returns number of categories.
349 */
349 */
350 int QBarCategoryAxis::count() const
350 int QBarCategoryAxis::count() const
351 {
351 {
352 Q_D(const QBarCategoryAxis);
352 Q_D(const QBarCategoryAxis);
353 return d->m_categories.count();
353 return d->m_categories.count();
354 }
354 }
355
355
356 /*!
356 /*!
357 Returns category at \a index. Index must be valid.
357 Returns category at \a index. Index must be valid.
358 */
358 */
359 QString QBarCategoryAxis::at(int index) const
359 QString QBarCategoryAxis::at(int index) const
360 {
360 {
361 Q_D(const QBarCategoryAxis);
361 Q_D(const QBarCategoryAxis);
362 return d->m_categories.at(index);
362 return d->m_categories.at(index);
363 }
363 }
364
364
365 /*!
365 /*!
366 Sets minimum category to \a min.
366 Sets minimum category to \a min.
367 */
367 */
368 void QBarCategoryAxis::setMin(const QString &min)
368 void QBarCategoryAxis::setMin(const QString &min)
369 {
369 {
370 Q_D(QBarCategoryAxis);
370 Q_D(QBarCategoryAxis);
371 d->setRange(min, d->m_maxCategory);
371 d->setRange(min, d->m_maxCategory);
372 }
372 }
373
373
374 /*!
374 /*!
375 Returns minimum category.
375 Returns minimum category.
376 */
376 */
377 QString QBarCategoryAxis::min() const
377 QString QBarCategoryAxis::min() const
378 {
378 {
379 Q_D(const QBarCategoryAxis);
379 Q_D(const QBarCategoryAxis);
380 return d->m_minCategory;
380 return d->m_minCategory;
381 }
381 }
382
382
383 /*!
383 /*!
384 Sets maximum category to \a max.
384 Sets maximum category to \a max.
385 */
385 */
386 void QBarCategoryAxis::setMax(const QString &max)
386 void QBarCategoryAxis::setMax(const QString &max)
387 {
387 {
388 Q_D(QBarCategoryAxis);
388 Q_D(QBarCategoryAxis);
389 d->setRange(d->m_minCategory, max);
389 d->setRange(d->m_minCategory, max);
390 }
390 }
391
391
392 /*!
392 /*!
393 Returns maximum category
393 Returns maximum category
394 */
394 */
395 QString QBarCategoryAxis::max() const
395 QString QBarCategoryAxis::max() const
396 {
396 {
397 Q_D(const QBarCategoryAxis);
397 Q_D(const QBarCategoryAxis);
398 return d->m_maxCategory;
398 return d->m_maxCategory;
399 }
399 }
400
400
401 /*!
401 /*!
402 Sets range from \a minCategory to \a maxCategory
402 Sets range from \a minCategory to \a maxCategory
403 */
403 */
404 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCategory)
404 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCategory)
405 {
405 {
406 Q_D(QBarCategoryAxis);
406 Q_D(QBarCategoryAxis);
407 d->setRange(minCategory,maxCategory);
407 d->setRange(minCategory,maxCategory);
408 }
408 }
409
409
410 /*!
410 /*!
411 Returns the type of the axis
411 Returns the type of the axis
412 */
412 */
413 QAbstractAxis::AxisType QBarCategoryAxis::type() const
413 QAbstractAxis::AxisType QBarCategoryAxis::type() const
414 {
414 {
415 return AxisTypeBarCategory;
415 return AxisTypeBarCategory;
416 }
416 }
417
417
418 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
418 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
419
419
420 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis *q)
420 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis *q)
421 : QAbstractAxisPrivate(q),
421 : QAbstractAxisPrivate(q),
422 m_min(0.0),
422 m_min(0.0),
423 m_max(0.0),
423 m_max(0.0),
424 m_count(0)
424 m_count(0)
425 {
425 {
426
426
427 }
427 }
428
428
429 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
429 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
430 {
430 {
431
431
432 }
432 }
433
433
434 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
434 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
435 {
435 {
436 setRange(min, m_maxCategory);
436 setRange(min, m_maxCategory);
437 }
437 }
438
438
439 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
439 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
440 {
440 {
441 setRange(m_minCategory, max);
441 setRange(m_minCategory, max);
442 }
442 }
443
443
444 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
444 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
445 {
445 {
446 QString value1 = min.toString();
446 QString value1 = min.toString();
447 QString value2 = max.toString();
447 QString value2 = max.toString();
448 setRange(value1, value2);
448 setRange(value1, value2);
449 }
449 }
450
450
451 void QBarCategoryAxisPrivate::setRange(qreal min, qreal max)
451 void QBarCategoryAxisPrivate::setRange(qreal min, qreal max)
452 {
452 {
453 Q_Q(QBarCategoryAxis);
453 Q_Q(QBarCategoryAxis);
454
454
455 bool categoryChanged = false;
455 bool categoryChanged = false;
456 bool changed = false;
456 bool changed = false;
457
457
458 if (min > max)
458 if (min > max)
459 return;
459 return;
460
460
461 if (!qFuzzyIsNull(m_min - min)) {
461 if (!qFuzzyIsNull(m_min - min)) {
462 m_min = min;
462 m_min = min;
463 changed = true;
463 changed = true;
464
464
465 int imin = m_min + 0.5;
465 int imin = m_min + 0.5;
466 if (imin >= 0 && imin < m_categories.count()) {
466 if (imin >= 0 && imin < m_categories.count()) {
467 QString minCategory = m_categories.at(imin);
467 QString minCategory = m_categories.at(imin);
468 if (m_minCategory != minCategory && !minCategory.isEmpty()) {
468 if (m_minCategory != minCategory && !minCategory.isEmpty()) {
469 m_minCategory = minCategory;
469 m_minCategory = minCategory;
470 categoryChanged = true;
470 categoryChanged = true;
471 emit q->minChanged(minCategory);
471 emit q->minChanged(minCategory);
472 }
472 }
473 }
473 }
474
474
475 }
475 }
476
476
477 if (!qFuzzyIsNull(m_max - max)) {
477 if (!qFuzzyIsNull(m_max - max)) {
478 m_max = max;
478 m_max = max;
479 changed = true;
479 changed = true;
480
480
481 int imax = m_max - 0.5;
481 int imax = m_max - 0.5;
482 if (imax >= 0 && imax < m_categories.count()) {
482 if (imax >= 0 && imax < m_categories.count()) {
483 QString maxCategory = m_categories.at(imax);
483 QString maxCategory = m_categories.at(imax);
484 if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) {
484 if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) {
485 m_maxCategory = maxCategory;
485 m_maxCategory = maxCategory;
486 categoryChanged = true;
486 categoryChanged = true;
487 emit q->maxChanged(maxCategory);
487 emit q->maxChanged(maxCategory);
488 }
488 }
489 }
489 }
490 }
490 }
491
491
492 if (categoryChanged){
492 if (categoryChanged){
493 emit q->rangeChanged(m_minCategory, m_maxCategory);
493 emit q->rangeChanged(m_minCategory, m_maxCategory);
494 }
494 }
495
495
496 if (changed) {
496 if (changed) {
497 emit rangeChanged(m_min,m_max);
497 emit rangeChanged(m_min,m_max);
498 }
498 }
499 }
499 }
500
500
501 void QBarCategoryAxisPrivate::setRange(const QString &minCategory, const QString &maxCategory)
501 void QBarCategoryAxisPrivate::setRange(const QString &minCategory, const QString &maxCategory)
502 {
502 {
503 Q_Q(QBarCategoryAxis);
503 Q_Q(QBarCategoryAxis);
504 bool changed = false;
504 bool changed = false;
505
505
506 //special case in case or clearing all categories
506 //special case in case or clearing all categories
507 if (minCategory.isNull() && maxCategory.isNull()) {
507 if (minCategory.isNull() && maxCategory.isNull()) {
508 m_minCategory = minCategory;
508 m_minCategory = minCategory;
509 m_maxCategory = maxCategory;
509 m_maxCategory = maxCategory;
510 m_min = 0;
510 m_min = 0;
511 m_max = 0;
511 m_max = 0;
512 m_count = 0;
512 m_count = 0;
513 emit q->minChanged(minCategory);
513 emit q->minChanged(minCategory);
514 emit q->maxChanged(maxCategory);
514 emit q->maxChanged(maxCategory);
515 emit q->rangeChanged(m_minCategory, m_maxCategory);
515 emit q->rangeChanged(m_minCategory, m_maxCategory);
516 emit rangeChanged(m_min,m_max);
516 emit rangeChanged(m_min,m_max);
517 return;
517 }
518 }
518
519
519 if (m_categories.indexOf(maxCategory) < m_categories.indexOf(minCategory))
520 if (m_categories.indexOf(maxCategory) < m_categories.indexOf(minCategory))
520 return;
521 return;
521
522
522 if (!minCategory.isEmpty() && m_minCategory != minCategory && m_categories.contains(minCategory)) {
523 if (!minCategory.isNull() && (m_minCategory != minCategory || m_minCategory.isNull())
524 && m_categories.contains(minCategory)) {
523 m_minCategory = minCategory;
525 m_minCategory = minCategory;
524 m_min = m_categories.indexOf(m_minCategory) - 0.5;
526 m_min = m_categories.indexOf(m_minCategory) - 0.5;
525 changed = true;
527 changed = true;
526 emit q->minChanged(minCategory);
528 emit q->minChanged(minCategory);
527 }
529 }
528
530
529 if (!maxCategory.isEmpty() && m_maxCategory != maxCategory && m_categories.contains(maxCategory)) {
531 if (!maxCategory.isNull() && (m_maxCategory != maxCategory || m_maxCategory.isNull())
532 && m_categories.contains(maxCategory)) {
530 m_maxCategory = maxCategory;
533 m_maxCategory = maxCategory;
531 m_max = m_categories.indexOf(m_maxCategory) + 0.5;
534 m_max = m_categories.indexOf(m_maxCategory) + 0.5;
532 changed = true;
535 changed = true;
533 emit q->maxChanged(maxCategory);
536 emit q->maxChanged(maxCategory);
534 }
537 }
535
538
536 if (changed) {
539 if (changed) {
537 m_count = m_max - m_min;
540 m_count = m_max - m_min;
538 emit q->rangeChanged(m_minCategory, m_maxCategory);
541 emit q->rangeChanged(m_minCategory, m_maxCategory);
539 emit rangeChanged(m_min,m_max);
542 emit rangeChanged(m_min,m_max);
540 }
543 }
541 }
544 }
542
545
543 void QBarCategoryAxisPrivate::initializeGraphics(QGraphicsItem* parent)
546 void QBarCategoryAxisPrivate::initializeGraphics(QGraphicsItem* parent)
544 {
547 {
545 Q_Q(QBarCategoryAxis);
548 Q_Q(QBarCategoryAxis);
546 ChartAxisElement* axis(0);
549 ChartAxisElement* axis(0);
547 if (orientation() == Qt::Vertical)
550 if (orientation() == Qt::Vertical)
548 axis = new ChartBarCategoryAxisY(q,parent);
551 axis = new ChartBarCategoryAxisY(q,parent);
549 if (orientation() == Qt::Horizontal)
552 if (orientation() == Qt::Horizontal)
550 axis = new ChartBarCategoryAxisX(q,parent);
553 axis = new ChartBarCategoryAxisX(q,parent);
551
554
552 m_item.reset(axis);
555 m_item.reset(axis);
553 QAbstractAxisPrivate::initializeGraphics(parent);
556 QAbstractAxisPrivate::initializeGraphics(parent);
554 }
557 }
555
558
556 void QBarCategoryAxisPrivate::updateCategoryDomain()
559 void QBarCategoryAxisPrivate::updateCategoryDomain()
557 {
560 {
558 bool changed = false;
561 bool changed = false;
559
562
560 qreal tmpMin = m_categories.indexOf(m_minCategory) - 0.5;
563 qreal tmpMin = m_categories.indexOf(m_minCategory) - 0.5;
561 if (!qFuzzyIsNull(m_min - tmpMin)) {
564 if (!qFuzzyIsNull(m_min - tmpMin)) {
562 m_min = tmpMin;
565 m_min = tmpMin;
563 changed = true;
566 changed = true;
564 }
567 }
565 qreal tmpMax = m_categories.indexOf(m_maxCategory) + 0.5;
568 qreal tmpMax = m_categories.indexOf(m_maxCategory) + 0.5;
566 if (!qFuzzyIsNull(m_max - tmpMax)) {
569 if (!qFuzzyIsNull(m_max - tmpMax)) {
567 m_max = tmpMax;
570 m_max = tmpMax;
568 changed = true;
571 changed = true;
569 }
572 }
570 m_count = m_max - m_min;
573 m_count = m_max - m_min;
571
574
572 if (changed)
575 if (changed)
573 emit rangeChanged(m_min,m_max);
576 emit rangeChanged(m_min,m_max);
574 }
577 }
575
578
576
579
577 void QBarCategoryAxisPrivate::initializeDomain(AbstractDomain *domain)
580 void QBarCategoryAxisPrivate::initializeDomain(AbstractDomain *domain)
578 {
581 {
579 Q_Q(QBarCategoryAxis);
582 Q_Q(QBarCategoryAxis);
580 if (m_max == m_min) {
583 if (m_max == m_min) {
581 int min;
584 int min;
582 int max;
585 int max;
583 if (orientation() == Qt::Vertical) {
586 if (orientation() == Qt::Vertical) {
584 min = domain->minY() + 0.5;
587 min = domain->minY() + 0.5;
585 max = domain->maxY() - 0.5;
588 max = domain->maxY() - 0.5;
586 } else {
589 } else {
587 min = domain->minX() + 0.5;
590 min = domain->minX() + 0.5;
588 max = domain->maxX() - 0.5;
591 max = domain->maxX() - 0.5;
589 }
592 }
590
593
591 if (min > 0 && min < m_categories.count() && max > 0 && max < m_categories.count())
594 if (min > 0 && min < m_categories.count() && max > 0 && max < m_categories.count())
592 q->setRange(m_categories.at(min), m_categories.at(max));
595 q->setRange(m_categories.at(min), m_categories.at(max));
593 } else {
596 } else {
594 if (orientation() == Qt::Vertical)
597 if (orientation() == Qt::Vertical)
595 domain->setRangeY(m_min, m_max);
598 domain->setRangeY(m_min, m_max);
596 else
599 else
597 domain->setRangeX(m_min, m_max);
600 domain->setRangeX(m_min, m_max);
598 }
601 }
599 }
602 }
600
603
601 #include "moc_qbarcategoryaxis.cpp"
604 #include "moc_qbarcategoryaxis.cpp"
602 #include "moc_qbarcategoryaxis_p.cpp"
605 #include "moc_qbarcategoryaxis_p.cpp"
603
606
604 QTCOMMERCIALCHART_END_NAMESPACE
607 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now