QCustomPlot柱狀圖

效果

barPlot

代碼

mainwindow.cpp

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

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

    ui->customPlot->xAxis->setRange(0, 10);
    ui->customPlot->yAxis->setRange(0, 10);


    /* 核心代碼 */
    /* 底部柱狀圖 */
    QCPBars *barBottom = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    /* 數據添加 */
    QVector<double> a(5);
    QVector<double> b(5);
    a<<1<<2<<3<<4<<5;
    b<<1<<2<<3<<4<<5;
    /* 樣式改變 */
    barBottom->setBrush(QColor(10, 140, 70, 160));

    barBottom->setData(a, b);

    /* 頂部柱狀圖 */
    QCPBars *barTop = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    QVector<double> c(5);
    QVector<double> d(5);
    c<<1<<2<<3<<4<<5;
    d<<1<<2<<1<<2<<1;
    barTop->setData(c, d);
    barTop->setBrush(QColor(10, 100, 50, 70));
    barTop->moveAbove(barBottom);

}

MainWindow::~MainWindow()
{
    delete ui;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章