chartview.cpp
98 lines
| 2.7 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r830 | /**************************************************************************** | ||
** | ||||
Titta Heikkala
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
Jani Honkonen
|
r830 | ** All rights reserved. | ||
Titta Heikkala
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
Jani Honkonen
|
r830 | ** | ||
Titta Heikkala
|
r2740 | ** This file is part of the Qt Charts module. | ||
Jani Honkonen
|
r830 | ** | ||
Titta Heikkala
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
Titta Heikkala
|
r2776 | ** agreement between you and The Qt Company. | ||
Jani Honkonen
|
r830 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
Titta Heikkala
|
r2740 | ** contact form at http://qt.io | ||
Jani Honkonen
|
r830 | ** | ||
****************************************************************************/ | ||||
Michal Klocek
|
r246 | #include "chartview.h" | ||
Titta Heikkala
|
r2714 | #include <QtCharts/QLineSeries> | ||
#include <QtCharts/QScatterSeries> | ||||
#include <QtCharts/QSplineSeries> | ||||
#include <QtCharts/QAreaSeries> | ||||
#include <QtCore/QTime> | ||||
Michal Klocek
|
r246 | |||
Jani Honkonen
|
r2102 | ChartView::ChartView(QChart *chart, QWidget *parent) | ||
Jani Honkonen
|
r2098 | : QChartView(chart, parent), | ||
m_index(-1), | ||||
m_chart(chart) | ||||
Michal Klocek
|
r246 | { | ||
Michal Klocek
|
r866 | m_chart->setTitle("Charts presenter"); | ||
Tero Ahola
|
r1462 | m_chart->setDropShadowEnabled(false); | ||
Jani Honkonen
|
r2098 | QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(handleTimeout())); | ||
Michal Klocek
|
r246 | m_timer.setInterval(3000); | ||
Marek Rosa
|
r1581 | //![1] | ||
Jani Honkonen
|
r2102 | QLineSeries *series0 = new QLineSeries(); | ||
Tero Ahola
|
r1226 | series0->setName("line"); | ||
Michal Klocek
|
r482 | |||
Jani Honkonen
|
r2102 | QScatterSeries *series1 = new QScatterSeries(); | ||
Tero Ahola
|
r1226 | series1->setName("scatter"); | ||
Michal Klocek
|
r482 | |||
Jani Honkonen
|
r2102 | QSplineSeries *series2 = new QSplineSeries(); | ||
Tero Ahola
|
r1226 | series2->setName("spline"); | ||
Michal Klocek
|
r560 | |||
Jani Honkonen
|
r2102 | QAreaSeries *series3 = new QAreaSeries(series0); | ||
Tero Ahola
|
r1226 | series3->setName("area"); | ||
Marek Rosa
|
r1581 | //![1] | ||
Michal Klocek
|
r246 | |||
Marek Rosa
|
r1581 | //![2] | ||
Michal Klocek
|
r246 | int numPoints = 10; | ||
for (int x = 0; x <= numPoints; ++x) { | ||||
Michal Klocek
|
r476 | qreal y = qrand() % 100; | ||
Jani Honkonen
|
r2098 | series0->append(x, y); | ||
series1->append(x, y); | ||||
series2->append(x, y); | ||||
Michal Klocek
|
r246 | } | ||
Marek Rosa
|
r1581 | //![2] | ||
Michal Klocek
|
r246 | |||
Marek Rosa
|
r1581 | //![3] | ||
Jani Honkonen
|
r2098 | m_series << series0; | ||
m_titles << m_chart->title() + ": LineChart"; | ||||
m_series << series1; | ||||
m_titles << m_chart->title() + ": ScatterChart"; | ||||
m_series << series2; | ||||
m_titles << m_chart->title() + ": SplineChart"; | ||||
m_series << series3; | ||||
m_titles << m_chart->title() + ": AreaChart"; | ||||
Marek Rosa
|
r1581 | //![3] | ||
Michal Klocek
|
r571 | |||
Michal Klocek
|
r246 | m_timer.start(); | ||
Michal Klocek
|
r871 | handleTimeout(); | ||
Michal Klocek
|
r246 | } | ||
ChartView::~ChartView() | ||||
{ | ||||
Jani Honkonen
|
r2098 | if (m_series.size() == 0) | ||
return; | ||||
Michal Klocek
|
r866 | m_chart->removeSeries(m_series.at(m_index)); | ||
Michal Klocek
|
r981 | m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone | ||
Michal Klocek
|
r257 | qDeleteAll(m_series); | ||
Michal Klocek
|
r246 | } | ||
Tero Ahola
|
r1015 | //![4] | ||
Michal Klocek
|
r246 | void ChartView::handleTimeout() | ||
{ | ||||
Jani Honkonen
|
r2098 | if (m_series.size() == 0) | ||
return; | ||||
if (m_index >= 0) | ||||
Marek Rosa
|
r1581 | m_chart->removeSeries(m_series.at(m_index)); | ||
Michal Klocek
|
r246 | m_index++; | ||
Marek Rosa
|
r1581 | m_index = m_index % m_series.size(); | ||
Michal Klocek
|
r866 | m_chart->addSeries(m_series.at(m_index)); | ||
m_chart->setTitle(m_titles.at(m_index)); | ||||
Marek Rosa
|
r1581 | m_chart->createDefaultAxes(); | ||
Michal Klocek
|
r246 | } | ||
Tero Ahola
|
r1015 | //![4] | ||