• Qt 画自定义饼图统计的例子


    先给出结果图,这个例子是将各种事件分类然后统计的其比例,然后画饼图显示出来

    这个是我仿照官方给的例子,让后自己理解后,修改的,要生成饼图,需要QT的 charts 支持,安装QT 没有选择这个的,需要下载这个模块,然后在.pro文件中年添加

    QT += charts

    首先重写饼图块,让鼠标悬浮在某个饼图块时,让这个块弹出来,然后显示块的信息,这个比较简单,如下所示

    1. //头文件
    2. #include
    3. QT_CHARTS_USE_NAMESPACE
    4. class CustomSlice : public QPieSlice
    5. {
    6. Q_OBJECT
    7. public:
    8. CustomSlice(QString label, qreal value);
    9. public Q_SLOTS:
    10. void showHighlight(bool show);
    11. };
    12. //cpp文件
    13. #include "customslice.h"
    14. QT_CHARTS_USE_NAMESPACE
    15. CustomSlice::CustomSlice(QString label, qreal value)
    16. : QPieSlice(label, value)
    17. {
    18. connect(this, &CustomSlice::hovered, this, &CustomSlice::showHighlight);
    19. }
    20. void CustomSlice::showHighlight(bool show)
    21. {
    22. setLabelVisible(show);//显示标签
    23. setExploded(show); // 弹出
    24. }

    主体代码如下,主要是初始化饼图,创建饼图,为饼图块随机上色,为饼图数据的显示做排序,只需要调用接口函数把相应的数据塞进去即可生成可视化的饼图

    statisticwindow.h

    1. #ifndef STATISTICCHARTSWINDOW_H
    2. #define STATISTICCHARTSWINDOW_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. class QPushButton;
    10. class CustomSlice;
    11. QT_CHARTS_USE_NAMESPACE
    12. class StatisticChartsWindow : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit StatisticChartsWindow(QWidget *parent = nullptr);
    17. ~StatisticChartsWindow();
    18. //创建一个饼图1
    19. void createPie1(QMapint> data, QString title);
    20. //创建一个饼图2
    21. void createPie2(QMapint> data, QString title);
    22. // 为饼图1添加块信息
    23. void appendSlice1(QString lable, int value);
    24. // 为饼图2添加块信息
    25. void appendSlice2(QString lable, int value);
    26. // 移除所有块信息
    27. void removeAllSlice();
    28. // 获取随机颜色为饼图的每个块上色
    29. Qt::GlobalColor getRandomColor();
    30. //获取排序后的数据
    31. QListint>> getsortListByValue(QMapint> &data);
    32. QVBoxLayout *VBoxLayout;
    33. QPieSeries *series1;
    34. QPieSeries *series2;
    35. QChart *chart1;
    36. QChart *chart2;
    37. QChartView *chartView1;
    38. QChartView *chartView2;
    39. QPushButton *closeButton;
    40. QList CustomSlice1List;
    41. QList CustomSlice2List;
    42. QList colorList;
    43. signals:
    44. void closeSig();
    45. public slots:
    46. };
    47. #endif // STATISTICCHARTSWINDOW_H

    statisticwindow.cpp

    1. #include "statisticwindow.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include "customslice.h"
    14. #include
    15. #include "pushbutton.h"
    16. StatisticChartsWindow::StatisticChartsWindow(QWidget *parent) : QWidget(parent)
    17. {
    18. VBoxLayout = new QVBoxLayout(this);
    19. series1 = new QPieSeries(this);// 饼图一
    20. chart1 = new QChart();
    21. chart1->setAnimationOptions(QChart::AllAnimations);
    22. chart1->legend()->setVisible(true);
    23. chart1->legend()->setAlignment(Qt::AlignRight);//设置标签在右侧
    24. chartView1 = new QChartView(chart1);
    25. series2 = new QPieSeries(this);// 饼图一
    26. chart2 = new QChart();
    27. chart2->setAnimationOptions(QChart::AllAnimations);
    28. chart2->legend()->setVisible(true);
    29. chart2->legend()->setAlignment(Qt::AlignRight);//设置标签在右侧
    30. chartView2 = new QChartView(chart2);
    31. //底部添加关闭按钮
    32. closeButton = new QPushButton("关闭", this);
    33. QHBoxLayout *hlayout = new QHBoxLayout();
    34. hlayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
    35. hlayout->addWidget(closeButton);
    36. hlayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
    37. //SC3C::Valwell::PushButton::initStyle(closeButton);
    38. QPalette palette = closeButton->palette();
    39. QColor color(19, 46, 74); // RGB红色
    40. palette.setColor(QPalette::Button, color);
    41. closeButton->setPalette(palette);
    42. closeButton->setStyleSheet("color: white;");
    43. colorList<
    44. <
    45. chartView1->chart()->setTheme(QChart::ChartThemeBlueCerulean);
    46. chartView2->chart()->setTheme(QChart::ChartThemeBlueCerulean);
    47. VBoxLayout->addWidget(chartView1);
    48. VBoxLayout->addWidget(chartView2);
    49. VBoxLayout->addLayout(hlayout);
    50. VBoxLayout->layout()->setSpacing(1);//底部添加关闭
    51. connect(closeButton, &QPushButton::clicked, [=]() {
    52. this->hide();
    53. emit closeSig();
    54. });
    55. this->setWindowFlags(this->windowFlags() | Qt::WindowCloseButtonHint);
    56. this->setStyleSheet("background-color: rgb(19, 46, 74);");
    57. }
    58. StatisticChartsWindow::~StatisticChartsWindow()
    59. {
    60. if(chart1) {
    61. delete chart1;
    62. }
    63. if(chart2) {
    64. delete chart2;
    65. }
    66. }
    67. void StatisticChartsWindow::createPie1(QMapint> data, QString title)
    68. {
    69. // 创建一个饼图系列
    70. series1->clear();
    71. int count=0; //计算总数
    72. QMap<int, QList> map;
    73. for(auto it=data.begin(); it!=data.end(); it++) {
    74. count += it.value();
    75. }
    76. QListint>> sortList = getsortListByValue(data);// 根据条数比例排序,从大到小
    77. for(QMapint> map: sortList) {
    78. QString keyLable = map.firstKey();
    79. int num = map.value(keyLable);
    80. double ratio = num/1.0/count*100;
    81. QString ratioStr = QString::number(ratio, 'f', 1);
    82. QString lable = QString("%1,条数:%2,占比,%3%").arg(keyLable).arg(num).arg(ratioStr);
    83. appendSlice1(lable, num); // 添加到饼图中
    84. }
    85. // 创建一个新的图表并添加系列
    86. chart1->setTitle(title);
    87. //chart1->removeAllSeries();
    88. chart1->addSeries(series1);
    89. }
    90. void StatisticChartsWindow::createPie2(QMapint> data, QString title)
    91. {
    92. // 创建一个饼图系列
    93. series2->clear();
    94. int count=0; //计算总数
    95. QMap<int, QList> map;
    96. for(auto it=data.begin(); it!=data.end(); it++) {
    97. count += it.value();
    98. }
    99. QListint>> sortList = getsortListByValue(data);
    100. for(QMapint> map: sortList) {
    101. QString keyLable = map.firstKey();
    102. int num = map.value(keyLable);
    103. double ratio = num/1.0/count*100;
    104. QString ratioStr = QString::number(ratio, 'f', 1);
    105. QString lable = QString("%1,条数:%2,占比,%3%").arg(keyLable).arg(num).arg(ratioStr);
    106. appendSlice2(lable, num);
    107. }
    108. // 创建一个新的图表并添加系列
    109. chart2->setTitle(title);
    110. //chart2->removeAllSeries();
    111. chart2->addSeries(series2);
    112. }
    113. void StatisticChartsWindow::appendSlice1(QString lable, int value)
    114. {
    115. CustomSlice *customSlice = new CustomSlice(lable, value);
    116. customSlice->setBrush(QBrush(getRandomColor())); //设置填充颜色
    117. //customSlice->setPen(QPen(Qt::black)); //设置线条颜色
    118. CustomSlice1List.append(customSlice);
    119. *series1 << customSlice;
    120. }
    121. void StatisticChartsWindow::appendSlice2(QString lable, int value)
    122. {
    123. CustomSlice *customSlice = new CustomSlice(lable, value);
    124. customSlice->setBrush(QBrush(getRandomColor())); //设置填充颜色
    125. CustomSlice2List.append(customSlice);
    126. *series2 << customSlice;}
    127. void StatisticChartsWindow::removeAllSlice()
    128. {
    129. for(CustomSlice* custom: CustomSlice1List) {
    130. series1->remove(custom);
    131. }
    132. for(CustomSlice* custom: CustomSlice2List) {
    133. series2->remove(custom);
    134. }
    135. qDeleteAll(CustomSlice1List);
    136. qDeleteAll(CustomSlice2List);
    137. CustomSlice1List.clear();
    138. CustomSlice2List.clear();
    139. }
    140. Qt::GlobalColor StatisticChartsWindow::getRandomColor()
    141. {
    142. int randomValue = QRandomGenerator::global()->bounded(0, colorList.size()-1);
    143. return colorList.takeAt(randomValue);
    144. }
    145. QListint>> StatisticChartsWindow::getsortListByValue(QMapint> &data)
    146. {
    147. QListint>> sortList;
    148. QList<int> valueList;
    149. for(auto it=data.begin(); it!=data.end(); it++) {
    150. if(!valueList.contains(it.value())) {
    151. valueList.append(it.value());
    152. }
    153. }
    154. //根据值逆序排序
    155. std::sort(valueList.begin(), valueList.end(), std::greater<int>());
    156. for(int value: valueList) {
    157. for(QString key: data.keys(value)) {
    158. QMapint> map;
    159. map.insert(key, value);
    160. sortList.append(map);
    161. }
    162. }
    163. return sortList;
    164. }

    我的这个例子是,点击统计按钮之后,获取相应的数据,然后生成相应的饼图

    1. QObject::connect(ui.statisticsBtn, &QPushButton::clicked, [=]() {
    2. g_dataCache->setSystemLog(SC3C::eSystemLogType::QUERY_SYSTEMLOG, QString("成功"),"查看日志统计");
    3. StatisticChartsWindow window;
    4. if(StatisticWindow) {
    5. tableView->hide();
    6. StatisticWindow->show();
    7. return;
    8. }
    9. StatisticWindow = new StatisticChartsWindow(q);
    10. QObject::connect(StatisticWindow, &StatisticChartsWindow::closeSig, q, [=]() {
    11. tableView->show();
    12. });
    13. // 标签名, 数量
    14. QMapint> map1 = { };
    15. QMapint> map2 = { };
    16. int logType = ui.logType->currentData().toInt();
    17. int eventType = ui.eventType->currentData().toInt();
    18. QString Name = ui.operatorName->currentText();
    19. tableModel.second->setFilterOperator("所有");
    20. // 获取数据,map1表示饼图一需要的数据
    21. getEventTypeStatisticHash(map1, map2);
    22. //恢复之前显示的
    23. tableModel.second->setFilterType(logType, eventType);
    24. tableModel.second->setFilterOperator(Name);
    25. //SC3C::Valwell::Widget::setBackgroundCommon2WithMargins(window);
    26. StatisticWindow->setFixedSize(q->size());
    27. //StatisticWindow->setStyleSheet("background-color: transparent;");
    28. StatisticWindow->createPie1(map1, "事件类型统计");
    29. StatisticWindow->createPie2(map2, "日志类型统计");
    30. StatisticWindow->show();
    31. tableView->hide();
    32. });

    只需要把map放入创建饼图的函数即可,map中对应的是QMap<标签名,数量>,也就是饼图右侧的标签

    1. StatisticWindow->createPie1(map1, "事件类型统计");
    2. StatisticWindow->createPie2(map2, "日志类型统计");

    这样就可以出饼图了

  • 相关阅读:
    JVM的生命周期
    俄罗斯方块(升级版)
    五分钟了解制造业核心5大系统的联系
    【MySQL】一文带你理解索引事务及其原理
    [附源码]计算机毕业设计JAVAjsp校园自行车租售管理系统
    软件测试经典面试题【必备100道】
    Spring源码概述
    [Swift]组件化开发
    springboot手动引入jar包的方式
    Go语言开源13周年啦,看看负责人说了啥
  • 原文地址:https://blog.csdn.net/qq_44667165/article/details/133384389