##// END OF EJS Templates
Added a screen shot to QCategoryAxis documentation
Tero Ahola -
r2220:d8b9a117ffd7
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,290 +1,299
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 \table
39 QChartView *chartView = new QChartView;
39 \row
40 QLineSeries *series = new QLineSeries;
40 \o \br
41 // ...
41 \br
42 chartView->chart()->addSeries(series);
42 \code
43
43 QChartView *chartView = new QChartView;
44 QCategoryAxis *axisX = new QCategoryAxis;
44 QLineSeries *series = new QLineSeries;
45 axisX->setStartValue(15);
45 // ...
46 axisX->append("First", 20);
46 chartView->chart()->addSeries(series);
47 axisX->append("Second", 37);
47
48 axisX->append("Third", 52);
48 QCategoryAxis *axisY = new QCategoryAxis;
49 chartView->chart()->setAxisX(axisX, series);
49 axisY->setMin(0);
50 \endcode
50 axisY->setMax(52);
51 axisY->setStartValue(15);
52 axisY->append("First", 20);
53 axisY->append("Second", 37);
54 axisY->append("Third", 52);
55 chartView->chart()->setAxisY(axisY, series);
56 \endcode
57 \o \br
58 \inlineimage api_category_axis.png
59 \endtable
51 */
60 */
52 /*!
61 /*!
53 \qmlclass CategoryAxis QCategoryAxis
62 \qmlclass CategoryAxis QCategoryAxis
54 \inherits AbstractAxis
63 \inherits AbstractAxis
55 \brief CategoryAxis allows putting a named ranges on the axis.
64 \brief CategoryAxis allows putting a named ranges on the axis.
56
65
57 For example:
66 For example:
58 \table
67 \table
59 \row
68 \row
60 \o \br
69 \o \br
61 \br
70 \br
62 \br
71 \br
63 \snippet ../demos/qmlaxes/qml/qmlaxes/View3.qml 1
72 \snippet ../demos/qmlaxes/qml/qmlaxes/View3.qml 1
64 \o \inlineimage demos_qmlaxes3.png
73 \o \inlineimage demos_qmlaxes3.png
65 \endtable
74 \endtable
66 */
75 */
67
76
68 /*!
77 /*!
69 \property QCategoryAxis::startValue
78 \property QCategoryAxis::startValue
70 Defines the low end of the first category on the axis.
79 Defines the low end of the first category on the axis.
71 */
80 */
72 /*!
81 /*!
73 \qmlproperty int CategoryAxis::startValue
82 \qmlproperty int CategoryAxis::startValue
74 Defines the low end of the first category on the axis.
83 Defines the low end of the first category on the axis.
75 */
84 */
76
85
77 /*!
86 /*!
78 Constructs an axis object which is a child of \a parent.
87 Constructs an axis object which is a child of \a parent.
79 */
88 */
80 QCategoryAxis::QCategoryAxis(QObject *parent):
89 QCategoryAxis::QCategoryAxis(QObject *parent):
81 QValueAxis(*new QCategoryAxisPrivate(this), parent)
90 QValueAxis(*new QCategoryAxisPrivate(this), parent)
82 {
91 {
83 }
92 }
84
93
85 /*!
94 /*!
86 Destroys the object
95 Destroys the object
87 */
96 */
88 QCategoryAxis::~QCategoryAxis()
97 QCategoryAxis::~QCategoryAxis()
89 {
98 {
90 }
99 }
91
100
92 /*!
101 /*!
93 \internal
102 \internal
94 */
103 */
95 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d, QObject *parent): QValueAxis(d, parent)
104 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d, QObject *parent): QValueAxis(d, parent)
96 {
105 {
97
106
98 }
107 }
99
108
100 /*!
109 /*!
101 \qmlmethod CategoryAxis::append(string label, real endValue)
110 \qmlmethod CategoryAxis::append(string label, real endValue)
102 Appends new category to the axis with an \a label. Category label has to be unique.
111 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.
112 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.
113 It has to be greater than the high end limit of the previous category.
105 Otherwise the method returns without adding a new category.
114 Otherwise the method returns without adding a new category.
106 */
115 */
107 /*!
116 /*!
108 Appends new category to the axis with an \a categoryLabel.
117 Appends new category to the axis with an \a categoryLabel.
109 Category label has to be unique.
118 Category label has to be unique.
110 Parameter \a categoryEndValue specifies the high end limit of the category.
119 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.
120 It has to be greater than the high end limit of the previous category.
112 Otherwise the method returns without adding a new category.
121 Otherwise the method returns without adding a new category.
113 */
122 */
114 void QCategoryAxis::append(const QString &categoryLabel, qreal categoryEndValue)
123 void QCategoryAxis::append(const QString &categoryLabel, qreal categoryEndValue)
115 {
124 {
116 Q_D(QCategoryAxis);
125 Q_D(QCategoryAxis);
117
126
118 if (!d->m_categories.contains(categoryLabel)) {
127 if (!d->m_categories.contains(categoryLabel)) {
119 if (d->m_categories.isEmpty()) {
128 if (d->m_categories.isEmpty()) {
120 Range range(d->m_categoryMinimum, categoryEndValue);
129 Range range(d->m_categoryMinimum, categoryEndValue);
121 d->m_categoriesMap.insert(categoryLabel, range);
130 d->m_categoriesMap.insert(categoryLabel, range);
122 d->m_categories.append(categoryLabel);
131 d->m_categories.append(categoryLabel);
123 } else if (categoryEndValue > endValue(d->m_categories.last())) {
132 } else if (categoryEndValue > endValue(d->m_categories.last())) {
124 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
133 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
125 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
134 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
126 d->m_categories.append(categoryLabel);
135 d->m_categories.append(categoryLabel);
127 }
136 }
128 }
137 }
129 }
138 }
130
139
131 /*!
140 /*!
132 Sets \a min to be the low end limit of the first category on the axis.
141 Sets \a min to be the low end limit of the first category on the axis.
133 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.
142 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 Otherwise nothing is done.
143 Otherwise nothing is done.
135 */
144 */
136 void QCategoryAxis::setStartValue(qreal min)
145 void QCategoryAxis::setStartValue(qreal min)
137 {
146 {
138 Q_D(QCategoryAxis);
147 Q_D(QCategoryAxis);
139 if (d->m_categories.isEmpty()) {
148 if (d->m_categories.isEmpty()) {
140 d->m_categoryMinimum = min;
149 d->m_categoryMinimum = min;
141 } else {
150 } else {
142 Range range = d->m_categoriesMap.value(d->m_categories.first());
151 Range range = d->m_categoriesMap.value(d->m_categories.first());
143 if (min < range.second)
152 if (min < range.second)
144 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
153 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
145 }
154 }
146 }
155 }
147
156
148 /*!
157 /*!
149 Returns the low end limit of the category specified by an \a categoryLabel
158 Returns the low end limit of the category specified by an \a categoryLabel
150 */
159 */
151 qreal QCategoryAxis::startValue(const QString &categoryLabel) const
160 qreal QCategoryAxis::startValue(const QString &categoryLabel) const
152 {
161 {
153 Q_D(const QCategoryAxis);
162 Q_D(const QCategoryAxis);
154 if (categoryLabel.isEmpty())
163 if (categoryLabel.isEmpty())
155 return d->m_categoryMinimum;
164 return d->m_categoryMinimum;
156 return d->m_categoriesMap.value(categoryLabel).first;
165 return d->m_categoriesMap.value(categoryLabel).first;
157 }
166 }
158
167
159 /*!
168 /*!
160 Returns the high end limit of the interval specified by an \a categoryLabel
169 Returns the high end limit of the interval specified by an \a categoryLabel
161 */
170 */
162 qreal QCategoryAxis::endValue(const QString &categoryLabel) const
171 qreal QCategoryAxis::endValue(const QString &categoryLabel) const
163 {
172 {
164 Q_D(const QCategoryAxis);
173 Q_D(const QCategoryAxis);
165 return d->m_categoriesMap.value(categoryLabel).second;
174 return d->m_categoriesMap.value(categoryLabel).second;
166 }
175 }
167
176
168 /*!
177 /*!
169 \qmlmethod CategoryAxis::remove(string label)
178 \qmlmethod CategoryAxis::remove(string label)
170 Removes a category specified by the \a label from the axis
179 Removes a category specified by the \a label from the axis
171 */
180 */
172 /*!
181 /*!
173 Removes an interval specified by the \a categoryLabel from the axis
182 Removes an interval specified by the \a categoryLabel from the axis
174 */
183 */
175 void QCategoryAxis::remove(const QString &categoryLabel)
184 void QCategoryAxis::remove(const QString &categoryLabel)
176 {
185 {
177 Q_D(QCategoryAxis);
186 Q_D(QCategoryAxis);
178 int labelIndex = d->m_categories.indexOf(categoryLabel);
187 int labelIndex = d->m_categories.indexOf(categoryLabel);
179
188
180 // check if such label exists
189 // check if such label exists
181 if (labelIndex != -1) {
190 if (labelIndex != -1) {
182 d->m_categories.removeAt(labelIndex);
191 d->m_categories.removeAt(labelIndex);
183 d->m_categoriesMap.remove(categoryLabel);
192 d->m_categoriesMap.remove(categoryLabel);
184
193
185 // the range of the interval that follows (if exists) needs to be updated
194 // the range of the interval that follows (if exists) needs to be updated
186 if (labelIndex < d->m_categories.count()) {
195 if (labelIndex < d->m_categories.count()) {
187 QString label = d->m_categories.at(labelIndex);
196 QString label = d->m_categories.at(labelIndex);
188 Range range = d->m_categoriesMap.value(label);
197 Range range = d->m_categoriesMap.value(label);
189
198
190 // set the range
199 // set the range
191 if (labelIndex == 0) {
200 if (labelIndex == 0) {
192 range.first = d->m_categoryMinimum;
201 range.first = d->m_categoryMinimum;
193 d->m_categoriesMap.insert(label, range);
202 d->m_categoriesMap.insert(label, range);
194 } else {
203 } else {
195 range.first = d->m_categoriesMap.value(d->m_categories.at(labelIndex - 1)).second;
204 range.first = d->m_categoriesMap.value(d->m_categories.at(labelIndex - 1)).second;
196 d->m_categoriesMap.insert(label, range);
205 d->m_categoriesMap.insert(label, range);
197 }
206 }
198 }
207 }
199 d->emitUpdated();
208 d->emitUpdated();
200 }
209 }
201 }
210 }
202
211
203 /*!
212 /*!
204 \qmlmethod CategoryAxis::replace(string oldLabel, string newLabel)
213 \qmlmethod CategoryAxis::replace(string oldLabel, string newLabel)
205 Replaces \a oldLabel of an existing category with a \a newLabel.
214 Replaces \a oldLabel of an existing category with a \a newLabel.
206 If the old label does not exist the method returns without making any changes.
215 If the old label does not exist the method returns without making any changes.
207 */
216 */
208 /*!
217 /*!
209 Replaces \a oldLabel of an existing category with a \a newLabel
218 Replaces \a oldLabel of an existing category with a \a newLabel
210 If the old label does not exist the method returns without making any changes.
219 If the old label does not exist the method returns without making any changes.
211 */
220 */
212 void QCategoryAxis::replaceLabel(const QString &oldLabel, const QString &newLabel)
221 void QCategoryAxis::replaceLabel(const QString &oldLabel, const QString &newLabel)
213 {
222 {
214 Q_D(QCategoryAxis);
223 Q_D(QCategoryAxis);
215 int labelIndex = d->m_categories.indexOf(oldLabel);
224 int labelIndex = d->m_categories.indexOf(oldLabel);
216
225
217 // check if such label exists
226 // check if such label exists
218 if (labelIndex != -1) {
227 if (labelIndex != -1) {
219 d->m_categories.replace(labelIndex, newLabel);
228 d->m_categories.replace(labelIndex, newLabel);
220 Range range = d->m_categoriesMap.value(oldLabel);
229 Range range = d->m_categoriesMap.value(oldLabel);
221 d->m_categoriesMap.remove(oldLabel);
230 d->m_categoriesMap.remove(oldLabel);
222 d->m_categoriesMap.insert(newLabel, range);
231 d->m_categoriesMap.insert(newLabel, range);
223 d->emitUpdated();
232 d->emitUpdated();
224 }
233 }
225 }
234 }
226
235
227 /*!
236 /*!
228 Returns the list of the intervals labels
237 Returns the list of the intervals labels
229 */
238 */
230 QStringList QCategoryAxis::categoriesLabels()
239 QStringList QCategoryAxis::categoriesLabels()
231 {
240 {
232 Q_D(QCategoryAxis);
241 Q_D(QCategoryAxis);
233 return d->m_categories;
242 return d->m_categories;
234 }
243 }
235
244
236 /*!
245 /*!
237 Returns number of intervals.
246 Returns number of intervals.
238 */
247 */
239 int QCategoryAxis::count() const
248 int QCategoryAxis::count() const
240 {
249 {
241 Q_D(const QCategoryAxis);
250 Q_D(const QCategoryAxis);
242 return d->m_categories.count();
251 return d->m_categories.count();
243 }
252 }
244
253
245 /*!
254 /*!
246 Returns the type of the axis
255 Returns the type of the axis
247 */
256 */
248 QAbstractAxis::AxisType QCategoryAxis::type() const
257 QAbstractAxis::AxisType QCategoryAxis::type() const
249 {
258 {
250 return QAbstractAxis::AxisTypeCategory;
259 return QAbstractAxis::AxisTypeCategory;
251 }
260 }
252
261
253 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
262 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
254
263
255 QCategoryAxisPrivate::QCategoryAxisPrivate(QCategoryAxis *q)
264 QCategoryAxisPrivate::QCategoryAxisPrivate(QCategoryAxis *q)
256 : QValueAxisPrivate(q),
265 : QValueAxisPrivate(q),
257 m_categoryMinimum(0)
266 m_categoryMinimum(0)
258 {
267 {
259
268
260 }
269 }
261
270
262 QCategoryAxisPrivate::~QCategoryAxisPrivate()
271 QCategoryAxisPrivate::~QCategoryAxisPrivate()
263 {
272 {
264
273
265 }
274 }
266
275
267 int QCategoryAxisPrivate::ticksCount() const
276 int QCategoryAxisPrivate::ticksCount() const
268 {
277 {
269 return m_categories.count() + 1;
278 return m_categories.count() + 1;
270 }
279 }
271
280
272 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max, int count)
281 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max, int count)
273 {
282 {
274 Q_UNUSED(count);
283 Q_UNUSED(count);
275 Q_UNUSED(min);
284 Q_UNUSED(min);
276 Q_UNUSED(max);
285 Q_UNUSED(max);
277 }
286 }
278
287
279 ChartAxis *QCategoryAxisPrivate::createGraphics(ChartPresenter *presenter)
288 ChartAxis *QCategoryAxisPrivate::createGraphics(ChartPresenter *presenter)
280 {
289 {
281 Q_Q(QCategoryAxis);
290 Q_Q(QCategoryAxis);
282 if (m_orientation == Qt::Vertical)
291 if (m_orientation == Qt::Vertical)
283 return new ChartCategoryAxisY(q, presenter);
292 return new ChartCategoryAxisY(q, presenter);
284 return new ChartCategoryAxisX(q, presenter);
293 return new ChartCategoryAxisX(q, presenter);
285 }
294 }
286
295
287 #include "moc_qcategoryaxis.cpp"
296 #include "moc_qcategoryaxis.cpp"
288 #include "moc_qcategoryaxis_p.cpp"
297 #include "moc_qcategoryaxis_p.cpp"
289
298
290 QTCOMMERCIALCHART_END_NAMESPACE
299 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now