qcustomplot添加2个坐标系
时间: 2023-10-20 10:21:57 浏览: 380
示例qcustomplot多条曲线多个Y轴.cpp
在 QCustomPlot 中,添加 2 个坐标系需要以下步骤:
1. 创建第二个坐标系对象,使用 `QCPAxisRect` 类的 `addAxis` 方法添加第二个 x 轴和 y 轴。
```cpp
// 创建第二个坐标系对象
QCPAxisRect *secondAxisRect = new QCPAxisRect(customPlot);
customPlot->plotLayout()->addElement(1, 0, secondAxisRect);
// 添加第二个 x 轴和 y 轴
QCPAxis *secondXAxis = secondAxisRect->addAxis(QCPAxis::atBottom);
QCPAxis *secondYAxis = secondAxisRect->addAxis(QCPAxis::atLeft);
```
2. 设置第二个坐标系的范围和标签,以及第二个 x 轴和 y 轴的属性。
```cpp
// 设置第二个坐标系的范围和标签
secondAxisRect->setupFullAxesBox(true);
secondAxisRect->setRangeZoom(Qt::Horizontal);
secondAxisRect->setRangeDrag(Qt::Horizontal);
secondAxisRect->axis(QCPAxis::atBottom)->setLabel("Second X Axis");
secondAxisRect->axis(QCPAxis::atLeft)->setLabel("Second Y Axis");
// 设置第二个 x 轴和 y 轴的属性
secondXAxis->setTickLabels(false);
secondYAxis->setTickLabels(false);
secondXAxis->setTicks(false);
secondYAxis->setTicks(false);
secondXAxis->setRange(customPlot->xAxis->range());
secondYAxis->setRange(customPlot->yAxis->range());
```
3. 在绘制数据时,使用第二个坐标系的 x 轴和 y 轴。
```cpp
// 绘制数据时使用第二个坐标系的 x 轴和 y 轴
graph1->setData(xData, yData);
graph1->rescaleAxes(false);
graph1->setPen(QPen(QColor(255, 0, 0)));
graph1->setName("Graph 1");
graph1->setSelectable(QCP::stSingleData);
graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph1->addToLegend();
graph1->setVisible(true);
graph1->setLineStyle(QCPGraph::lsNone);
graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph1->setPen(QPen(QColor(255, 0, 0)));
customPlot->addGraph(secondXAxis, secondYAxis);
graph2->setData(xData, yData2);
graph2->rescaleAxes(true);
graph2->setPen(QPen(QColor(0, 0, 255)));
graph2->setName("Graph 2");
graph2->setSelectable(QCP::stSingleData);
graph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph2->addToLegend();
graph2->setVisible(true);
graph2->setLineStyle(QCPGraph::lsNone);
graph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph2->setPen(QPen(QColor(0, 0, 255)));
customPlot->addGraph(secondXAxis, secondYAxis);
```
完整代码如下:
```cpp
// 创建第二个坐标系对象
QCPAxisRect *secondAxisRect = new QCPAxisRect(customPlot);
customPlot->plotLayout()->addElement(1, 0, secondAxisRect);
// 添加第二个 x 轴和 y 轴
QCPAxis *secondXAxis = secondAxisRect->addAxis(QCPAxis::atBottom);
QCPAxis *secondYAxis = secondAxisRect->addAxis(QCPAxis::atLeft);
// 设置第二个坐标系的范围和标签
secondAxisRect->setupFullAxesBox(true);
secondAxisRect->setRangeZoom(Qt::Horizontal);
secondAxisRect->setRangeDrag(Qt::Horizontal);
secondAxisRect->axis(QCPAxis::atBottom)->setLabel("Second X Axis");
secondAxisRect->axis(QCPAxis::atLeft)->setLabel("Second Y Axis");
// 设置第二个 x 轴和 y 轴的属性
secondXAxis->setTickLabels(false);
secondYAxis->setTickLabels(false);
secondXAxis->setTicks(false);
secondYAxis->setTicks(false);
secondXAxis->setRange(customPlot->xAxis->range());
secondYAxis->setRange(customPlot->yAxis->range());
// 绘制数据时使用第二个坐标系的 x 轴和 y 轴
graph1->setData(xData, yData);
graph1->rescaleAxes(false);
graph1->setPen(QPen(QColor(255, 0, 0)));
graph1->setName("Graph 1");
graph1->setSelectable(QCP::stSingleData);
graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph1->addToLegend();
graph1->setVisible(true);
graph1->setLineStyle(QCPGraph::lsNone);
graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph1->setPen(QPen(QColor(255, 0, 0)));
customPlot->addGraph(secondXAxis, secondYAxis);
graph2->setData(xData, yData2);
graph2->rescaleAxes(true);
graph2->setPen(QPen(QColor(0, 0, 255)));
graph2->setName("Graph 2");
graph2->setSelectable(QCP::stSingleData);
graph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph2->addToLegend();
graph2->setVisible(true);
graph2->setLineStyle(QCPGraph::lsNone);
graph2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
graph2->setPen(QPen(QColor(0, 0, 255)));
customPlot->addGraph(secondXAxis, secondYAxis);
```
阅读全文