1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtWidgets/QApplication> |
31 | #include <QtWidgets/QMainWindow> |
32 | #include <QtCharts/QChartView> |
33 | #include <QtCharts/QLineSeries> |
34 | #include <QtCore/QDateTime> |
35 | #include <QtCharts/QDateTimeAxis> |
36 | #include <QtCore/QFile> |
37 | #include <QtCore/QTextStream> |
38 | #include <QtCore/QDebug> |
39 | #include <QtCharts/QValueAxis> |
40 | |
41 | QT_CHARTS_USE_NAMESPACE |
42 | |
43 | int main(int argc, char *argv[]) |
44 | { |
45 | QApplication a(argc, argv); |
46 | |
47 | //![1] |
48 | QLineSeries *series = new QLineSeries(); |
49 | //![1] |
50 | |
51 | //![2] |
52 | // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt |
53 | // http://www.swpc.noaa.gov/ftpdir/weekly/README |
54 | // http://www.weather.gov/disclaimer |
55 | QFile sunSpots(":sun" ); |
56 | if (!sunSpots.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { |
57 | return 1; |
58 | } |
59 | |
60 | QTextStream stream(&sunSpots); |
61 | while (!stream.atEnd()) { |
62 | QString line = stream.readLine(); |
63 | if (line.startsWith(s: "#" ) || line.startsWith(s: ":" )) |
64 | continue; |
65 | QStringList values = line.split(sep: QLatin1Char(' '), behavior: Qt::SkipEmptyParts); |
66 | QDateTime momentInTime; |
67 | momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15)); |
68 | series->append(x: momentInTime.toMSecsSinceEpoch(), y: values[2].toDouble()); |
69 | } |
70 | sunSpots.close(); |
71 | //![2] |
72 | |
73 | //![3] |
74 | QChart *chart = new QChart(); |
75 | chart->addSeries(series); |
76 | chart->legend()->hide(); |
77 | chart->setTitle("Sunspots count (by Space Weather Prediction Center)" ); |
78 | //![3] |
79 | |
80 | //![4] |
81 | QDateTimeAxis *axisX = new QDateTimeAxis; |
82 | axisX->setTickCount(10); |
83 | axisX->setFormat("MMM yyyy" ); |
84 | axisX->setTitleText("Date" ); |
85 | chart->addAxis(axis: axisX, alignment: Qt::AlignBottom); |
86 | series->attachAxis(axis: axisX); |
87 | |
88 | QValueAxis *axisY = new QValueAxis; |
89 | axisY->setLabelFormat("%i" ); |
90 | axisY->setTitleText("Sunspots count" ); |
91 | chart->addAxis(axis: axisY, alignment: Qt::AlignLeft); |
92 | series->attachAxis(axis: axisY); |
93 | //![4] |
94 | |
95 | //![5] |
96 | QChartView *chartView = new QChartView(chart); |
97 | chartView->setRenderHint(hint: QPainter::Antialiasing); |
98 | //![5] |
99 | |
100 | //![6] |
101 | QMainWindow window; |
102 | window.setCentralWidget(chartView); |
103 | window.resize(w: 820, h: 600); |
104 | window.show(); |
105 | //![6] |
106 | |
107 | return a.exec(); |
108 | } |
109 | |