##// END OF EJS Templates
QDateTimeAxis: added snippet on how to use it
Marek Rosa -
r1882:325b4aac9444
parent child
Show More
@@ -1,320 +1,341
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 "qdatetimeaxis.h"
21 #include "qdatetimeaxis.h"
22 #include "qdatetimeaxis_p.h"
22 #include "qdatetimeaxis_p.h"
23 #include "chartdatetimeaxisx_p.h"
23 #include "chartdatetimeaxisx_p.h"
24 #include "chartdatetimeaxisy_p.h"
24 #include "chartdatetimeaxisy_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include <cmath>
26 #include <cmath>
27 #include <QDebug>
27 #include <QDebug>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 /*!
30 /*!
31 \class QDateTimeAxis
31 \class QDateTimeAxis
32 \brief The QDateTimeAxis class is used for manipulating chart's axis.
32 \brief The QDateTimeAxis class is used for manipulating chart's axis.
33 \mainclass
33 \mainclass
34
34
35 The labels can be configured by setting an appropriate DateTime format.
35 The labels can be configured by setting an appropriate DateTime format.
36 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
36 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
37 QDateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
37 QDateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
38
39 Example code on how to use QDateTimeAxis.
40 \code
41 QChartView *chartView = new QChartView;
42 QLineSeries *series = new QLineSeries;
43
44 QDateTime xValue;
45 xValue.setDate(QDate(2012, 1 , 18));
46 xValue.setTime(QTime(9, 34));
47 series->append(xValue.toMSecsSinceEpoch(), 12);
48
49 xValue.setDate(QDate(2013, 5 , 11));
50 xValue.setTime(QTime(11, 14));
51 series->append(xValue.toMSecsSinceEpoch(), 22);
52 chartView->chart()->addSeries(series);
53
54 // ...
55 QDateTimeAxis *axisX = new QDateTimeAxis;
56 axisX->setFormat("dd-MM-yyyy h:mm");
57 chartView->chart()->setAxisX(series, axisX);
58 \endcode
38 */
59 */
39
60
40 /*!
61 /*!
41 \qmlclass DateTimeAxis QDateTimeAxis
62 \qmlclass DateTimeAxis QDateTimeAxis
42 \brief The DateTimeAxis element is used for manipulating chart's axes
63 \brief The DateTimeAxis element is used for manipulating chart's axes
43
64
44 The labels can be configured by setting an appropriate DateTime format.
65 The labels can be configured by setting an appropriate DateTime format.
45 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
66 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
46 DateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
67 DateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
47 */
68 */
48
69
49 /*!
70 /*!
50 \property QDateTimeAxis::min
71 \property QDateTimeAxis::min
51 Defines the minimum value on the axis.
72 Defines the minimum value on the axis.
52 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
73 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
53 */
74 */
54 /*!
75 /*!
55 \qmlproperty real ValuesAxis::min
76 \qmlproperty real ValuesAxis::min
56 Defines the minimum value on the axis.
77 Defines the minimum value on the axis.
57 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
78 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
58 */
79 */
59
80
60 /*!
81 /*!
61 \property QDateTimeAxis::max
82 \property QDateTimeAxis::max
62 Defines the maximum value on the axis.
83 Defines the maximum value on the axis.
63 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
84 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
64 */
85 */
65 /*!
86 /*!
66 \qmlproperty real ValuesAxis::max
87 \qmlproperty real ValuesAxis::max
67 Defines the maximum value on the axis.
88 Defines the maximum value on the axis.
68 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
89 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
69 */
90 */
70
91
71 /*!
92 /*!
72 \fn void QDateTimeAxis::minChanged(QDateTime min)
93 \fn void QDateTimeAxis::minChanged(QDateTime min)
73 Axis emits signal when \a min of axis has changed.
94 Axis emits signal when \a min of axis has changed.
74 */
95 */
75 /*!
96 /*!
76 \qmlsignal ValuesAxis::onMinChanged(QDateTime min)
97 \qmlsignal ValuesAxis::onMinChanged(QDateTime min)
77 Axis emits signal when \a min of axis has changed.
98 Axis emits signal when \a min of axis has changed.
78 */
99 */
79
100
80 /*!
101 /*!
81 \fn void QDateTimeAxis::maxChanged(QDateTime max)
102 \fn void QDateTimeAxis::maxChanged(QDateTime max)
82 Axis emits signal when \a max of axis has changed.
103 Axis emits signal when \a max of axis has changed.
83 */
104 */
84 /*!
105 /*!
85 \qmlsignal ValuesAxis::onMaxChanged(QDateTime max)
106 \qmlsignal ValuesAxis::onMaxChanged(QDateTime max)
86 Axis emits signal when \a max of axis has changed.
107 Axis emits signal when \a max of axis has changed.
87 */
108 */
88
109
89 /*!
110 /*!
90 \fn void QDateTimeAxis::rangeChanged(QDateTime min, QDateTime max)
111 \fn void QDateTimeAxis::rangeChanged(QDateTime min, QDateTime max)
91 Axis emits signal when \a min or \a max of axis has changed.
112 Axis emits signal when \a min or \a max of axis has changed.
92 */
113 */
93
114
94 /*!
115 /*!
95 \property QDateTimeAxis::tickCount
116 \property QDateTimeAxis::tickCount
96 The number of tick marks for the axis.
117 The number of tick marks for the axis.
97 */
118 */
98
119
99 /*!
120 /*!
100 \qmlproperty int ValuesAxis::tickCount
121 \qmlproperty int ValuesAxis::tickCount
101 The number of tick marks for the axis.
122 The number of tick marks for the axis.
102 */
123 */
103
124
104 /*!
125 /*!
105 Constructs an axis object which is a child of \a parent.
126 Constructs an axis object which is a child of \a parent.
106 */
127 */
107 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
128 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
108 QAbstractAxis(*new QDateTimeAxisPrivate(this),parent)
129 QAbstractAxis(*new QDateTimeAxisPrivate(this),parent)
109 {
130 {
110
131
111 }
132 }
112
133
113 /*!
134 /*!
114 \internal
135 \internal
115 */
136 */
116 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
137 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
117 {
138 {
118
139
119 }
140 }
120
141
121 /*!
142 /*!
122 Destroys the object
143 Destroys the object
123 */
144 */
124 QDateTimeAxis::~QDateTimeAxis()
145 QDateTimeAxis::~QDateTimeAxis()
125 {
146 {
126
147
127 }
148 }
128
149
129 void QDateTimeAxis::setMin(QDateTime min)
150 void QDateTimeAxis::setMin(QDateTime min)
130 {
151 {
131 Q_D(QDateTimeAxis);
152 Q_D(QDateTimeAxis);
132 if (min.isValid())
153 if (min.isValid())
133 setRange(min, qMax(d->m_max, min));
154 setRange(min, qMax(d->m_max, min));
134 }
155 }
135
156
136 QDateTime QDateTimeAxis::min() const
157 QDateTime QDateTimeAxis::min() const
137 {
158 {
138 Q_D(const QDateTimeAxis);
159 Q_D(const QDateTimeAxis);
139 return d->m_min;
160 return d->m_min;
140 }
161 }
141
162
142 void QDateTimeAxis::setMax(QDateTime max)
163 void QDateTimeAxis::setMax(QDateTime max)
143 {
164 {
144 Q_D(QDateTimeAxis);
165 Q_D(QDateTimeAxis);
145 if (max.isValid())
166 if (max.isValid())
146 setRange(qMin(d->m_min, max), max);
167 setRange(qMin(d->m_min, max), max);
147 }
168 }
148
169
149 QDateTime QDateTimeAxis::max() const
170 QDateTime QDateTimeAxis::max() const
150 {
171 {
151 Q_D(const QDateTimeAxis);
172 Q_D(const QDateTimeAxis);
152 return d->m_max;
173 return d->m_max;
153 }
174 }
154
175
155 /*!
176 /*!
156 Sets range from \a min to \a max on the axis.
177 Sets range from \a min to \a max on the axis.
157 If min is greater than max then this function returns without making any changes.
178 If min is greater than max then this function returns without making any changes.
158 */
179 */
159 void QDateTimeAxis::setRange(QDateTime min, QDateTime max)
180 void QDateTimeAxis::setRange(QDateTime min, QDateTime max)
160 {
181 {
161 Q_D(QDateTimeAxis);
182 Q_D(QDateTimeAxis);
162 if (!min.isValid() || !max.isValid() || min > max)
183 if (!min.isValid() || !max.isValid() || min > max)
163 return;
184 return;
164
185
165 bool changed = false;
186 bool changed = false;
166 if (d->m_min != min) {
187 if (d->m_min != min) {
167 d->m_min = min;
188 d->m_min = min;
168 changed = true;
189 changed = true;
169 emit minChanged(min);
190 emit minChanged(min);
170 }
191 }
171
192
172 if (d->m_max != max) {
193 if (d->m_max != max) {
173 d->m_max = max;
194 d->m_max = max;
174 changed = true;
195 changed = true;
175 emit maxChanged(max);
196 emit maxChanged(max);
176 }
197 }
177
198
178 if (changed) {
199 if (changed) {
179 emit rangeChanged(d->m_min,d->m_max);
200 emit rangeChanged(d->m_min,d->m_max);
180 d->emitUpdated();
201 d->emitUpdated();
181 }
202 }
182 }
203 }
183
204
184 /*!
205 /*!
185 Sets \a format string that is used when creating label for the axis out of the QDateTime object.
206 Sets \a format string that is used when creating label for the axis out of the QDateTime object.
186 Check QDateTime documentation for information on how the string should be defined.
207 Check QDateTime documentation for information on how the string should be defined.
187 \sa format()
208 \sa format()
188 */
209 */
189 void QDateTimeAxis::setFormat(QString format)
210 void QDateTimeAxis::setFormat(QString format)
190 {
211 {
191 Q_D(QDateTimeAxis);
212 Q_D(QDateTimeAxis);
192 d->m_format = format;
213 d->m_format = format;
193 }
214 }
194
215
195 /*!
216 /*!
196 Returns the format string that is used when creating label for the axis out of the QDateTime object.
217 Returns the format string that is used when creating label for the axis out of the QDateTime object.
197 Check QDateTime documentation for information on how the string should be defined.
218 Check QDateTime documentation for information on how the string should be defined.
198 \sa setFormat()
219 \sa setFormat()
199 */
220 */
200 QString QDateTimeAxis::format() const
221 QString QDateTimeAxis::format() const
201 {
222 {
202 Q_D(const QDateTimeAxis);
223 Q_D(const QDateTimeAxis);
203 return d->m_format;
224 return d->m_format;
204 }
225 }
205
226
206 /*!
227 /*!
207 Sets \a count for ticks on the axis.
228 Sets \a count for ticks on the axis.
208 */
229 */
209 void QDateTimeAxis::setTickCount(int count)
230 void QDateTimeAxis::setTickCount(int count)
210 {
231 {
211 Q_D(QDateTimeAxis);
232 Q_D(QDateTimeAxis);
212 if (d->m_tickCount != count && count >=2) {
233 if (d->m_tickCount != count && count >=2) {
213 d->m_tickCount = count;
234 d->m_tickCount = count;
214 d->emitUpdated();
235 d->emitUpdated();
215 }
236 }
216 }
237 }
217
238
218 /*!
239 /*!
219 \fn int QDateTimeAxis::tickCount() const
240 \fn int QDateTimeAxis::tickCount() const
220 Return number of ticks on the axis
241 Return number of ticks on the axis
221 */
242 */
222 int QDateTimeAxis::tickCount() const
243 int QDateTimeAxis::tickCount() const
223 {
244 {
224 Q_D(const QDateTimeAxis);
245 Q_D(const QDateTimeAxis);
225 return d->m_tickCount;
246 return d->m_tickCount;
226 }
247 }
227
248
228 /*!
249 /*!
229 Returns the type of the axis
250 Returns the type of the axis
230 */
251 */
231 QAbstractAxis::AxisType QDateTimeAxis::type() const
252 QAbstractAxis::AxisType QDateTimeAxis::type() const
232 {
253 {
233 return AxisTypeDateTime;
254 return AxisTypeDateTime;
234 }
255 }
235
256
236 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
257 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
237
258
238 QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis* q):
259 QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis* q):
239 QAbstractAxisPrivate(q),
260 QAbstractAxisPrivate(q),
240 m_tickCount(5)
261 m_tickCount(5)
241 {
262 {
242 m_min = QDateTime::fromMSecsSinceEpoch(0);
263 m_min = QDateTime::fromMSecsSinceEpoch(0);
243 m_max = QDateTime::fromMSecsSinceEpoch(0);
264 m_max = QDateTime::fromMSecsSinceEpoch(0);
244 m_format = "dd-MMM-yyyy\nh:mm";
265 m_format = "dd-MMM-yyyy\nh:mm";
245 }
266 }
246
267
247 QDateTimeAxisPrivate::~QDateTimeAxisPrivate()
268 QDateTimeAxisPrivate::~QDateTimeAxisPrivate()
248 {
269 {
249
270
250 }
271 }
251
272
252 void QDateTimeAxisPrivate::handleDomainUpdated()
273 void QDateTimeAxisPrivate::handleDomainUpdated()
253 {
274 {
254 Q_Q(QDateTimeAxis);
275 Q_Q(QDateTimeAxis);
255 Domain* domain = qobject_cast<Domain*>(sender());
276 Domain* domain = qobject_cast<Domain*>(sender());
256 Q_ASSERT(domain);
277 Q_ASSERT(domain);
257
278
258 if(orientation()==Qt::Horizontal){
279 if(orientation()==Qt::Horizontal){
259 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
280 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
260 }else if(orientation()==Qt::Vertical){
281 }else if(orientation()==Qt::Vertical){
261 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
282 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
262 }
283 }
263 }
284 }
264
285
265
286
266 void QDateTimeAxisPrivate::setMin(const QVariant &min)
287 void QDateTimeAxisPrivate::setMin(const QVariant &min)
267 {
288 {
268 Q_Q(QDateTimeAxis);
289 Q_Q(QDateTimeAxis);
269 if (min.canConvert(QVariant::DateTime))
290 if (min.canConvert(QVariant::DateTime))
270 q->setMin(min.toDateTime());
291 q->setMin(min.toDateTime());
271 }
292 }
272
293
273 void QDateTimeAxisPrivate::setMax(const QVariant &max)
294 void QDateTimeAxisPrivate::setMax(const QVariant &max)
274 {
295 {
275
296
276 Q_Q(QDateTimeAxis);
297 Q_Q(QDateTimeAxis);
277 if (max.canConvert(QVariant::DateTime))
298 if (max.canConvert(QVariant::DateTime))
278 q->setMax(max.toDateTime());
299 q->setMax(max.toDateTime());
279 }
300 }
280
301
281 void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max)
302 void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max)
282 {
303 {
283 Q_Q(QDateTimeAxis);
304 Q_Q(QDateTimeAxis);
284 if (min.canConvert(QVariant::DateTime) && max.canConvert(QVariant::DateTime))
305 if (min.canConvert(QVariant::DateTime) && max.canConvert(QVariant::DateTime))
285 q->setRange(min.toDateTime(), max.toDateTime());
306 q->setRange(min.toDateTime(), max.toDateTime());
286 }
307 }
287
308
288 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
309 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
289 {
310 {
290 Q_Q(QDateTimeAxis);
311 Q_Q(QDateTimeAxis);
291 if(m_orientation == Qt::Vertical){
312 if(m_orientation == Qt::Vertical){
292 return new ChartDateTimeAxisY(q,presenter);
313 return new ChartDateTimeAxisY(q,presenter);
293 }else{
314 }else{
294 return new ChartDateTimeAxisX(q,presenter);
315 return new ChartDateTimeAxisX(q,presenter);
295 }
316 }
296
317
297 }
318 }
298
319
299 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
320 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
300 {
321 {
301 Q_Q(QDateTimeAxis);
322 Q_Q(QDateTimeAxis);
302 if(m_max == m_min) {
323 if(m_max == m_min) {
303 if(m_orientation==Qt::Vertical){
324 if(m_orientation==Qt::Vertical){
304 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
325 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
305 }else{
326 }else{
306 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
327 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
307 }
328 }
308 } else {
329 } else {
309 if(m_orientation==Qt::Vertical){
330 if(m_orientation==Qt::Vertical){
310 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
331 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
311 }else{
332 }else{
312 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
333 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
313 }
334 }
314 }
335 }
315 }
336 }
316
337
317 #include "moc_qdatetimeaxis.cpp"
338 #include "moc_qdatetimeaxis.cpp"
318 #include "moc_qdatetimeaxis_p.cpp"
339 #include "moc_qdatetimeaxis_p.cpp"
319
340
320 QTCOMMERCIALCHART_END_NAMESPACE
341 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,421 +1,423
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 "qvalueaxis.h"
21 #include "qvalueaxis.h"
22 #include "qvalueaxis_p.h"
22 #include "qvalueaxis_p.h"
23 #include "chartvalueaxisx_p.h"
23 #include "chartvalueaxisx_p.h"
24 #include "chartvalueaxisy_p.h"
24 #include "chartvalueaxisy_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "chartdataset_p.h"
26 #include "chartdataset_p.h"
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 /*!
31 /*!
32 \class QValueAxis
32 \class QValueAxis
33 \brief The QValueAxis class is used for manipulating chart's axis.
33 \brief The QValueAxis class is used for manipulating chart's axis.
34 \mainclass
34 \mainclass
35
35
36 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
36 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
37 Values of axis are drawn to position of ticks.
37 Values of axis are drawn to position of ticks.
38
38
39 Example code on how to use QValueAxis.
39 Example code on how to use QValueAxis.
40 \code
40 \code
41 QChartView *chartView = new QChartView;
41 QChartView *chartView = new QChartView;
42 QLineSeries *series = new QLineSeries;
42 QLineSeries *series = new QLineSeries;
43 // ...
43 // ...
44 chartView->chart()->addSeries(series);
45
44 QValueAxis *axisX = new QValueAxis;
46 QValueAxis *axisX = new QValueAxis;
45 axisX->setRange(10, 20.5);
47 axisX->setRange(10, 20.5);
46 axisX->setTickCount(10);
48 axisX->setTickCount(10);
47 axisX->setLabelFormat("%.2f");
49 axisX->setLabelFormat("%.2f");
48 chartView->chart()->setAxisX(series, axisX);
50 chartView->chart()->setAxisX(series, axisX);
49 \endcode
51 \endcode
50 */
52 */
51
53
52 /*!
54 /*!
53 \qmlclass ValueAxis QValueAxis
55 \qmlclass ValueAxis QValueAxis
54 \inherits AbstractAxis
56 \inherits AbstractAxis
55 \brief The ValueAxis element is used for manipulating chart's axes
57 \brief The ValueAxis element is used for manipulating chart's axes
56
58
57 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
59 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
58 Values of axis are drawn to position of ticks
60 Values of axis are drawn to position of ticks
59
61
60 To access Axes you can use ChartView API. For example:
62 To access Axes you can use ChartView API. For example:
61 \code
63 \code
62 ChartView {
64 ChartView {
63 ValueAxis {
65 ValueAxis {
64 id: xAxis
66 id: xAxis
65 min: 0
67 min: 0
66 max: 10
68 max: 10
67 }
69 }
68 // Add a few series...
70 // Add a few series...
69 }
71 }
70 \endcode
72 \endcode
71 */
73 */
72
74
73 /*!
75 /*!
74 \property QValueAxis::min
76 \property QValueAxis::min
75 Defines the minimum value on the axis.
77 Defines the minimum value on the axis.
76 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
78 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
77 */
79 */
78 /*!
80 /*!
79 \qmlproperty real ValueAxis::min
81 \qmlproperty real ValueAxis::min
80 Defines the minimum value on the axis.
82 Defines the minimum value on the axis.
81 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
83 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
82 */
84 */
83
85
84 /*!
86 /*!
85 \property QValueAxis::max
87 \property QValueAxis::max
86 Defines the maximum value on the axis.
88 Defines the maximum value on the axis.
87 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
89 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
88 */
90 */
89 /*!
91 /*!
90 \qmlproperty real ValueAxis::max
92 \qmlproperty real ValueAxis::max
91 Defines the maximum value on the axis.
93 Defines the maximum value on the axis.
92 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
94 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
93 */
95 */
94
96
95 /*!
97 /*!
96 \property QValueAxis::labelFormat
98 \property QValueAxis::labelFormat
97 Defines the label format for the axis.
99 Defines the label format for the axis.
98 See QString::sprintf() for the details.
100 See QString::sprintf() for the details.
99 */
101 */
100 /*!
102 /*!
101 \qmlproperty real ValueAxis::labelFormat
103 \qmlproperty real ValueAxis::labelFormat
102 Defines the label format for the axis.
104 Defines the label format for the axis.
103 See QString::sprintf() for the details.
105 See QString::sprintf() for the details.
104 */
106 */
105
107
106 /*!
108 /*!
107 \fn void QValueAxis::minChanged(qreal min)
109 \fn void QValueAxis::minChanged(qreal min)
108 Axis emits signal when \a min of axis has changed.
110 Axis emits signal when \a min of axis has changed.
109 */
111 */
110 /*!
112 /*!
111 \qmlsignal ValueAxis::onMinChanged(real min)
113 \qmlsignal ValueAxis::onMinChanged(real min)
112 Axis emits signal when \a min of axis has changed.
114 Axis emits signal when \a min of axis has changed.
113 */
115 */
114
116
115 /*!
117 /*!
116 \fn void QValueAxis::maxChanged(qreal max)
118 \fn void QValueAxis::maxChanged(qreal max)
117 Axis emits signal when \a max of axis has changed.
119 Axis emits signal when \a max of axis has changed.
118 */
120 */
119 /*!
121 /*!
120 \qmlsignal ValueAxis::onMaxChanged(real max)
122 \qmlsignal ValueAxis::onMaxChanged(real max)
121 Axis emits signal when \a max of axis has changed.
123 Axis emits signal when \a max of axis has changed.
122 */
124 */
123
125
124 /*!
126 /*!
125 \fn void QValueAxis::rangeChanged(qreal min, qreal max)
127 \fn void QValueAxis::rangeChanged(qreal min, qreal max)
126 Axis emits signal when \a min or \a max of axis has changed.
128 Axis emits signal when \a min or \a max of axis has changed.
127 */
129 */
128
130
129 /*!
131 /*!
130 \property QValueAxis::tickCount
132 \property QValueAxis::tickCount
131 The number of tick marks for the axis.
133 The number of tick marks for the axis.
132 */
134 */
133
135
134 /*!
136 /*!
135 \qmlproperty int ValueAxis::tickCount
137 \qmlproperty int ValueAxis::tickCount
136 The number of tick marks for the axis.
138 The number of tick marks for the axis.
137 */
139 */
138
140
139 /*!
141 /*!
140 \property QValueAxis::niceNumbersEnabled
142 \property QValueAxis::niceNumbersEnabled
141 Whether the nice numbers algorithm is enabled or not for the axis.
143 Whether the nice numbers algorithm is enabled or not for the axis.
142 */
144 */
143
145
144 /*!
146 /*!
145 \qmlproperty bool ValueAxis::niceNumbersEnabled
147 \qmlproperty bool ValueAxis::niceNumbersEnabled
146 Whether the nice numbers algorithm is enabled or not for the axis.
148 Whether the nice numbers algorithm is enabled or not for the axis.
147 */
149 */
148
150
149 /*!
151 /*!
150 Constructs an axis object which is a child of \a parent.
152 Constructs an axis object which is a child of \a parent.
151 */
153 */
152 QValueAxis::QValueAxis(QObject *parent) :
154 QValueAxis::QValueAxis(QObject *parent) :
153 QAbstractAxis(*new QValueAxisPrivate(this),parent)
155 QAbstractAxis(*new QValueAxisPrivate(this),parent)
154 {
156 {
155
157
156 }
158 }
157
159
158 /*!
160 /*!
159 \internal
161 \internal
160 */
162 */
161 QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
163 QValueAxis::QValueAxis(QValueAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
162 {
164 {
163
165
164 }
166 }
165
167
166 /*!
168 /*!
167 Destroys the object
169 Destroys the object
168 */
170 */
169 QValueAxis::~QValueAxis()
171 QValueAxis::~QValueAxis()
170 {
172 {
171 Q_D(QValueAxis);
173 Q_D(QValueAxis);
172 if(d->m_dataset) {
174 if(d->m_dataset) {
173 d->m_dataset->removeAxis(this);
175 d->m_dataset->removeAxis(this);
174 }
176 }
175 }
177 }
176
178
177 void QValueAxis::setMin(qreal min)
179 void QValueAxis::setMin(qreal min)
178 {
180 {
179 Q_D(QValueAxis);
181 Q_D(QValueAxis);
180 setRange(min, qMax(d->m_max, min));
182 setRange(min, qMax(d->m_max, min));
181 }
183 }
182
184
183 qreal QValueAxis::min() const
185 qreal QValueAxis::min() const
184 {
186 {
185 Q_D(const QValueAxis);
187 Q_D(const QValueAxis);
186 return d->m_min;
188 return d->m_min;
187 }
189 }
188
190
189 void QValueAxis::setMax(qreal max)
191 void QValueAxis::setMax(qreal max)
190 {
192 {
191 Q_D(QValueAxis);
193 Q_D(QValueAxis);
192 setRange(qMin(d->m_min, max), max);
194 setRange(qMin(d->m_min, max), max);
193 }
195 }
194
196
195 qreal QValueAxis::max() const
197 qreal QValueAxis::max() const
196 {
198 {
197 Q_D(const QValueAxis);
199 Q_D(const QValueAxis);
198 return d->m_max;
200 return d->m_max;
199 }
201 }
200
202
201 /*!
203 /*!
202 Sets range from \a min to \a max on the axis.
204 Sets range from \a min to \a max on the axis.
203 If min is greater than max then this function returns without making any changes.
205 If min is greater than max then this function returns without making any changes.
204 */
206 */
205 void QValueAxis::setRange(qreal min, qreal max)
207 void QValueAxis::setRange(qreal min, qreal max)
206 {
208 {
207 Q_D(QValueAxis);
209 Q_D(QValueAxis);
208 bool changed = false;
210 bool changed = false;
209
211
210 if (min > max) return;
212 if (min > max) return;
211
213
212 if(d->m_niceNumbers) {
214 if(d->m_niceNumbers) {
213 int ticks = d->m_tickCount;
215 int ticks = d->m_tickCount;
214 d->looseNiceNumbers(min, max, ticks);
216 d->looseNiceNumbers(min, max, ticks);
215 if(ticks!=d->m_tickCount) setTickCount(ticks);
217 if(ticks!=d->m_tickCount) setTickCount(ticks);
216 }
218 }
217
219
218 if (!qFuzzyIsNull(d->m_min - min)) {
220 if (!qFuzzyIsNull(d->m_min - min)) {
219 d->m_min = min;
221 d->m_min = min;
220 changed = true;
222 changed = true;
221 emit minChanged(min);
223 emit minChanged(min);
222 }
224 }
223
225
224 if (!qFuzzyIsNull(d->m_max - max)) {
226 if (!qFuzzyIsNull(d->m_max - max)) {
225 d->m_max = max;
227 d->m_max = max;
226 changed = true;
228 changed = true;
227 emit maxChanged(max);
229 emit maxChanged(max);
228 }
230 }
229
231
230 if (changed) {
232 if (changed) {
231 emit rangeChanged(min,max);
233 emit rangeChanged(min,max);
232 d->emitUpdated();
234 d->emitUpdated();
233 }
235 }
234 }
236 }
235
237
236 /*!
238 /*!
237 Sets \a count for ticks on the axis.
239 Sets \a count for ticks on the axis.
238 */
240 */
239 void QValueAxis::setTickCount(int count)
241 void QValueAxis::setTickCount(int count)
240 {
242 {
241 Q_D(QValueAxis);
243 Q_D(QValueAxis);
242 if (d->m_tickCount != count && count >=2) {
244 if (d->m_tickCount != count && count >=2) {
243 d->m_tickCount = count;
245 d->m_tickCount = count;
244 d->emitUpdated();
246 d->emitUpdated();
245 }
247 }
246 }
248 }
247
249
248 /*!
250 /*!
249 \fn int QValueAxis::tickCount() const
251 \fn int QValueAxis::tickCount() const
250 Return number of ticks on the axis
252 Return number of ticks on the axis
251 */
253 */
252 int QValueAxis::tickCount() const
254 int QValueAxis::tickCount() const
253 {
255 {
254 Q_D(const QValueAxis);
256 Q_D(const QValueAxis);
255 return d->m_tickCount;
257 return d->m_tickCount;
256 }
258 }
257
259
258 void QValueAxis::setNiceNumbersEnabled(bool enable)
260 void QValueAxis::setNiceNumbersEnabled(bool enable)
259 {
261 {
260 Q_D(QValueAxis);
262 Q_D(QValueAxis);
261 if (d->m_niceNumbers != enable){
263 if (d->m_niceNumbers != enable){
262 d->m_niceNumbers = enable;
264 d->m_niceNumbers = enable;
263 if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) {
265 if(enable && !qFuzzyIsNull(d->m_max - d->m_min)) {
264 setRange(d->m_min,d->m_max);
266 setRange(d->m_min,d->m_max);
265 }
267 }
266 }
268 }
267 }
269 }
268
270
269 bool QValueAxis::niceNumbersEnabled() const
271 bool QValueAxis::niceNumbersEnabled() const
270 {
272 {
271 Q_D(const QValueAxis);
273 Q_D(const QValueAxis);
272 return d->m_niceNumbers;
274 return d->m_niceNumbers;
273 }
275 }
274
276
275 void QValueAxis::setLabelFormat(const QString &format)
277 void QValueAxis::setLabelFormat(const QString &format)
276 {
278 {
277 Q_D(QValueAxis);
279 Q_D(QValueAxis);
278 d->m_format = format;
280 d->m_format = format;
279 }
281 }
280
282
281 QString QValueAxis::labelFormat() const
283 QString QValueAxis::labelFormat() const
282 {
284 {
283 Q_D(const QValueAxis);
285 Q_D(const QValueAxis);
284 return d->m_format;
286 return d->m_format;
285 }
287 }
286
288
287 /*!
289 /*!
288 Returns the type of the axis
290 Returns the type of the axis
289 */
291 */
290 QAbstractAxis::AxisType QValueAxis::type() const
292 QAbstractAxis::AxisType QValueAxis::type() const
291 {
293 {
292 return AxisTypeValue;
294 return AxisTypeValue;
293 }
295 }
294
296
295 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
297 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
296
298
297 QValueAxisPrivate::QValueAxisPrivate(QValueAxis* q):
299 QValueAxisPrivate::QValueAxisPrivate(QValueAxis* q):
298 QAbstractAxisPrivate(q),
300 QAbstractAxisPrivate(q),
299 m_min(0),
301 m_min(0),
300 m_max(0),
302 m_max(0),
301 m_tickCount(5),
303 m_tickCount(5),
302 m_niceNumbers(false),
304 m_niceNumbers(false),
303 m_format("%g")
305 m_format("%g")
304 {
306 {
305
307
306 }
308 }
307
309
308 QValueAxisPrivate::~QValueAxisPrivate()
310 QValueAxisPrivate::~QValueAxisPrivate()
309 {
311 {
310
312
311 }
313 }
312
314
313 void QValueAxisPrivate::handleDomainUpdated()
315 void QValueAxisPrivate::handleDomainUpdated()
314 {
316 {
315 Q_Q(QValueAxis);
317 Q_Q(QValueAxis);
316 Domain* domain = qobject_cast<Domain*>(sender());
318 Domain* domain = qobject_cast<Domain*>(sender());
317 Q_ASSERT(domain);
319 Q_ASSERT(domain);
318
320
319 if(orientation()==Qt::Horizontal){
321 if(orientation()==Qt::Horizontal){
320 q->setRange(domain->minX(),domain->maxX());
322 q->setRange(domain->minX(),domain->maxX());
321 }else if(orientation()==Qt::Vertical){
323 }else if(orientation()==Qt::Vertical){
322 q->setRange(domain->minY(),domain->maxY());
324 q->setRange(domain->minY(),domain->maxY());
323 }
325 }
324 }
326 }
325
327
326
328
327 void QValueAxisPrivate::setMin(const QVariant &min)
329 void QValueAxisPrivate::setMin(const QVariant &min)
328 {
330 {
329 Q_Q(QValueAxis);
331 Q_Q(QValueAxis);
330 bool ok;
332 bool ok;
331 qreal value = min.toReal(&ok);
333 qreal value = min.toReal(&ok);
332 if(ok) q->setMin(value);
334 if(ok) q->setMin(value);
333 }
335 }
334
336
335 void QValueAxisPrivate::setMax(const QVariant &max)
337 void QValueAxisPrivate::setMax(const QVariant &max)
336 {
338 {
337
339
338 Q_Q(QValueAxis);
340 Q_Q(QValueAxis);
339 bool ok;
341 bool ok;
340 qreal value = max.toReal(&ok);
342 qreal value = max.toReal(&ok);
341 if(ok) q->setMax(value);
343 if(ok) q->setMax(value);
342 }
344 }
343
345
344 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
346 void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
345 {
347 {
346 Q_Q(QValueAxis);
348 Q_Q(QValueAxis);
347 bool ok1;
349 bool ok1;
348 bool ok2;
350 bool ok2;
349 qreal value1 = min.toReal(&ok1);
351 qreal value1 = min.toReal(&ok1);
350 qreal value2 = max.toReal(&ok2);
352 qreal value2 = max.toReal(&ok2);
351 if(ok1&&ok2) q->setRange(value1,value2);
353 if(ok1&&ok2) q->setRange(value1,value2);
352 }
354 }
353
355
354 ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter)
356 ChartAxis* QValueAxisPrivate::createGraphics(ChartPresenter* presenter)
355 {
357 {
356 Q_Q(QValueAxis);
358 Q_Q(QValueAxis);
357 if(m_orientation == Qt::Vertical){
359 if(m_orientation == Qt::Vertical){
358 return new ChartValueAxisY(q,presenter);
360 return new ChartValueAxisY(q,presenter);
359 }else{
361 }else{
360 return new ChartValueAxisX(q,presenter);
362 return new ChartValueAxisX(q,presenter);
361 }
363 }
362
364
363 }
365 }
364
366
365 void QValueAxisPrivate::intializeDomain(Domain* domain)
367 void QValueAxisPrivate::intializeDomain(Domain* domain)
366 {
368 {
367 Q_Q(QValueAxis);
369 Q_Q(QValueAxis);
368 if(qFuzzyCompare(m_max,m_min)) {
370 if(qFuzzyCompare(m_max,m_min)) {
369 if(m_orientation==Qt::Vertical){
371 if(m_orientation==Qt::Vertical){
370 q->setRange(domain->minY(),domain->maxY());
372 q->setRange(domain->minY(),domain->maxY());
371 }else{
373 }else{
372 q->setRange(domain->minX(), domain->maxX());
374 q->setRange(domain->minX(), domain->maxX());
373 }
375 }
374 } else {
376 } else {
375 if(m_orientation==Qt::Vertical){
377 if(m_orientation==Qt::Vertical){
376 domain->setRangeY(m_min, m_max);
378 domain->setRangeY(m_min, m_max);
377 }else{
379 }else{
378 domain->setRangeX(m_min, m_max);
380 domain->setRangeX(m_min, m_max);
379 }
381 }
380 }
382 }
381 }
383 }
382
384
383 //algorithm defined by Paul S.Heckbert GraphicalGems I
385 //algorithm defined by Paul S.Heckbert GraphicalGems I
384
386
385 void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
387 void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const
386 {
388 {
387 qreal range = niceNumber(max-min,true); //range with ceiling
389 qreal range = niceNumber(max-min,true); //range with ceiling
388 qreal step = niceNumber(range/(ticksCount-1),false);
390 qreal step = niceNumber(range/(ticksCount-1),false);
389 min = qFloor(min/step);
391 min = qFloor(min/step);
390 max = qCeil(max/step);
392 max = qCeil(max/step);
391 ticksCount = int(max-min) +1;
393 ticksCount = int(max-min) +1;
392 min*=step;
394 min*=step;
393 max*=step;
395 max*=step;
394 }
396 }
395
397
396 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
398 //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n
397
399
398 qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const
400 qreal QValueAxisPrivate::niceNumber(qreal x,bool ceiling) const
399 {
401 {
400 qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
402 qreal z = qPow(10,qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x
401 qreal q = x/z;//q<10 && q>=1;
403 qreal q = x/z;//q<10 && q>=1;
402
404
403 if(ceiling) {
405 if(ceiling) {
404 if(q <= 1.0) q=1;
406 if(q <= 1.0) q=1;
405 else if(q <= 2.0) q=2;
407 else if(q <= 2.0) q=2;
406 else if(q <= 5.0) q=5;
408 else if(q <= 5.0) q=5;
407 else q=10;
409 else q=10;
408 }
410 }
409 else {
411 else {
410 if(q < 1.5) q=1;
412 if(q < 1.5) q=1;
411 else if(q < 3.0) q=2;
413 else if(q < 3.0) q=2;
412 else if(q < 7.0) q=5;
414 else if(q < 7.0) q=5;
413 else q=10;
415 else q=10;
414 }
416 }
415 return q*z;
417 return q*z;
416 }
418 }
417
419
418 #include "moc_qvalueaxis.cpp"
420 #include "moc_qvalueaxis.cpp"
419 #include "moc_qvalueaxis_p.cpp"
421 #include "moc_qvalueaxis_p.cpp"
420
422
421 QTCOMMERCIALCHART_END_NAMESPACE
423 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now