##// END OF EJS Templates
datetimeaxis example documented
Marek Rosa -
r1969:ea1f56016da9
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 \page examples.html
2 \page examples.html
3 \title Examples
3 \title Examples
4 \keyword Examples
4 \keyword Examples
5
5
6 \raw HTML
6 \raw HTML
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 <tr>
8 <tr>
9 <th class="titleheader" width="33%">
9 <th class="titleheader" width="33%">
10 List of examples
10 List of examples
11 </th>
11 </th>
12 </tr>
12 </tr>
13 <tr>
13 <tr>
14 <td valign="top">
14 <td valign="top">
15 <ul>
15 <ul>
16 <li><a href="examples-areachart.html">Area chart</a></li>
16 <li><a href="examples-areachart.html">Area chart</a></li>
17 <li><a href="examples-barchart.html">Bar chart</a></li>
17 <li><a href="examples-barchart.html">Bar chart</a></li>
18 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
18 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
19 <li><a href="examples-customchart.html">Custom chart</a></li>
19 <li><a href="examples-customchart.html">Custom chart</a></li>
20 <li><a href="examples-datetimeaxis.html">DateTimeAxis example</a></li>
20 <li><a href="examples-donutbreakdown.html">Donut breakdown chart</a></li>
21 <li><a href="examples-donutbreakdown.html">Donut breakdown chart</a></li>
21 <li><a href="examples-donutchart.html">Donut chart</a></li>
22 <li><a href="examples-donutchart.html">Donut chart</a></li>
22 <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li>
23 <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li>
23 <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li>
24 <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li>
24 <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li>
25 <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li>
25 <li><a href="examples-legend.html">Legend</a></li>
26 <li><a href="examples-legend.html">Legend</a></li>
26 <li><a href="examples-lineandbar.html">Line and barchart</a></li>
27 <li><a href="examples-lineandbar.html">Line and barchart</a></li>
27 <li><a href="examples-linechart.html">Line chart</a></li>
28 <li><a href="examples-linechart.html">Line chart</a></li>
28 <li><a href="examples-modeldata.html">Model data</a></li>
29 <li><a href="examples-modeldata.html">Model data</a></li>
29 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
30 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
30 <li><a href="examples-piechart.html">Pie chart</a></li>
31 <li><a href="examples-piechart.html">Pie chart</a></li>
31 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
32 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
32 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
33 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
33 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
34 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
34 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
35 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
35 <li><a href="examples-splinechart.html">Spline chart</a></li>
36 <li><a href="examples-splinechart.html">Spline chart</a></li>
36 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
37 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
37 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
38 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
38 <li><a href="examples-temperaturerecords.html">Temperature records</a></li>
39 <li><a href="examples-temperaturerecords.html">Temperature records</a></li>
39 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
40 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
40 </ul>
41 </ul>
41 </td>
42 </td>
42 </tr>
43 </tr>
43 </table>
44 </table>
44 \endraw
45 \endraw
45 */
46 */
@@ -1,87 +1,92
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QLineSeries>
24 #include <QLineSeries>
25 #include <QDateTime>
25 #include <QDateTime>
26 #include <QDateTimeAxis>
26 #include <QDateTimeAxis>
27 #include <QFile>
27 #include <QFile>
28 #include <QTextStream>
28 #include <QTextStream>
29 #include <QDebug>
29 #include <QDebug>
30
30
31 QTCOMMERCIALCHART_USE_NAMESPACE
31 QTCOMMERCIALCHART_USE_NAMESPACE
32
32
33 int main(int argc, char *argv[])
33 int main(int argc, char *argv[])
34 {
34 {
35 QApplication a(argc, argv);
35 QApplication a(argc, argv);
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
37
37
38 //![1]
38 //![1]
39 QLineSeries* series = new QLineSeries();
39 QLineSeries* series = new QLineSeries();
40 //![1]
40 //![1]
41
41
42 //![2]
42 //![2]
43 // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt
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 QFile sunSpots(":sun");
46 QFile sunSpots(":sun");
45 if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) {
47 if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) {
46 return 1;
48 return 1;
47 }
49 }
48
50
49 QTextStream stream(&sunSpots);
51 QTextStream stream(&sunSpots);
50 while (!stream.atEnd()) {
52 while (!stream.atEnd()) {
51 QString line = stream.readLine();
53 QString line = stream.readLine();
52 if (line.startsWith("#") || line.startsWith(":"))
54 if (line.startsWith("#") || line.startsWith(":"))
53 continue;
55 continue;
54 QStringList values = line.split(" ", QString::SkipEmptyParts);
56 QStringList values = line.split(" ", QString::SkipEmptyParts);
55 QDateTime momentInTime;
57 QDateTime momentInTime;
56 momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));
58 momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));
57 series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
59 series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
58 }
60 }
59 sunSpots.close();
61 sunSpots.close();
60 //![2]
62 //![2]
61
63
62 //![3]
64 //![3]
63 QChart* chart = new QChart();
65 QChart* chart = new QChart();
64 chart->legend()->hide();
65 chart->addSeries(series);
66 chart->addSeries(series);
67 chart->legend()->hide();
66 chart->createDefaultAxes();
68 chart->createDefaultAxes();
69 chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
70 //![3]
71
72 //![4]
67 QDateTimeAxis *axisX = new QDateTimeAxis;
73 QDateTimeAxis *axisX = new QDateTimeAxis;
68 axisX->setTickCount(10);
74 axisX->setTickCount(10);
69 axisX->setFormat("MMM yyyy");
75 axisX->setFormat("MMM yyyy");
70 chart->setAxisX(axisX, series);
76 chart->setAxisX(axisX, series);
71 chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
72 //![3]
73
74 //![4]
77 //![4]
78
79 //![5]
75 QChartView* chartView = new QChartView(chart);
80 QChartView* chartView = new QChartView(chart);
76 chartView->setRenderHint(QPainter::Antialiasing);
81 chartView->setRenderHint(QPainter::Antialiasing);
77 //![4]
78
79 //![5]
82 //![5]
83
84 //![6]
80 QMainWindow window;
85 QMainWindow window;
81 window.setCentralWidget(chartView);
86 window.setCentralWidget(chartView);
82 window.resize(800, 600);
87 window.resize(800, 600);
83 window.show();
88 window.show();
84 //![5]
89 //![6]
85
90
86 return a.exec();
91 return a.exec();
87 }
92 }
General Comments 0
You need to be logged in to leave comments. Login now