欢迎您的访问
专注架构,Java,数据结构算法,Python技术分享

QCustomPlot使用心得二:轴范围调整,rescaleAxes 用法

官网图例https://www.qcustomplot.com/index.php/demos/simpledemo

 1   QCustomPlot* customPlot = ui->customPlot_6;
 2     // 添加两个graph
 3       customPlot->addGraph();
 4       customPlot->graph(0)->setPen(QPen(Qt::blue)); // 第一条曲线颜色
 5       customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // 第一条曲线和0轴围成区域填充的颜色
 6       customPlot->addGraph();
 7       customPlot->graph(1)->setPen(QPen(Qt::red)); // 第二条曲线颜色
 8       // 生成数据
 9       QVector<double> x(251), y0(251), y1(251);
10       for (int i=0; i<251; ++i)
11       {
12         x[i] = i;
13         y0[i] = qExp(-i/150.0)*qCos(i/10.0); // 指数衰减的cos
14         y1[i] = qExp(-i/150.0);              // 衰减指数
15       }
16       // 配置右侧和顶部轴显示刻度,但不显示标签:
17       customPlot->xAxis2->setVisible(true);
18       customPlot->xAxis2->setTickLabels(false);
19       customPlot->yAxis2->setVisible(true);
20       customPlot->yAxis2->setTickLabels(false);
21       // 让左边和下边轴与上边和右边同步改变范围
22       connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
23       connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
24       // 设置数据点
25       customPlot->graph(0)->setData(x, y0);
26       customPlot->graph(1)->setData(x, y1);
27       // 让范围自行缩放,使图0完全适合于可见区域:
28       customPlot->graph(0)->rescaleAxes();
29       // 图1也是一样自动调整范围,但只是放大范围(如果图1小于图0):
30       customPlot->graph(1)->rescaleAxes(true);
31       // 允许用户用鼠标拖动轴范围,用鼠标滚轮缩放,点击选择图形:
32       customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
33       customPlot->replot();

(1)上下轴,左右轴范围同步

利用rangeChanged信号传递轴范围QCPRange,范围改变时将xAxis的范围传给xAxis2,yAxis也是,就能实现轴范围同步了。

connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

(2)自动调整范围,使数据全部可见。

调用rescaleAxes (bool onlyEnlarge = false)重新调整与此绘图表关联的键和值轴,以显示所有的数据

onlyEnlarge 默认false,表示范围可以缩小放大,如果为true表示只能放大,而不会缩小范围。

例如曲线:

调用 customPlot->graph(0)->rescaleAxes();后范围被缩小了,曲线正好占满整个区域,但是调用customPlot->graph(0)->rescaleAxes(true)就不会有变化,因为区域不会缩小。

利用这点可以通过多次调用rescaleaxis来完整地显示多个graph的数据。让graph(0)自动缩放rescaleAxes(),在graph(0)范围的基础上rescaleAxes(true)只会扩大范围或不变而不缩小,这样最终能够显示graph(n)所有的数据

1      // 让范围自行缩放,使图0完全适合于可见区域:
2       customPlot->graph(0)->rescaleAxes();
3      // 图1也是一样自动调整范围,但只是放大或不变范围
4       customPlot->graph(1)->rescaleAxes(true);
5      // 图2也是一样自动调整范围,但只是放大或不变范围
6       customPlot->graph(2)->rescaleAxes(true);
7      // 图3也是一样自动调整范围,但只是放大或不变范围
8       customPlot->graph(2)->rescaleAxes(true);
9        。。。

 

来源:https://blog.csdn.net/yxy244

赞(1) 打赏
版权归原创作者所有,任何形式转载请联系作者;码农code之路 » QCustomPlot使用心得二:轴范围调整,rescaleAxes 用法

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