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