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