当前位置: 首页 > 工具软件 > BarChart > 使用案例 >

一、QChart-barchart柱状图

羊舌航
2023-12-01

1.柱状图示例

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QtCharts/QChartView>
#include <QtCharts/QBarSeries>
#include <QBarCategoryAxis>
#include <QValueAxis>
#include <QBarSet>

QT_CHARTS_USE_NAMESPACE

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void Init();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"

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

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

void MainWindow::Init()
{
    //  barchart柱状图;
    QBarSet *set0 = new QBarSet("警告");
    QBarSet *set1 = new QBarSet("报警");
    QBarSet *set2 = new QBarSet("危险");
    set2->setColor("red");  // 设置颜色

    //设置数据集标签字体
    QFont font("宋体", 8, 0);
    font.setBold(true);
    set0->setLabelFont(font);
    set1->setLabelFont(font);
    set2->setLabelFont(font);

    //设置数据集标签颜色
    set0->setLabelColor(QColor(0,0,0));
    set1->setLabelColor(QColor(0, 0, 0));
    set2->setLabelColor(QColor(0, 0, 0));

    *set0 << 1.5 << 2 << 3 << 3.5 << 4;
    *set1 << 2 << 4.5 << 0 << 3.5 << 4;
    *set2 << 3 << 5 << 1.5 << 2 << 3.5;

    QBarSeries *series = new QBarSeries();
    series->append(set0);
    series->append(set1);
    series->append(set2);

    series->setLabelsFormat("@value");
    series->setLabelsPosition(QAbstractBarSeries::LabelsCenter);//设置在柱形图内中间
    //series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);//设置在柱形图内部顶端
    //series->setLabelsPosition(QAbstractBarSeries::LabelsInsideBase);//设置在柱形图内部下端
    //series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);//设置在柱形图外部上端
    series->setLabelsVisible(true);
    series->setBarWidth(0.5);


    QChart *chart = new QChart();
    chart->addSeries(series);						//问题所在
    chart->setTitle("火情记录");
    chart->setAnimationOptions(QChart::SeriesAnimations);

    QStringList dateList;
    dateList << "11" << "12" << "13" << "14" << "15";
    QBarCategoryAxis *axisX = new QBarCategoryAxis();
    axisX->append(dateList);
    axisX->setTitleText("日期");

    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0,10);
    axisY->setLabelFormat("%d");
    axisY->setTickCount(11);                         // 设置刻度数目
    axisY->setTitleText("次数");

    chart->setAxisX(axisX, series);                  //设置X坐标轴
    chart->setAxisY(axisY, series);                  //设置Y坐标轴


    chart->legend()->setVisible(true);              //设置图例为显示状态
    chart->legend()->setAlignment(Qt::AlignBottom); //设置图例的显示位置在底部

    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setContentsMargins(0,0,0,0);
    hLayout->addWidget(chartView);
    ui->widget->setLayout(hLayout);
}



 类似资料: