#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();
}
QCustomPlot的用法
最新推荐文章于 2024-06-18 17:48:35 发布