##// END OF EJS Templates
QDateTimeAxis: precise range specified in Docs
Marek Rosa -
r1927:d735cf852a78
parent child
Show More
@@ -1,343 +1,344
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 QDateTimeAxis works correctly with dates from 4714 BCE to 287396 CE
37 There are also other limitiation that are specified in QDateTime documentation. Please refer to it.
37 QDateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
38 QDateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
38
39
39 \image api_datatime_axis.png
40 \image api_datatime_axis.png
40
41
41 Example code on how to use QDateTimeAxis.
42 Example code on how to use QDateTimeAxis.
42 \code
43 \code
43 QChartView *chartView = new QChartView;
44 QChartView *chartView = new QChartView;
44 QLineSeries *series = new QLineSeries;
45 QLineSeries *series = new QLineSeries;
45
46
46 QDateTime xValue;
47 QDateTime xValue;
47 xValue.setDate(QDate(2012, 1 , 18));
48 xValue.setDate(QDate(2012, 1 , 18));
48 xValue.setTime(QTime(9, 34));
49 xValue.setTime(QTime(9, 34));
49 series->append(xValue.toMSecsSinceEpoch(), 12);
50 series->append(xValue.toMSecsSinceEpoch(), 12);
50
51
51 xValue.setDate(QDate(2013, 5 , 11));
52 xValue.setDate(QDate(2013, 5 , 11));
52 xValue.setTime(QTime(11, 14));
53 xValue.setTime(QTime(11, 14));
53 series->append(xValue.toMSecsSinceEpoch(), 22);
54 series->append(xValue.toMSecsSinceEpoch(), 22);
54 chartView->chart()->addSeries(series);
55 chartView->chart()->addSeries(series);
55
56
56 // ...
57 // ...
57 QDateTimeAxis *axisX = new QDateTimeAxis;
58 QDateTimeAxis *axisX = new QDateTimeAxis;
58 axisX->setFormat("dd-MM-yyyy h:mm");
59 axisX->setFormat("dd-MM-yyyy h:mm");
59 chartView->chart()->setAxisX(series, axisX);
60 chartView->chart()->setAxisX(series, axisX);
60 \endcode
61 \endcode
61 */
62 */
62
63
63 /*!
64 /*!
64 \qmlclass DateTimeAxis QDateTimeAxis
65 \qmlclass DateTimeAxis QDateTimeAxis
65 \brief The DateTimeAxis element is used for manipulating chart's axes
66 \brief The DateTimeAxis element is used for manipulating chart's axes
66
67
67 The labels can be configured by setting an appropriate DateTime format.
68 The labels can be configured by setting an appropriate DateTime format.
68 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
69 Note that any date before 4714 BCE or after about 1.4 million CE may not be accurately stored.
69 DateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
70 DateTimeAxis can be setup to show axis line with tick marks, grid lines and shades.
70 */
71 */
71
72
72 /*!
73 /*!
73 \property QDateTimeAxis::min
74 \property QDateTimeAxis::min
74 Defines the minimum value on the axis.
75 Defines the minimum value on the axis.
75 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
76 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
76 */
77 */
77 /*!
78 /*!
78 \qmlproperty real ValuesAxis::min
79 \qmlproperty real ValuesAxis::min
79 Defines the minimum value on the axis.
80 Defines the minimum value on the axis.
80 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
81 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
81 */
82 */
82
83
83 /*!
84 /*!
84 \property QDateTimeAxis::max
85 \property QDateTimeAxis::max
85 Defines the maximum value on the axis.
86 Defines the maximum value on the axis.
86 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
87 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
87 */
88 */
88 /*!
89 /*!
89 \qmlproperty real ValuesAxis::max
90 \qmlproperty real ValuesAxis::max
90 Defines the maximum value on the axis.
91 Defines the maximum value on the axis.
91 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
92 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
92 */
93 */
93
94
94 /*!
95 /*!
95 \fn void QDateTimeAxis::minChanged(QDateTime min)
96 \fn void QDateTimeAxis::minChanged(QDateTime min)
96 Axis emits signal when \a min of axis has changed.
97 Axis emits signal when \a min of axis has changed.
97 */
98 */
98 /*!
99 /*!
99 \qmlsignal ValuesAxis::onMinChanged(QDateTime min)
100 \qmlsignal ValuesAxis::onMinChanged(QDateTime min)
100 Axis emits signal when \a min of axis has changed.
101 Axis emits signal when \a min of axis has changed.
101 */
102 */
102
103
103 /*!
104 /*!
104 \fn void QDateTimeAxis::maxChanged(QDateTime max)
105 \fn void QDateTimeAxis::maxChanged(QDateTime max)
105 Axis emits signal when \a max of axis has changed.
106 Axis emits signal when \a max of axis has changed.
106 */
107 */
107 /*!
108 /*!
108 \qmlsignal ValuesAxis::onMaxChanged(QDateTime max)
109 \qmlsignal ValuesAxis::onMaxChanged(QDateTime max)
109 Axis emits signal when \a max of axis has changed.
110 Axis emits signal when \a max of axis has changed.
110 */
111 */
111
112
112 /*!
113 /*!
113 \fn void QDateTimeAxis::rangeChanged(QDateTime min, QDateTime max)
114 \fn void QDateTimeAxis::rangeChanged(QDateTime min, QDateTime max)
114 Axis emits signal when \a min or \a max of axis has changed.
115 Axis emits signal when \a min or \a max of axis has changed.
115 */
116 */
116
117
117 /*!
118 /*!
118 \property QDateTimeAxis::tickCount
119 \property QDateTimeAxis::tickCount
119 The number of tick marks for the axis.
120 The number of tick marks for the axis.
120 */
121 */
121
122
122 /*!
123 /*!
123 \qmlproperty int ValuesAxis::tickCount
124 \qmlproperty int ValuesAxis::tickCount
124 The number of tick marks for the axis.
125 The number of tick marks for the axis.
125 */
126 */
126
127
127 /*!
128 /*!
128 Constructs an axis object which is a child of \a parent.
129 Constructs an axis object which is a child of \a parent.
129 */
130 */
130 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
131 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
131 QAbstractAxis(*new QDateTimeAxisPrivate(this),parent)
132 QAbstractAxis(*new QDateTimeAxisPrivate(this),parent)
132 {
133 {
133
134
134 }
135 }
135
136
136 /*!
137 /*!
137 \internal
138 \internal
138 */
139 */
139 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
140 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
140 {
141 {
141
142
142 }
143 }
143
144
144 /*!
145 /*!
145 Destroys the object
146 Destroys the object
146 */
147 */
147 QDateTimeAxis::~QDateTimeAxis()
148 QDateTimeAxis::~QDateTimeAxis()
148 {
149 {
149
150
150 }
151 }
151
152
152 void QDateTimeAxis::setMin(QDateTime min)
153 void QDateTimeAxis::setMin(QDateTime min)
153 {
154 {
154 Q_D(QDateTimeAxis);
155 Q_D(QDateTimeAxis);
155 if (min.isValid())
156 if (min.isValid())
156 setRange(min, qMax(d->m_max, min));
157 setRange(min, qMax(d->m_max, min));
157 }
158 }
158
159
159 QDateTime QDateTimeAxis::min() const
160 QDateTime QDateTimeAxis::min() const
160 {
161 {
161 Q_D(const QDateTimeAxis);
162 Q_D(const QDateTimeAxis);
162 return d->m_min;
163 return d->m_min;
163 }
164 }
164
165
165 void QDateTimeAxis::setMax(QDateTime max)
166 void QDateTimeAxis::setMax(QDateTime max)
166 {
167 {
167 Q_D(QDateTimeAxis);
168 Q_D(QDateTimeAxis);
168 if (max.isValid())
169 if (max.isValid())
169 setRange(qMin(d->m_min, max), max);
170 setRange(qMin(d->m_min, max), max);
170 }
171 }
171
172
172 QDateTime QDateTimeAxis::max() const
173 QDateTime QDateTimeAxis::max() const
173 {
174 {
174 Q_D(const QDateTimeAxis);
175 Q_D(const QDateTimeAxis);
175 return d->m_max;
176 return d->m_max;
176 }
177 }
177
178
178 /*!
179 /*!
179 Sets range from \a min to \a max on the axis.
180 Sets range from \a min to \a max on the axis.
180 If min is greater than max then this function returns without making any changes.
181 If min is greater than max then this function returns without making any changes.
181 */
182 */
182 void QDateTimeAxis::setRange(QDateTime min, QDateTime max)
183 void QDateTimeAxis::setRange(QDateTime min, QDateTime max)
183 {
184 {
184 Q_D(QDateTimeAxis);
185 Q_D(QDateTimeAxis);
185 if (!min.isValid() || !max.isValid() || min > max)
186 if (!min.isValid() || !max.isValid() || min > max)
186 return;
187 return;
187
188
188 bool changed = false;
189 bool changed = false;
189 if (d->m_min != min) {
190 if (d->m_min != min) {
190 d->m_min = min;
191 d->m_min = min;
191 changed = true;
192 changed = true;
192 emit minChanged(min);
193 emit minChanged(min);
193 }
194 }
194
195
195 if (d->m_max != max) {
196 if (d->m_max != max) {
196 d->m_max = max;
197 d->m_max = max;
197 changed = true;
198 changed = true;
198 emit maxChanged(max);
199 emit maxChanged(max);
199 }
200 }
200
201
201 if (changed) {
202 if (changed) {
202 emit rangeChanged(d->m_min,d->m_max);
203 emit rangeChanged(d->m_min,d->m_max);
203 d->emitUpdated();
204 d->emitUpdated();
204 }
205 }
205 }
206 }
206
207
207 /*!
208 /*!
208 Sets \a format string that is used when creating label for the axis out of the QDateTime object.
209 Sets \a format string that is used when creating label for the axis out of the QDateTime object.
209 Check QDateTime documentation for information on how the string should be defined.
210 Check QDateTime documentation for information on how the string should be defined.
210 \sa format()
211 \sa format()
211 */
212 */
212 void QDateTimeAxis::setFormat(QString format)
213 void QDateTimeAxis::setFormat(QString format)
213 {
214 {
214 Q_D(QDateTimeAxis);
215 Q_D(QDateTimeAxis);
215 d->m_format = format;
216 d->m_format = format;
216 }
217 }
217
218
218 /*!
219 /*!
219 Returns the format string that is used when creating label for the axis out of the QDateTime object.
220 Returns the format string that is used when creating label for the axis out of the QDateTime object.
220 Check QDateTime documentation for information on how the string should be defined.
221 Check QDateTime documentation for information on how the string should be defined.
221 \sa setFormat()
222 \sa setFormat()
222 */
223 */
223 QString QDateTimeAxis::format() const
224 QString QDateTimeAxis::format() const
224 {
225 {
225 Q_D(const QDateTimeAxis);
226 Q_D(const QDateTimeAxis);
226 return d->m_format;
227 return d->m_format;
227 }
228 }
228
229
229 /*!
230 /*!
230 Sets \a count for ticks on the axis.
231 Sets \a count for ticks on the axis.
231 */
232 */
232 void QDateTimeAxis::setTickCount(int count)
233 void QDateTimeAxis::setTickCount(int count)
233 {
234 {
234 Q_D(QDateTimeAxis);
235 Q_D(QDateTimeAxis);
235 if (d->m_tickCount != count && count >=2) {
236 if (d->m_tickCount != count && count >=2) {
236 d->m_tickCount = count;
237 d->m_tickCount = count;
237 d->emitUpdated();
238 d->emitUpdated();
238 }
239 }
239 }
240 }
240
241
241 /*!
242 /*!
242 \fn int QDateTimeAxis::tickCount() const
243 \fn int QDateTimeAxis::tickCount() const
243 Return number of ticks on the axis
244 Return number of ticks on the axis
244 */
245 */
245 int QDateTimeAxis::tickCount() const
246 int QDateTimeAxis::tickCount() const
246 {
247 {
247 Q_D(const QDateTimeAxis);
248 Q_D(const QDateTimeAxis);
248 return d->m_tickCount;
249 return d->m_tickCount;
249 }
250 }
250
251
251 /*!
252 /*!
252 Returns the type of the axis
253 Returns the type of the axis
253 */
254 */
254 QAbstractAxis::AxisType QDateTimeAxis::type() const
255 QAbstractAxis::AxisType QDateTimeAxis::type() const
255 {
256 {
256 return AxisTypeDateTime;
257 return AxisTypeDateTime;
257 }
258 }
258
259
259 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260
261
261 QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis* q):
262 QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis* q):
262 QAbstractAxisPrivate(q),
263 QAbstractAxisPrivate(q),
263 m_min(QDateTime::fromMSecsSinceEpoch(0)),
264 m_min(QDateTime::fromMSecsSinceEpoch(0)),
264 m_max(QDateTime::fromMSecsSinceEpoch(0)),
265 m_max(QDateTime::fromMSecsSinceEpoch(0)),
265 m_tickCount(5)
266 m_tickCount(5)
266 {
267 {
267 m_format = "dd-MM-yyyy\nh:mm";
268 m_format = "dd-MM-yyyy\nh:mm";
268 }
269 }
269
270
270 QDateTimeAxisPrivate::~QDateTimeAxisPrivate()
271 QDateTimeAxisPrivate::~QDateTimeAxisPrivate()
271 {
272 {
272
273
273 }
274 }
274
275
275 void QDateTimeAxisPrivate::handleDomainUpdated()
276 void QDateTimeAxisPrivate::handleDomainUpdated()
276 {
277 {
277 Q_Q(QDateTimeAxis);
278 Q_Q(QDateTimeAxis);
278 Domain* domain = qobject_cast<Domain*>(sender());
279 Domain* domain = qobject_cast<Domain*>(sender());
279 Q_ASSERT(domain);
280 Q_ASSERT(domain);
280
281
281 if(orientation()==Qt::Horizontal){
282 if(orientation()==Qt::Horizontal){
282 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
283 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
283 }else if(orientation()==Qt::Vertical){
284 }else if(orientation()==Qt::Vertical){
284 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
285 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
285 }
286 }
286 }
287 }
287
288
288
289
289 void QDateTimeAxisPrivate::setMin(const QVariant &min)
290 void QDateTimeAxisPrivate::setMin(const QVariant &min)
290 {
291 {
291 Q_Q(QDateTimeAxis);
292 Q_Q(QDateTimeAxis);
292 if (min.canConvert(QVariant::DateTime))
293 if (min.canConvert(QVariant::DateTime))
293 q->setMin(min.toDateTime());
294 q->setMin(min.toDateTime());
294 }
295 }
295
296
296 void QDateTimeAxisPrivate::setMax(const QVariant &max)
297 void QDateTimeAxisPrivate::setMax(const QVariant &max)
297 {
298 {
298
299
299 Q_Q(QDateTimeAxis);
300 Q_Q(QDateTimeAxis);
300 if (max.canConvert(QVariant::DateTime))
301 if (max.canConvert(QVariant::DateTime))
301 q->setMax(max.toDateTime());
302 q->setMax(max.toDateTime());
302 }
303 }
303
304
304 void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max)
305 void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max)
305 {
306 {
306 Q_Q(QDateTimeAxis);
307 Q_Q(QDateTimeAxis);
307 if (min.canConvert(QVariant::DateTime) && max.canConvert(QVariant::DateTime))
308 if (min.canConvert(QVariant::DateTime) && max.canConvert(QVariant::DateTime))
308 q->setRange(min.toDateTime(), max.toDateTime());
309 q->setRange(min.toDateTime(), max.toDateTime());
309 }
310 }
310
311
311 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
312 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
312 {
313 {
313 Q_Q(QDateTimeAxis);
314 Q_Q(QDateTimeAxis);
314 if(m_orientation == Qt::Vertical){
315 if(m_orientation == Qt::Vertical){
315 return new ChartDateTimeAxisY(q,presenter);
316 return new ChartDateTimeAxisY(q,presenter);
316 }else{
317 }else{
317 return new ChartDateTimeAxisX(q,presenter);
318 return new ChartDateTimeAxisX(q,presenter);
318 }
319 }
319
320
320 }
321 }
321
322
322 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
323 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
323 {
324 {
324 Q_Q(QDateTimeAxis);
325 Q_Q(QDateTimeAxis);
325 if(m_max == m_min) {
326 if(m_max == m_min) {
326 if(m_orientation==Qt::Vertical){
327 if(m_orientation==Qt::Vertical){
327 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
328 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
328 }else{
329 }else{
329 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
330 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
330 }
331 }
331 } else {
332 } else {
332 if(m_orientation==Qt::Vertical){
333 if(m_orientation==Qt::Vertical){
333 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
334 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
334 }else{
335 }else{
335 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
336 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
336 }
337 }
337 }
338 }
338 }
339 }
339
340
340 #include "moc_qdatetimeaxis.cpp"
341 #include "moc_qdatetimeaxis.cpp"
341 #include "moc_qdatetimeaxis_p.cpp"
342 #include "moc_qdatetimeaxis_p.cpp"
342
343
343 QTCOMMERCIALCHART_END_NAMESPACE
344 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now