QCustomplot (三) 多个曲线、带状填充

QCustomplot (二) 为正弦曲线线型号、宽度和颜色、填充 一文,我们为正弦曲线增加了线型、线宽、颜色和坐标轴填充。很多时候我们需要曲线并不止一个,这一节要完成的功能是:

  • 增加一个余弦曲线
  • 设置曲线间填充

如果你没有忘记,增加多个曲线只需要在画布上调用addGraph方法,两个曲线设置方法完全一样,不过是通过graph序号来区分他们的属性设置,这都是旧知识了;为了显示坐标之间围成的区域,setChannelFillGraph(QCPGraph)将会被使用,将从调用曲线开始到指定曲线围成的面积

    QVector<double> x,ys,yc;

    for(double xi=-2*M_PI;xi<2*M_PI;xi+=0.1)
    {
        x.push_back(xi);
        ys.push_back(sin(xi));
        yc.push_back(cos(xi));
    }

    QPen pen;
    pen.setColor(Qt::red);
    this->ui->qcust_curve->addGraph(0);
    this->ui->qcust_curve->graph(0)->setData(x,ys);
    this->ui->qcust_curve->graph(0)->setName("y=sin(x)");
    this->ui->qcust_curve->graph(0)->setPen(pen);
    this->ui->qcust_curve->rescaleAxes(true);

    pen.setColor(Qt::blue);
    this->ui->qcust_curve->addGraph();
    this->ui->qcust_curve->graph(1)->setData(x,yc);
    this->ui->qcust_curve->graph(1)->setName("y=cos(x)");
    this->ui->qcust_curve->graph(1)->setPen(pen);
    this->ui->qcust_curve->rescaleAxes(true);

    this->ui->qcust_curve->graph(0)->setBrush(QBrush(QColor(255,50,30,20)));
    this->ui->qcust_curve->graph(0)->setChannelFillGraph( this->ui->qcust_curve->graph(1));

    this->ui->qcust_curve->xAxis->setLabel("X");
    this->ui->qcust_curve->yAxis->setLabel("Y");
    this->ui->qcust_curve->legend->setVisible(true);

结果如下图:
在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值