怎样添加图例到一个饼图中? 下篇
要点关注:
- BaseChart.linearGradientColor定义梯度颜色,再使用BaseChart.setBackground来设置它以应用。
- BaseChart.setRoundedFrame设置整体圆润框架效果
- BaseChart.setDropShadow设置柔和下落阴影
- BaseChart.setColors设置扇区颜色
- PieChart.setSectorStyle设置扇区梯度阴影
源代码:
注:代码60行左右的部分涉及到了CDML ,在
笔记【10】CDML标记语言 中有介绍。
#-*-encoding:utf-8 -*-
from pychartdir import *
setLicenseCode("SXZVFNRN9MZ9L8LGA0E2B1BB");#这个是注册,防止有logo产生
data = [21, 18, 15, 12, 8, 24]
labels = ["Labor", "Licenses", "Taxes", "Legal", "Facilities", "Production"]
colors = ['0x66aaee', '0xeebb22', '0xbbbbbb', '0x8844ff', '0xdd2222', '0x009900']
c = PieChart(600, 320)
c.setBackground(c.linearGradientColor(0, 0, 0, 100, '0x99ccff', '0xffffff'),
'0x888888')
c.setRoundedFrame()
c.setDropShadow()
#设置图表标题。标题上方留出16像素空白
c.addTitle("Pie Chart With Legend Demonstration", "timesbi.ttf", 18).setMargin2(0, 0,
16, 0)
c.setPieSize(160, 165, 110)
# 画3D饼图,厚度为25像素
c.set3D(25)
c.setData(data, labels)
#设置扇区颜色
c.setColors2(DataColor, colors)
#给扇区设置梯度阴影效果
c.setSectorStyle(LocalGradientShading)
# 使用侧边标签布局方法,使标签距离饼图16像素
c.setLabelLayout(SideLayout,16)
# 设置只显示扇区编号
c.setLabelFormat("{={sector}+1}")
#设置标签样式。
c.setLabelStyle("arialbd.ttf", 10).setBackground(Transparent, '0x444444')
#生成图例栏对象b,左侧中部坐标(300,175),并设置字体arial,加粗倾斜,字体大小。
b = c.addLegend(330, 175, 1, "arialbi.ttf", 10)
#设置左对齐。
b.setAlignment(Left)
#设置图例背景和圆角效果
b.setBackground(Transparent, '0x444444')
b.setRoundedCorners()
# 设置图例栏边距16像素,图例内容行间距5像素
b.setMargin(16)
b.setKeySpacing(0, 5)
#设置图例各项内容图标无边缘。
b.setKeyBorder(SameAsMainColor)
b.setText(
#表示图例实体的各部分要靠顶对齐
#显示扇区编号,然后往后tab一个22像素距离
"<*block,valign=top*>{={sector}+1}.<*advanceTo=22*>"\
#120像素块来展示label
"<*block,width=120*>{label}<*/*>"\
#40像素块来展示百分比,而且是右对齐
"<*block,width=40,halign=right*>{percent}<*/*>%")
c.makeChart("legendpie2.png")
这已经是一个有点美丽的饼图了有没有!