欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QBarSeries相关内容

程序员文章站 2022-05-02 19:26:39
...

学习一下Qt Charts的柱状图部分。参考书籍:Qt5.9 C++开发指南
效果图
QBarSeries相关内容
QBarSeries相关内容
QBarSeries相关内容
QBarSeries相关内容
头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCharts/QChart>
using namespace QtCharts;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    //刷新柱状图
    void on_pushButton_clicked();

private:
    void updateTableWidget_2();

    //创建QBarSeries
    void createQBarSeries();
    //创建QPercentBarSeries
    void createQPercentBarSeries();
    //创建QStackedBarSeries
    void createQStackedBarSeries();

    //创建QHorizontalBarSeries
    void createQHorizontalBarSeries();
    //创建QHorizontalPercentBarSeries
    void createQHorizontalPercentBarSeries();
    //创建QHorizontalStackedBarSeries
    void createQHorizontalStackedBarSeries();

private:
    Ui::MainWindow *ui;

    QList<QChart *> m_charts;
};

#endif // MAINWINDOW_H

cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidgetItem>
#include <QBarSeries>
#include <QBarSet>
#include <QBarCategoryAxis>
#include <QLineSeries>
#include <QValueAxis>
#include <QPercentBarSeries>
#include <QStackedBarSeries>
#include <QHorizontalBarSeries>
#include <QHorizontalPercentBarSeries>
#include <QHorizontalStackedBarSeries>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //初始化表格一
    QStringList headText = QStringList() << QStringLiteral("姓名") << QStringLiteral("语文") << QStringLiteral("数学") << QStringLiteral("英语") << QStringLiteral("平均分");
    ui->tableWidget->setHorizontalHeaderLabels(headText);
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        int Chinese = ui->tableWidget->item(i, 1)->text().toInt();
        int Math = ui->tableWidget->item(i, 2)->text().toInt();
        int English = ui->tableWidget->item(i, 3)->text().toInt();
        float avgScore = (Chinese + Math + English) / 3.0;
        QTableWidgetItem *itemAvgScore = new QTableWidgetItem(QString::number(avgScore, 'f', 2));
        ui->tableWidget->setItem(i, 4, itemAvgScore);
    }
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //初始化表格二
    QStringList headText2 = QStringList() << QStringLiteral("分数段") << QStringLiteral("语文") << QStringLiteral("数学") << QStringLiteral("英语");
    ui->tableWidget_2->setHorizontalHeaderLabels(headText2);
    for (int i = 0; i < 5; i++)
    {
        for (int j = 1; j < 4; j++)
        {
            QTableWidgetItem *item = new QTableWidgetItem;
            ui->tableWidget_2->setItem(i, j, item);
        }
    }
    //根据表格一,更新表格二
    updateTableWidget_2();
    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    createQBarSeries();
    createQPercentBarSeries();
    createQStackedBarSeries();

    createQHorizontalBarSeries();
    createQHorizontalPercentBarSeries();
    createQHorizontalStackedBarSeries();

    m_charts.append(ui->chartViewBarSeries->chart());
    m_charts.append(ui->chartViewPercentBarSeries->chart());
    m_charts.append(ui->chartViewStackedBarSeries->chart());
    m_charts.append(ui->chartViewHorizontalBarSeries->chart());
    m_charts.append(ui->chartViewHorizontalPercentBarSeries->chart());
    m_charts.append(ui->chartViewHorizontalStackedBarSeries->chart());
}

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

void MainWindow::updateTableWidget_2()
{
    //统计
    int count[5][3] = {0};
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        for (int j = 1; j < 4; j++)
        {
            if (ui->tableWidget->item(i, j)->text().toInt() >= 90)
                 count[0][j - 1]++;
            else if (ui->tableWidget->item(i, j)->text().toInt() >= 80 && ui->tableWidget->item(i, j)->text().toInt() <= 89)
                 count[1][j - 1]++;
            else if (ui->tableWidget->item(i, j)->text().toInt() >= 70 && ui->tableWidget->item(i, j)->text().toInt() <= 79)
                 count[2][j - 1]++;
            else if (ui->tableWidget->item(i, j)->text().toInt() >= 60 && ui->tableWidget->item(i, j)->text().toInt() <= 69)
                 count[3][j - 1]++;
            else if (ui->tableWidget->item(i, j)->text().toInt() < 60)
                 count[4][j - 1]++;
        }
    }
    for (int i = 0; i < 5; i++)
    {
        for (int j = 1; j < 4; j++)
        {
            ui->tableWidget_2->item(i, j)->setText(QString::number(count[i][j - 1]++));
        }
    }
}

