##// END OF EJS Templates
datetimeaxis example documented
Marek Rosa -
r1986:012112176bdd
parent child
Show More
@@ -0,0 +1,37
1 /*!
2 \example examples/datetimeaxis
3 \title DateTimeAxis Example
4 \subtitle
5
6 The example shows how to use QLineChart with QDateTimeAxis.
7
8 \image examples_datetimeaxis.png
9
10 To create line chart, QLineSeries instance is needed. Let's create one.
11
12 \snippet ../examples/datetimeaxis/main.cpp 1
13
14 On the charts we will present how the number of sun spots changes in time. The data (by Space Weather Prediction Center) is read from a text file.
15 In the snippet below notice how QDateTime::toMSecsSinceEpoch method is used to convert the QDateTime object into a number that can be passed to QLineSeries append method.
16
17 \snippet ../examples/datetimeaxis/main.cpp 2
18
19 To present the data on the char we need QChart instance. We add the series to it, hide the legend, create the default axes and set the title of the chart.
20
21 \snippet ../examples/datetimeaxis/main.cpp 3
22
23 Becasue we use QLineSeries calling createDefaultAxes will create QValueAxis both as X and Y axis. To use QDateTimeAxis we need to set it manually to the chart.
24 First the instance of QDateTimeAxis is created, then the number of ticks that are to be shown is set. The number of sun spots is provided as an average for the month therfore we don't need the axis labels to contain the information about the time and the day. This is achived by setting a custom label format.
25 Please refer to QDateTime::toString() method documntation to learn about the avaiable format options.
26
27 \snippet ../examples/datetimeaxis/main.cpp 4
28
29 Then we create a QChartView object with QChart as a parameter. This way we don't need to create QGraphicsView scene ourselves. We also set the Antialiasing on to have the rendered lines look nicer.
30
31 \snippet ../examples/datetimeaxis/main.cpp 5
32
33 Chart is ready to be shown.
34
35 \snippet ../examples/datetimeaxis/main.cpp 6
36
37 */
@@ -1,45 +1,46
1 1 /*!
2 2 \page examples.html
3 3 \title Examples
4 4 \keyword Examples
5 5
6 6 \raw HTML
7 7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 8 <tr>
9 9 <th class="titleheader" width="33%">
10 10 List of examples
11 11 </th>
12 12 </tr>
13 13 <tr>
14 14 <td valign="top">
15 15 <ul>
16 16 <li><a href="examples-areachart.html">Area chart</a></li>
17 17 <li><a href="examples-barchart.html">Bar chart</a></li>
18 18 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
19 19 <li><a href="examples-customchart.html">Custom chart</a></li>
20 <li><a href="examples-datetimeaxis.html">DateTimeAxis example</a></li>
20 21 <li><a href="examples-donutbreakdown.html">Donut breakdown chart</a></li>
21 22 <li><a href="examples-donutchart.html">Donut chart</a></li>
22 23 <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li>
23 24 <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li>
24 25 <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li>
25 26 <li><a href="examples-legend.html">Legend</a></li>
26 27 <li><a href="examples-lineandbar.html">Line and barchart</a></li>
27 28 <li><a href="examples-linechart.html">Line chart</a></li>
28 29 <li><a href="examples-modeldata.html">Model data</a></li>
29 30 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
30 31 <li><a href="examples-piechart.html">Pie chart</a></li>
31 32 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
32 33 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
33 34 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
34 35 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
35 36 <li><a href="examples-splinechart.html">Spline chart</a></li>
36 37 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
37 38 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
38 39 <li><a href="examples-temperaturerecords.html">Temperature records</a></li>
39 40 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
40 41 </ul>
41 42 </td>
42 43 </tr>
43 44 </table>
44 45 \endraw
45 46 */
@@ -1,87 +1,92
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 <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QLineSeries>
25 25 #include <QDateTime>
26 26 #include <QDateTimeAxis>
27 27 #include <QFile>
28 28 #include <QTextStream>
29 29 #include <QDebug>
30 30
31 31 QTCOMMERCIALCHART_USE_NAMESPACE
32 32
33 33 int main(int argc, char *argv[])
34 34 {
35 35 QApplication a(argc, argv);
36 36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
37 37
38 38 //![1]
39 39 QLineSeries* series = new QLineSeries();
40 40 //![1]
41 41
42 42 //![2]
43 43 // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt
44 // http://www.swpc.noaa.gov/ftpdir/weekly/README
45 // http://www.weather.gov/disclaimer
44 46 QFile sunSpots(":sun");
45 47 if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) {
46 48 return 1;
47 49 }
48 50
49 51 QTextStream stream(&sunSpots);
50 52 while (!stream.atEnd()) {
51 53 QString line = stream.readLine();
52 54 if (line.startsWith("#") || line.startsWith(":"))
53 55 continue;
54 56 QStringList values = line.split(" ", QString::SkipEmptyParts);
55 57 QDateTime momentInTime;
56 58 momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));
57 59 series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
58 60 }
59 61 sunSpots.close();
60 62 //![2]
61 63
62 64 //![3]
63 QChart* chart = new QChart();
64 chart->legend()->hide();
65 QChart* chart = new QChart();
65 66 chart->addSeries(series);
67 chart->legend()->hide();
66 68 chart->createDefaultAxes();
69 chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
70 //![3]
71
72 //![4]
67 73 QDateTimeAxis *axisX = new QDateTimeAxis;
68 74 axisX->setTickCount(10);
69 75 axisX->setFormat("MMM yyyy");
70 76 chart->setAxisX(axisX, series);
71 chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
72 //![3]
73
74 77 //![4]
78
79 //![5]
75 80 QChartView* chartView = new QChartView(chart);
76 81 chartView->setRenderHint(QPainter::Antialiasing);
77 //![4]
78
79 82 //![5]
83
84 //![6]
80 85 QMainWindow window;
81 86 window.setCentralWidget(chartView);
82 87 window.resize(800, 600);
83 88 window.show();
84 //![5]
89 //![6]
85 90
86 91 return a.exec();
87 92 }
General Comments 0
You need to be logged in to leave comments. Login now