如何用QCustomPlot实现实时动态曲线图:代码详解
最编程
2024-02-23 08:35:10
...
该绘图方法使用QCustomPlot,定义一个继承自QCustomPlot的类Qcurve。
曲线管理
在类中,使用一个map管理各条曲线
QMap<QString, QPointer<QCPGraph>> mGraph;
图例显示:
在类的构造函数中使用
this->legend->setVisible(true);
以显示图例
定时刷新:
使用一个定时器进行定时刷新
timerTick = new QTimer();
timerTick->setInterval(50);
connect(timerTick,SIGNAL(timeout()),this,SLOT(tick()));
timerTick->start();
void QCurve::tick()
{
this->replot();
}
添加和删除曲线
添加曲线
bool addCurve(QString linename)
{
if(!mGraph.contains(linename))
{
QPointer<QCPGraph> graph = this->addGraph(this->xAxis,this->yAxis);
graph->setName(linename);
colorindex++;
colorindex = colorindex%18;
QPen pen = QPen(baseColors[colorindex]);
pen.setWidth(m_width);
graph->setPen(pen);
mGraph.insert(linename,graph);
return true;
}
else
return false;
}
删除曲线
bool deleteCurve(QString linename)
{
if(mGraph.contains(linename))
{
auto graphit = mGraph.find(linename);
this->removeGraph(graphit.value());
mGraph.remove(linename);
return true;
}
else
{
return false;
}
}
添加数据
bool addData(QString linename,int ms, double data)
{
if(mGraph.contains(linename))
{
auto graphit = mGraph.find(linename);
graphit.value()->addData(ms,data);
if(graphit.value().data()->dataCount()>m_range)
graphit.value()->removeDataBefore(m_range);
this->xAxis->rescale();
graphit.value()->rescaleValueAxis(!firstdata, true);
this->xAxis->setRange(this->xAxis->range().upper, m_range, Qt::AlignRight);
firstdata = false;
return true;
}
else //没有则添加
{
if(addCurve(linename))
return true;
return false;
}
}
修改QCustomPlot源码
本文使用个v2.0.1的qcustomplot,没有了v1.x版本的removeDataBefore函数,因此修改qcustomplot源码进行添加,在QCPGraph类中增加如下公有成员函数。
void QCPGraph::removeDataBefore(int size)
{
mDataContainer->removeBefore(mDataContainer->size()-size);
}