CustomPlot test

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"qcustomplot.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
        ui->setupUi(this);

        QCustomPlot *pCustomPlot = new QCustomPlot(this);
        pCustomPlot->resize(400, 300);


        // 可變數組存放繪圖的座標的數據,分別存放x和y座標的數據,101爲數據長度
        QVector<double> x(101), y(101);

        // 添加數據,這裏演示y = x^3,爲了正負對稱,x從-10到+10
        for (int i = 0; i < 100; ++i)
        {
            x[i] = i;
            y[i] = 20*sin(x[i]);  // x的y次方;
        }

        // 向繪圖區域QCustomPlot添加一條曲線
        QCPGraph *pGraph = pCustomPlot->addGraph();
        pCustomPlot->addGraph();
        pCustomPlot->addGraph();


        // 添加數據

        pCustomPlot->graph(0)->setData(x, y);
        pCustomPlot->graph(1)->setData(x, y);




        pCustomPlot->xAxis->setLabel("x");
        pCustomPlot->yAxis->setLabel("y");

        // 設置背景色
        pCustomPlot->setBackground(QColor(50, 50, 50));

        pGraph->setPen(QPen(QColor(32, 178, 170)));


        pCustomPlot->xAxis->setTickLabelColor(Qt::red);
        pCustomPlot->xAxis->setLabelColor(QColor(255, 0, 0));
        //設置x基準軸顏色
        pCustomPlot->xAxis->setBasePen(QPen(QColor(255, 255, 0)));
        pCustomPlot->xAxis->setTickPen(QPen(QColor(255, 255, 0)));
        pCustomPlot->xAxis->setSubTickPen(QColor(255, 165, 0));
        QFont xFont = pCustomPlot->xAxis->labelFont();
        xFont.setPixelSize(20);
        pCustomPlot->xAxis->setLabelFont(xFont);
        //獲取句柄並進行設置,去除默認的虛線網格,導出類,編輯設置相應的參數
        QCPGrid* grid=pCustomPlot->xAxis->grid();
        grid->setVisible(false);
        grid=pCustomPlot->yAxis->grid();
        grid->setVisible(false);



        //設置刻度文字字體的顏色
        pCustomPlot->yAxis->setTickLabelColor(Qt::red);
        //設置旁邊的標籤字體的顏色
        pCustomPlot->yAxis->setLabelColor(QColor(255, 0, 0));
        //設置Y基準軸的顏色
        pCustomPlot->yAxis->setBasePen(QPen(QColor(255, 255, 0)));
        //主刻度顏色
        pCustomPlot->yAxis->setTickPen(QPen(QColor(255, 255, 255)));
        //子刻度顏色
        pCustomPlot->yAxis->setSubTickPen(QColor(255, 255, 0));
        //yFont先獲取y座標軸的標籤的字體,在進行編輯yFont
        QFont yFont = pCustomPlot->yAxis->labelFont();
        //編輯
        yFont.setPixelSize(20);
        //設置yFont
        pCustomPlot->yAxis->setLabelFont(yFont);
        //設置xy座標基準軸的子刻度主刻度的長度,第一個參數表示向內延的像素單位,第二個向外的像素單位
        pCustomPlot->xAxis->setTickLength(10, 0);
        pCustomPlot->xAxis->setSubTickLength(5, 0);
        pCustomPlot->yAxis->setTickLength(10,0);
        pCustomPlot->yAxis->setSubTickLength(5,0);

       // pCustomPlot->yAxis->setAutoTicks(false);
       // pCustomPlot->yAxis->setAutoTickLabels(false);
        //pCustomPlot->yAxis->setTickVector(QVector<double>() << 0 <<10<<20<<30);
        //pCustomPlot->yAxis->setTickVectorLabels(QVector<QString>() << "Not so\nhigh" << "Very\nhigh");

       // pCustomPlot->xAxis->setAutoTickStep(false);
       //pCustomPlot->xAxis->setTickStep(0.1); // one month in seconds
        //pCustomPlot->xAxis->setSubTickCount(3);

        // 設置座標軸顯示範圍,否則只能看到默認範圍
        pCustomPlot->xAxis->setRange(0, 100, Qt::AlignLeft);
       // pCustomPlot->xAxis->setRange(-100, 100);
        pCustomPlot->yAxis->setRange(-100, 100);

        //保存成圖片格式
       // pCustomPlot->savePng("D://customPlot.png", 400, 300);

        //讓座標系支持拖放,縮放,
        // pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

}

MainWindow::~MainWindow()
{
    delete ui;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章