##// 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 */
@@ -17,6 +17,7
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>
@@ -41,6 +41,8 int main(int argc, char *argv[])
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;
@@ -60,28 +62,31 int main(int argc, char *argv[])
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