QCustomPlot绘图示例

   ui->widgetBackgroundSound->legend->setVisible(true);

    QCPGraph* g1 = ui->widgetBackgroundSound->addGraph();
    QCPGraph* g2 = ui->widgetBackgroundSound->addGraph();
    QCPGraph* g3 = ui->widgetBackgroundSound->addGraph();
    QCPGraph* g4 = nullptr;
    QCPGraph* g5 = nullptr;
    if (!y4File.isEmpty())
    {
        g4 = ui->widgetBackgroundSound->addGraph();
    }
    if (!y5File.isEmpty())
    {
        g5 = ui->widgetBackgroundSound->addGraph();
    }

    g1->setName(y1Name);
    g2->setName(y2Name);
    g3->setName(y3Name);
    if (g4) g4->setName(y4Name);
    if (g5) g5->setName(y5Name);


    if (ui->widgetBackgroundSound->legend->hasElement(0, 0))
    {
        ui->widgetBackgroundSound->legend->insertRow(0);
    }

    arma::vec xdata;
    xdata.load(xFile.toStdString(), arma::raw_ascii);

    arma::vec y1data;
    y1data.load(y1File.toStdString(), arma::raw_ascii);

    arma::vec y2data;
    y2data.load(y2File.toStdString(), arma::raw_ascii);

    arma::vec y3data;
    y3data.load(y3File.toStdString(), arma::raw_ascii);

    arma::vec y4data;
    if (!y4File.isEmpty())
    {
        y4data.load(y4File.toStdString(), arma::raw_ascii);
    }
    
    arma::vec y5data;
    if (!y5File.isEmpty())
    {
        y5data.load(y5File.toStdString(), arma::raw_ascii);
    }

    QVector<double> x, y1, y2, y3, y4, y5;
    for (int i = 0; i < xdata.size(); ++i)
    {
        x << xdata(i);
        y1 << y1data(i);
        y2 << y2data(i);
        y3 << y3data(i);
        if (!y4File.isEmpty())
        {
            y4 << y4data(i);
        }    
        if (!y5File.isEmpty())
        {
            y5 << y5data(i);
        }
    }

    g1->setData(x, y1);
    g2->setData(x, y2);
    g3->setData(x, y3);
    if (g4) g4->setData(x, y4);
    if (g5) g5->setData(x, y5);

    //设置样式
    g1->setPen(QPen(Qt::blue, 2.0, Qt::SolidLine));
    g2->setPen(QPen(Qt::red, 2.0, Qt::DashLine));
    g3->setPen(QPen(Qt::black, 2.0, Qt::DotLine));
    if (g4) g4->setPen(QPen(Qt::darkMagenta, 2.0, Qt::DashDotLine));
    if (g5) g5->setPen(QPen(Qt::darkGreen, 2.0, Qt::DashDotDotLine));

    ui->widgetBackgroundSound->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->widgetBackgroundSound->xAxis->setLabel(QStringLiteral("作用距离range (km)"));
    ui->widgetBackgroundSound->yAxis->setLabel(QStringLiteral("级 (dB)"));
    ui->widgetBackgroundSound->axisRect()->insetLayout()->setInsetAlignment(0, align);
    ui->widgetBackgroundSound->xAxis->setRange(QCPRange(xLow, xHigh));
    ui->widgetBackgroundSound->yAxis->setRange(QCPRange(yLow, yHigh));
    ui->widgetBackgroundSound->replot();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CppBlock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值