How to show percentage in Qt Pie charts on chart like this?
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())