gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:44:44 25 4
gpt4 key购买 nike

如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com