qcustomPlot 2.0绘制动态曲线(坐标轴可移动)

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();

}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值