| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "wavechart.h" |
| 5 |
#include <qmath.h> |
| 6 |
#include <qrandom.h> |
| 7 |
|
| 8 |
QT_USE_NAMESPACE |
| 9 |
|
| 10 |
static const int numPoints =100; |
| 11 |
|
| 12 |
WaveChart::WaveChart(QChart* chart, QWidget* parent) : |
| 13 |
QChartView(chart, parent), |
| 14 |
m_series(new QLineSeries()), |
| 15 |
m_wave(0), |
| 16 |
m_step(2 * M_PI / numPoints) |
| 17 |
{ |
| 18 |
QPen blue(Qt::blue); |
| 19 |
blue.setWidth(3); |
| 20 |
m_series->setPen(blue); |
| 21 |
|
| 22 |
chart->legend()->setVisible(false); |
| 23 |
|
| 24 |
int fluctuate = 100; |
| 25 |
|
| 26 |
for (qreal x = 0; x <= 2 * M_PI; x += m_step) { |
| 27 |
m_series->append(x, fabs(sin(x) * fluctuate)); |
| 28 |
} |
| 29 |
|
| 30 |
chart->addSeries(m_series); |
| 31 |
chart->createDefaultAxes(); |
| 32 |
|
| 33 |
QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(update())); |
| 34 |
m_timer.setInterval(5000); |
| 35 |
m_timer.start(); |
| 36 |
} |
| 37 |
|
| 38 |
void WaveChart::update() |
| 39 |
{ |
| 40 |
|
| 41 |
int fluctuate; |
| 42 |
const QList<QPointF>& points = m_series->points(); |
| 43 |
for (qreal i = 0, x = 0; x <= 2 * M_PI; x += m_step, i++) { |
| 44 |
fluctuate = QRandomGenerator::global()->bounded(100); |
| 45 |
m_series->replace(x,points[i].y(),x,fabs(sin(x) * fluctuate)); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
} |