1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include "chartview.h"
31#include <QtCharts/QLineSeries>
32#include <QtCharts/QScatterSeries>
33#include <QtCharts/QSplineSeries>
34#include <QtCharts/QAreaSeries>
35#include <QtCore/QRandomGenerator>
36
37ChartView::ChartView(QChart *chart, QWidget *parent)
38 : QChartView(chart, parent),
39 m_index(-1),
40 m_chart(chart)
41{
42 m_chart->setTitle("Charts presenter");
43 m_chart->setDropShadowEnabled(false);
44 QObject::connect(sender: &m_timer, SIGNAL(timeout()), receiver: this, SLOT(handleTimeout()));
45 m_timer.setInterval(3000);
46
47 //![1]
48 QLineSeries *series0 = new QLineSeries();
49 series0->setName("line");
50
51 QScatterSeries *series1 = new QScatterSeries();
52 series1->setName("scatter");
53
54 QSplineSeries *series2 = new QSplineSeries();
55 series2->setName("spline");
56
57 QAreaSeries *series3 = new QAreaSeries(series0);
58 series3->setName("area");
59 //![1]
60
61 //![2]
62 int numPoints = 10;
63
64 for (int x = 0; x <= numPoints; ++x) {
65 qreal y = QRandomGenerator::global()->bounded(highest: 100);
66 series0->append(x, y);
67 series1->append(x, y);
68 series2->append(x, y);
69 }
70 //![2]
71
72 //![3]
73 m_series << series0;
74 m_titles << m_chart->title() + ": LineChart";
75 m_series << series1;
76 m_titles << m_chart->title() + ": ScatterChart";
77 m_series << series2;
78 m_titles << m_chart->title() + ": SplineChart";
79 m_series << series3;
80 m_titles << m_chart->title() + ": AreaChart";
81 //![3]
82
83 m_timer.start();
84 handleTimeout();
85}
86
87ChartView::~ChartView()
88{
89 if (m_series.size() == 0)
90 return;
91 m_chart->removeSeries(series: m_series.at(i: m_index));
92 m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone
93 qDeleteAll(c: m_series);
94}
95
96//![4]
97void ChartView::handleTimeout()
98{
99 if (m_series.size() == 0)
100 return;
101 if (m_index >= 0)
102 m_chart->removeSeries(series: m_series.at(i: m_index));
103 m_index++;
104 m_index = m_index % m_series.size();
105 m_chart->addSeries(series: m_series.at(i: m_index));
106 m_chart->setTitle(m_titles.at(i: m_index));
107 m_chart->createDefaultAxes();
108}
109//![4]
110

source code of qtcharts/tests/manual/presenterchart/chartview.cpp