|
May
13
|
|
|
Categories: Python
| Tags: ChartDirector, Python
| Views: 2,438
|
下面我们看看如何用不同的方式来显示饼图的标签
1. 侧边标签层
先看看效果图:
标签的效果: 金属背景颜色, 圆角, 玻璃阴影效果
侧边标签层的标签将放置在图表的左侧和右侧
侧边标签层有一个好处, 那就是标签会自动上下排列, 防止交叠.
金色的背景由 pychartdir.goldColor产生
粉色金属背景由pychartdir.metalColor产生, 用Box.setBackground来设置标题TextBox的背景颜色
侧边标签层由PieChart.setLabelLayout来指定
玻璃效果由pychartdir.glassEffect来增加, 用Box.setBackground来分别指定
圆角标签用Box.setRoundedCorners来设置
如果数据中有些排序好的小值, 这个小值代表的块将会被放在一起, 这样标签就不需要太多的移动, 这个小值块更多的是在左边或者右边,而不是上边或者下边.
如果数据是降序排列的, 那么图的初始角度是顺时针135度,来保证那些小值块在45-135之间(在图的右边), 如果是升序, 则初始角度是45度.
代码:
#!/usr/bin/python from pychartdir import * # The data for the pie chart data = [35, 30, 25, 7, 6, 5, 4, 3, 2, 1] # The labels for the pie chart labels = ["Labor", "Production", "Facilities", "Taxes", "Misc", "Legal", "Insurance", "Licenses", "Transport", "Interest"] # Create a PieChart object of size 560 x 270 pixels, with a golden background and a 1 # pixel 3D border c = PieChart(560, 270, goldColor(), -1, 1) # Add a title box using 15 pts Times Bold Italic font and metallic pink background # color c.addTitle("Project Cost Breakdown", "timesbi.ttf", 15).setBackground(metalColor( 0xff9999)) # Set the center of the pie at (280, 135) and the radius to 110 pixels c.setPieSize(280, 135, 110) # Draw the pie in 3D with 20 pixels 3D depth c.set3D(20) # Use the side label layout method c.setLabelLayout(SideLayout) # Set the label box background color the same as the sector color, with glass effect, # and with 5 pixels rounded corners t = c.setLabelStyle() t.setBackground(SameAsMainColor, Transparent, glassEffect()) t.setRoundedCorners(5) # Set the border color of the sector the same color as the fill color. Set the line # color of the join line to black (0x0) c.setLineColor(SameAsMainColor, 0x000000) # Set the start angle to 135 degrees may improve layout when there are many small # sectors at the end of the data array (that is, data sorted in descending order). It # is because this makes the small sectors position near the horizontal axis, where # the text label has the least tendency to overlap. For data sorted in ascending # order, a start angle of 45 degrees can be used instead. c.setStartAngle(135) # Set the pie data and the pie labels c.setData(data, labels) # Output the chart c.makeChart("sidelabelpie.png")
2. 环形标签层
看效果图:
这种环形标签有几种效果, 默认地是像最开始的例子那样,在图表的外面,紧挨着图.
Pie.setLabelPos可以用来控制标签与圆周之间的距离. 并可以在圆周和标签之间添加连接线, 当标签比较远的时候连接线是很有用的.
如果标签与圆周之间的距离是负值, 那么标签将会显示在圆周之内.
代码:
#!/usr/bin/python from pychartdir import * def createChart(img) : # The data for the pie chart data = [42, 18, 8] # The labels for the pie chart labels = ["Agree", "Disagree", "Not Sure"] # The colors to use for the sectors colors = [0x66ff66, 0xff6666, 0xffff00] # Create a PieChart object of size 300 x 300 pixels. Set the background to a # gradient color from blue (aaccff) to sky blue (ffffff), with a grey (888888) # border. Use rounded corners and soft drop shadow. c = PieChart(300, 300) c.setBackground(c.linearGradientColor(0, 0, 0, c.getHeight() / 2, 0xaaccff, 0xffffff), 0x888888) c.setRoundedFrame() c.setDropShadow() if img == "0" : #============================================================ # Draw a pie chart where the label is on top of the pie #============================================================ # Set the center of the pie at (150, 150) and the radius to 120 pixels c.setPieSize(150, 150, 120) # Set the label position to -40 pixels from the perimeter of the pie (-ve # means label is inside the pie) c.setLabelPos(-40) else : #============================================================ # Draw a pie chart where the label is outside the pie #============================================================ # Set the center of the pie at (150, 150) and the radius to 80 pixels c.setPieSize(150, 150, 80) # Set the sector label position to be 20 pixels from the pie. Use a join line # to connect the labels to the sectors. c.setLabelPos(20, LineColor) # Set the pie data and the pie labels c.setData(data, labels) # Set the sector colors c.setColors2(DataColor, colors) # Use local gradient shading, with a 1 pixel semi-transparent black (bb000000) # border c.setSectorStyle(LocalGradientShading, 0xbb000000L, 1) # Output the chart c.makeChart("circlelabelpie%s.png" % img) createChart("0") createChart("1")
From 迷途知返, post PyChartDirector中文教程[7]:饼图的标签
RELATED POSTS:
Leave a comment
| Trackback

