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;
}

发布了60 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/BadAyase/article/details/104050267