void MainWindow::createQBarSeries()
{
    //此处没有new QChart和setChart();证明chartView内部处理了
    ui->chartViewBarSeries->setRenderHint(QPainter::Antialiasing);
    ui->chartViewBarSeries->chart()->setTitle(QStringLiteral("QBarSeries演示"));
    ui->chartViewBarSeries->chart()->setTheme(QChart::ChartThemeDark);
    //创建数据集
    QBarSet *setChinese = new QBarSet(QStringLiteral("语文"));
    QBarSet *setMath = new QBarSet(QStringLiteral("数学"));
    QBarSet *setEnglish = new QBarSet(QStringLiteral("英语"));
    QLineSeries *lineSeries = new QLineSeries;
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
            setChinese->append(ui->tableWidget->item(i, 1)->text().toInt());
            setMath->append(ui->tableWidget->item(i, 2)->text().toInt());
            setEnglish->append(ui->tableWidget->item(i, 3)->text().toInt());
            lineSeries->append(i, ui->tableWidget->item(i, 4)->text().toFloat());
    }
    //创建柱状图序列,并添加数据集
    QBarSeries *barSeries = new QBarSeries;
    barSeries->append(setChinese);
    barSeries->append(setMath);
    barSeries->append(setEnglish);
    ui->chartViewBarSeries->chart()->addSeries(barSeries);
    //创建柱状图坐标轴
    QBarCategoryAxis *barAxis = new QBarCategoryAxis();
    barAxis->append(QStringList() << QStringLiteral("学生1") << QStringLiteral("学生2") << QStringLiteral("学生3") << QStringLiteral("学生4") << QStringLiteral("学生5"));
    ui->chartViewBarSeries->chart()->setAxisX(barAxis, barSeries);//横坐标,坐标轴
    //添加折线图序列
    lineSeries->setName(QStringLiteral("平均分"));
    lineSeries->setPen(QPen(Qt::red, 2));
    lineSeries->setPointsVisible(true);
    ui->chartViewBarSeries->chart()->addSeries(lineSeries);
    ui->chartViewBarSeries->chart()->setAxisX(barAxis, lineSeries);//横坐标,坐标轴
    //添加Y轴:数值型坐标轴
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(0, 100);
    axisY->setTitleText(QStringLiteral("分数"));
    axisY->setTickCount(6);
    axisY->setLabelFormat("%d");
    ui->chartViewBarSeries->chart()->setAxisY(axisY, barSeries);
    ui->chartViewBarSeries->chart()->setAxisY(axisY, lineSeries);
    //设置图例位置
    ui->chartViewBarSeries->chart()->legend()->setAlignment(Qt::AlignBottom);
}

void MainWindow::createQPercentBarSeries()
{
    ui->chartViewPercentBarSeries->setRenderHint(QPainter::Antialiasing);
    ui->chartViewPercentBarSeries->chart()->setTheme(QChart::ChartThemeDark);
    ui->chartViewPercentBarSeries->chart()->setTitle(QStringLiteral("QPercentBarSeries演示"));
    //创建数据集
    QBarSet *Chinese = new QBarSet(QStringLiteral("语文"));
    QBarSet *Math = new QBarSet(QStringLiteral("数学"));
    QBarSet *English = new QBarSet(QStringLiteral("英语"));
    for (int i = 0; i < ui->tableWidget_2->rowCount(); i++)
    {
        Chinese->append(ui->tableWidget_2->item(i, 1)->text().toFloat());
        Math->append(ui->tableWidget_2->item(i, 2)->text().toFloat());
        English->append(ui->tableWidget_2->item(i, 3)->text().toFloat());
    }
    //创建百分比柱状图序列,并添加数据集
    QPercentBarSeries *percentBarSeries = new QPercentBarSeries;
    percentBarSeries->append(Chinese);
    percentBarSeries->append(Math);
    percentBarSeries->append(English);
    percentBarSeries->setBarWidth(0.3);
    percentBarSeries->setLabelsVisible(true);
    ui->chartViewPercentBarSeries->chart()->addSeries(percentBarSeries);
    //创建坐标轴
    QBarCategoryAxis *axisX = new QBarCategoryAxis();
    axisX->append(QStringList() << QStringLiteral("90分以上") << QStringLiteral("80-89分") << QStringLiteral("70-79分") << QStringLiteral("60-69分") << QStringLiteral("60分以下"));
    ui->chartViewPercentBarSeries->chart()->setAxisX(axisX, percentBarSeries);
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(0, 100);
    axisY->setTitleText(QStringLiteral("百分比"));
    axisY->setTickCount(6);
    ui->chartViewPercentBarSeries->chart()->setAxisY(axisY, percentBarSeries);
    //图例
    ui->chartViewPercentBarSeries->chart()->legend()->setAlignment(Qt::AlignRight);
}

