QCustomPlot 实时棒图Demo
近期,因为工作原因使用到了 QCustomPlot 来实现实时棒图功能,因此参考QCustomPlot源码包中样例,编写了一个小样例程序。
本样例程序采用定时器来模拟,数据改变刷新。
不多说了,直接上代码:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class QCPBars;
class QCustomPlot;
class QTimer;
class QComboBox;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_timeout();
void currentTextChanged(const QString &);
private:
void initBars ();
void drawBars ();
private:
Ui::Widget *ui;
QCustomPlot *m_pBarswave; //绘制组件
QCPBars *m_pDegreeTrialBars; //瞬时值,实时值绘制棒图组件
QVector<double> m_vecTicks; //棒柱的位置数据
QVector<QString> m_vecLabels; //对应棒柱子的标签
QVector<double> m_vecRealDat; //对应棒图高度值--存在几个值则存在几个棒图柱子
QComboBox *m_pBarsSelectBox;
QTimer* m_pTestTimer;
};
#endif // WIDGET_H
核心函数实现
void Widget::initBars()
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
QCustomPlot *customPlot = m_pBarswave;
QCPBarsGroup *barGroup = new QCPBarsGroup(customPlot);
m_pDegreeTrialBars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
m_pDegreeTrialBars->setBarsGroup(barGroup);
m_pDegreeTrialBars->setWidth(0.5);
//设置背景
QLinearGradient gradient(0, 0, 0, 400);
gradient.setColorAt(0, QColor(90, 90, 90));
gradient.setColorAt(0.38, QColor(105, 105, 105));
gradient.setColorAt(1, QColor(70, 70, 70));
customPlot->setBackground(QBrush(gradient));
m_pDegreeTrialBars->setBaseValue(-20);
m_pDegreeTrialBars->setAntialiased(false); // gives more crisp, pixel aligned bar borders
m_pDegreeTrialBars->setName("DegreeTrial");
m_pDegreeTrialBars->setPen(QPen(QColor(0, 168, 140).lighter(130)));
m_pDegreeTrialBars->setBrush(QColor(0, 168, 140));
customPlot->xAxis->setTickLabelRotation(60);
customPlot->xAxis->setSubTicks(false);
customPlot->xAxis->setTickLength(0, 4);
customPlot->xAxis->setBasePen(QPen(Qt::white));
customPlot->xAxis->setTickPen(QPen(Qt::white));
customPlot->xAxis->grid()->setVisible(true);
customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
customPlot->xAxis->setTickLabelColor(Qt::white);
customPlot->xAxis->setLabelColor(Qt::white);
// prepare y axis:
customPlot->yAxis->setRange(-60, 100); //设置Y向值范围
customPlot->yAxis->setPadding(5); // a bit more space to the left border
customPlot->yAxis->setLabel(tr("DgRate(%)"));
customPlot->yAxis->setBasePen(QPen(Qt::white));
customPlot->yAxis->setTickPen(QPen(Qt::white));
customPlot->yAxis->setSubTickPen(QPen(Qt::white));
customPlot->yAxis->grid()->setSubGridVisible(true);
customPlot->yAxis->setTickLabelColor(Qt::white);
customPlot->yAxis->setLabelColor(Qt::white);
customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
// setup legend:
customPlot->legend->setVisible(true);
//customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
customPlot->legend->setBrush(QColor(255, 255, 255, 100));
customPlot->legend->setBorderPen(Qt::NoPen);
QFont legendFont = font();
legendFont.setPointSize(5);
customPlot->legend->setFont(legendFont);
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}
void Widget::drawBars()
{
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
int size = m_pBarsSelectBox->currentIndex()+1;
for (int i=0; i<size; ++i)
{
m_vecTicks[i] = i+1;
m_vecLabels[i] = QString::number(i+1);
m_vecRealDat[i] = ((qrand()%10) * (qrand()%20)) % 100;
}
textTicker->addTicks(m_vecTicks, m_vecLabels);
m_pBarswave->xAxis->setRange(0, 1+size);
m_pDegreeTrialBars->setData(m_vecTicks, m_vecRealDat);
m_pBarswave->replot();
}
样例源码:
https://download.csdn.net/download/wangxuejun1972/21111493