QPieChart 图例和百分比标签
QPieChart Legend and Percentage Label
是否可以在 QPieChart 中将百分比显示为圆上的标签和图例中的字符串?
这是我的示例代码
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQtChart Pie Chart")
self.setGeometry(100,100, 1280,600)
self.show()
self.create_piechart()
def create_piechart(self):
series = QtChart.QPieSeries()
series.append("Python", 80)
series.append("C++", 70)
series.append("Java", 50)
series.append("C#", 40)
series.append("PHP", 30)
series.setLabelsVisible(True)
series.setLabelsPosition(QtChart.QPieSlice.LabelOutside)
for slice in series.slices():
slice.setLabel("{:.2f}%".format(100 * slice.percentage()))
chart = QChart()
chart.legend()
chart.addSeries(series)
chart.createDefaultAxes()
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.setTitle("Pie Chart Example")
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
chartview = QChartView(chart)
chartview.setRenderHint(QPainter.Antialiasing)
self.setCentralWidget(chartview)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())
为了在饼图上显示百分比,您需要更改 QPieSeries。
从
更改标签位置
series.setLabelsPosition(QtChart.QPieSlice.LabelOutside)
至 series.setLabelsPosition(QtChart.QPieSlice.LabelInsideHorizontal)
之后图例标签也是百分比。要更改图例标签修改图例标记
chart.legend().markers(series)[0].setLabel("Python")
chart.legend().markers(series)[1].setLabel("C++")
chart.legend().markers(series)[2].setLabel("Java")
chart.legend().markers(series)[3].setLabel("C#")
chart.legend().markers(series)[4].setLabel("PHP")
结果:
Link 到 documentation
完整代码
from PyQt5 import QtChart
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtChart import QChart, QChartView
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQtChart Pie Chart")
self.setGeometry(100, 100, 1280, 600)
self.show()
self.create_piechart()
def create_piechart(self):
series = QtChart.QPieSeries()
series.append("Python", 80)
series.append("C++", 70)
series.append("Java", 50)
series.append("C#", 40)
series.append("PHP", 30)
series.setLabelsVisible(True)
series.setLabelsPosition(QtChart.QPieSlice.LabelInsideHorizontal)
for slice in series.slices():
slice.setLabel("{:.2f}%".format(100 * slice.percentage()))
chart = QChart()
chart.addSeries(series)
chart.createDefaultAxes()
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.setTitle("Pie Chart Example")
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
chart.legend().markers(series)[0].setLabel("Python")
chart.legend().markers(series)[1].setLabel("C++")
chart.legend().markers(series)[2].setLabel("Java")
chart.legend().markers(series)[3].setLabel("C#")
chart.legend().markers(series)[4].setLabel("PHP")
chartview = QChartView(chart)
chartview.setRenderHint(QPainter.Antialiasing)
self.setCentralWidget(chartview)
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())
是否可以在 QPieChart 中将百分比显示为圆上的标签和图例中的字符串?
这是我的示例代码
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQtChart Pie Chart")
self.setGeometry(100,100, 1280,600)
self.show()
self.create_piechart()
def create_piechart(self):
series = QtChart.QPieSeries()
series.append("Python", 80)
series.append("C++", 70)
series.append("Java", 50)
series.append("C#", 40)
series.append("PHP", 30)
series.setLabelsVisible(True)
series.setLabelsPosition(QtChart.QPieSlice.LabelOutside)
for slice in series.slices():
slice.setLabel("{:.2f}%".format(100 * slice.percentage()))
chart = QChart()
chart.legend()
chart.addSeries(series)
chart.createDefaultAxes()
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.setTitle("Pie Chart Example")
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
chartview = QChartView(chart)
chartview.setRenderHint(QPainter.Antialiasing)
self.setCentralWidget(chartview)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())
为了在饼图上显示百分比,您需要更改 QPieSeries。
从
更改标签位置
series.setLabelsPosition(QtChart.QPieSlice.LabelOutside)
至 series.setLabelsPosition(QtChart.QPieSlice.LabelInsideHorizontal)
之后图例标签也是百分比。要更改图例标签修改图例标记
chart.legend().markers(series)[0].setLabel("Python")
chart.legend().markers(series)[1].setLabel("C++")
chart.legend().markers(series)[2].setLabel("Java")
chart.legend().markers(series)[3].setLabel("C#")
chart.legend().markers(series)[4].setLabel("PHP")
结果:
Link 到 documentation
完整代码
from PyQt5 import QtChart
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtChart import QChart, QChartView
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQtChart Pie Chart")
self.setGeometry(100, 100, 1280, 600)
self.show()
self.create_piechart()
def create_piechart(self):
series = QtChart.QPieSeries()
series.append("Python", 80)
series.append("C++", 70)
series.append("Java", 50)
series.append("C#", 40)
series.append("PHP", 30)
series.setLabelsVisible(True)
series.setLabelsPosition(QtChart.QPieSlice.LabelInsideHorizontal)
for slice in series.slices():
slice.setLabel("{:.2f}%".format(100 * slice.percentage()))
chart = QChart()
chart.addSeries(series)
chart.createDefaultAxes()
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.setTitle("Pie Chart Example")
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
chart.legend().markers(series)[0].setLabel("Python")
chart.legend().markers(series)[1].setLabel("C++")
chart.legend().markers(series)[2].setLabel("Java")
chart.legend().markers(series)[3].setLabel("C#")
chart.legend().markers(series)[4].setLabel("PHP")
chartview = QChartView(chart)
chartview.setRenderHint(QPainter.Antialiasing)
self.setCentralWidget(chartview)
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())