##// END OF EJS Templates
datetimeaxis example vertical axis format set to integers
Marek Rosa -
r2332:e54231dffb7f
parent child
Show More
@@ -1,95 +1,100
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 #include <QValueAxis>
30 #include <QValueAxis>
31
31
32 QTCOMMERCIALCHART_USE_NAMESPACE
32 QTCOMMERCIALCHART_USE_NAMESPACE
33
33
34 int main(int argc, char *argv[])
34 int main(int argc, char *argv[])
35 {
35 {
36 QApplication a(argc, argv);
36 QApplication a(argc, argv);
37 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
37 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
38
38
39 //![1]
39 //![1]
40 QLineSeries *series = new QLineSeries();
40 QLineSeries *series = new QLineSeries();
41 //![1]
41 //![1]
42
42
43 //![2]
43 //![2]
44 // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt
44 // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt
45 // http://www.swpc.noaa.gov/ftpdir/weekly/README
45 // http://www.swpc.noaa.gov/ftpdir/weekly/README
46 // http://www.weather.gov/disclaimer
46 // http://www.weather.gov/disclaimer
47 QFile sunSpots(":sun");
47 QFile sunSpots(":sun");
48 if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) {
48 if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) {
49 return 1;
49 return 1;
50 }
50 }
51
51
52 QTextStream stream(&sunSpots);
52 QTextStream stream(&sunSpots);
53 while (!stream.atEnd()) {
53 while (!stream.atEnd()) {
54 QString line = stream.readLine();
54 QString line = stream.readLine();
55 if (line.startsWith("#") || line.startsWith(":"))
55 if (line.startsWith("#") || line.startsWith(":"))
56 continue;
56 continue;
57 QStringList values = line.split(" ", QString::SkipEmptyParts);
57 QStringList values = line.split(" ", QString::SkipEmptyParts);
58 QDateTime momentInTime;
58 QDateTime momentInTime;
59 momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));
59 momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15));
60 series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
60 series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
61 }
61 }
62 sunSpots.close();
62 sunSpots.close();
63 //![2]
63 //![2]
64
64
65 //![3]
65 //![3]
66 QChart *chart = new QChart();
66 QChart *chart = new QChart();
67 chart->addSeries(series);
67 chart->addSeries(series);
68 chart->legend()->hide();
68 chart->legend()->hide();
69 chart->createDefaultAxes();
70 chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
69 chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
71 //![3]
70 //![3]
72
71
73 //![4]
72 //![4]
74 QDateTimeAxis *axisX = new QDateTimeAxis;
73 QDateTimeAxis *axisX = new QDateTimeAxis;
75 axisX->setTickCount(10);
74 axisX->setTickCount(10);
76 axisX->setFormat("MMM yyyy");
75 axisX->setFormat("MMM yyyy");
77 axisX->setTitleText("Date");
76 axisX->setTitleText("Date");
78 chart->setAxisX(axisX, series);
77 chart->addAxis(axisX, Qt::AlignBottom);
79 chart->axisY(series)->setTitleText("Sunspots count");
78 series->attachAxis(axisX);
79
80 QValueAxis *axisY = new QValueAxis;
81 axisY->setLabelFormat("%i");
82 axisY->setTitleText("Sunspots count");
83 chart->addAxis(axisY, Qt::AlignLeft);
84 series->attachAxis(axisY);
80 //![4]
85 //![4]
81
86
82 //![5]
87 //![5]
83 QChartView *chartView = new QChartView(chart);
88 QChartView *chartView = new QChartView(chart);
84 chartView->setRenderHint(QPainter::Antialiasing);
89 chartView->setRenderHint(QPainter::Antialiasing);
85 //![5]
90 //![5]
86
91
87 //![6]
92 //![6]
88 QMainWindow window;
93 QMainWindow window;
89 window.setCentralWidget(chartView);
94 window.setCentralWidget(chartView);
90 window.resize(800, 600);
95 window.resize(800, 600);
91 window.show();
96 window.show();
92 //![6]
97 //![6]
93
98
94 return a.exec();
99 return a.exec();
95 }
100 }
General Comments 0
You need to be logged in to leave comments. Login now