QPieSeries相关内容

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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
using namespace QtCharts;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    //福布斯2020全球富豪榜前20名-财富分段占比
    void createQPieSeries1();
    //福布斯2020全球富豪榜前20名-国家地区占比
    void createQPieSeries2();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

cpp文件

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

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

    QStringList headText = QStringList() << QStringLiteral("世界排名") << QStringLiteral("名字") << QStringLiteral("财富(10亿美元)") << QStringLiteral("财富来源") << QStringLiteral("国家/地区");
    ui->tableWidget->setHorizontalHeaderLabels(headText);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    createQPieSeries1();
    createQPieSeries2();
}

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

void MainWindow::createQPieSeries1()
{
    //获取各分段人数
    int count[3] = {0};
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        int value = ui->tableWidget->item(i, 2)->text().toInt();
        if (value >= 1000)
            count[0]++;
        else if (value >= 500 && value < 1000)
            count[1]++;
        else if (value < 500)
            count[2]++;
    }
    //基本设置
    ui->chartView1->setRenderHint(QPainter::Antialiasing);
    ui->chartView1->chart()->setTitle(QStringLiteral("福布斯2020全球富豪榜前20名-财富分段占比"));
    ui->chartView1->chart()->setTheme(QChart::ChartThemeBlueCerulean);
    //创建饼图序列,并添加数据
    QPieSeries *pieSeries = new QPieSeries;
    pieSeries->append(QStringLiteral("1000亿以上"), count[0]);
    pieSeries->append(QStringLiteral("500-1000亿"), count[1]);
    pieSeries->append(QStringLiteral("500亿以下"), count[2]);
    //设置饼图分块的标签文字
    for (int i = 0; i < pieSeries->slices().count(); i++)
    {
        QPieSlice *slice = pieSeries->slices().at(i);
        QString labelText = slice->label() + QString(": %1%2, %3%4%").arg(slice->value()).arg(QStringLiteral("人")).arg(QStringLiteral("占比")).arg(slice->percentage() * 100);
        slice->setLabel(labelText);
    }

    pieSeries->setLabelsVisible(true);
    ui->chartView1->chart()->addSeries(pieSeries);
    ui->chartView1->chart()->legend()->setAlignment(Qt::AlignRight);
}

void MainWindow::createQPieSeries2()
{
    //获取各分段人数
    int countries[4] = {0};//美国、中国、法国、其他地区
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        QString country = ui->tableWidget->item(i, 4)->text();
        if (country == QStringLiteral("美国"))
            countries[0]++;
        else if (country == QStringLiteral("中国"))
            countries[1]++;
        else if (country == QStringLiteral("法国"))
            countries[2]++;
        else
            countries[3]++;
    }
    //基本设置
    ui->chartView2->setRenderHint(QPainter::Antialiasing);
    ui->chartView2->chart()->setTitle(QStringLiteral("福布斯2020全球富豪榜前20名-国家地区占比"));
    ui->chartView2->chart()->setTheme(QChart::ChartThemeDark);
    //创建饼图序列,并添加数据
    QPieSeries *pieSeries = new QPieSeries;
    pieSeries->append(QStringLiteral("美国"), countries[0]);
    pieSeries->append(QStringLiteral("中国"), countries[1]);
    pieSeries->append(QStringLiteral("法国"), countries[2]);
    pieSeries->append(QStringLiteral("其他地区"), countries[3]);
    //设置饼图分块的标签文字
    QPieSlice *slice;
    for (int i = 0; i < pieSeries->slices().count(); i++)
    {
        slice = pieSeries->slices().at(i);
        QString labelText = slice->label() + QString("%1%").arg(slice->percentage() * 100);
        slice->setLabel(labelText);
        //slice->setLabelColor(Qt::black);
        slice->setLabelBrush(Qt::black);//设置标签字画刷:字的颜色
        slice->setPen(QPen(Qt::lightGray, 2));//设置绘制分块的边框的画笔
    }
    slice->setExploded(true);//设置最后一个分块弹出
    pieSeries->setLabelsVisible(true);
    pieSeries->setHoleSize(0.1);//设置饼图中心的空心圆的大小
    pieSeries->setLabelsPosition(QPieSlice::LabelInsideNormal);//设置标签位置:饼图分块中间
    ui->chartView2->chart()->addSeries(pieSeries);
    ui->chartView2->chart()->legend()->setAlignment(Qt::AlignRight);
}

你可能感兴趣的:(Qt,Charts)