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