customplot例程(1)--增加图例

五迷三道 提交于 2019-11-26 09:15:08
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();

 

 

 

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

运行结果如下

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!