浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

A breakdown pie chart - ReportLab Snippets (Beta) - ReportLab.com

A breakdown pie chart - ReportLab Snippets (Beta)

Code snippets are bits of re-usable code submitted by the ReportLab community.
We don't promise that they are accurate, up-to-date or correct - use them at your own risk!

We'd love it if you could participate by submitting your own snippets and sharing your experience with others by commenting.

You will need to create a user account by filling out our very simple form.

View all snippets

 


 

A breakdown pie chart

 

opensourcerl-toolkit

RLIMG: A breakdown pie chart

Author:
rptlab
Posted:
Dec. 3, 2009
Language:
Python
Tags:
opensource rl-toolkit

This shows a Pie chart with a Legend, being saved as both a bitmap for the web and a PDF chart. This code was generated using Diagra (ReportLab's commercial charts package), which allows you to define charts in a GUI. However, you don't need this package to run it, and all the chart features demonstrated are available in our open source package.

 

  1. #Autogenerated by ReportLab guiedit do not edit  
  2. from reportlab.graphics.charts.piecharts import Pie  
  3. from reportlab.lib.colors import black, red, purple, green, maroon, brown, pink, white, HexColor  
  4. from reportlab.graphics.charts.legends import Legend  
  5. from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin  
  6. from reportlab.lib.validators import Auto  
  7. from reportlab.lib.colors import HexColor, black  
  8.   
  9. pdf_chart_colors = [  
  10.         HexColor("#0000e5"),  
  11.         HexColor("#1f1feb"),  
  12.         HexColor("#5757f0"),  
  13.         HexColor("#8f8ff5"),  
  14.         HexColor("#c7c7fa"),  
  15.         HexColor("#f5c2c2"),  
  16.         HexColor("#eb8585"),  
  17.         HexColor("#e04747"),  
  18.         HexColor("#d60a0a"),  
  19.         HexColor("#cc0000"),  
  20.         HexColor("#ff0000"),  
  21.         ]  
  22.   
  23. def setItems(n, obj, attr, values):  
  24.     m = len(values)  
  25.     i = m // n  
  26.     for j in xrange(n):  
  27.         setattr(obj[j],attr,values[j*i % m])  
  28.   
  29. class BreakdownPieDrawing(_DrawingEditorMixin,Drawing):  
  30.     def __init__(self,width=400,height=200,*args,**kw):  
  31.         apply(Drawing.__init__,(self,width,height)+args,kw)  
  32.         # adding a pie chart to the drawing   
  33.         self._add(self,Pie(),name='pie',validate=None,desc=None)  
  34.         self.pie.width                  = 150  
  35.         self.pie.height                 = self.pie.width  
  36.         self.pie.x                      = 20  
  37.         self.pie.y                      = (height-self.pie.height)/2  
  38.         self.pie.data = [26.90,13.30,11.10,9.40,8.50,7.80,7.00,6.20,8.80,1.00]  
  39.         self.pie.labels = ['Financials','Energy','Health Care','Telecoms','Consumer','Consumer 2','Industrials','Materials','Other','Liquid Assets']  
  40.         self.pie.simpleLabels           = 1  
  41.         self.pie.slices.label_visible   = 0  
  42.         self.pie.slices.fontColor       = None  
  43.         self.pie.slices.strokeColor     = white  
  44.         self.pie.slices.strokeWidth     = 1  
  45.         # adding legend  
  46.         self._add(self,Legend(),name='legend',validate=None,desc=None)  
  47.         self.legend.x               = 200  
  48.         self.legend.y               = height/2  
  49.         self.legend.dx              = 8  
  50.         self.legend.dy              = 8  
  51.         self.legend.fontName        = 'Helvetica'  
  52.         self.legend.fontSize        = 7  
  53.         self.legend.boxAnchor       = 'w'  
  54.         self.legend.columnMaximum   = 10  
  55.         self.legend.strokeWidth     = 1  
  56.         self.legend.strokeColor     = black  
  57.         self.legend.deltax          = 75  
  58.         self.legend.deltay          = 10  
  59.         self.legend.autoXPadding    = 5  
  60.         self.legend.yGap            = 0  
  61.         self.legend.dxTextSpace     = 5  
  62.         self.legend.alignment       = 'right'  
  63.         self.legend.dividerLines    = 1|2|4  
  64.         self.legend.dividerOffsY    = 4.5  
  65.         self.legend.subCols.rpad    = 30  
  66.         n = len(self.pie.data)  
  67.         setItems(n,self.pie.slices,'fillColor',pdf_chart_colors)  
  68.         self.legend.colorNamePairs = [(self.pie.slices[i].fillColor, (self.pie.labels[i][0:20], '%0.2f' % self.pie.data[i])) for i in xrange(n)]  
  69.   
  70. if __name__=="__main__"#NORUNTESTS  
  71.     drawing = BreakdownPieDrawing()  
  72.     # the drawing will be saved as pdf and png below, you could do other things with it obviously.  
  73.     drawing.save(formats=['pdf','png'],outDir='.',fnRoot=None)  

 

 


 

posted on 2012-04-25 14:13  lexus  阅读(484)  评论(0编辑  收藏  举报