##// END OF EJS Templates
added description of using barcategory axis with line chart
sauimone -
r1650:d4aaf12f2380
parent child
Show More
@@ -1,383 +1,384
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 "qbarcategoriesaxis.h"
21 #include "qbarcategoriesaxis.h"
22 #include "qbarcategoriesaxis_p.h"
22 #include "qbarcategoriesaxis_p.h"
23 #include "chartcategoriesaxisx_p.h"
23 #include "chartcategoriesaxisx_p.h"
24 #include "chartcategoriesaxisy_p.h"
24 #include "chartcategoriesaxisy_p.h"
25 #include <qmath.h>
25 #include <qmath.h>
26 #include <QDebug>
26 #include <QDebug>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 /*!
29 /*!
30 \class QBarCategoriesAxis
30 \class QBarCategoriesAxis
31 \brief The QBarCategoriesAxis class is used for manipulating chart's axis.
31 \brief The QBarCategoriesAxis class is used for manipulating chart's axis.
32 \mainclass
32 \mainclass
33
33
34 BarCategoriesAxis can be setup to show axis line with tick marks, grid lines and shades.
34 BarCategoriesAxis can be setup to show axis line with tick marks, grid lines and shades.
35 Categories are drawn between ticks.
35 Categories are drawn between ticks. Note that you can use this also with lineseries too.
36 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
36 */
37 */
37
38
38 /*!
39 /*!
39 \qmlclass BarCategoriesAxis QBarCategoriesAxis
40 \qmlclass BarCategoriesAxis QBarCategoriesAxis
40 \brief The Axis element is used for manipulating chart's axes.
41 \brief The Axis element is used for manipulating chart's axes.
41
42
42 Axis can be setup to show axis line with tick marks, grid lines and shades.
43 Axis can be setup to show axis line with tick marks, grid lines and shades.
43 Categories are drawn between ticks.
44 Categories are drawn between ticks. Note that you can use this also with lineseries too.
44
45
45 To access BarCategoriesAxis you can use ChartView API. For example:
46 To access BarCategoriesAxis you can use ChartView API. For example:
46 \code
47 \code
47 ChartView {
48 ChartView {
48 BarCategoriesAxis {
49 BarCategoriesAxis {
49 id: categoryAxis
50 id: categoryAxis
50 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
51 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
51 }
52 }
52 // Add a few series...
53 // Add a few series...
53 }
54 }
54 \endcode
55 \endcode
55 */
56 */
56
57
57 /*!
58 /*!
58 \property QBarCategoriesAxis::categories
59 \property QBarCategoriesAxis::categories
59 Defines the categories of axis
60 Defines the categories of axis
60 */
61 */
61 /*!
62 /*!
62 \qmlproperty QStringList BarCategoriesAxis::categories
63 \qmlproperty QStringList BarCategoriesAxis::categories
63 Defines the categories of axis
64 Defines the categories of axis
64 */
65 */
65
66
66 /*!
67 /*!
67 \property QBarCategoriesAxis::min
68 \property QBarCategoriesAxis::min
68 Defines the minimum value on the axis.
69 Defines the minimum value on the axis.
69 */
70 */
70 /*!
71 /*!
71 \qmlproperty real BarCategoriesAxis::min
72 \qmlproperty real BarCategoriesAxis::min
72 Defines the minimum value on the axis.
73 Defines the minimum value on the axis.
73 */
74 */
74
75
75 /*!
76 /*!
76 \property QBarCategoriesAxis::max
77 \property QBarCategoriesAxis::max
77 Defines the maximum value on the axis.
78 Defines the maximum value on the axis.
78 */
79 */
79 /*!
80 /*!
80 \qmlproperty real BarCategoriesAxis::max
81 \qmlproperty real BarCategoriesAxis::max
81 Defines the maximum value on the axis.
82 Defines the maximum value on the axis.
82 */
83 */
83
84
84 /*!
85 /*!
85 \fn void QBarCategoriesAxis::minChanged(const QString &min)
86 \fn void QBarCategoriesAxis::minChanged(const QString &min)
86 Axis emits signal when \a min of axis has changed.
87 Axis emits signal when \a min of axis has changed.
87 */
88 */
88
89
89 /*!
90 /*!
90 \fn void QBarCategoriesAxis::maxChanged(const QString &max)
91 \fn void QBarCategoriesAxis::maxChanged(const QString &max)
91 Axis emits signal when \a max of axis has changed.
92 Axis emits signal when \a max of axis has changed.
92 */
93 */
93
94
94 /*!
95 /*!
95 \fn void QBarCategoriesAxis::rangeChanged(const QString &min, const QString &max)
96 \fn void QBarCategoriesAxis::rangeChanged(const QString &min, const QString &max)
96 Axis emits signal when \a min or \a max of axis has changed.
97 Axis emits signal when \a min or \a max of axis has changed.
97 */
98 */
98
99
99 /*!
100 /*!
100 Constructs an axis object which is a child of \a parent.
101 Constructs an axis object which is a child of \a parent.
101 */
102 */
102 QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent):
103 QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent):
103 QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent)
104 QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent)
104 {
105 {
105 }
106 }
106
107
107 /*!
108 /*!
108 Destroys the object
109 Destroys the object
109 */
110 */
110 QBarCategoriesAxis::~QBarCategoriesAxis()
111 QBarCategoriesAxis::~QBarCategoriesAxis()
111 {
112 {
112 }
113 }
113
114
114 /*!
115 /*!
115 \internal
116 \internal
116 */
117 */
117 QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
118 QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
118 {
119 {
119
120
120 }
121 }
121
122
122 /*!
123 /*!
123 Appends \a categories to axis
124 Appends \a categories to axis
124 */
125 */
125 void QBarCategoriesAxis::append(const QStringList &categories)
126 void QBarCategoriesAxis::append(const QStringList &categories)
126 {
127 {
127 if(categories.isEmpty()) return;
128 if(categories.isEmpty()) return;
128
129
129 Q_D(QBarCategoriesAxis);
130 Q_D(QBarCategoriesAxis);
130 if (d->m_categories.isEmpty()) {
131 if (d->m_categories.isEmpty()) {
131 d->m_categories.append(categories);
132 d->m_categories.append(categories);
132 setRange(categories.first(),categories.last());
133 setRange(categories.first(),categories.last());
133 }else{
134 }else{
134 d->m_categories.append(categories);
135 d->m_categories.append(categories);
135 }
136 }
136 emit d->updated();
137 emit d->updated();
137 emit categoriesChanged();
138 emit categoriesChanged();
138 }
139 }
139
140
140 /*!
141 /*!
141 Appends \a category to axis
142 Appends \a category to axis
142 */
143 */
143 void QBarCategoriesAxis::append(const QString &category)
144 void QBarCategoriesAxis::append(const QString &category)
144 {
145 {
145 Q_D(QBarCategoriesAxis);
146 Q_D(QBarCategoriesAxis);
146 if (d->m_categories.isEmpty()) {
147 if (d->m_categories.isEmpty()) {
147 d->m_categories.append(category);
148 d->m_categories.append(category);
148 setRange(category,category);
149 setRange(category,category);
149 }else{
150 }else{
150 d->m_categories.append(category);
151 d->m_categories.append(category);
151 }
152 }
152 emit d->updated();
153 emit d->updated();
153 emit categoriesChanged();
154 emit categoriesChanged();
154 }
155 }
155
156
156 /*!
157 /*!
157 Removes \a category from axis
158 Removes \a category from axis
158 */
159 */
159 void QBarCategoriesAxis::remove(const QString &category)
160 void QBarCategoriesAxis::remove(const QString &category)
160 {
161 {
161 Q_D(QBarCategoriesAxis);
162 Q_D(QBarCategoriesAxis);
162 if (d->m_categories.contains(category)) {
163 if (d->m_categories.contains(category)) {
163 d->m_categories.removeAt(d->m_categories.indexOf(category));
164 d->m_categories.removeAt(d->m_categories.indexOf(category));
164 setRange(d->m_categories.first(),d->m_categories.last());
165 setRange(d->m_categories.first(),d->m_categories.last());
165 emit d->updated();
166 emit d->updated();
166 emit categoriesChanged();
167 emit categoriesChanged();
167 }
168 }
168 }
169 }
169
170
170 /*!
171 /*!
171 Inserts \a category to axis at \a index
172 Inserts \a category to axis at \a index
172 */
173 */
173 void QBarCategoriesAxis::insert(int index, const QString &category)
174 void QBarCategoriesAxis::insert(int index, const QString &category)
174 {
175 {
175 Q_D(QBarCategoriesAxis);
176 Q_D(QBarCategoriesAxis);
176 if (d->m_categories.isEmpty()) {
177 if (d->m_categories.isEmpty()) {
177 d->m_categories.insert(index,category);
178 d->m_categories.insert(index,category);
178 setRange(category,category);
179 setRange(category,category);
179 }else{
180 }else{
180 d->m_categories.insert(index,category);
181 d->m_categories.insert(index,category);
181 }
182 }
182 emit d->updated();
183 emit d->updated();
183 emit categoriesChanged();
184 emit categoriesChanged();
184 }
185 }
185
186
186 /*!
187 /*!
187 Removes all categories.
188 Removes all categories.
188 */
189 */
189 void QBarCategoriesAxis::clear()
190 void QBarCategoriesAxis::clear()
190 {
191 {
191 Q_D(QBarCategoriesAxis);
192 Q_D(QBarCategoriesAxis);
192 d->m_categories.clear();
193 d->m_categories.clear();
193 setRange(QString::null,QString::null);
194 setRange(QString::null,QString::null);
194 emit d->updated();
195 emit d->updated();
195 emit categoriesChanged();
196 emit categoriesChanged();
196 }
197 }
197
198
198 void QBarCategoriesAxis::setCategories(const QStringList &categories)
199 void QBarCategoriesAxis::setCategories(const QStringList &categories)
199 {
200 {
200 Q_D(QBarCategoriesAxis);
201 Q_D(QBarCategoriesAxis);
201 if(d->m_categories!=categories){
202 if(d->m_categories!=categories){
202 d->m_categories = categories;
203 d->m_categories = categories;
203 setRange(categories.first(),categories.last());
204 setRange(categories.first(),categories.last());
204 emit d->updated();
205 emit d->updated();
205 emit categoriesChanged();
206 emit categoriesChanged();
206 }
207 }
207 }
208 }
208
209
209 QStringList QBarCategoriesAxis::categories()
210 QStringList QBarCategoriesAxis::categories()
210 {
211 {
211 Q_D(QBarCategoriesAxis);
212 Q_D(QBarCategoriesAxis);
212 return d->m_categories;
213 return d->m_categories;
213 }
214 }
214
215
215 /*!
216 /*!
216 Returns number of categories.
217 Returns number of categories.
217 */
218 */
218 int QBarCategoriesAxis::count() const
219 int QBarCategoriesAxis::count() const
219 {
220 {
220 Q_D(const QBarCategoriesAxis);
221 Q_D(const QBarCategoriesAxis);
221 return d->m_categories.count();
222 return d->m_categories.count();
222 }
223 }
223
224
224 /*!
225 /*!
225 Returns category at \a index. Index must be valid.
226 Returns category at \a index. Index must be valid.
226 */
227 */
227 QString QBarCategoriesAxis::at(int index) const
228 QString QBarCategoriesAxis::at(int index) const
228 {
229 {
229 Q_D(const QBarCategoriesAxis);
230 Q_D(const QBarCategoriesAxis);
230 return d->m_categories.at(index);
231 return d->m_categories.at(index);
231 }
232 }
232
233
233 /*!
234 /*!
234 Sets minimum category to \a min.
235 Sets minimum category to \a min.
235 */
236 */
236 void QBarCategoriesAxis::setMin(const QString& min)
237 void QBarCategoriesAxis::setMin(const QString& min)
237 {
238 {
238 Q_D(QBarCategoriesAxis);
239 Q_D(QBarCategoriesAxis);
239 setRange(min,d->m_maxCategory);
240 setRange(min,d->m_maxCategory);
240 }
241 }
241
242
242 /*!
243 /*!
243 Returns minimum category.
244 Returns minimum category.
244 */
245 */
245 QString QBarCategoriesAxis::min() const
246 QString QBarCategoriesAxis::min() const
246 {
247 {
247 Q_D(const QBarCategoriesAxis);
248 Q_D(const QBarCategoriesAxis);
248 return d->m_minCategory;
249 return d->m_minCategory;
249 }
250 }
250
251
251 /*!
252 /*!
252 Sets maximum category to \a max.
253 Sets maximum category to \a max.
253 */
254 */
254 void QBarCategoriesAxis::setMax(const QString& max)
255 void QBarCategoriesAxis::setMax(const QString& max)
255 {
256 {
256 Q_D(QBarCategoriesAxis);
257 Q_D(QBarCategoriesAxis);
257 setRange(d->m_minCategory,max);
258 setRange(d->m_minCategory,max);
258 }
259 }
259
260
260 /*!
261 /*!
261 Returns maximum category
262 Returns maximum category
262 */
263 */
263 QString QBarCategoriesAxis::max() const
264 QString QBarCategoriesAxis::max() const
264 {
265 {
265 Q_D(const QBarCategoriesAxis);
266 Q_D(const QBarCategoriesAxis);
266 return d->m_maxCategory;
267 return d->m_maxCategory;
267 }
268 }
268
269
269 /*!
270 /*!
270 Sets range from \a minCategory to \a maxCategory
271 Sets range from \a minCategory to \a maxCategory
271 */
272 */
272 void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
273 void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
273 {
274 {
274 Q_D(QBarCategoriesAxis);
275 Q_D(QBarCategoriesAxis);
275
276
276 int minIndex = d->m_categories.indexOf(minCategory);
277 int minIndex = d->m_categories.indexOf(minCategory);
277 if (minIndex == -1) {
278 if (minIndex == -1) {
278 return;
279 return;
279 }
280 }
280 int maxIndex = d->m_categories.indexOf(maxCategory);
281 int maxIndex = d->m_categories.indexOf(maxCategory);
281 if (maxIndex == -1) {
282 if (maxIndex == -1) {
282 return;
283 return;
283 }
284 }
284
285
285 if (maxIndex <= minIndex) {
286 if (maxIndex <= minIndex) {
286 // max must be greater than min
287 // max must be greater than min
287 return;
288 return;
288 }
289 }
289
290
290 bool changed = false;
291 bool changed = false;
291 if (!qFuzzyIsNull(d->m_min - (minIndex))||d->m_minCategory!=minCategory) {
292 if (!qFuzzyIsNull(d->m_min - (minIndex))||d->m_minCategory!=minCategory) {
292 d->m_minCategory = minCategory;
293 d->m_minCategory = minCategory;
293 d->m_min = minIndex;
294 d->m_min = minIndex;
294 emit minChanged(minCategory);
295 emit minChanged(minCategory);
295 changed = true;
296 changed = true;
296 }
297 }
297
298
298 if (!qFuzzyIsNull(d->m_max - (maxIndex))||d->m_maxCategory!=maxCategory ) {
299 if (!qFuzzyIsNull(d->m_max - (maxIndex))||d->m_maxCategory!=maxCategory ) {
299 d->m_max = maxIndex;
300 d->m_max = maxIndex;
300 d->m_maxCategory = maxCategory;
301 d->m_maxCategory = maxCategory;
301 emit maxChanged(maxCategory);
302 emit maxChanged(maxCategory);
302 changed = true;
303 changed = true;
303 }
304 }
304
305
305 if (changed) {
306 if (changed) {
306 d->emitRange();
307 d->emitRange();
307 }
308 }
308 }
309 }
309
310
310 /*!
311 /*!
311 Returns the type of the axis
312 Returns the type of the axis
312 */
313 */
313 QAbstractAxis::AxisType QBarCategoriesAxis::type() const
314 QAbstractAxis::AxisType QBarCategoriesAxis::type() const
314 {
315 {
315 return AxisTypeCategories;
316 return AxisTypeCategories;
316 }
317 }
317
318
318 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
319 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
319
320
320 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
321 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
321 QAbstractAxisPrivate(q)
322 QAbstractAxisPrivate(q)
322 {
323 {
323
324
324 }
325 }
325
326
326 QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate()
327 QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate()
327 {
328 {
328
329
329 }
330 }
330
331
331 void QBarCategoriesAxisPrivate::setMin(const QVariant &min)
332 void QBarCategoriesAxisPrivate::setMin(const QVariant &min)
332 {
333 {
333 setRange(min,m_maxCategory);
334 setRange(min,m_maxCategory);
334 }
335 }
335
336
336 void QBarCategoriesAxisPrivate::setMax(const QVariant &max)
337 void QBarCategoriesAxisPrivate::setMax(const QVariant &max)
337 {
338 {
338 setRange(m_minCategory,max);
339 setRange(m_minCategory,max);
339 }
340 }
340
341
341 void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
342 void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
342 {
343 {
343 Q_Q(QBarCategoriesAxis);
344 Q_Q(QBarCategoriesAxis);
344 QString value1 = min.toString();
345 QString value1 = min.toString();
345 QString value2 = max.toString();
346 QString value2 = max.toString();
346 q->setRange(value1,value2);
347 q->setRange(value1,value2);
347 }
348 }
348
349
349 int QBarCategoriesAxisPrivate::ticksCount() const
350 int QBarCategoriesAxisPrivate::ticksCount() const
350 {
351 {
351 return m_categories.count()+1;
352 return m_categories.count()+1;
352 }
353 }
353
354
354 void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
355 void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
355 {
356 {
356 m_min = min;
357 m_min = min;
357 m_max = max;
358 m_max = max;
358 m_ticksCount = count;
359 m_ticksCount = count;
359 }
360 }
360
361
361 ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
362 ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
362 {
363 {
363 Q_Q( QBarCategoriesAxis);
364 Q_Q( QBarCategoriesAxis);
364 if(m_orientation == Qt::Vertical){
365 if(m_orientation == Qt::Vertical){
365 return new ChartCategoriesAxisY(q,presenter);
366 return new ChartCategoriesAxisY(q,presenter);
366 }else{
367 }else{
367 return new ChartCategoriesAxisX(q,presenter);
368 return new ChartCategoriesAxisX(q,presenter);
368 }
369 }
369 }
370 }
370
371
371 void QBarCategoriesAxisPrivate::emitRange()
372 void QBarCategoriesAxisPrivate::emitRange()
372 {
373 {
373 Q_Q( QBarCategoriesAxis);
374 Q_Q( QBarCategoriesAxis);
374 if(!q->signalsBlocked()) {
375 if(!q->signalsBlocked()) {
375 emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false);
376 emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false);
376 }
377 }
377 }
378 }
378
379
379
380
380 #include "moc_qbarcategoriesaxis.cpp"
381 #include "moc_qbarcategoriesaxis.cpp"
381 #include "moc_qbarcategoriesaxis_p.cpp"
382 #include "moc_qbarcategoriesaxis_p.cpp"
382
383
383 QTCOMMERCIALCHART_END_NAMESPACE
384 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now