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