chartview.cpp
100 lines
| 2.8 KiB
| text/x-c
|
CppLexer
|
r830 | /**************************************************************************** | ||
** | ||||
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
|
r830 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r830 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r830 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r246 | #include "chartview.h" | ||
|
r2714 | #include <QtCharts/QLineSeries> | ||
#include <QtCharts/QScatterSeries> | ||||
#include <QtCharts/QSplineSeries> | ||||
#include <QtCharts/QAreaSeries> | ||||
#include <QtCore/QTime> | ||||
|
r246 | |||
|
r2102 | ChartView::ChartView(QChart *chart, QWidget *parent) | ||
|
r2098 | : QChartView(chart, parent), | ||
m_index(-1), | ||||
m_chart(chart) | ||||
|
r246 | { | ||
|
r866 | m_chart->setTitle("Charts presenter"); | ||
|
r1462 | m_chart->setDropShadowEnabled(false); | ||
|
r2098 | QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(handleTimeout())); | ||
|
r246 | m_timer.setInterval(3000); | ||
|
r1581 | //![1] | ||
|
r2102 | QLineSeries *series0 = new QLineSeries(); | ||
|
r1226 | series0->setName("line"); | ||
|
r482 | |||
|
r2102 | QScatterSeries *series1 = new QScatterSeries(); | ||
|
r1226 | series1->setName("scatter"); | ||
|
r482 | |||
|
r2102 | QSplineSeries *series2 = new QSplineSeries(); | ||
|
r1226 | series2->setName("spline"); | ||
|
r560 | |||
|
r2102 | QAreaSeries *series3 = new QAreaSeries(series0); | ||
|
r1226 | series3->setName("area"); | ||
|
r1581 | //![1] | ||
|
r246 | |||
|
r1581 | //![2] | ||
|
r246 | int numPoints = 10; | ||
for (int x = 0; x <= numPoints; ++x) { | ||||
|
r476 | qreal y = qrand() % 100; | ||
|
r2098 | series0->append(x, y); | ||
series1->append(x, y); | ||||
series2->append(x, y); | ||||
|
r246 | } | ||
|
r1581 | //![2] | ||
|
r246 | |||
|
r1581 | //![3] | ||
|
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"; | ||||
|
r1581 | //![3] | ||
|
r571 | |||
|
r246 | m_timer.start(); | ||
|
r871 | handleTimeout(); | ||
|
r246 | } | ||
ChartView::~ChartView() | ||||
{ | ||||
|
r2098 | if (m_series.size() == 0) | ||
return; | ||||
|
r866 | m_chart->removeSeries(m_series.at(m_index)); | ||
|
r981 | m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone | ||
|
r257 | qDeleteAll(m_series); | ||
|
r246 | } | ||
|
r1015 | //![4] | ||
|
r246 | void ChartView::handleTimeout() | ||
{ | ||||
|
r2098 | if (m_series.size() == 0) | ||
return; | ||||
if (m_index >= 0) | ||||
|
r1581 | m_chart->removeSeries(m_series.at(m_index)); | ||
|
r246 | m_index++; | ||
|
r1581 | m_index = m_index % m_series.size(); | ||
|
r866 | m_chart->addSeries(m_series.at(m_index)); | ||
m_chart->setTitle(m_titles.at(m_index)); | ||||
|
r1581 | m_chart->createDefaultAxes(); | ||
|
r246 | } | ||
|
r1015 | //![4] | ||