1、获取当前时间
void MainWindow::dataupdate()
{
//声明时间获取变量,实际情况中,获取的是当前时间的总秒数
double nowtime = QTime::currentTime().minute()*60+QTime::currentTime().second();
//设置x轴名称
ui->complot->xAxis->setLabel("时间");
//设置x轴范围,实际情况设置的是5秒之内的数据
ui->complot->xAxis->setRange(nowtime-5,nowtime);
//设置y轴范围
ui->complot->yAxis->setRange(ydown,yup);
//添加数据,效果就是在曲线的右侧添加数据进来,因为x数据是最新的时间
ui->complot->graph(0)->addData(nowtime,y);
//必须刷新曲线
ui->complot->replot();
}
2、没有获取当前时间
(1)mainwindow.h中定义变量
(2)mainwindow.c中初始化x坐标轴
void MainWindow::QPlot_init(QCustomPlot *customPlot)
{
// 图表添加两条曲线
pGraph1_1 = customPlot->addGraph();
pGraph1_2 = customPlot->addGraph();
//坐标轴使用时间刻度
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
customPlot->xAxis->setTicker(timeTicker);
customPlot->xAxis->setTickLabelRotation(30);//设置x轴时间旋转角度为30度
}
(3)x轴时间显示效果