Python-Pie chart in reportlabs

前端 未结 2 1306
粉色の甜心
粉色の甜心 2021-01-24 19:17

I\'m referring this link for generation of Pie Chart. In this the colors are pre-defined for each data, ie 10 colors for 10 data points. What if I have 11 data points and then t

2条回答
  •  爱一瞬间的悲伤
    2021-01-24 20:09

    I know it's kind of late to be answering, but in case someone else has the same problem, here is what worked for me:

    At the start of the code you write:

    from reportlab.lib.colors import HexColor
    pdf_chart_colors = [
        HexColor("#0000e5"),
        HexColor("#1f1feb"),
        HexColor("#5757f0"),
        HexColor("#8f8ff5"),
        HexColor("#c7c7fa"),
        HexColor("#f5c2c2"),
        HexColor("#eb8585"),
        HexColor("#e04747"),
        HexColor("#d60a0a"),
        HexColor("#cc0000"),
        HexColor("#ff0000"),
        ]
    

    You can change the color codes to get any color you like, some examples: http://www.creativecolorschemes.com/resources/free-color-schemes/beautiful-color-scheme.shtml

    And when generating the pie chart you only need to add two lines at the end:

    def piechart(Values, Names):        
        d = Drawing(100, 125)
        cht = Pie()
        ...
        n = len(cht.data)
        setItems(n,cht.slices,'fillColor',pdf_chart_colors)
    

    Where setItems has been previously defined as:

    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])
    

    Use this link (last pie chart example) for extra ereference: http://m.blog.csdn.net/blog/wdt3385/10142163

提交回复
热议问题