|
May
18
|
|
|
Categories: Python
| Tags: ChartDirector, Python
| Views: 3,254
|
上节我们说了标签,这节我们来说图例:
先看图
这个例子不仅展示了图例的显示,也展示了标签的样式的设定和饼图边缘阴影化。
-图例增加通过:BaseChart.addLegend
-改变标签样式使用:PieChart.setLabelFormat
-增加边缘阴影:PieChart.setSectorStyle
代码:
#!/usr/bin/python from pychartdir import * # The data for the pie chart data = [25, 18, 15, 12, 8, 30, 35] # The labels for the pie chart labels = ["Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities", "Production"] # Create a PieChart object of size 450 x 270 pixels c = PieChart(450, 270) # Set the center of the pie at (150, 100) and the radius to 80 pixels c.setPieSize(150, 135, 100) # add a legend box where the top left corner is at (330, 50) c.addLegend(330, 60) # modify the sector label format to show percentages only c.setLabelFormat("{percent}%") # Set the pie data and the pie labels c.setData(data, labels) # Use rounded edge shading, with a 1 pixel white (FFFFFF) border c.setSectorStyle(RoundedEdgeShading, 0xffffff, 1) # Output the chart c.makeChart("legendpie.png")
我们再来看看更为复杂一点的图例:
我们看看这张图有些什么效果:背景颜色渐变、圆角图例框、软阴影、自定义区域颜色、区域阴影渐变、用CDML自定义区域标签、格式化的图例文字
-背景颜色渐变:BaseChart.linearGradientColor
-圆角图例框:BaseChart.setRoundedFrame
-软阴影:BaseChart.setDropShadow
-区域自定义颜色:BaseChart.setColors
-区域渐变阴影:PieChart.setSectorStyle
-自定义区域标签:PieChart.setLabelFormat(设置序号)、PieChart.setLabelStyle(设置样式)、Box.setBackground(标签加框)
-图例文字设置:用LegendBox的TextBox.setText,图例文字模板是用如下CDML描述的:
<*block,valign=top*>{={sector}+1}.<*advanceTo=22*><*block,width=120*>{label}<*/*><*block,width=40,halign=right*>{percent}<*/*>%
<*block,valign=top*> 表示图例中不同的部分都顶端对齐,这样图例第二部分可以多行显示
{={sector}+1}.<*advanceTo=22*> 显示序号,并TAB到22px的位置(sector从0开始,所以要+1)
<*block,width=120*>{label}<*/*> 在120px长的block中显示标签,如果标签文字长于120px,则多行显示
<*block,width=40,halign=right*>{percent}<*/*>% 在40px长的block中显示百分比,并右对齐
代码:
#!/usr/bin/python from pychartdir import * # The data for the pie chart data = [21, 18, 15, 12, 8, 24] # The labels for the pie chart labels = ["Labor", "Licenses", "Taxes", "Legal", "Facilities", "Production"] # The colors to use for the sectors colors = [0x66aaee, 0xeebb22, 0xbbbbbb, 0x8844ff, 0xdd2222, 0x009900] # Create a PieChart object of size 600 x 320 pixels. Use a vertical gradient color # from light blue (99ccff) to white (ffffff) spanning the top 100 pixels as # background. Set border to grey (888888). Use rounded corners. Enable soft drop # shadow. c = PieChart(600, 320) c.setBackground(c.linearGradientColor(0, 0, 0, 100, 0x99ccff, 0xffffff), 0x888888) c.setRoundedFrame() c.setDropShadow() # Add a title using 18 pts Times New Roman Bold Italic font. Add 16 pixels top margin # to the title. c.addTitle("Pie Chart With Legend Demonstration", "timesbi.ttf", 18).setMargin2(0, 0, 16, 0) # Set the center of the pie at (160, 165) and the radius to 110 pixels c.setPieSize(160, 165, 110) # Draw the pie in 3D with a pie thickness of 25 pixels c.set3D(25) # Set the pie data and the pie labels c.setData(data, labels) # Set the sector colors c.setColors2(DataColor, colors) # Use local gradient shading for the sectors c.setSectorStyle(LocalGradientShading) # Use the side label layout method, with the labels positioned 16 pixels from the pie # bounding box c.setLabelLayout(SideLayout, 16) # Show only the sector number as the sector label c.setLabelFormat("{={sector}+1}") # Set the sector label style to Arial Bold 10pt, with a dark grey (444444) border c.setLabelStyle("arialbd.ttf", 10).setBackground(Transparent, 0x444444) # Add a legend box, with the center of the left side anchored at (330, 175), and # using 10 pts Arial Bold Italic font b = c.addLegend(330, 175, 1, "arialbi.ttf", 10) b.setAlignment(Left) # Set the legend box border to dark grey (444444), and with rounded conerns b.setBackground(Transparent, 0x444444) b.setRoundedCorners() # Set the legend box margin to 16 pixels, and the extra line spacing between the # legend entries as 5 pixels b.setMargin(16) b.setKeySpacing(0, 5) # Set the legend box icon to have no border (border color same as fill color) b.setKeyBorder(SameAsMainColor) # Set the legend text to show the sector number, followed by a 120 pixels wide block # showing the sector label, and a 40 pixels wide block showing the percentage b.setText( "<*block,valign=top*>{={sector}+1}.<*advanceTo=22*><*block,width=120*>{label}" \ "<*/*><*block,width=40,halign=right*>{percent}<*/*>%") # Output the chart c.makeChart("legendpie2.png")
From 迷途知返, post PyChartDirector中文教程[8]:饼图的图例
RELATED POSTS:



