##// END OF EJS Templates
Added DateTimeAxis Example
Marek Rosa -
r1739:3245355f8941
parent child
Show More
@@ -0,0 +1,5
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4 TARGET = datetimeaxis
5 SOURCES += main.cpp
@@ -0,0 +1,82
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include <QApplication>
22 #include <QMainWindow>
23 #include <QChartView>
24 #include <QLineSeries>
25 #include <QDateTime>
26 #include <QDateTimeAxis>
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
30 int main(int argc, char *argv[])
31 {
32 QApplication a(argc, argv);
33 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
34
35 //![1]
36 QLineSeries* series = new QLineSeries();
37 //![1]
38
39 //![2]
40 QDateTime momentInTime;
41 for (int i = 0; i < 100; i++) {
42 momentInTime.setDate(QDate(2012, 1 , (1 + i / 9) % 28));
43 momentInTime.setTime(QTime(9 + i % 9, 0));
44 if (i > 0)
45 series->append(momentInTime.toMSecsSinceEpoch(), series->points().at(i - 1).y() * (0.95 + (qrand() % 11) / 100.0));
46 else
47 series->append(momentInTime.toMSecsSinceEpoch(), 45);
48 }
49 //![2]
50
51 //![3]
52 QChart* chart = new QChart();
53 chart->legend()->hide();
54 chart->addSeries(series);
55 chart->createDefaultAxes();
56 QDateTimeAxis *axisX = new QDateTimeAxis;
57 axisX->setTicksCount(20);
58 momentInTime.setDate(QDate(2012,1,1));
59 momentInTime.setTime(QTime(6, 0));
60 axisX->setMin(momentInTime);
61 momentInTime.setDate(QDate(2012,1,7));
62 momentInTime.setTime(QTime(18, 0));
63 axisX->setMax(momentInTime);
64 chart->setAxisX(axisX, series);
65 chart->setTitle("Date and Time axis chart example");
66 //![3]
67
68 //![4]
69 QChartView* chartView = new QChartView(chart);
70 chartView->setRenderHint(QPainter::Antialiasing);
71 //![4]
72
73
74 //![5]
75 QMainWindow window;
76 window.setCentralWidget(chartView);
77 window.resize(800, 600);
78 window.show();
79 //![5]
80
81 return a.exec();
82 }
@@ -1,32 +1,33
1 1 CURRENTLY_BUILDING_COMPONENTS = "examples"
2 2 !include( ../config.pri ) {
3 3 error( "Couldn't find the config.pri file!" )
4 4 }
5 5
6 6 TEMPLATE = subdirs
7 7 SUBDIRS += \
8 8 areachart \
9 9 customchart \
10 10 linechart \
11 11 percentbarchart \
12 12 piechart \
13 13 piechartdrilldown \
14 14 presenterchart \
15 15 scatterchart \
16 16 scatterinteractions \
17 17 splinechart \
18 18 stackedbarchart \
19 19 stackedbarchartdrilldown \
20 20 zoomlinechart \
21 21 modeldata \
22 22 barchart \
23 23 legend \
24 24 barmodelmapper \
25 25 qmlpiechart \
26 26 lineandbar \
27 27 horizontalbarchart \
28 28 horizontalstackedbarchart \
29 29 horizontalpercentbarchart \
30 30 donut \
31 31 donutdrilldown \
32 scrollchart
32 scrollchart \
33 datetimeaxis
@@ -1,334 +1,334
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 Note that any date before 4800 BCE or after about 1.4 million CE may not be accurately stored.
36 36
37 37 ValuesAxis can be setup to show axis line with tick marks, grid lines and shades.
38 38 Values of axis are drawn to position of ticks
39 39 */
40 40
41 41 /*!
42 42 \qmlclass ValuesAxis QDateTimeAxis
43 43 \brief The ValuesAxis element is used for manipulating chart's axes
44 44
45 45 ValueAxis can be setup to show axis line with tick marks, grid lines and shades.
46 46 Values of axis are drawn to position of ticks
47 47
48 48 To access Axes you can use ChartView API. For example:
49 49 \code
50 50 ChartView {
51 51 ValuesAxis {
52 52 id: xAxis
53 53 min: 0
54 54 max: 10
55 55 }
56 56 // Add a few series...
57 57 }
58 58 \endcode
59 59 */
60 60
61 61 /*!
62 62 \property QDateTimeAxis::min
63 63 Defines the minimum value on the axis.
64 64 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
65 65 */
66 66 /*!
67 67 \qmlproperty real ValuesAxis::min
68 68 Defines the minimum value on the axis.
69 69 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
70 70 */
71 71
72 72 /*!
73 73 \property QDateTimeAxis::max
74 74 Defines the maximum value on the axis.
75 75 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
76 76 */
77 77 /*!
78 78 \qmlproperty real ValuesAxis::max
79 79 Defines the maximum value on the axis.
80 80 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
81 81 */
82 82
83 83 /*!
84 84 \fn void QDateTimeAxis::minChanged(QDateTime min)
85 85 Axis emits signal when \a min of axis has changed.
86 86 */
87 87 /*!
88 88 \qmlsignal ValuesAxis::onMinChanged(QDateTime min)
89 89 Axis emits signal when \a min of axis has changed.
90 90 */
91 91
92 92 /*!
93 93 \fn void QDateTimeAxis::maxChanged(QDateTime max)
94 94 Axis emits signal when \a max of axis has changed.
95 95 */
96 96 /*!
97 97 \qmlsignal ValuesAxis::onMaxChanged(QDateTime max)
98 98 Axis emits signal when \a max of axis has changed.
99 99 */
100 100
101 101 /*!
102 102 \fn void QDateTimeAxis::rangeChanged(QDateTime min, QDateTime max)
103 103 Axis emits signal when \a min or \a max of axis has changed.
104 104 */
105 105
106 106 /*!
107 107 \property QDateTimeAxis::ticksCount
108 108 The number of tick marks for the axis.
109 109 */
110 110
111 111 /*!
112 112 \qmlproperty int ValuesAxis::ticksCount
113 113 The number of tick marks for the axis.
114 114 */
115 115
116 116 /*!
117 117 Constructs an axis object which is a child of \a parent.
118 118 */
119 119 QDateTimeAxis::QDateTimeAxis(QObject *parent) :
120 120 QAbstractAxis(*new QDateTimeAxisPrivate(this),parent)
121 121 {
122 122
123 123 }
124 124
125 125 /*!
126 126 \internal
127 127 */
128 128 QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d,QObject *parent) : QAbstractAxis(d,parent)
129 129 {
130 130
131 131 }
132 132
133 133 /*!
134 134 Destroys the object
135 135 */
136 136 QDateTimeAxis::~QDateTimeAxis()
137 137 {
138 138
139 139 }
140 140
141 141 void QDateTimeAxis::setMin(QDateTime min)
142 142 {
143 143 Q_D(QDateTimeAxis);
144 144 if (min.isValid())
145 145 setRange(min, qMax(d->m_max, min));
146 146 }
147 147
148 148 QDateTime QDateTimeAxis::min() const
149 149 {
150 150 Q_D(const QDateTimeAxis);
151 151 return d->m_min;
152 152 }
153 153
154 154 void QDateTimeAxis::setMax(QDateTime max)
155 155 {
156 156 Q_D(QDateTimeAxis);
157 157 if (max.isValid())
158 158 setRange(qMin(d->m_min, max), max);
159 159 }
160 160
161 161 QDateTime QDateTimeAxis::max() const
162 162 {
163 163 Q_D(const QDateTimeAxis);
164 164 return d->m_max;
165 165 }
166 166
167 167 /*!
168 168 Sets range from \a min to \a max on the axis.
169 169 If min is greater than max then this function returns without making any changes.
170 170 */
171 171 void QDateTimeAxis::setRange(QDateTime min, QDateTime max)
172 172 {
173 173 Q_D(QDateTimeAxis);
174 174 if (!min.isValid() || !max.isValid() || min > max)
175 175 return;
176 176
177 177 bool changed = false;
178 178 if (d->m_min != min) {
179 179 d->m_min = min;
180 180 changed = true;
181 181 emit minChanged(min);
182 182 }
183 183
184 184 if (d->m_max != max) {
185 185 d->m_max = max;
186 186 changed = true;
187 187 emit maxChanged(max);
188 188 }
189 189
190 190 // if(d->m_niceNumbers) d->looseNiceNumbers(d->m_min, d->m_max, d->m_tickCount);
191 191
192 192 if (changed) {
193 193 emit rangeChanged(d->m_min,d->m_max);
194 194 d->emitUpdated();
195 195 }
196 196 }
197 197
198 198 /*!
199 199 Sets \a format string that is used when creating label for the axis out of the QDateTime object.
200 200 Check QDateTime documentation for information on how the string should be defined.
201 201 \sa formatString()
202 202 */
203 203 void QDateTimeAxis::setFormatString(QString format)
204 204 {
205 205 Q_D(QDateTimeAxis);
206 206 d->m_format = format;
207 207 }
208 208
209 209 /*!
210 210 Returns the format string that is used when creating label for the axis out of the QDateTime object.
211 211 Check QDateTime documentation for information on how the string should be defined.
212 212 \sa setFormatString()
213 213 */
214 214 QString QDateTimeAxis::formatString() const
215 215 {
216 216 Q_D(const QDateTimeAxis);
217 217 return d->m_format;
218 218 }
219 219
220 220 /*!
221 221 Sets \a count for ticks on the axis.
222 222 */
223 223 void QDateTimeAxis::setTicksCount(int count)
224 224 {
225 225 Q_D(QDateTimeAxis);
226 226 if (d->m_tickCount != count && count >=2) {
227 227 d->m_tickCount = count;
228 228 d->emitUpdated();
229 229 }
230 230 }
231 231
232 232 /*!
233 233 \fn int QDateTimeAxis::ticksCount() const
234 234 Return number of ticks on the axis
235 235 */
236 236 int QDateTimeAxis::ticksCount() const
237 237 {
238 238 Q_D(const QDateTimeAxis);
239 239 return d->m_tickCount;
240 240 }
241 241
242 242 /*!
243 243 Returns the type of the axis
244 244 */
245 245 QAbstractAxis::AxisType QDateTimeAxis::type() const
246 246 {
247 247 return AxisTypeDateTime;
248 248 }
249 249
250 250 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
251 251
252 252 QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis* q):
253 253 QAbstractAxisPrivate(q),
254 254 m_tickCount(5)
255 255 {
256 256 m_min = QDateTime::fromMSecsSinceEpoch(0);
257 257 m_max = QDateTime::fromMSecsSinceEpoch(0);
258 m_format = "hh:mm:ss\ndd-mm-yyyy";
258 m_format = "dd-MMM-yy\nh:mm";
259 259 }
260 260
261 261 QDateTimeAxisPrivate::~QDateTimeAxisPrivate()
262 262 {
263 263
264 264 }
265 265
266 266 void QDateTimeAxisPrivate::handleDomainUpdated()
267 267 {
268 268 Q_Q(QDateTimeAxis);
269 269 Domain* domain = qobject_cast<Domain*>(sender());
270 270 Q_ASSERT(domain);
271 271
272 272 if(orientation()==Qt::Horizontal){
273 273 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
274 274 }else if(orientation()==Qt::Vertical){
275 275 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
276 276 }
277 277 }
278 278
279 279
280 280 void QDateTimeAxisPrivate::setMin(const QVariant &min)
281 281 {
282 282 Q_Q(QDateTimeAxis);
283 283 if (min.canConvert(QVariant::DateTime))
284 284 q->setMin(min.toDateTime());
285 285 }
286 286
287 287 void QDateTimeAxisPrivate::setMax(const QVariant &max)
288 288 {
289 289
290 290 Q_Q(QDateTimeAxis);
291 291 if (max.canConvert(QVariant::DateTime))
292 292 q->setMax(max.toDateTime());
293 293 }
294 294
295 295 void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max)
296 296 {
297 297 Q_Q(QDateTimeAxis);
298 298 if (min.canConvert(QVariant::DateTime) && max.canConvert(QVariant::DateTime))
299 299 q->setRange(min.toDateTime(), max.toDateTime());
300 300 }
301 301
302 302 ChartAxis* QDateTimeAxisPrivate::createGraphics(ChartPresenter* presenter)
303 303 {
304 304 Q_Q(QDateTimeAxis);
305 305 if(m_orientation == Qt::Vertical){
306 306 return new ChartDateTimeAxisY(q,presenter);
307 307 }else{
308 308 return new ChartDateTimeAxisX(q,presenter);
309 309 }
310 310
311 311 }
312 312
313 313 void QDateTimeAxisPrivate::intializeDomain(Domain* domain)
314 314 {
315 315 Q_Q(QDateTimeAxis);
316 316 if(m_max == m_min) {
317 317 if(m_orientation==Qt::Vertical){
318 318 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY()));
319 319 }else{
320 320 q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX()));
321 321 }
322 322 } else {
323 323 if(m_orientation==Qt::Vertical){
324 324 domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
325 325 }else{
326 326 domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch());
327 327 }
328 328 }
329 329 }
330 330
331 331 #include "moc_qdatetimeaxis.cpp"
332 332 #include "moc_qdatetimeaxis_p.cpp"
333 333
334 334 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now