PlotLine.h
//
// Created by dengch on 2023/9/12.
//
#ifndef HIGHALTITUDEPLATFORM_PLOTLINE_H
#define HIGHALTITUDEPLATFORM_PLOTLINE_H
#include "QObject"
#include "QtCharts"
#include <QChartView>
#include <QSplineSeries>
#include <QScatterSeries>
#include <QDebug>
#include <QValueAxis>
class PlotLine: public QObject
{
Q_OBJECT
public:
PlotLine(QChartView* chartView, QObject* parent = nullptr);
void receivedData(int);
private:
int maxSize;
int maxX;
int maxY;
QValueAxis* axisY;
QValueAxis* axisX;
QList<int> data;
QSplineSeries* splineSeries;
QChart* chart;
QChartView* chartView;
public slots:
void timerTimeOut();
};
#endif //HIGHALTITUDEPLATFORM_PLOTLINE_H
PlotLine.cpp
//
// Created by dengch on 2023/9/12.
//
#include "PlotLine.h"
#include "QtMath"
PlotLine::PlotLine(QChartView* chartView, QObject* parent) : chartView(chartView), QObject(parent)
{
maxSize = 51;
maxX = 5000;
maxY = 200;
splineSeries = new QSplineSeries();
chart = new QChart();
axisY = new QValueAxis();
axisX = new QValueAxis();
chart->legend()->hide();
chart->setTitle("实时动态曲线示例");
chart->addSeries(splineSeries);
axisY->setLabelFormat("%i");
axisY->setTitleText("温度/℃");
chart->addAxis(axisY, Qt::AlignLeft);
axisY->setRange(-maxY, maxY);
splineSeries->attachAxis(axisY);
axisX->setLabelFormat("%i");
axisX->setTitleText("时间/ms");
chart->addAxis(axisX, Qt::AlignBottom);
axisX->setRange(0, maxX);
splineSeries->attachAxis(axisX);
chartView->setChart(chart);
chartView->setRenderHint(QPainter::Antialiasing);
qsrand(time(NULL));
}
void PlotLine::receivedData(int value)
{
data.append(value);
while (data.size() > maxSize)
{
/* 移除data中第一个数据 */
data.removeFirst();
}
splineSeries->clear();
int xSpace = maxX / (maxSize - 1);
for (int i = 0; i < data.size(); ++i)
{
splineSeries->append(xSpace * i, data.at(i));
}
}
void PlotLine::timerTimeOut()
{
auto data = qrand() % maxY;
maxY = qMax(maxY, data);
axisY->setRange(-maxY, maxY * 2);
receivedData(data);
}
mainwindow.h
//
// Created by dengch on 2023/9/11.
//
#ifndef HIGHALTITUDEPLATFORM_MAINWINDOW_H
#define HIGHALTITUDEPLATFORM_MAINWINDOW_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
~MainWindow() override;
private:
Ui::MainWindow* ui;
QTimer* timer;
};
#endif //HIGHALTITUDEPLATFORM_MAINWINDOW_H
mainwindow.cpp
//
// Created by dengch on 2023/9/11.
//
// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
#include "mainwindow.h"
#include "ui_MainWindow.h"
#include "../Chart/Draw.h"
#include "../Chart/PlotLine.h"
MainWindow::MainWindow(QWidget* parent) : QWidget(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout* vbox = new QVBoxLayout;
QChartView* chartView = new QChartView(this);
vbox->addWidget(chartView);
ui->groupBox_tbq->setLayout(vbox);
PlotLine* plotLine = new PlotLine(chartView, this);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, plotLine, &PlotLine::timerTimeOut);
timer->start(50);
}
MainWindow::~MainWindow()
{
delete ui;
}