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