赞
踩
略
import sys from PyQt5.QtGui import QPainter, QPen from PyQt5.QtCore import Qt, QPoint from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtChart import (QChartView, QChart, QBarSeries, QBarSet, QLineSeries, QPieSeries, QLegend, QBarCategoryAxis, QValueAxis) # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'plot.ui' # # Created by: PyQt5 UI code generator 5.15.7 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtChart import QChartView class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") self.graphicsView = QChartView(self.centralwidget) self.graphicsView.setStyleSheet("background-color: rgba(255, 255, 255, 100);") self.graphicsView.setObjectName("graphicsView") self.verticalLayout.addWidget(self.graphicsView) self.graphicsView_2 = QChartView(self.centralwidget) self.graphicsView_2.setStyleSheet("background-color: rgba(255, 255, 255, 100);") self.graphicsView_2.setObjectName("graphicsView_2") self.verticalLayout.addWidget(self.graphicsView_2) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) class Window(QMainWindow): def __init__(self): super().__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.createChart() def createChart(self): # 创建条状单元 barSet0 = QBarSet('Jane') barSet1 = QBarSet('Jone') barSet0.append([1, 2, 3, 4, 5, 6]) barSet1.append([5, 2, 2, 4, 3, 7]) # 条状图 barSeries = QBarSeries() barSeries.append(barSet0) barSeries.append(barSet1) # 创建图表 chart = QChart() chart.addSeries(barSeries) chart.setTitle('柱状图示例') # 设置横向坐标(X轴) categories = ['一月', '二月', '三月', '四月', '五月', '六月'] axisX = QBarCategoryAxis() axisX.append(categories) chart.addAxis(axisX, Qt.AlignBottom) barSeries.attachAxis(axisX) axisX.setRange('一月', '六月') # 设置纵向坐标(Y轴) axisY = QValueAxis() axisY.setRange(0, 20) chart.addAxis(axisY, Qt.AlignLeft) barSeries.attachAxis(axisY) # 图例属性 chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) #设置饼图数据 pieSeries = QPieSeries() pieSeries.append('Jane', 1) pieSeries.append('Joe', 2) pieSeries.append('Andy', 3) pieSeries.append('Barbara', 4) pieSeries.append('Axel', 5) #处理索引号为1的片 pieSlice = pieSeries.slices()[1] pieSlice.setExploded() pieSlice.setLabelVisible() #设置标签可见,缺省不可见 pieSlice.setPen(QPen(Qt.darkGreen, 2)) pieSlice.setBrush(Qt.green) #创建图表 chart2 = QChart() chart2.addSeries(pieSeries) chart2.setTitle('简单的饼图示例') chart2.legend().hide() # 图表视图 self.ui.graphicsView.setChart(chart) self.ui.graphicsView.setRenderHint(QPainter.Antialiasing) self.ui.graphicsView_2.setChart(chart2) self.ui.graphicsView_2.setRenderHint(QPainter.Antialiasing) if __name__ == '__main__': app = QApplication(sys.argv) mywindow = Window() mywindow.show() sys.exit(app.exec_())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。