I am trying to use QtCharts to plot randomly-generated data into a scatter plot in real time. I want to do this using QtCharts, and have even tried repurposing the Qt Audio Example into a solution, but have had no luck. Any advice on how to correctly do this?
Asked
Active
Viewed 2,024 times
1 Answers
0
I changed Qt Scatterchart Example For you :
you need just to use a QTimer
and QRandomGenerator
.
chartview.h :
#ifndef CHARTVIEW_H
#define CHARTVIEW_H
#include <QtCharts/QChartView>
QT_CHARTS_USE_NAMESPACE
class ChartView: public QChartView
{
Q_OBJECT
public:
explicit ChartView(QWidget *parent = 0);
private:
QTimer *_timer;
int _x = 0;
};
#endif // CHARTVIEW_H
chartview.cpp :
#include "chartview.h"
#include <QtCharts/QScatterSeries>
#include <QtCharts/QLegendMarker>
#include <QTimer>
#include <QtMath>
#include <QtCore/QRandomGenerator>
ChartView::ChartView(QWidget *parent):
QChartView(new QChart(), parent)
{
QScatterSeries *series0 = new QScatterSeries();
series0->setName("scatter1");
series0->setMarkerShape(QScatterSeries::MarkerShapeCircle);
series0->setMarkerSize(15.0);
QScatterSeries *series2 = new QScatterSeries();
series2->setName("scatter2");
series2->setMarkerShape(QScatterSeries::MarkerShapeCircle);
series2->setMarkerSize(15.0);
_timer = new QTimer();
connect(_timer, &QTimer::timeout, this, [series0, this]()
{
series0->append(_x++, QRandomGenerator::global()->bounded(10));
});
_timer->start(1000);
*series2 << QPointF(1, 5) << QPointF(4, 6) << QPointF(6, 3) << QPointF(9, 5);
setRenderHint(QPainter::Antialiasing);
chart()->addSeries(series0);
chart()->addSeries(series2);
chart()->setTitle("Simple scatterchart example");
chart()->createDefaultAxes();
chart()->setDropShadowEnabled(false);
chart()->legend()->setMarkerShape(QLegend::MarkerShapeFromSeries);
}
The OutPut is :

Parisa.H.R
- 3,303
- 3
- 19
- 38
-
Thank you very much. Say I wanted to my x-value to increase by one each time so to represent each second passing, but I wanted my y-values to still plot randomly. How would I append the series so that the x-value increases by 1 each time? – MrFreeze2017 Jul 19 '21 at 04:12
-
it's easy , I will edit it to show that. I define _x variable and append it as x value in connect slots , and then increase it by x++ ; and set 1000 ms for timer means 1 second , each second connect calls and value of _x increased. – Parisa.H.R Jul 19 '21 at 06:54