博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A breakdown pie chart - ReportLab Snippets (Beta)
阅读量:6004 次
发布时间:2019-06-20

本文共 6542 字,大约阅读时间需要 21 分钟。

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 by filling out our very simple form.

 


 

A breakdown pie chart

 

RLIMG: A breakdown pie chart

Author:
Posted:
Dec. 3, 2009
Language:
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)  
#Autogenerated by ReportLab guiedit do not editfrom reportlab.graphics.charts.piecharts import Piefrom reportlab.lib.colors import black, red, purple, green, maroon, brown, pink, white, HexColorfrom reportlab.graphics.charts.legends import Legendfrom reportlab.graphics.shapes import Drawing, _DrawingEditorMixinfrom reportlab.lib.validators import Autofrom reportlab.lib.colors import HexColor, blackpdf_chart_colors = [        HexColor("#0000e5"),        HexColor("#1f1feb"),        HexColor("#5757f0"),        HexColor("#8f8ff5"),        HexColor("#c7c7fa"),        HexColor("#f5c2c2"),        HexColor("#eb8585"),        HexColor("#e04747"),        HexColor("#d60a0a"),        HexColor("#cc0000"),        HexColor("#ff0000"),        ]def setItems(n, obj, attr, values):    m = len(values)    i = m // n    for j in xrange(n):        setattr(obj[j],attr,values[j*i % m])class BreakdownPieDrawing(_DrawingEditorMixin,Drawing):    def __init__(self,width=400,height=200,*args,**kw):        apply(Drawing.__init__,(self,width,height)+args,kw)        # adding a pie chart to the drawing        self._add(self,Pie(),name='pie',validate=None,desc=None)        self.pie.width                  = 150        self.pie.height                 = self.pie.width        self.pie.x                      = 20        self.pie.y                      = (height-self.pie.height)/2        self.pie.data = [26.90,13.30,11.10,9.40,8.50,7.80,7.00,6.20,8.80,1.00]        self.pie.labels = ['Financials','Energy','Health Care','Telecoms','Consumer','Consumer 2','Industrials','Materials','Other','Liquid Assets']        self.pie.simpleLabels           = 1        self.pie.slices.label_visible   = 0        self.pie.slices.fontColor       = None        self.pie.slices.strokeColor     = white        self.pie.slices.strokeWidth     = 1        # adding legend        self._add(self,Legend(),name='legend',validate=None,desc=None)        self.legend.x               = 200        self.legend.y               = height/2        self.legend.dx              = 8        self.legend.dy              = 8        self.legend.fontName        = 'Helvetica'        self.legend.fontSize        = 7        self.legend.boxAnchor       = 'w'        self.legend.columnMaximum   = 10        self.legend.strokeWidth     = 1        self.legend.strokeColor     = black        self.legend.deltax          = 75        self.legend.deltay          = 10        self.legend.autoXPadding    = 5        self.legend.yGap            = 0        self.legend.dxTextSpace     = 5        self.legend.alignment       = 'right'        self.legend.dividerLines    = 1|2|4        self.legend.dividerOffsY    = 4.5        self.legend.subCols.rpad    = 30        n = len(self.pie.data)        setItems(n,self.pie.slices,'fillColor',pdf_chart_colors)        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)]if __name__=="__main__": #NORUNTESTS    drawing = BreakdownPieDrawing()    # the drawing will be saved as pdf and png below, you could do other things with it obviously.    drawing.save(formats=['pdf','png'],outDir='.',fnRoot=None)

 

 


 

转载地址:http://mypmx.baihongyu.com/

你可能感兴趣的文章
mac的git的21个客户端
查看>>
Spring Cloud自定义引导属性源
查看>>
[共通]手机端网页开发问题及解决方法整理
查看>>
我的友情链接
查看>>
${basePath}
查看>>
linux命令之uniq简单用法
查看>>
使用Eclipse调试Java程序的10个技巧
查看>>
Hive分桶表
查看>>
oracle10g 启动时报错:ORA-32004 ORA-19905
查看>>
思科分发列表过滤路由(RIP)动态路由协议篇
查看>>
可登录的用户数量是1.6万个,软件的性能得到充分的考验
查看>>
[实战]MVC5+EF6+MySql企业网盘实战(23)——文档列表
查看>>
[译] ES2018(ES9)的新特性
查看>>
Javascript基础复习 数据类型
查看>>
C# Selenium 破解腾讯滑动验证
查看>>
bom与dom的区别
查看>>
Matlab2012a下配置LibSVM—3.18
查看>>
Java生成-zipf分布的数据集(自定义倾斜度,用作spark data skew测试)
查看>>
修复CefSharp浏览器组件中文输入Bug
查看>>
正则与sed,grep,awk三剑客
查看>>