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