PyQt4中绘制柱状图、饼图、燃尽图

绘制柱状图

'''重写绘图类方法'''
def paintEvent(self,event):
    qp = QtGui.QPainter()
    qp.begin(self)
    self.drawChart(qp)

'''绘制坐标轴'''
def drawChart(self,qp):
    size = self.size()
    height = size.height()
    width = size.width()
    '''
    绘制轴
    '''
    self.pointOrigin = QtCore.QPoint(50,height-50) #原点
    self.pointX = QtCore.QPoint(width-20,height-50) 
    self.pointY = QtCore.QPoint(50,20)
    points = [self.pointY,self.pointOrigin,self.pointX]
    qp.drawPolyline(points)
    '''
    绘制箭头
    '''
    pointXUp = QtCore.QPoint(width-30,height-60)
    pointXDown = QtCore.QPoint(width-30,height-40)
    points2 = [pointXUp,self.pointX,pointXDown]
    qp.drawPolyline(points2)
    pointYLeft = QtCore.QPoint(40,30)
    pointYRight = QtCore.QPoint(60,30)
    points3 = [pointYLeft,self.pointY,pointYRight]
    qp.drawPolyline(points3)

    font = QtGui.QFont()
    font.setPointSize(10)
    qp.setFont(font)
    qp.drawText(0,50,'Y')
    qp.drawText(width-60,height-30,'X')

    '''
    根据横坐标长度及柱子个数划分每个柱图的宽度,10为柱间隙
    '''
    pillarDic = {'apple':4,'orange':23,'banana':22,'cherry':13,'strawbarry':7} #存放展示的数据
    pillarNum = pillarDic.__len__() #柱子个数
    self.widthPer = ((pointXDown.x()-self.pointOrigin.x())*1.0-5)/pillarNum-10
    '''首先找出数据中的最大值'''
    max = 0
    for key in pillarDic :
        if pillarDic[key]>=max:
            max = self.statusDict[key]
    num=0 #柱子编号
    for key in pillarDic:
        num+=1
        count = pillarDic[key]
        self.drawPillar(qp,num,count,max,QtGui.QColor('#cccc00'),key) #随机给颜色

'''定义绘制柱形图的公用方法'''
# num--柱子编号,
# count--数据值,
# max--最大数据
# color--柱子颜色
# name--标注
def drawPillar(self,qp,num,count,max,color,name):
    qp.setBrush(color)
    y = self.pointOrigin.y()-80
    '''
    根据最大值按比例绘制柱子高度
    '''
    qp.drawRect(self.pointOrigin.x()+num*10+self.widthPer*(num-1),self.pointOrigin.y()
                -count*1.0*y/max,self.widthPer,count*1.0*y/max)
    '''绘制标注'''
    qp.drawText(self.pointOrigin.x()+num*10+self.widthPer*(num-1)+self.widthPer*45/100,self.pointOrigin.y()-count*1.0*y/max-5,u'%d'%count)
    qp.drawText(self.pointOrigin.x()+num*10+self.widthPer*(num-1)+self.widthPer*45/100,self.size().height()-20,u'%s'%name)

绘制饼图

'''
写死的饼状图,待优化
'''
cakeDic = {'apple':4,'orange':23,'banana':22} #展示数据
cakeSum = 0 #数据总量
for key in cakeDic:
    cakeSum += cakeDic[key]
d = width/2-100 #大饼直径
if d>=height-50: #防止饼图超过widget高度
    d = height-50
'''根据数据占比绘制饼图'''
gray = QtGui.QColor(25,148,117)
qp.setBrush(gray)
qp.drawPie(30,40,d,d,0,360*16*1.0*cakeDic[apple]/cakeSum )
cyan = QtGui.QColor(199,206,178)
qp.setBrush(cyan)
qp.drawPie(30,40,d,d,360*16*1.0*cakeDic[apple]/cakeSum ,360*16*1.0*cakeDic[orange]/cakeSum )
orange = QtGui.QColor(224,128,49)
qp.setBrush(orange)
qp.drawPie(30,40,d,d,360*16-360*16*1.0*cakeDic[banana]/cakeSum ,360*16*1.0*cakeDic[banana]/cakeSum )
'''
绘制颜色标注
'''
font = QtGui.QFont()
font.setPointSize(9)
qp.setFont(font)
qp.drawText(d+50,d-40,u'%s %d'%(cakeDic,cakeDic ))
qp.fillRect(d+30,d-50,15,15,gray)
qp.drawText(d+50,d-10,u'%s %d'%(cakeDic ,cakeDic ))
qp.fillRect(d+30,d-20,15,15,cyan)
qp.drawText(d+50,d+20,u'%s %d'%(cakeDic ,cakeDic ))
qp.fillRect(d+30,d+10,15,15,orange)

绘制燃尽图

未完待续…

你可能感兴趣的:(Python,PyQt4)