Show percentage in Qt Pie Charts?

大城市里の小女人 提交于 2020-06-08 13:50:49

问题


How to show percentage in Qt Pie charts on chart like this?


回答1:


To do this task you have to enable to make the labels visible and place the position as LabelInsideHorizontal, in the following code the solution is shown.

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

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

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())


来源:https://stackoverflow.com/questions/44511811/show-percentage-in-qt-pie-charts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!