续qcustomplot高级轴(多轴联动):代码部分

1、初始化轴:保留x1(下)和y1(左)

2、设置标题

3、创建布局管理器,设置布局策略

4、将元素加到图层中

5、设置相同空白

6、调用 addAxisRect()

代码如下

QCPAxisRect *MPlot::addAxisRect(Qt::Orientation ori, QString &strY, QString &strX, QString &strTitile)
{
     QCPAxisRect *rect = NULL;
     if(!rect)
     {
         rect = new QCPAxisRect(this);
         //初始化标题、初始化轴
         int row, col;
         getRowCol(row, col, ori);
         qDebug() << "row" << row << "col" << col;
         initAxis(rect->axis(QCPAxis::atLeft), rect->axis(QCPAxis::atBottom), strY, strX);
         //创建标题
         QCPTextElement *title = new QCPTextElement(this);
         title->setText(strTitile);

         //布局
         QCPLayoutGrid *rectLayout = new QCPLayoutGrid;

         rectLayout->addElement(0,0,title);
         rectLayout->setRowSpacing(0);
         rectLayout->addElement(rectLayout->rowCount(), 0, rect);

         this->plotLayout()->addElement(row, col, rectLayout);
         this->plotLayout()->setRowSpacing(0);

         //将元素加到图层中
         foreach (QCPAxisRect *rect, this->axisRects())
         {
             foreach (QCPAxis *axis, rect->axes())
             {
                 axis->setLayer("axes");
                 axis->grid()->setLayer("grid");
             }
         }

         //设置相同空白
         rect->setMarginGroup(QCP::msLeft, marginGroup);
     }
     return rect;
}

//调用:图1水平方向布局,图2垂直方向布局
mPlot->addAxisRect(Qt::Horizontal, QString("V"), QString("T"), QString("v-t"));
mPlot->addAxisRect(Qt::Horizontal, QString("S"), QString("T"), QString("s-t"));

效果图如上,这例子仅供参考,如有不足之处,请大家留言。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
QCustomPlot是一个Qt的图表库,可以用来绘制统计图表和科学图表等。实现图表联动可以让多个图表之间的操作互相影响,提高用户体验。以下是一个简单的实现QCustomPlot图表联动的例子: ```cpp // 创建两个QCustomPlot对象 QCustomPlot *plot1 = new QCustomPlot(this); QCustomPlot *plot2 = new QCustomPlot(this); // 连接两个图表的xy轴 plot1->xAxis->setRange(0, 10); plot2->xAxis->setRange(0, 10); plot1->yAxis->setRange(0, 100); plot2->yAxis->setRange(0, 100); connect(plot1->xAxis, SIGNAL(rangeChanged(QCPRange)), plot2->xAxis, SLOT(setRange(QCPRange))); connect(plot1->yAxis, SIGNAL(rangeChanged(QCPRange)), plot2->yAxis, SLOT(setRange(QCPRange))); connect(plot2->xAxis, SIGNAL(rangeChanged(QCPRange)), plot1->xAxis, SLOT(setRange(QCPRange))); connect(plot2->yAxis, SIGNAL(rangeChanged(QCPRange)), plot1->yAxis, SLOT(setRange(QCPRange))); // 在两个图表中添加相同的图形 QVector<double> xData(100), yData(100), zData(100); for (int i = 0; i < 100; ++i) { xData[i] = i; yData[i] = qSin(i / 10.0); zData[i] = qCos(i / 10.0); } QCPGraph *graph1 = plot1->addGraph(); graph1->setData(xData, yData); QCPGraph *graph2 = plot2->addGraph(); graph2->setData(xData, zData); // 显示两个图表 plot1->replot(); plot2->replot(); ``` 上面的代码中,我们创建了两个QCustomPlot对象plot1和plot2,然后连接了它们的xy轴。当一个图表的发生变化时,另一个图表的相应也会发生变化,从而实现了图表联动。最后,我们在两个图表中添加相同的图形,并显示了两个图表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值