##// END OF EJS Templates
Improved qmlchart demo and CategoryAxis documentation
Tero Ahola -
r2000:eb59a57300e4
parent child
Show More
@@ -1,39 +1,118
1 /*!
1 /*!
2 \example demos/qmlchart
2 \example demos/qmlchart
3 \title Qml charts demo
3 \title Qml charts demo
4 \subtitle
4 \subtitle
5
5
6 This is a basic demonstration how to use the different chart types by using qml. Creating each chart type begins
6 This is a basic demonstration how to use the different chart types by using qml. Creating each chart type begins
7 with creation of a ChartView. For example to create a pie, we use the PieSeries API together with a few PieSlices:
7 with creation of a ChartView.
8 \snippet ../demos/qmlchart/qml/qmlchart/View1.qml 1
9 \image demos_qmlchart1.png
10
8
11 To create a chart with line series:
9 \table
12 \snippet ../demos/qmlchart/qml/qmlchart/View2.qml 1
10 \row
13 \image demos_qmlchart2.png
11 \o \br
12 To create a pie, we use the PieSeries API together with a few PieSlices:
13 \br
14 \br
15 \snippet ../demos/qmlchart/qml/qmlchart/View1.qml 1
16 \o \inlineimage demos_qmlchart1.png
17 \endtable
14
18
15 And spline series:
19 \table
16 \snippet ../demos/qmlchart/qml/qmlchart/View3.qml 1
20 \row
17 \image demos_qmlchart3.png
21 \o \br
22 To create a chart with line series:
23 \br
24 \br
25 \snippet ../demos/qmlchart/qml/qmlchart/View2.qml 1
26 \o \inlineimage demos_qmlchart2.png
27 \endtable
18
28
19 Then we create a chart that illustrates the success of Russian, Swedish and Finnish ice-hockey players in the
29 \table
20 NHL All-Star player selections by using three area series:
30 \row
21 \snippet ../demos/qmlchart/qml/qmlchart/View4.qml 1
31 \o \br
22 \image demos_qmlchart4.png
32 And spline series:
33 \br
34 \br
35 \snippet ../demos/qmlchart/qml/qmlchart/View3.qml 1
36 \o \inlineimage demos_qmlchart3.png
37 \endtable
23
38
24 Then couple of scatter series:
39 \table
25 \snippet ../demos/qmlchart/qml/qmlchart/View5.qml 1
40 \row
26 \image demos_qmlchart5.png
41 \o \br
42 Then we create a chart that illustrates the NHL All-Star player selections by using three area series:
43 \br
44 \br
45 \snippet ../demos/qmlchart/qml/qmlchart/View4.qml 1
46 \o \inlineimage demos_qmlchart4.png
47 \endtable
48
49 \table
50 \row
51 \o \br
52 Then couple of scatter series:
53 \br
54 \br
55 \snippet ../demos/qmlchart/qml/qmlchart/View5.qml 1
56 \o \inlineimage demos_qmlchart5.png
57 \endtable
27
58
28 And a few different bar series:
59 And a few different bar series:
29 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
60 \table
30 \image demos_qmlchart6.png
61 \row
31 \snippet ../demos/qmlchart/qml/qmlchart/View7.qml 1
62 \o \br
32 \image demos_qmlchart7.png
63 \br
33 \snippet ../demos/qmlchart/qml/qmlchart/View8.qml 1
64 \br
34 \image demos_qmlchart8.png
65 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
35
66 \o \inlineimage demos_qmlchart6.png
36 And finally an example how to create a donut chart with two pie series:
67 \endtable
37 \snippet ../demos/qmlchart/qml/qmlchart/View12.qml 1
68 \table
38 \image demos_qmlchart12.png
69 \row
70 \o \br
71 \br
72 \br
73 \snippet ../demos/qmlchart/qml/qmlchart/View7.qml 1
74 \o \inlineimage demos_qmlchart7.png
75 \endtable
76 \table
77 \row
78 \o \br
79 \br
80 \br
81 \snippet ../demos/qmlchart/qml/qmlchart/View8.qml 1
82 \o \inlineimage demos_qmlchart8.png
83 \endtable
84 \table
85 \row
86 \o \br
87 \br
88 \br
89 \snippet ../demos/qmlchart/qml/qmlchart/View9.qml 1
90 \o \inlineimage demos_qmlchart9.png
91 \endtable
92 \table
93 \row
94 \o \br
95 \br
96 \br
97 \snippet ../demos/qmlchart/qml/qmlchart/View10.qml 1
98 \o \inlineimage demos_qmlchart10.png
99 \endtable
100 \table
101 \row
102 \o \br
103 \br
104 \br
105 \snippet ../demos/qmlchart/qml/qmlchart/View11.qml 1
106 \o \inlineimage demos_qmlchart11.png
107 \endtable
108
109 \table
110 \row
111 \o \br
112 And finally an example how to create a donut chart with two pie series:
113 \br
114 \br
115 \snippet ../demos/qmlchart/qml/qmlchart/View12.qml 1
116 \o \inlineimage demos_qmlchart12.png
117 \endtable
39 */
118 */
@@ -1,295 +1,295
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 "qcategoryaxis.h"
21 #include "qcategoryaxis.h"
22 #include "qcategoryaxis_p.h"
22 #include "qcategoryaxis_p.h"
23 #include "chartcategoryaxisx_p.h"
23 #include "chartcategoryaxisx_p.h"
24 #include "chartcategoryaxisy_p.h"
24 #include "chartcategoryaxisy_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 QCategoryAxis
30 \class QCategoryAxis
31 \brief The QCategoryAxis class allows putting a named ranges on the axis.
31 \brief The QCategoryAxis class allows putting a named ranges on the axis.
32 \mainclass
32 \mainclass
33
33
34 This class can be used when the underlying data needs to be given extra meaning.
34 This class can be used when the underlying data needs to be given extra meaning.
35 Unlike with the QBarCategoryAxis the QCategoryAxis allows the categories ranges widths to be specified freely.
35 Unlike with the QBarCategoryAxis the QCategoryAxis allows the categories ranges widths to be specified freely.
36
36
37 Example code on how to use QCategoryAxis.
37 Example code on how to use QCategoryAxis.
38 \code
38 \code
39 QChartView *chartView = new QChartView;
39 QChartView *chartView = new QChartView;
40 QLineSeries *series = new QLineSeries;
40 QLineSeries *series = new QLineSeries;
41 // ...
41 // ...
42 chartView->chart()->addSeries(series);
42 chartView->chart()->addSeries(series);
43
43
44 QCategoryAxis *axisX = new QCategoryAxis;
44 QCategoryAxis *axisX = new QCategoryAxis;
45 axisX->setStartValue(15);
45 axisX->setStartValue(15);
46 axisX->append("First", 20);
46 axisX->append("First", 20);
47 axisX->append("Second", 37);
47 axisX->append("Second", 37);
48 axisX->append("Third", 52);
48 axisX->append("Third", 52);
49 chartView->chart()->setAxisX(series, axisX);
49 chartView->chart()->setAxisX(series, axisX);
50 \endcode
50 \endcode
51 */
51 */
52 /*!
52 /*!
53 \qmlclass CategoryAxis QCategoryAxis
53 \qmlclass CategoryAxis QCategoryAxis
54 \inherits AbstractAxis
54 \inherits AbstractAxis
55 \brief CategoryAxis allows putting a named ranges on the axis.
55 \brief CategoryAxis allows putting a named ranges on the axis.
56
56
57 For example:
57 For example:
58 \code
58 \table
59 CategoryAxis {
59 \row
60 startValue: 0.0
60 \o \br
61 CategoryRange { endValue: 1.0; label: "min (0-1)" }
61 \br
62 CategoryRange { endValue: 3.0; label: "standard (1-3)" }
62 \br
63 CategoryRange { endValue: 4.0; label: "high (3-4)" }
63 \snippet ../demos/qmlaxes/qml/qmlaxes/View3.qml 1
64 }
64 \o \inlineimage demos_qmlaxes3.png
65 \endcode
65 \endtable
66 */
66 */
67
67
68 /*!
68 /*!
69 \property QCategoryAxis::startValue
69 \property QCategoryAxis::startValue
70 Defines the low end of the first category on the axis.
70 Defines the low end of the first category on the axis.
71 */
71 */
72 /*!
72 /*!
73 \qmlproperty int CategoryAxis::startValue
73 \qmlproperty int CategoryAxis::startValue
74 Defines the low end of the first category on the axis.
74 Defines the low end of the first category on the axis.
75 */
75 */
76
76
77 /*!
77 /*!
78 Constructs an axis object which is a child of \a parent.
78 Constructs an axis object which is a child of \a parent.
79 */
79 */
80 QCategoryAxis::QCategoryAxis(QObject *parent):
80 QCategoryAxis::QCategoryAxis(QObject *parent):
81 QValueAxis(*new QCategoryAxisPrivate(this),parent)
81 QValueAxis(*new QCategoryAxisPrivate(this),parent)
82 {
82 {
83 }
83 }
84
84
85 /*!
85 /*!
86 Destroys the object
86 Destroys the object
87 */
87 */
88 QCategoryAxis::~QCategoryAxis()
88 QCategoryAxis::~QCategoryAxis()
89 {
89 {
90 }
90 }
91
91
92 /*!
92 /*!
93 \internal
93 \internal
94 */
94 */
95 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent):QValueAxis(d,parent)
95 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent):QValueAxis(d,parent)
96 {
96 {
97
97
98 }
98 }
99
99
100 /*!
100 /*!
101 \qmlmethod CategoryAxis::append(string label, real endValue)
101 \qmlmethod CategoryAxis::append(string label, real endValue)
102 Appends new category to the axis with an \a label. Category label has to be unique.
102 Appends new category to the axis with an \a label. Category label has to be unique.
103 Parameter \a endValue specifies the high end limit of the category.
103 Parameter \a endValue specifies the high end limit of the category.
104 It has to be greater than the high end limit of the previous category.
104 It has to be greater than the high end limit of the previous category.
105 Otherwise the method returns without adding a new category.
105 Otherwise the method returns without adding a new category.
106 */
106 */
107 /*!
107 /*!
108 Appends new category to the axis with an \a categoryLabel.
108 Appends new category to the axis with an \a categoryLabel.
109 Category label has to be unique.
109 Category label has to be unique.
110 Parameter \a categoryEndValue specifies the high end limit of the category.
110 Parameter \a categoryEndValue specifies the high end limit of the category.
111 It has to be greater than the high end limit of the previous category.
111 It has to be greater than the high end limit of the previous category.
112 Otherwise the method returns without adding a new category.
112 Otherwise the method returns without adding a new category.
113 */
113 */
114 void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue)
114 void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue)
115 {
115 {
116 Q_D(QCategoryAxis);
116 Q_D(QCategoryAxis);
117
117
118 if (!d->m_categories.contains(categoryLabel))
118 if (!d->m_categories.contains(categoryLabel))
119 {
119 {
120 if(d->m_categories.isEmpty()){
120 if(d->m_categories.isEmpty()){
121 Range range(d->m_categoryMinimum, categoryEndValue);
121 Range range(d->m_categoryMinimum, categoryEndValue);
122 d->m_categoriesMap.insert(categoryLabel, range);
122 d->m_categoriesMap.insert(categoryLabel, range);
123 d->m_categories.append(categoryLabel);
123 d->m_categories.append(categoryLabel);
124 }else if (categoryEndValue > endValue(d->m_categories.last())){
124 }else if (categoryEndValue > endValue(d->m_categories.last())){
125 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
125 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
126 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
126 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
127 d->m_categories.append(categoryLabel);
127 d->m_categories.append(categoryLabel);
128 }
128 }
129 }
129 }
130 }
130 }
131
131
132 /*!
132 /*!
133 Sets \a min to be the low end limit of the first category on the axis.
133 Sets \a min to be the low end limit of the first category on the axis.
134 If there is already some categories added to the axis then passed value must be lower than the high end value of the already defined first category range.
134 If there is already some categories added to the axis then passed value must be lower than the high end value of the already defined first category range.
135 Otherwise nothing is done.
135 Otherwise nothing is done.
136 */
136 */
137 void QCategoryAxis::setStartValue(qreal min)
137 void QCategoryAxis::setStartValue(qreal min)
138 {
138 {
139 Q_D(QCategoryAxis);
139 Q_D(QCategoryAxis);
140 if(d->m_categories.isEmpty()){
140 if(d->m_categories.isEmpty()){
141 d->m_categoryMinimum = min;
141 d->m_categoryMinimum = min;
142 }else{
142 }else{
143 Range range = d->m_categoriesMap.value(d->m_categories.first());
143 Range range = d->m_categoriesMap.value(d->m_categories.first());
144 if (min < range.second)
144 if (min < range.second)
145 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
145 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
146 }
146 }
147 }
147 }
148
148
149 /*!
149 /*!
150 Returns the low end limit of the category specified by an \a categoryLabel
150 Returns the low end limit of the category specified by an \a categoryLabel
151 */
151 */
152 qreal QCategoryAxis::startValue(const QString& categoryLabel) const
152 qreal QCategoryAxis::startValue(const QString& categoryLabel) const
153 {
153 {
154 Q_D(const QCategoryAxis);
154 Q_D(const QCategoryAxis);
155 if (categoryLabel.isEmpty())
155 if (categoryLabel.isEmpty())
156 return d->m_categoryMinimum;
156 return d->m_categoryMinimum;
157 else
157 else
158 return d->m_categoriesMap.value(categoryLabel).first;
158 return d->m_categoriesMap.value(categoryLabel).first;
159 }
159 }
160
160
161 /*!
161 /*!
162 Returns the high end limit of the interval specified by an \a categoryLabel
162 Returns the high end limit of the interval specified by an \a categoryLabel
163 */
163 */
164 qreal QCategoryAxis::endValue(const QString& categoryLabel) const
164 qreal QCategoryAxis::endValue(const QString& categoryLabel) const
165 {
165 {
166 Q_D(const QCategoryAxis);
166 Q_D(const QCategoryAxis);
167 return d->m_categoriesMap.value(categoryLabel).second;
167 return d->m_categoriesMap.value(categoryLabel).second;
168 }
168 }
169
169
170 /*!
170 /*!
171 \qmlmethod CategoryAxis::remove(string label)
171 \qmlmethod CategoryAxis::remove(string label)
172 Removes a category specified by the \a label from the axis
172 Removes a category specified by the \a label from the axis
173 */
173 */
174 /*!
174 /*!
175 Removes an interval specified by the \a categoryLabel from the axis
175 Removes an interval specified by the \a categoryLabel from the axis
176 */
176 */
177 void QCategoryAxis::remove(const QString &categoryLabel)
177 void QCategoryAxis::remove(const QString &categoryLabel)
178 {
178 {
179 Q_D(QCategoryAxis);
179 Q_D(QCategoryAxis);
180 int labelIndex = d->m_categories.indexOf(categoryLabel);
180 int labelIndex = d->m_categories.indexOf(categoryLabel);
181
181
182 // check if such label exists
182 // check if such label exists
183 if (labelIndex != -1) {
183 if (labelIndex != -1) {
184 d->m_categories.removeAt(labelIndex);
184 d->m_categories.removeAt(labelIndex);
185 d->m_categoriesMap.remove(categoryLabel);
185 d->m_categoriesMap.remove(categoryLabel);
186
186
187 // the range of the interval that follows (if exists) needs to be updated
187 // the range of the interval that follows (if exists) needs to be updated
188 if (labelIndex < d->m_categories.count()) {
188 if (labelIndex < d->m_categories.count()) {
189 QString label = d->m_categories.at(labelIndex);
189 QString label = d->m_categories.at(labelIndex);
190 Range range = d->m_categoriesMap.value(label);
190 Range range = d->m_categoriesMap.value(label);
191
191
192 // set the range
192 // set the range
193 if (labelIndex == 0) {
193 if (labelIndex == 0) {
194 range.first = d->m_categoryMinimum;
194 range.first = d->m_categoryMinimum;
195 d->m_categoriesMap.insert(label, range);
195 d->m_categoriesMap.insert(label, range);
196 } else {
196 } else {
197 range.first = d->m_categoriesMap.value(d->m_categories.at(labelIndex - 1)).second;
197 range.first = d->m_categoriesMap.value(d->m_categories.at(labelIndex - 1)).second;
198 d->m_categoriesMap.insert(label, range);
198 d->m_categoriesMap.insert(label, range);
199 }
199 }
200 }
200 }
201 d->emitUpdated();
201 d->emitUpdated();
202 }
202 }
203 }
203 }
204
204
205 /*!
205 /*!
206 \qmlmethod CategoryAxis::replace(string oldLabel, string newLabel)
206 \qmlmethod CategoryAxis::replace(string oldLabel, string newLabel)
207 Replaces \a oldLabel of an existing category with a \a newLabel.
207 Replaces \a oldLabel of an existing category with a \a newLabel.
208 If the old label does not exist the method returns without making any changes.
208 If the old label does not exist the method returns without making any changes.
209 */
209 */
210 /*!
210 /*!
211 Replaces \a oldLabel of an existing category with a \a newLabel
211 Replaces \a oldLabel of an existing category with a \a newLabel
212 If the old label does not exist the method returns without making any changes.
212 If the old label does not exist the method returns without making any changes.
213 */
213 */
214 void QCategoryAxis::replaceLabel(const QString& oldLabel, const QString& newLabel)
214 void QCategoryAxis::replaceLabel(const QString& oldLabel, const QString& newLabel)
215 {
215 {
216 Q_D(QCategoryAxis);
216 Q_D(QCategoryAxis);
217 int labelIndex = d->m_categories.indexOf(oldLabel);
217 int labelIndex = d->m_categories.indexOf(oldLabel);
218
218
219 // check if such label exists
219 // check if such label exists
220 if (labelIndex != -1) {
220 if (labelIndex != -1) {
221 d->m_categories.replace(labelIndex, newLabel);
221 d->m_categories.replace(labelIndex, newLabel);
222 Range range = d->m_categoriesMap.value(oldLabel);
222 Range range = d->m_categoriesMap.value(oldLabel);
223 d->m_categoriesMap.remove(oldLabel);
223 d->m_categoriesMap.remove(oldLabel);
224 d->m_categoriesMap.insert(newLabel, range);
224 d->m_categoriesMap.insert(newLabel, range);
225 d->emitUpdated();
225 d->emitUpdated();
226 }
226 }
227
227
228 }
228 }
229
229
230 /*!
230 /*!
231 Returns the list of the intervals labels
231 Returns the list of the intervals labels
232 */
232 */
233 QStringList QCategoryAxis::categoriesLabels()
233 QStringList QCategoryAxis::categoriesLabels()
234 {
234 {
235 Q_D(QCategoryAxis);
235 Q_D(QCategoryAxis);
236 return d->m_categories;
236 return d->m_categories;
237 }
237 }
238
238
239 /*!
239 /*!
240 Returns number of intervals.
240 Returns number of intervals.
241 */
241 */
242 int QCategoryAxis::count() const
242 int QCategoryAxis::count() const
243 {
243 {
244 Q_D(const QCategoryAxis);
244 Q_D(const QCategoryAxis);
245 return d->m_categories.count();
245 return d->m_categories.count();
246 }
246 }
247
247
248 /*!
248 /*!
249 Returns the type of the axis
249 Returns the type of the axis
250 */
250 */
251 QAbstractAxis::AxisType QCategoryAxis::type() const
251 QAbstractAxis::AxisType QCategoryAxis::type() const
252 {
252 {
253 return QAbstractAxis::AxisTypeCategory;
253 return QAbstractAxis::AxisTypeCategory;
254 }
254 }
255
255
256 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
256 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
257
257
258 QCategoryAxisPrivate::QCategoryAxisPrivate(QCategoryAxis* q):
258 QCategoryAxisPrivate::QCategoryAxisPrivate(QCategoryAxis* q):
259 QValueAxisPrivate(q),
259 QValueAxisPrivate(q),
260 m_categoryMinimum(0)
260 m_categoryMinimum(0)
261 {
261 {
262
262
263 }
263 }
264
264
265 QCategoryAxisPrivate::~QCategoryAxisPrivate()
265 QCategoryAxisPrivate::~QCategoryAxisPrivate()
266 {
266 {
267
267
268 }
268 }
269
269
270 int QCategoryAxisPrivate::ticksCount() const
270 int QCategoryAxisPrivate::ticksCount() const
271 {
271 {
272 return m_categories.count() + 1;
272 return m_categories.count() + 1;
273 }
273 }
274
274
275 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
275 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
276 {
276 {
277 Q_UNUSED(count);
277 Q_UNUSED(count);
278 Q_UNUSED(min);
278 Q_UNUSED(min);
279 Q_UNUSED(max);
279 Q_UNUSED(max);
280 }
280 }
281
281
282 ChartAxis* QCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
282 ChartAxis* QCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
283 {
283 {
284 Q_Q(QCategoryAxis);
284 Q_Q(QCategoryAxis);
285 if(m_orientation == Qt::Vertical){
285 if(m_orientation == Qt::Vertical){
286 return new ChartCategoryAxisY(q,presenter);
286 return new ChartCategoryAxisY(q,presenter);
287 }else{
287 }else{
288 return new ChartCategoryAxisX(q,presenter);
288 return new ChartCategoryAxisX(q,presenter);
289 }
289 }
290 }
290 }
291
291
292 #include "moc_qcategoryaxis.cpp"
292 #include "moc_qcategoryaxis.cpp"
293 #include "moc_qcategoryaxis_p.cpp"
293 #include "moc_qcategoryaxis_p.cpp"
294
294
295 QTCOMMERCIALCHART_END_NAMESPACE
295 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now