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 "datasource.h"
31#include <QtCore/QtMath>
32#include <QtCore/QRandomGenerator>
33
34QT_CHARTS_USE_NAMESPACE
35
36DataSource::DataSource(QObject *parent) :
37 QObject(parent),
38 m_index(-1)
39{
40 generateData(seriesCount: 0, rowCount: 0, colCount: 0);
41}
42
43void DataSource::update(QAbstractSeries *series, int seriesIndex)
44{
45 if (series) {
46 QXYSeries *xySeries = static_cast<QXYSeries *>(series);
47 const QVector<QVector<QPointF> > &seriesData = m_data.at(i: seriesIndex);
48 if (seriesIndex == 0)
49 m_index++;
50 if (m_index > seriesData.count() - 1)
51 m_index = 0;
52
53 QVector<QPointF> points = seriesData.at(i: m_index);
54 // Use replace instead of clear + append, it's optimized for performance
55 xySeries->replace(points);
56 }
57}
58
59void DataSource::handleSceneChanged()
60{
61 m_dataUpdater.start();
62}
63
64void DataSource::updateAllSeries()
65{
66 static int frameCount = 0;
67 static QString labelText = QStringLiteral("FPS: %1");
68
69 for (int i = 0; i < m_seriesList.size(); i++)
70 update(series: m_seriesList[i], seriesIndex: i);
71
72 frameCount++;
73 int elapsed = m_fpsTimer.elapsed();
74 if (elapsed >= 1000) {
75 elapsed = m_fpsTimer.restart();
76 qreal fps = qreal(0.1 * int(10000.0 * (qreal(frameCount) / qreal(elapsed))));
77 m_fpsLabel->setText(labelText.arg(a: QString::number(fps, f: 'f', prec: 1)));
78 m_fpsLabel->adjustSize();
79 frameCount = 0;
80 }
81}
82
83void DataSource::startUpdates(const QList<QXYSeries *> &seriesList, QLabel *fpsLabel)
84{
85 m_seriesList = seriesList;
86 m_fpsLabel = fpsLabel;
87
88 m_dataUpdater.setInterval(0);
89 m_dataUpdater.setSingleShot(true);
90 QObject::connect(sender: &m_dataUpdater, signal: &QTimer::timeout,
91 receiver: this, slot: &DataSource::updateAllSeries);
92
93 m_fpsTimer.start();
94 updateAllSeries();
95}
96
97void DataSource::generateData(int seriesCount, int rowCount, int colCount)
98{
99 m_data.clear();
100
101 qreal xAdjustment = 20.0 / (colCount * rowCount);
102 qreal yMultiplier = 3.0 / qreal(seriesCount);
103
104 // Append the new data depending on the type
105 for (int k(0); k < seriesCount; k++) {
106 QVector<QVector<QPointF> > seriesData;
107 qreal height = qreal(k) * (10.0 / qreal(seriesCount)) + 0.3;
108 for (int i(0); i < rowCount; i++) {
109 QVector<QPointF> points;
110 points.reserve(asize: colCount);
111 for (int j(0); j < colCount; j++) {
112 qreal x(0);
113 qreal y(0);
114 // data with sin + random component
115 y = height + (yMultiplier * qSin(M_PI / 50 * j)
116 + (yMultiplier * QRandomGenerator::global()->generateDouble()));
117 // 0.000001 added to make values logaxis compatible
118 x = 0.000001 + 20.0 * (qreal(j) / qreal(colCount)) + (xAdjustment * qreal(i));
119 points.append(t: QPointF(x, y));
120 }
121 seriesData.append(t: points);
122 }
123 m_data.append(t: seriesData);
124 }
125}
126

source code of qtcharts/examples/charts/openglseries/datasource.cpp