qcustomPlot 动态曲线
注:项目需要,故阅读官方demo,学习后应用在项目中。
(官方代码截选)
static QTime time(QTime::currentTime());
// calculate two new data points:
double key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
static double lastPointKey = 0;
if (key-lastPointKey > 0.002) // at most add point every 2 ms
{
// add data to lines:
ui->customPlot->graph(0)->addData(key, qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843));
ui->customPlot->gaph(1)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364));
// rescale value (vertical) axis to fit the current data:
//ui->customPlot->graph(0)->rescaleValueAxis();
//ui->customPlot->graph(1)->rescaleValueAxis(true);
lastPointKey = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
ui->customPlot->replot();
效果图如下:
本项目中部分代码如下:
void Kinect_respiratory_Qt::realtimeDataSlot() {
static QTime time(QTime::currentTime());
//key 单位为秒
double key = time.elapsed() / 1000; //演示开始后经过的时间,单位为秒
static double lastPointKey = 0;
//数据输入
if (key - lastPointKey > 0.002)
{
if (!(currentOptimalValue.empty()))
{
if (camShiftTrackers.size() == 1)
{
double value0 = currentOptimalValue[0];
double weightValue0 = tempWeightValue[0];
if (ui.checkBox_obj1->isChecked())
{
ui.widget_respiratory_signal->graph(0)->addData(key, value0);
ui.widget_respiratory_signal->graph(1)->addData(key,weightValue0);
}
}
}
lastPointKey = key;
}
ui.widget_respiratory_signal->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iSelectPlottables);//支持鼠标拖放、滚动缩放
ui.widget_respiratory_signal->graph(0)->rescaleValueAxis();
ui.widget_respiratory_signal->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc,5));//显示散点
ui.widget_respiratory_signal->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));
ui.widget_respiratory_signal->graph(1)->rescaleValueAxis(true);//自动设定graph(1)曲线在y轴的范围
ui.widget_respiratory_signal->xAxis->setRange(key,8,Qt::AlignRight);
ui.widget_respiratory_signal->replot();
}