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 "chart.h" |
31 | #include <QtCharts/QAbstractAxis> |
32 | #include <QtCharts/QSplineSeries> |
33 | #include <QtCharts/QValueAxis> |
34 | #include <QtCore/QRandomGenerator> |
35 | #include <QtCore/QDebug> |
36 | |
37 | Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags): |
38 | QChart(QChart::ChartTypeCartesian, parent, wFlags), |
39 | m_series(0), |
40 | m_axisX(new QValueAxis()), |
41 | m_axisY(new QValueAxis()), |
42 | m_step(0), |
43 | m_x(5), |
44 | m_y(1) |
45 | { |
46 | QObject::connect(sender: &m_timer, signal: &QTimer::timeout, receiver: this, slot: &Chart::handleTimeout); |
47 | m_timer.setInterval(1000); |
48 | |
49 | m_series = new QSplineSeries(this); |
50 | QPen green(Qt::red); |
51 | green.setWidth(3); |
52 | m_series->setPen(green); |
53 | m_series->append(x: m_x, y: m_y); |
54 | |
55 | addSeries(series: m_series); |
56 | |
57 | addAxis(axis: m_axisX,alignment: Qt::AlignBottom); |
58 | addAxis(axis: m_axisY,alignment: Qt::AlignLeft); |
59 | m_series->attachAxis(axis: m_axisX); |
60 | m_series->attachAxis(axis: m_axisY); |
61 | m_axisX->setTickCount(5); |
62 | m_axisX->setRange(min: 0, max: 10); |
63 | m_axisY->setRange(min: -5, max: 10); |
64 | |
65 | m_timer.start(); |
66 | } |
67 | |
68 | Chart::~Chart() |
69 | { |
70 | |
71 | } |
72 | |
73 | void Chart::handleTimeout() |
74 | { |
75 | qreal x = plotArea().width() / m_axisX->tickCount(); |
76 | qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount(); |
77 | m_x += y; |
78 | m_y = QRandomGenerator::global()->bounded(highest: 5) - 2.5; |
79 | m_series->append(x: m_x, y: m_y); |
80 | scroll(dx: x, dy: 0); |
81 | if (m_x == 100) |
82 | m_timer.stop(); |
83 | } |
84 | |