QCustomPlot的X轴是时间轴的曲线绘制

在这里插入图片描述

主要设置X轴的参数

SharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");

demo如下

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    dataTimer = new QTimer(this);

    //注意ui->widget 是我的customplot类
    ui->widget->addGraph();
    ui->widget->graph(0)->setPen(QPen(QColor(40, 110, 255)));

    //设置x轴格式
    QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
    timeTicker->setTimeFormat("%h:%m:%s");
    ui->widget->xAxis->setTicker(timeTicker);
    ui->widget->axisRect()->setupFullAxesBox();
    ui->widget->yAxis->setRange(-1, 1);

    int nowtime = (QTime::currentTime().hour()*60+QTime::currentTime().minute())*60+QTime::currentTime().second();

    //设置x轴为当前时间
    ui->widget->xAxis->setRange(nowtime-5,nowtime);
    ui->widget->graph(0)->addData(nowtime, 5);

    connect(dataTimer, &QTimer::timeout, this, &Widget::realtimeDataSlot);
    dataTimer->start(500);
}

Widget::~Widget()
{
    delete ui;
}

int Widget::realtimeDataSlot()
{
    static QTime timeStart = QTime::currentTime();
    double key = timeStart.msecsTo(QTime::currentTime()) / 1000.0;
    static double lastPointKey = 0;
    if (key - lastPointKey > 0.002)
    {
        ui->widget->graph(0)->addData(key, qSin(key) + std::rand() / (double)RAND_MAX * 1 * qSin(key / 0.3843));
        lastPointKey = key;
    }
    ui->widget->graph(0)->rescaleValueAxis();
    ui->widget->xAxis->setRange(key, 8, Qt::AlignRight);
    ui->widget->replot();
    return 0;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
QCustomPlot是一个强大的C++图形库,可用于创建动态曲线图。如果您想要绘制X坐标为时间的动态曲线,可以按照以下步骤操作: 1.创建一个QCustomPlot对象并设置其X轴为时间类型: ```c++ QCustomPlot *customPlot = new QCustomPlot(); customPlot->xAxis->setType(QCPAxis::atDateTime); ``` 2.创建一个QCPGraph对象并将其添加到QCustomPlot中: ```c++ QCPGraph *graph = customPlot->addGraph(); ``` 3.在每个时间点上添加数据。假设您有一个名为“data”的QVector对象,其中包含时间和值的数据: ```c++ for (int i=0; i<data.size(); ++i) { double time = QDateTime::fromString(data[i].time, "yyyy-MM-dd hh:mm:ss.zzz").toMSecsSinceEpoch(); double value = data[i].value; graph->addData(time, value); } ``` 在这里,我们将时间转换为毫秒级别,因为QCustomPlot使用毫秒作为时间单位。 4.为X设置时间格式。您可以使用QCustomPlot时间格式化字符串来设置您想要的日期和时间格式: ```c++ customPlot->xAxis->setDateTimeFormat("hh:mm:ss"); ``` 5.启用自适应X范围,以便在动态添加数据时自动扩展X范围: ```c++ customPlot->xAxis->setRange(QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime().addSecs(-60)), QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime())); customPlot->xAxis->setAutoTickStep(false); customPlot->xAxis->setTickStep(10); ``` 在这里,我们将X范围设置为最近60秒,然后启用手动刻度步长,并将其设置为10秒。 6.最后,您需要调用replot()函数来绘制动态曲线: ```c++ customPlot->replot(); ``` 完整代码示例: ```c++ QCustomPlot *customPlot = new QCustomPlot(); customPlot->xAxis->setType(QCPAxis::atDateTime); QCPGraph *graph = customPlot->addGraph(); for (int i=0; i<data.size(); ++i) { double time = QDateTime::fromString(data[i].time, "yyyy-MM-dd hh:mm:ss.zzz").toMSecsSinceEpoch(); double value = data[i].value; graph->addData(time, value); } customPlot->xAxis->setDateTimeFormat("hh:mm:ss"); customPlot->xAxis->setRange(QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime().addSecs(-60)), QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime())); customPlot->xAxis->setAutoTickStep(false); customPlot->xAxis->setTickStep(10); customPlot->replot(); ``` 希望这可以帮助您创建X坐标为时间的动态曲线

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小白鱼儿_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值