main.cpp
92 lines
| 2.6 KiB
| text/x-c
|
CppLexer
|
r1739 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include <QApplication> | ||||
#include <QMainWindow> | ||||
#include <QChartView> | ||||
#include <QLineSeries> | ||||
#include <QDateTime> | ||||
#include <QDateTimeAxis> | ||||
|
r1931 | #include <QFile> | ||
#include <QTextStream> | ||||
#include <QDebug> | ||||
|
r1739 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
int main(int argc, char *argv[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | ||||
//![1] | ||||
QLineSeries* series = new QLineSeries(); | ||||
//![1] | ||||
//![2] | ||||
|
r1931 | // data from http://www.swpc.noaa.gov/ftpdir/weekly/RecentIndices.txt | ||
|
r1986 | // http://www.swpc.noaa.gov/ftpdir/weekly/README | ||
// http://www.weather.gov/disclaimer | ||||
|
r1931 | QFile sunSpots(":sun"); | ||
if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||||
return 1; | ||||
|
r1739 | } | ||
|
r1931 | |||
QTextStream stream(&sunSpots); | ||||
while (!stream.atEnd()) { | ||||
QString line = stream.readLine(); | ||||
if (line.startsWith("#") || line.startsWith(":")) | ||||
continue; | ||||
|
r1938 | QStringList values = line.split(" ", QString::SkipEmptyParts); | ||
|
r1931 | QDateTime momentInTime; | ||
|
r1938 | momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15)); | ||
series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble()); | ||||
|
r1931 | } | ||
sunSpots.close(); | ||||
|
r1739 | //![2] | ||
//![3] | ||||
|
r1986 | QChart* chart = new QChart(); | ||
|
r1739 | chart->addSeries(series); | ||
|
r1986 | chart->legend()->hide(); | ||
|
r1739 | chart->createDefaultAxes(); | ||
|
r1986 | chart->setTitle("Sunspots count (by Space Weather Prediction Center)"); | ||
//![3] | ||||
//![4] | ||||
|
r1739 | QDateTimeAxis *axisX = new QDateTimeAxis; | ||
|
r1931 | axisX->setTickCount(10); | ||
axisX->setFormat("MMM yyyy"); | ||||
|
r1739 | chart->setAxisX(axisX, series); | ||
//![4] | ||||
|
r1986 | |||
//![5] | ||||
|
r1739 | QChartView* chartView = new QChartView(chart); | ||
chartView->setRenderHint(QPainter::Antialiasing); | ||||
//![5] | ||||
|
r1986 | |||
//![6] | ||||
|
r1739 | QMainWindow window; | ||
window.setCentralWidget(chartView); | ||||
window.resize(800, 600); | ||||
window.show(); | ||||
|
r1986 | //![6] | ||
|
r1739 | |||
return a.exec(); | ||||
} | ||||