QT_03_customplot例程--增加图例

QCustomPlot *customPlot = ui->customPlot;
        customPlot->legend->setVisible(true);                     //图例 显示
        customPlot->legend->setFont(QFont("Helvetica",9));        //图例 字体
        QPen pen;                 //画笔
        QStringList lineNames;    //图例中曲线名字链表
        lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStepCenter" << "lsImpulse";
        // add graphs with different line styles:(增加曲线,并设置相应的样式)
        for (int i=QCPGraph::lsNone; i<=QCPGraph::lsImpulse; ++i)    
        { 
          customPlot->addGraph();        //增加曲线
          pen.setColor(QColor(qSin(i*1+1.2)*80+80, qSin(i*0.3+0)*80+80, qSin(i*0.3+1.5)*80+80));                                //给pen设置新颜色
          customPlot->graph()->setPen(pen);      //给曲线增加pen
          customPlot->graph()->setName(lineNames[i]);                    //图例名字
          customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i);     //曲线样式
          customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));    //曲线上点的样式

          // generate data:(生成曲线)
          QVector<double> x(15), y(15);
          for (int j=0; j<15; ++j)
          {
            x[j] = j/15.0 * 5*3.14 + 0.01;
            y[j] = 7*qSin(x[j])/x[j] - (i-QCPGraph::lsNone)*5 + (QCPGraph::lsImpulse)*5 + 2;
          }
          customPlot->graph()->setData(x, y);
          customPlot->graph()->rescaleAxes(true);
        }
        // zoom out a bit:
        customPlot->yAxis->scaleRange(1.1, customPlot->yAxis->range().center());
        customPlot->xAxis->scaleRange(1.1, customPlot->xAxis->range().center());
        // set blank axis lines:
        customPlot->xAxis->setTicks(false);
        customPlot->yAxis->setTicks(true);
        customPlot->xAxis->setTickLabels(false);
        customPlot->yAxis->setTickLabels(true);
        // make top right axes clones of bottom left axes:
        customPlot->axisRect()->setupFullAxesBox();

此上代码为官方样例。-注释

运行结果如下

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fantongl

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

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

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

打赏作者

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

抵扣说明:

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

余额充值