void MainWindow::createQStackedBarSeries()
{
    ui->chartViewStackedBarSeries->setRenderHint(QPainter::Antialiasing);
    ui->chartViewStackedBarSeries->chart()->setTitle(QStringLiteral("QStackedBarSeries演示"));
    ui->chartViewStackedBarSeries->chart()->setTheme(QChart::ChartThemeDark);
    //创建数据集
    QBarSet *Chinese = new QBarSet(QStringLiteral("语文"));
    QBarSet *Math = new QBarSet(QStringLiteral("数学"));
    QBarSet *English = new QBarSet(QStringLiteral("英语"));
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        Chinese->append(ui->tableWidget->item(i, 1)->text().toInt());
        Math->append(ui->tableWidget->item(i, 2)->text().toInt());
        English->append(ui->tableWidget->item(i, 3)->text().toInt());
    }
    //创建堆叠柱状图,并添加数据
    QStackedBarSeries *stackedBarSeries = new QStackedBarSeries;
    stackedBarSeries->append(Chinese);
    stackedBarSeries->append(Math);
    stackedBarSeries->append(English);
    stackedBarSeries->setLabelsVisible(true);
    ui->chartViewStackedBarSeries->chart()->addSeries(stackedBarSeries);
    //创建坐标轴
    QBarCategoryAxis *axisX = new QBarCategoryAxis;
    axisX->append(QStringList() << QStringLiteral("学生1") << QStringLiteral("学生2") << QStringLiteral("学生3") << QStringLiteral("学生4") << QStringLiteral("学生5"));
    ui->chartViewStackedBarSeries->chart()->setAxisX(axisX, stackedBarSeries);
    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(0, 300);
    axisY->setTitleText(QStringLiteral("总分"));
    ui->chartViewStackedBarSeries->chart()->setAxisY(axisY, stackedBarSeries);
    //图例
    ui->chartViewStackedBarSeries->chart()->legend()->setAlignment(Qt::AlignRight);
}

void MainWindow::createQHorizontalBarSeries()
{
    //此处没有new QChart和setChart();证明chartView内部处理了
    ui->chartViewHorizontalBarSeries->setRenderHint(QPainter::Antialiasing);
    ui->chartViewHorizontalBarSeries->chart()->setTitle(QStringLiteral("QHorizontalBarSeries演示"));
    ui->chartViewHorizontalBarSeries->chart()->setTheme(QChart::ChartThemeDark);
    //创建数据集
    QBarSet *setChinese = new QBarSet(QStringLiteral("语文"));
    QBarSet *setMath = new QBarSet(QStringLiteral("数学"));
    QBarSet *setEnglish = new QBarSet(QStringLiteral("英语"));
    QLineSeries *lineSeries = new QLineSeries;
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
            setChinese->append(ui->tableWidget->item(i, 1)->text().toInt());
            setMath->append(ui->tableWidget->item(i, 2)->text().toInt());
            setEnglish->append(ui->tableWidget->item(i, 3)->text().toInt());
            lineSeries->append(ui->tableWidget->item(i, 4)->text().toFloat(), i);
    }
    //创建柱状图序列,并添加数据集
    QHorizontalBarSeries *barSeries = new QHorizontalBarSeries;
    barSeries->append(setChinese);
    barSeries->append(setMath);
    barSeries->append(setEnglish);
    ui->chartViewHorizontalBarSeries->chart()->addSeries(barSeries);
    //创建柱状图坐标轴
    QBarCategoryAxis *axisY = new QBarCategoryAxis();
    axisY->append(QStringList() << QStringLiteral("学生1") << QStringLiteral("学生2") << QStringLiteral("学生3") << QStringLiteral("学生4") << QStringLiteral("学生5"));
    ui->chartViewHorizontalBarSeries->chart()->setAxisY(axisY, barSeries);
    //添加折线图序列
    lineSeries->setName(QStringLiteral("平均分"));
    lineSeries->setPen(QPen(Qt::red, 2));
    lineSeries->setPointsVisible(true);
    ui->chartViewHorizontalBarSeries->chart()->addSeries(lineSeries);
    ui->chartViewHorizontalBarSeries->chart()->setAxisY(axisY, lineSeries);
    //添加X轴:数值型坐标轴
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0, 100);
    axisX->setTitleText(QStringLiteral("分数"));
    axisX->setTickCount(6);
    axisX->setLabelFormat("%d");
    axisX->setLabelsVisible(true);
    ui->chartViewHorizontalBarSeries->chart()->setAxisX(axisX, barSeries);
    ui->chartViewHorizontalBarSeries->chart()->setAxisX(axisX, lineSeries);
    //设置图例位置
    ui->chartViewHorizontalBarSeries->chart()->legend()->setAlignment(Qt::AlignRight);
}

