c++ - 在 Qt 饼图中显示百分比?

标签 c++ qt qt5 qtcharts

如何在 Qt Pie charts 中显示百分比在这样的图表上?

最佳答案

要完成此任务,您必须启用使标签可见并将位置放置为 LabelInsideHorizo​​ntal,在以下代码中显示了解决方案。

series->setLabelsVisible();
series->setLabelsPosition(QPieSlice::LabelInsideHorizontal);

for(auto slice : series->slices())
    slice->setLabel(QString("%1%").arg(100*slice->percentage(), 0, 'f', 1));

enter image description here

PyQt5:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    series = QtChart.QPieSeries()
    series.append("Jane", 1)
    series.append("Joe", 2)
    series.append("Andy", 3)
    series.append("Barbara", 4)
    series.append("Axel", 5)

    chart = QtChart.QChart()
    chart.addSeries(series)
    chart.setTitle("Simple piechart example")
    chart.legend().hide()

    series.setLabelsVisible()
    series.setLabelsPosition(QtChart.QPieSlice.LabelInsideHorizontal)

    for slice in series.slices():
        slice.setLabel("{:.1f}%".format(100 * slice.percentage()))

    chartView = QtChart.QChartView(chart)
    chartView.setRenderHint(QtGui.QPainter.Antialiasing)

    window = QtWidgets.QMainWindow()
    window.setCentralWidget(chartView)
    window.resize(640, 480)
    window.show()

    sys.exit(app.exec())

关于c++ - 在 Qt 饼图中显示百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44511811/

相关文章:

c++ - 为什么派生类可以调用基类的成员函数?

c++ - OpenGL:QApplication的执行流程

qt - 如何在PyQt4中为QTreeWidget实现itemChecked和itemUnchecked信号?

windows-10 - 如何让 Qt 使用用户定义的类?

qt - 如何防止 qmake 在链接器命令行上添加控制台子系统?

c++ - 我的编译器不允许我使用 getline

c++ - 在 C++ 中并行加载 128 个文件

C++ 类的访问冲突

c++ - 派生自 QSettings 并存储 QIcon

c++11 - QSettings 中 QVariant 中的枚举类