Qcustomplot 时间轴显示曲线

#ifndef LOADPLOT_H
#define LOADPLOT_H

#include "liftcommon_global.h"

#include <QWidget>
#include "qcustomplot.h"

class LIFTCOMMONSHARED_EXPORT LoadPlot : public QCustomPlot
{
    Q_OBJECT
public:
    explicit LoadPlot(QWidget *parent = nullptr);

    void setLineValue(double yValue);

    void addData(double key, double value);

private:
    QCPGraph *m_graph;
    QCPGraph *m_graphLimit;
    QCPItemStraightLine *m_line;

    QVector<double> m_keyvec;
    QVector<double> m_valuevec;
};

#endif // LOADPLOT_H
#include "loadplot.h"

LoadPlot::LoadPlot(QWidget *parent) :
    QCustomPlot(parent)
{
    setBackground(QBrush(QColor(53, 54, 57)));

    legend->setVisible(true);
    legend->setFont(QFont("Helvetica",8));
    legend->setBrush(QColor(255,255,255,0));
    legend->setTextColor(QColor(225,255,18));
    legend->setBorderPen(Qt::NoPen);

    m_line = new QCPItemStraightLine(this);
    m_line->setPen(QPen(Qt::red));
    m_line->setLayer("overlay");
    m_line->setClipToAxisRect(true);

    m_line->point1->setTypeX(QCPItemPosition::ptAbsolute);
    m_line->point2->setTypeY(QCPItemPosition::ptPlotCoords);
    m_line->point1->setCoords(xAxis->range().lower,0);
    m_line->point2->setCoords(xAxis->range().upper,0);

    QPen axis(QColor(98,112,128,220),0.5,Qt::DotLine);
    this->xAxis->grid()->setPen(axis);
    this->yAxis->grid()->setPen(axis);

    QFont font;
    font.setPixelSize(11);

    this->xAxis->grid()->setZeroLinePen(Qt::NoPen);
    this->xAxis->setTickLabelFont(font);
    this->xAxis->setLabelColor(QColor(217, 255, 253));
    this->xAxis->setBasePen(QPen(QColor(230,230,230,230),0.8));
    this->xAxis->setTickLabelColor(QColor(217, 255, 253));
    this->xAxis->setTickPen(QColor(217, 255, 253));
    this->xAxis->setSubTicks(false);


    this->yAxis->grid()->setZeroLinePen(Qt::NoPen);
    this->yAxis->setTickLabelFont(font);
    this->yAxis->setLabelColor(QColor(217, 255, 253));
    this->yAxis->setBasePen(QPen(QColor(230,230,230,230),0.8));
    this->yAxis->setTickLabelColor(QColor(217, 255, 253));
    this->yAxis->setTickPen(QColor(217, 255, 253));
    this->yAxis->setSubTicks(false);


    QSharedPointer<QCPAxisTickerDateTime> dateTick(new QCPAxisTickerDateTime);
    dateTick->setTickCount(8);
    dateTick->setDateTimeFormat("mm:ss");
    xAxis->setTicker(dateTick);

    m_graph = addGraph();
    m_graph->setName("吊重");
    m_graph->setPen(QPen(QColor(26, 245, 26)));
    m_graph->setBrush(QBrush(QColor(26, 245, 26,150)));

    m_graphLimit = addGraph();
    m_graphLimit->setName("预警门限");
    m_graphLimit->setPen(QPen(QColor(255, 0, 0)));

    replot();
}

void LoadPlot::setLineValue(double yValue)
{
    m_line->point1->setCoords(xAxis->range().lower, yValue);
    m_line->point2->setCoords(xAxis->range().upper, yValue);
    replot();
}

void LoadPlot::addData(double key, double value)
{
    while (m_keyvec.count()>=8)
    {
        m_keyvec.pop_front();
        m_valuevec.pop_front();
    }


    m_keyvec.append(key);
    m_valuevec.append(value);

    xAxis->setRange(m_keyvec.front(),m_keyvec.back());//设定x轴的范围
    yAxis->rescale(true);

    m_graph->setData(m_keyvec,m_valuevec);
    replot();
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值