datasource.cpp
90 lines
| 2.6 KiB
| text/x-c
|
CppLexer
Miikka Heikkinen
|
r2488 | /**************************************************************************** | ||
** | ||||
Titta Heikkala
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
Miikka Heikkinen
|
r2488 | ** All rights reserved. | ||
Titta Heikkala
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
Miikka Heikkinen
|
r2488 | ** | ||
Titta Heikkala
|
r2740 | ** This file is part of the Qt Charts module. | ||
Miikka Heikkinen
|
r2488 | ** | ||
Titta Heikkala
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
Titta Heikkala
|
r2776 | ** agreement between you and The Qt Company. | ||
Miikka Heikkinen
|
r2488 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
Titta Heikkala
|
r2740 | ** contact form at http://qt.io | ||
Miikka Heikkinen
|
r2488 | ** | ||
****************************************************************************/ | ||||
#include "datasource.h" | ||||
Titta Heikkala
|
r2714 | #include <QtCharts/QXYSeries> | ||
#include <QtCharts/QAreaSeries> | ||||
Miikka Heikkinen
|
r2488 | #include <QtQuick/QQuickView> | ||
#include <QtQuick/QQuickItem> | ||||
Titta Heikkala
|
r2714 | #include <QtCore/QDebug> | ||
#include <QtCore/QtMath> | ||||
Miikka Heikkinen
|
r2488 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
Miikka Heikkinen
|
r2488 | |||
Q_DECLARE_METATYPE(QAbstractSeries *) | ||||
Titta Heikkala
|
r2694 | Q_DECLARE_METATYPE(QAbstractAxis *) | ||
Miikka Heikkinen
|
r2488 | |||
DataSource::DataSource(QQuickView *appViewer, QObject *parent) : | ||||
QObject(parent), | ||||
m_appViewer(appViewer), | ||||
m_index(-1) | ||||
{ | ||||
qRegisterMetaType<QAbstractSeries*>(); | ||||
Titta Heikkala
|
r2694 | qRegisterMetaType<QAbstractAxis*>(); | ||
Miikka Heikkinen
|
r2488 | |||
generateData(0, 5, 1024); | ||||
} | ||||
void DataSource::update(QAbstractSeries *series) | ||||
{ | ||||
Heikkinen Miikka
|
r2526 | if (series) { | ||
QXYSeries *xySeries = static_cast<QXYSeries *>(series); | ||||
m_index++; | ||||
if (m_index > m_data.count() - 1) | ||||
m_index = 0; | ||||
Miikka Heikkinen
|
r2488 | |||
Miikka Heikkinen
|
r2798 | QVector<QPointF> points = m_data.at(m_index); | ||
Heikkinen Miikka
|
r2526 | // Use replace instead of clear + append, it's optimized for performance | ||
xySeries->replace(points); | ||||
} | ||||
Miikka Heikkinen
|
r2488 | } | ||
void DataSource::generateData(int type, int rowCount, int colCount) | ||||
{ | ||||
// Remove previous data | ||||
Miikka Heikkinen
|
r2798 | foreach (QVector<QPointF> row, m_data) | ||
Miikka Heikkinen
|
r2488 | row.clear(); | ||
m_data.clear(); | ||||
// Append the new data depending on the type | ||||
for (int i(0); i < rowCount; i++) { | ||||
Miikka Heikkinen
|
r2798 | QVector<QPointF> points; | ||
points.reserve(colCount); | ||||
Miikka Heikkinen
|
r2488 | for (int j(0); j < colCount; j++) { | ||
qreal x(0); | ||||
qreal y(0); | ||||
switch (type) { | ||||
case 0: | ||||
// data with sin + random component | ||||
Titta Heikkala
|
r2613 | y = qSin(3.14159265358979 / 50 * j) + 0.5 + (qreal) rand() / (qreal) RAND_MAX; | ||
Miikka Heikkinen
|
r2488 | x = j; | ||
break; | ||||
case 1: | ||||
// linear data | ||||
x = j; | ||||
y = (qreal) i / 10; | ||||
break; | ||||
default: | ||||
// unknown, do nothing | ||||
break; | ||||
} | ||||
points.append(QPointF(x, y)); | ||||
} | ||||
m_data.append(points); | ||||
} | ||||
} | ||||