zoukankan      html  css  js  c++  java
  • A breakdown pie chart ReportLab Snippets (Beta)

    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)  

  • 相关阅读:
    问题:oracle if;结果:Oracle IF语句的使用
    问题:PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用;结果:PL/SQL中不能用exists函数?
    问题:oracle decode;结果:oracle中的decode的使用
    问题:只能在执行 Render() 的过程中调用 RegisterForEventValidation;结果:只能在执行 Render() 的过程中调用 RegisterForEventValidation
    问题:oracle long 与 clob;结果:long类型比clob到底差在什么地方?
    问题:oracle 字符串转换成日期;结果:[oracle] to_date() 与 to_char() 日期和字符串转换
    问题:oracle CLOB类型;结果:oracle中Blob和Clob类型的区别
    问题:C#根据生日计算属相;结果:C#实现根据年份计算生肖属相的方法
    po dto vo bo
    eclipse中自动加载源码的方法
  • 原文地址:https://www.cnblogs.com/lexus/p/2469741.html
Copyright © 2011-2022 走看看