zoukankan      html  css  js  c++  java
  • QT下使用QCustomPlot绘制曲线

    在QT下绘制曲线有两种方案,一种是通过Qwt绘制,另外一种是本文将要提到的QCustomPlot进行绘制。

    在官网上下载QCustomPlot的相关压缩包,有beta以及release版本,这里我下载的release版本(自带的相关例程plot-examples)。

    将相关的源码添加到工程pro,编译个时候会报错,我们需要添加一个库 QT += printsupport

    使用QT Designer进行界面的设计,新建一个Widget,右键->提升为 QCustomPlot,

    相关初始化

    void TCWareWidget::InitCustomPlot()
    {
        QPen pen;
        pen.setColor(Qt::blue);
    
        ui->customPlot->legend->setVisible(true);
        ui->customPlot->addGraph();
        ui->customPlot->graph(0)->setPen(pen);
        ui->customPlot->graph(0)->setName("数据1");
    
        pen.setColor(Qt::red);
        ui->customPlot->addGraph();
        ui->customPlot->graph(1)->setPen(pen);
        ui->customPlot->graph(1)->setName("数据2");
    
        pen.setColor(Qt::green);
        ui->customPlot->addGraph();
        ui->customPlot->graph(2)->setPen(pen);
        ui->customPlot->graph(2)->setName("数据3");
    
        pen.setColor(Qt::cyan);
        ui->customPlot->addGraph();
        ui->customPlot->graph(3)->setPen(pen);
        ui->customPlot->graph(3)->setName("数据4");
    
        pen.setColor(Qt::magenta);
        ui->customPlot->addGraph();
        ui->customPlot->graph(4)->setPen(pen);
        ui->customPlot->graph(4)->setName("数据5");
    
        pen.setColor(Qt::yellow);
        ui->customPlot->addGraph();
        ui->customPlot->graph(5)->setPen(pen);
        ui->customPlot->graph(5)->setName("数据6");
    
        pen.setColor(Qt::darkRed);
        ui->customPlot->addGraph();
        ui->customPlot->graph(6)->setPen(pen);
        ui->customPlot->graph(6)->setName("数据7");
    
        pen.setColor(Qt::darkBlue);
        ui->customPlot->addGraph();
        ui->customPlot->graph(7)->setPen(pen);
        ui->customPlot->graph(7)->setName("数据8");
    
    //    ui->customPlot->graph(0)->setLineStyle(QCPGraph::lsNone);
    //    ui->customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross,4));
    
    //    ui->customPlot->addGraph();
    //    pen.setColor(Qt::red);
    //    ui->customPlot->graph(1)->setPen(pen);
    //    ui->customPlot->graph(1)->setName("拟合");
    
    //    ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    //    ui->customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
    //    ui->customPlot->xAxis->setAutoTickStep(false);
    //    ui->customPlot->xAxis->setTickStep(2);
    //    ui->customPlot->axisRect()->setupFullAxesBox();
    
        ui->customPlot->xAxis->setLabel("x(V)");
        ui->customPlot->yAxis->setLabel("y(ml)");
    
    
    //    ui->customPlot->resize(500,300);
    
    //    ui->customPlot->xAxis->setRange(0,11);
    //    ui->customPlot->yAxis->setRange(0,1100);
        ui->customPlot->xAxis->setRange(0,1);
        ui->customPlot->yAxis->setRange(0,1);
    
        connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
        connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
    
        ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    }

    数据显示绘制

    int TCWareWidget::ShowWavePlot()
    {
        QVector <QVector<double> > x;
        int dataSize;
    
        x.resize(readDataList.size());
        for(int i = 0; i < readDataList.size();i++)
        {
            dataSize = readDataList[i].count();
            x[i].resize(dataSize);
            for(int j = 0; j < dataSize;j++)
            {
                x[i][j] = 0.02 * j;
            }
            ui->customPlot->graph(i)->setData(x[i],readDataList[i]);
        }
    
        double xRange = 0.02 * dataSize;
        ui->customPlot->xAxis->setRange(0,xRange);
        ui->customPlot->yAxis->setRange(minData -1,maxData + 1);
    
        ui->customPlot->replot();
    }

    至此,绘制图形完毕。

  • 相关阅读:
    bzoj 2115: [Wc2011] Xor【线性基+dfs】
    bzoj 1027: [JSOI2007]合金【凸包+Floyd】
    bzoj 4824: [Cqoi2017]老C的键盘【树形dp】
    bzoj 2111: [ZJOI2010]Perm 排列计数【树形dp+lucas】
    bzoj 4822: [Cqoi2017]老C的任务【扫描线+树状数组+二维差分】
    bzoj 4823: [Cqoi2017]老C的方块【最大权闭合子图】
    bzoj 4826: [Hnoi2017]影魔【单调栈+树状数组+扫描线】
    洛谷 P3731 [HAOI2017]新型城市化【最大流(二分图匹配)+tarjan】
    洛谷 P3732 [HAOI2017]供给侧改革【trie树】
    poj 1474 Video Surveillance 【半平面交】
  • 原文地址:https://www.cnblogs.com/wanzaiyimeng/p/7119932.html
Copyright © 2011-2022 走看看