Customplot多条曲线的控制
前言
开始使用Qcharts画图,大数据性能极差。于是转用Customplot画图,主要进行数据的实时更新和大量数据的加载
一、模拟数据
采用子线程创建模拟数据,采用队列存储。
#pragma once
#include <QThread>
#include<QQueue>
#include<QMap>
#include<QMutex>
#include<QPointF>
#include<QVariant>
using namespace std;
class QTgui : public QThread
{
Q_OBJECT
signals:
void sigCurrentImage1(QVariant img);
public:
QTgui(QObject *parent = nullptr);
~QTgui();
std::thread * tthread;
void run();
void SetRandNum();
private:
int a;
QString data;
QMutex tex;
QMap<QString, QQueue<QPointF>> datamap;
};
#include "QTgui .h"
#include <QDebug>
#include <QThread>
#include <QTime>
#include<QQueue>
#include<QMap>
QTgui::QTgui(QObject *parent)
: QThread(parent)
{
a = 0;
}
QTgui ::~QTgui()
{
}
void QTgui::run()
{
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
while (1)
{
SetRandNum();
QVariant var;
var.setValue(datamap);
emit sigCurrentImage1(var);
this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void QTgui::SetRandNum()
{
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < 3600; j++)
{
QTime time(QTime::currentTime());
int nowtime = time.msecsSinceStartOfDay();
QPointF currentpoint = QPointF(nowtime, qrand() % 101);
if (datamap.count(QString::number(i)) > 0)
{
if (datamap[QString::number(i)].size() > 3600)
{
datamap[QString::number(i)].dequeue();
datamap[QString::number(i)].enqueue(currentpoint);
}
else
{
datamap[QString::number(i)].enqueue(currentpoint);
}
}
else
{
QQueue<QPointF> dataqueue;
dataqueue.enqueue(currentpoint);
datamap.insert(QString::number(i), dataqueue);
}
}
auto aa = 0;
}
}
数据有坐标点和时间点2种,测试注释掉了时间点。
二、主界面槽函数绑定
主要用于连接数据线程和UI槽函数的连接。
#pragma once
#include<QPointF>
#include <QtWidgets/QWidget>
#include "ui_QtWidgetsApplication7.h"
#include "QTgui .h"
#include<QQueue>
#include<QMap>
class QtWidgetsApplication7 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication7(QWidget *parent = Q_NULLPTR);
void Init();
~QtWidgetsApplication7();
QTgui* g1;
signals:
void SendData(QMap<QString, QQueue<QPointF>> data);
private:
Ui::QtWidgetsApplication7Class ui;
int a = 1;
private slots:
void slot2(QVariant data);
};
#include "QtWidgetsApplication7.h"
#include"qcustomplot.h"
#include"LineChartTest.h"
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
QtWidgetsApplication7::QtWidgetsApplication7(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
Init();
}
void QtWidgetsApplication7::Init()
{
g1 = new QTgui(this);
g1->start();
connect(g1, SIGNAL(sigCurrentImage1(QVariant)), this, SLOT(slot2(QVariant)));
LineChartTest *cht = new LineChartTest(this);
connect(this, SIGNAL(SendData(QMap<QString, QQueue<QPointF>>)), cht, SLOT(ShowData(QMap<QString, QQueue<QPointF>>)));
connect(this, SIGNAL(SendData(QMap<QString, QQueue<QPointF>>)), cht, SLOT(Run(QMap<QString, QQueue<QPointF>>)));
QVBoxLayout *layout3 = new QVBoxLayout();
layout3->addWidget(cht);
ui.widget->setLayout(layout3);
}
QtWidgetsApplication7::~QtWidgetsApplication7()
{
g1->terminate();
g1->deleteLater();
}
void QtWidgetsApplication7::slot2(QVariant data)
{
ui.label->setText(QString::number(a++));
QMap<QString, QQueue<QPointF>> d=data.value<QMap<QString, QQueue<QPointF>>>();
SendData(d);
}
三、UI界面设置和显示
展示实时数据和更新界面。启用定时器更新界面。
#pragma once
#include <QtWidgets/QWidget>
#include "QTgui .h"
#include "ui_LineChartTest.h"
#include "QCustomPlot.h"
using namespace std;
class LineChartTest : public QWidget
{
Q_OBJECT
public:
LineChartTest(QWidget *parent = Q_NULLPTR);
~LineChartTest();
void initPlotForm();
public slots:
void ShowData(QMap<QString, QQueue<QPointF>> a);
void Show_Plot();
void Run(QMap<QString, QQueue<QPointF>> data);
private:
Ui::LineChartTest ui;
int data;
void ShowChart();
QCustomPlot *customPlot;
QCPGraph *series[32];
QQueue<QPointF> point;
QTimer *timer;
bool status;
};
#include "LineChartTest.h"
#include <QWidget>
#include "QCustomPlot.h"
#pragma execution_character_set("utf-8")
LineChartTest::LineChartTest(QWidget *parent)
: QWidget(parent), status(false)
{
ui.setupUi(this);
initPlotForm();
timer = new QTimer();
timer->start(200);
connect(timer, SIGNAL(timeout()), this, SLOT(Show_Plot()));
QThread *thd = new QThread();
customPlot->moveToThread(thd);
thd->start();
}
void LineChartTest::ShowChart()
{
}
LineChartTest::~LineChartTest()
{
timer->deleteLater();
}
void LineChartTest::ShowData(QMap<QString, QQueue<QPointF>> getdata)
{
}
void LineChartTest::initPlotForm()
{
this->setWindowFlags(Qt::FramelessWindowHint);
this->setWindowState((windowState()&~(Qt::WindowMinimized | Qt::WindowFullScreen)) | Qt::WindowMaximized);
QVBoxLayout *layout = new QVBoxLayout();
customPlot = new QCustomPlot(this);
customPlot->xAxis->ticker()->setTickCount(37);
customPlot->xAxis->setLabel("X轴");
customPlot->yAxis->setLabel("Y轴");
customPlot->yAxis->setRange(-30, 200);
customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
customPlot->xAxis->setLowerEnding(QCPLineEnding::esDisc);
customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
layout->addWidget(customPlot);
customPlot->setOpenGl(true);
customPlot->addGraph(0)->setPen(QPen(Qt::red));
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui.widget->setLayout(layout);
}
void LineChartTest::Run(QMap<QString, QQueue<QPointF>> data)
{
int nowtime = QTime::currentTime().msecsSinceStartOfDay();
customPlot->clearGraphs();
customPlot->xAxis->rescale(true);
customPlot->yAxis->rescale(true);
customPlot->xAxis->setRange(0, data["0"].size());
series[0]= customPlot->addGraph();
series[0]->setName("温度1");
series[0]->setPen(QPen(Qt::red));
series[1] = customPlot->addGraph();
series[1]->setName("湿度1");
series[1]->setPen(QPen(Qt::blue));
for (int i = 0; i < data[QString::number(0)].size(); i++)
{
series[0]->addData(i, data[QString::number(0)].at(i).y());
series[1]->addData(i, data[QString::number(0)].at(i).y()+50);
}
}
void LineChartTest::Show_Plot()
{
if (status)
{
for (int i = 0; i < 2; i++)
{
if (series[i]->name() == "温度1")
{
series[i]->setVisible(false);
}
}
status = !status;
}
else
{
for (int i = 0; i < 2; i++)
{
if (series[i]->name() == "湿度1")
{
series[i]->setVisible(false);
}
}
status = !status;
}
customPlot->replot(QCustomPlot::rpQueuedReplot);
}
效果展示
下载地址:https://download.csdn.net/download/qq_38491692/85311194
总结
本文主要介绍了一个基本的绘制曲线的流程,线程产生数据,定时更新。解决界面卡顿问题。
|