QCustomPlot use experience two: axis range adjustment, rescaleAxes usage

Official website legend https://www.qcustomplot.com/index.php/demos/simpledemo

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

(1) The range of the upper and lower axis and the left and right axis are synchronized

Use the rangeChanged signal to transfer the axis range QCPRange. When the range is changed, the range of xAxis is transferred to xAxis2, and so is the yAxis. The axis range can be synchronized.

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

(2) Automatically adjust the range to make all the data visible.

Call rescaleAxes (bool onlyEnlarge = false) to re-adjust the key and value axis associated with this drawing table to display all the data

onlyEnlarge defaults to false, indicating that the range can be reduced and enlarged, if true, it means that it can only be enlarged, but not reduced.

For example, curve:

calling customPlot->graph(0)->rescaleAxes(); After the range is reduced, the curve just occupies the entire area, but calling customPlot->graph(0)->rescaleAxes(true) will not change , Because the area will not shrink.

Using this point, you can call rescaleaxis multiple times to completely display the data of multiple graphs. Let graph(0) automatically scale rescaleAxes(), based on the range of graph(0), rescaleAxes(true) will only expand the scope or remain unchanged without shrinking, so that all the data of graph(n) can be finally displayed

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

Guess you like

Origin blog.csdn.net/qq_39400113/article/details/114998180