void MainWindow::createQHorizontalPercentBarSeries()
{
    ui->chartViewHorizontalPercentBarSeries->setRenderHint(QPainter::Antialiasing);
    ui->chartViewHorizontalPercentBarSeries->chart()->setTheme(QChart::ChartThemeDark);
    ui->chartViewHorizontalPercentBarSeries->chart()->setTitle(QStringLiteral("QHorizontalPercentBarSeries演示"));
    //创建数据集
    QBarSet *Chinese = new QBarSet(QStringLiteral("语文"));
    QBarSet *Math = new QBarSet(QStringLiteral("数学"));
    QBarSet *English = new QBarSet(QStringLiteral("英语"));
    for (int i = 0; i < ui->tableWidget_2->rowCount(); i++)
    {
        Chinese->append(ui->tableWidget_2->item(i, 1)->text().toFloat());
        Math->append(ui->tableWidget_2->item(i, 2)->text().toFloat());
        English->append(ui->tableWidget_2->item(i, 3)->text().toFloat());
    }
    //创建百分比柱状图序列,并添加数据集
    QHorizontalPercentBarSeries *percentBarSeries = new QHorizontalPercentBarSeries;
    percentBarSeries->append(Chinese);
    percentBarSeries->append(Math);
    percentBarSeries->append(English);
    percentBarSeries->setBarWidth(0.3);
    percentBarSeries->setLabelsVisible(true);
    ui->chartViewHorizontalPercentBarSeries->chart()->addSeries(percentBarSeries);
    //创建坐标轴
    QBarCategoryAxis *axisY = new QBarCategoryAxis();
    axisY->append(QStringList() << QStringLiteral("90分以上") << QStringLiteral("80-89分") << QStringLiteral("70-79分") << QStringLiteral("60-69分") << QStringLiteral("60分以下"));
    ui->chartViewHorizontalPercentBarSeries->chart()->setAxisY(axisY, percentBarSeries);
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0, 100);
    axisX->setTitleText(QStringLiteral("百分比"));
    axisX->setTickCount(6);
    ui->chartViewHorizontalPercentBarSeries->chart()->setAxisX(axisX, percentBarSeries);
    //图例
    ui->chartViewHorizontalPercentBarSeries->chart()->legend()->setAlignment(Qt::AlignRight);
}

void MainWindow::createQHorizontalStackedBarSeries()
{
    ui->chartViewHorizontalStackedBarSeries->setRenderHint(QPainter::Antialiasing);
    ui->chartViewHorizontalStackedBarSeries->chart()->setTitle(QStringLiteral("QHorizontalStackedBarSeries演示"));
    ui->chartViewHorizontalStackedBarSeries->chart()->setTheme(QChart::ChartThemeDark);
    //创建数据集
    QBarSet *Chinese = new QBarSet(QStringLiteral("语文"));
    QBarSet *Math = new QBarSet(QStringLiteral("数学"));
    QBarSet *English = new QBarSet(QStringLiteral("英语"));
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        Chinese->append(ui->tableWidget->item(i, 1)->text().toInt());
        Math->append(ui->tableWidget->item(i, 2)->text().toInt());
        English->append(ui->tableWidget->item(i, 3)->text().toInt());
    }
    //创建堆叠柱状图,并添加数据
    QHorizontalStackedBarSeries *stackedBarSeries = new QHorizontalStackedBarSeries;
    stackedBarSeries->append(Chinese);
    stackedBarSeries->append(Math);
    stackedBarSeries->append(English);
    stackedBarSeries->setLabelsVisible(true);
    ui->chartViewHorizontalStackedBarSeries->chart()->addSeries(stackedBarSeries);
    //创建坐标轴
    QBarCategoryAxis *axisY = new QBarCategoryAxis;
    axisY->append(QStringList() << QStringLiteral("学生1") << QStringLiteral("学生2") << QStringLiteral("学生3") << QStringLiteral("学生4") << QStringLiteral("学生5"));
    ui->chartViewHorizontalStackedBarSeries->chart()->setAxisY(axisY, stackedBarSeries);
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0, 300);
    axisX->setTitleText(QStringLiteral("总分"));
    ui->chartViewHorizontalStackedBarSeries->chart()->setAxisX(axisX, stackedBarSeries);
    //图例
    ui->chartViewHorizontalStackedBarSeries->chart()->legend()->setAlignment(Qt::AlignRight);
}

void MainWindow::on_pushButton_clicked()
{
    updateTableWidget_2();

    foreach (QChart *chart, m_charts)
    {
        chart->removeAllSeries();           //删除所有序列
        chart->removeAxis(chart->axisX());  //删除坐标轴
        chart->removeAxis(chart->axisY());  //删除坐标轴
    }
    //刷新
    createQBarSeries();
    createQPercentBarSeries();
    createQStackedBarSeries();

    createQHorizontalBarSeries();
    createQHorizontalPercentBarSeries();
    createQHorizontalStackedBarSeries();
}
相关标签: Qt Charts