main.cpp
109 lines
| 3.5 KiB
| text/x-c
|
CppLexer
Miikka Heikkinen
|
r2854 | /**************************************************************************** | ||
Marek Rosa
|
r1739 | ** | ||
Miikka Heikkinen
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
Marek Rosa
|
r1739 | ** | ||
Miikka Heikkinen
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
Marek Rosa
|
r1739 | ** | ||
Miikka Heikkinen
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
Titta Heikkala
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
Miikka Heikkinen
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
Marek Rosa
|
r1739 | ** | ||
Titta Heikkala
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
Miikka Heikkinen
|
r2854 | ****************************************************************************/ | ||
Marek Rosa
|
r1739 | |||
Titta Heikkala
|
r2714 | #include <QtWidgets/QApplication> | ||
#include <QtWidgets/QMainWindow> | ||||
#include <QtCharts/QChartView> | ||||
#include <QtCharts/QLineSeries> | ||||
#include <QtCore/QDateTime> | ||||
#include <QtCharts/QDateTimeAxis> | ||||
#include <QtCore/QFile> | ||||
#include <QtCore/QTextStream> | ||||
#include <QtCore/QDebug> | ||||
#include <QtCharts/QValueAxis> | ||||
Marek Rosa
|
r1739 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
Marek Rosa
|
r1739 | |||
int main(int argc, char *argv[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
Jani Honkonen
|
r2098 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); | ||
Marek Rosa
|
r1739 | |||
//![1] | ||||
Jani Honkonen
|
r2102 | QLineSeries *series = new QLineSeries(); | ||
Marek Rosa
|
r1739 | //![1] | ||
//![2] | ||||
Marek Rosa
|
r1931 | // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt | ||
Marek Rosa
|
r1986 | // http://www.swpc.noaa.gov/ftpdir/weekly/README | ||
// http://www.weather.gov/disclaimer | ||||
Marek Rosa
|
r1931 | QFile sunSpots(":sun"); | ||
if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||||
return 1; | ||||
Marek Rosa
|
r1739 | } | ||
Marek Rosa
|
r1931 | |||
QTextStream stream(&sunSpots); | ||||
while (!stream.atEnd()) { | ||||
QString line = stream.readLine(); | ||||
if (line.startsWith("#") || line.startsWith(":")) | ||||
continue; | ||||
Marek Rosa
|
r1938 | QStringList values = line.split(" ", QString::SkipEmptyParts); | ||
Marek Rosa
|
r1931 | QDateTime momentInTime; | ||
Marek Rosa
|
r1938 | momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15)); | ||
series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble()); | ||||
Marek Rosa
|
r1931 | } | ||
sunSpots.close(); | ||||
Marek Rosa
|
r1739 | //![2] | ||
//![3] | ||||
Jani Honkonen
|
r2102 | QChart *chart = new QChart(); | ||
Marek Rosa
|
r1739 | chart->addSeries(series); | ||
Marek Rosa
|
r1986 | chart->legend()->hide(); | ||
chart->setTitle("Sunspots count (by Space Weather Prediction Center)"); | ||||
//![3] | ||||
//![4] | ||||
Marek Rosa
|
r1739 | QDateTimeAxis *axisX = new QDateTimeAxis; | ||
Marek Rosa
|
r1931 | axisX->setTickCount(10); | ||
axisX->setFormat("MMM yyyy"); | ||||
Michal Klocek
|
r2273 | axisX->setTitleText("Date"); | ||
Marek Rosa
|
r2332 | chart->addAxis(axisX, Qt::AlignBottom); | ||
series->attachAxis(axisX); | ||||
QValueAxis *axisY = new QValueAxis; | ||||
axisY->setLabelFormat("%i"); | ||||
axisY->setTitleText("Sunspots count"); | ||||
chart->addAxis(axisY, Qt::AlignLeft); | ||||
series->attachAxis(axisY); | ||||
Marek Rosa
|
r1739 | //![4] | ||
Marek Rosa
|
r1986 | |||
//![5] | ||||
Jani Honkonen
|
r2102 | QChartView *chartView = new QChartView(chart); | ||
Marek Rosa
|
r1739 | chartView->setRenderHint(QPainter::Antialiasing); | ||
//![5] | ||||
Marek Rosa
|
r1986 | |||
//![6] | ||||
Marek Rosa
|
r1739 | QMainWindow window; | ||
window.setCentralWidget(chartView); | ||||
Titta Heikkala
|
r2647 | window.resize(820, 600); | ||
Marek Rosa
|
r1739 | window.show(); | ||
Marek Rosa
|
r1986 | //![6] | ||
Marek Rosa
|
r1739 | |||
return a.exec(); | ||||
} | ||||