QCustomPlot的用法

#include <QApplication>

#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QWidget>
#include <qcustomplot.h>
#include <QMainWindow>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    
    //得到数据
    QVector<double> x(5),y0(5),y1(5),y2(5);
    for(int i=0;i<5;i++){
        x[i]=i;
        y0[i]=i+10;
        y1[i]=i+20;
        y2[i]=i+50;
    }

    //创建QCustomPlot,添加曲线graph,并设置曲线的数据
    QCustomPlot *customPlot=new QCustomPlot;
    customPlot->addGraph();
    customPlot->graph(0)->setName("fist line");
    customPlot->graph(0)->setData(x,y0);

    customPlot->addGraph();
    customPlot->graph(1)->setName("second line");
    customPlot->graph(1)->setData(x,y1);

    customPlot->addGraph();
    customPlot->graph(2)->setName("third line");
    customPlot->graph(2)->setData(x,y2);

    //自适应轴
    customPlot->graph(0)->rescaleAxes(true);
    customPlot->graph(1)->rescaleAxes(true);
    customPlot->graph(2)->rescaleAxes(true);

    //显示铭文
    customPlot->legend->setVisible(true);

    //设置customPlot可进行拽托或伸缩
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    customPlot->axisRect()->setRangeDrag(Qt::Horizontal);  //设置允许在某一方向上拽托
    customPlot->axisRect()->setRangeZoom(Qt::Horizontal);  //设置允许在某一方向上伸缩

    QMainWindow window;
    window.setCentralWidget(customPlot);
    window.resize(500,300);
    window.show();
    

/*
    //得到数据
    QVector<double> x(8),y(8);
    for(int i=0;i<8;i++){
        x[i]=i;
        y[i]=i*i;
    }

    //创建QCustomPlot,添加曲线graph,并设置曲线的数据
    QCustomPlot *customPlot=new QCustomPlot;
    customPlot->addGraph();
    customPlot->graph(0)->setName("fist line");
    customPlot->graph(0)->setData(x,y);

    //设置轴
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTick(1.0, "Bacteria");
    textTicker->addTick(2.0, "Protozoa");
    textTicker->addTick(3.0, "Chromista");
    textTicker->addTick(4.0, "Plants");
    textTicker->addTick(5.0, "Fungi");
    textTicker->addTick(6.0, "Animals");
    textTicker->addTick(8.0, "Vogons");

    customPlot->xAxis->setTicker(textTicker);

    //自适应轴
    customPlot->graph(0)->rescaleAxes(true);

    //显示铭文
    customPlot->legend->setVisible(true);

    //设置customPlot可进行拽托或放大
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    QMainWindow window;
    window.setCentralWidget(customPlot);
    window.resize(500,300);
    window.show();
*/

    return app.exec();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值