美文网首页C++Qt学习
QCustomPlot入门--Apple的学习笔记

QCustomPlot入门--Apple的学习笔记

作者: applecai | 来源:发表于2019-11-20 21:42 被阅读0次

    前言

    QCustomPlot,QWT,QChart是qt的3大图形插件。
    QCustomPlot我调研下来是三者中最好的。
    安装入门:https://www.qcustomplot.com/index.php/tutorials/settingup
    官网教程:https://www.qcustomplot.com/index.php/tutorials/basicplotting
    参考安装入门网址,很方便的将QCustomPlot集成到QT5.12。
    参考官网教程,可以快速入门plot绘制。因为我总觉得plot我需要学习下,将来工作中能帮助到我。

    入门代码

        // 产生数据:
        QVector<double> x(101), y(101); // initialize with entries 0..100
        for (int i=0; i<101; ++i)
        {
          x[i] = i/50.0 - 1; // x goes from -1 to 1
          y[i] = x[i]*x[i]; // let's plot a quadratic function
        }
        // create graph and assign data to it:
        QCustomPlot *pCustomPlot = ui->customPlot;
        QCPGraph *pGraph = pCustomPlot->addGraph();
        // 设置背景色
        pCustomPlot->setBackground(QColor(200, 200, 130));
        // 连接数据
        pCustomPlot->graph(0)->setData(x, y);
        // give the axes some labels:
        pCustomPlot->xAxis->setLabel("x");
        pCustomPlot->yAxis->setLabel("y");
        // set axes ranges, so we see all data:
        pCustomPlot->xAxis->setRange(-1, 1.5);
        pCustomPlot->yAxis->setRange(0, 1);
        // 虚线背景格子的刻度
        pCustomPlot->xAxis->ticker()->setTickCount(4);
        // 设置描绘线的格式,包括折线,脉冲线等
        pCustomPlot->graph(0)->setLineStyle(QCPGraph::lsLine );
        // 可以显示所有数据,若一开始的x轴显示范围设置为-1 到 1,则不需要添加此句
        pCustomPlot->graph(0)->rescaleAxes();
        // 设置绘制图形的颜色
        pGraph->setPen(QPen(QColor(255, 0, 0)));
        // 设置图形离散值标注的格式,包括点,叉,空心圆,菱形等
        pGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDiamond, 3));
        // 填充色,RGB最后一个是通道的透明度
        pGraph->setBrush(QBrush(QColor(255,160,50,30)));
        // 绘制线可移动,可拖放,可选择
        pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
        // 追加点
        // pCustomPlot->graph(0)->addData(0,1);
        // 设置x/y轴文本色、轴线色、字体等
        // x轴不显示文本
        // pCustomPlot->xAxis->setTickLabels(false);
        pCustomPlot->xAxis->setTickLabelColor(Qt::darkCyan);
        // 设置x轴的标签字体颜色
        pCustomPlot->xAxis->setLabelColor(QColor(255, 0, 0));
        // 设置x轴画笔的颜色
        pCustomPlot->xAxis->setBasePen(QPen(QColor(255, 0, 0)));
        // 设置x轴大坐标刻度画笔的颜色
        pCustomPlot->xAxis->setTickPen(QPen(QColor(0, 0, 255)));
        // 设置x轴小坐标刻度画笔的颜色
        pCustomPlot->xAxis->setSubTickPen(QColor(55, 188, 77));
        // 在最大值方向画箭头
        pCustomPlot->xAxis->setUpperEnding(QCPLineEnding::esLineArrow);
        // 设置x轴画笔的粗细
        QFont xFont = pCustomPlot->xAxis->labelFont();
        xFont.setPixelSize(20);
        pCustomPlot->xAxis->setLabelFont(xFont);
        // 同样的方法去设置y轴格式
        pCustomPlot->yAxis->setTickLabelColor(Qt::blue);
        pCustomPlot->yAxis->setLabelColor(QColor(0, 160, 230));
        pCustomPlot->yAxis->setBasePen(QPen(QColor(32, 178, 170)));
        pCustomPlot->yAxis->setTickPen(QPen(QColor(128, 0, 255)));
        pCustomPlot->yAxis->setSubTickPen(QColor(255, 165, 0));
        QFont yFont = pCustomPlot->yAxis->labelFont();
        yFont.setPixelSize(20);
        pCustomPlot->yAxis->setLabelFont(yFont);
    
        // 设置右边和顶端的坐标需要显示,但是不显示刻度
        pCustomPlot->xAxis2->setVisible(true);
        pCustomPlot->xAxis2->setTickLabels(false);
        pCustomPlot->yAxis2->setVisible(true);
        pCustomPlot->yAxis2->setTickLabels(false);
    
        // 设置背景线格式
        QCPGrid *pGrid = ui->customPlot->yAxis->grid();
        // 设置背景线可见
        pGrid->setVisible(true);
        // 设置背景线大格子按y轴的大刻度延长线颜色为绿色,为实线
        pGrid->setPen(QPen(QColor(0, 255, 0), 0, Qt::SolidLine));
        // 设置背景线小刻度延长线可见
        pGrid->setSubGridVisible(true);
        // 设置背景线小格子按y轴的小刻度延长线颜色为蓝色,为虚线
        pGrid->setSubGridPen(QPen(QColor(0, 0, 255), 0, Qt::DotLine));
        // 重绘
        // ui->customPlot->replot();
    

    运行效果

    效果1.png

    还可以拖动放大,是我最喜欢的功能


    效果3.png

    相关文章

      网友评论

        本文标题:QCustomPlot入门--Apple的学习笔记

        本文链接:https://www.haomeiwen.com/subject/lnpaictx.html