| 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 <QtCharts/QXYSeries> | 
| 32 | #include <QtCharts/QAreaSeries> | 
| 33 | #include <QtQuick/QQuickView> | 
| 34 | #include <QtQuick/QQuickItem> | 
| 35 | #include <QtCore/QDebug> | 
| 36 | #include <QtCore/QRandomGenerator> | 
| 37 | #include <QtCore/QtMath> | 
| 38 |  | 
| 39 | QT_CHARTS_USE_NAMESPACE | 
| 40 |  | 
| 41 | Q_DECLARE_METATYPE(QAbstractSeries *) | 
| 42 | Q_DECLARE_METATYPE(QAbstractAxis *) | 
| 43 |  | 
| 44 | DataSource::DataSource(QQuickView *appViewer, QObject *parent) : | 
| 45 |     QObject(parent), | 
| 46 |     m_appViewer(appViewer), | 
| 47 |     m_index(-1) | 
| 48 | { | 
| 49 |     qRegisterMetaType<QAbstractSeries*>(); | 
| 50 |     qRegisterMetaType<QAbstractAxis*>(); | 
| 51 |  | 
| 52 |     generateData(type: 0, rowCount: 5, colCount: 1024); | 
| 53 | } | 
| 54 |  | 
| 55 | void DataSource::update(QAbstractSeries *series) | 
| 56 | { | 
| 57 |     if (series) { | 
| 58 |         QXYSeries *xySeries = static_cast<QXYSeries *>(series); | 
| 59 |         m_index++; | 
| 60 |         if (m_index > m_data.count() - 1) | 
| 61 |             m_index = 0; | 
| 62 |  | 
| 63 |         QVector<QPointF> points = m_data.at(i: m_index); | 
| 64 |         // Use replace instead of clear + append, it's optimized for performance | 
| 65 |         xySeries->replace(points); | 
| 66 |     } | 
| 67 | } | 
| 68 |  | 
| 69 | void DataSource::generateData(int type, int rowCount, int colCount) | 
| 70 | { | 
| 71 |     // Remove previous data | 
| 72 |     m_data.clear(); | 
| 73 |  | 
| 74 |     // Append the new data depending on the type | 
| 75 |     for (int i(0); i < rowCount; i++) { | 
| 76 |         QVector<QPointF> points; | 
| 77 |         points.reserve(asize: colCount); | 
| 78 |         for (int j(0); j < colCount; j++) { | 
| 79 |             qreal x(0); | 
| 80 |             qreal y(0); | 
| 81 |             switch (type) { | 
| 82 |             case 0: | 
| 83 |                 // data with sin + random component | 
| 84 |                 y = qSin(M_PI / 50 * j) + 0.5 + QRandomGenerator::global()->generateDouble(); | 
| 85 |                 x = j; | 
| 86 |                 break; | 
| 87 |             case 1: | 
| 88 |                 // linear data | 
| 89 |                 x = j; | 
| 90 |                 y = (qreal) i / 10; | 
| 91 |                 break; | 
| 92 |             default: | 
| 93 |                 // unknown, do nothing | 
| 94 |                 break; | 
| 95 |             } | 
| 96 |             points.append(t: QPointF(x, y)); | 
| 97 |         } | 
| 98 |         m_data.append(t: points); | 
| 99 |     } | 
| 100 | } | 
| 101 |  |