auto pMyCustomPlot = new QCustomPlot(this);
QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
timeTicker->setDateTimeFormat("yyyy-MM-dd\nhh:mm:ss"); // 日期和时间换行显示,防止过长,显示不下
pMyCustomPlot->xAxis->setTicker(timeTicker); // 将x轴设置为日期时间格式,你也可以将xAxis改为yAxis将Y轴设置为日期时间格式
// dt为QDateTime类型,将dt转为秒
auto xValue = dt.toSecsSinceEpoch();
auto yValue = ... // Y轴的值
pMyCustomPlot->grap(i)->addData(xValue, yValue); // 将(xValue, yValue)添加到第i条曲线上,i可以根据自己的业务更改
设置QCustomPlot坐标格式为日期时间格式(yyyy-MM-dd hh:mm:ss)
于 2024-12-24 18:49:40 首次发布