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