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