Ejemplo n.º 1
0
def draw_pie(data=[], labels=[], use_colors=[], width=360,):
    '''更多属性请查询reportlab.graphics.charts.piecharts.WedgeProperties'''

    pie = Pie()
    pie.x = 60 # x,y饼图在框中的坐标
    pie.y = 20
    pie.slices.label_boxStrokeColor = colors.white  #标签边框的颜色

    pie.data = data      # 饼图上的数据
    pie.labels = labels  # 数据的标签
    pie.simpleLabels = 0 # 0 标签在标注线的右侧;1 在线上边
    pie.sameRadii = 1    # 0 饼图是椭圆;1 饼图是圆形

    pie.slices.strokeColor = colors.red       # 圆饼的边界颜色
    pie.strokeWidth=1                         # 圆饼周围空白区域的宽度
    pie.strokeColor= colors.white             # 整体饼图边界的颜色
    pie.slices.label_pointer_piePad = 10       # 圆饼和标签的距离
    pie.slices.label_pointer_edgePad = 25    # 标签和外边框的距离
    pie.width = width
    pie.direction = 'clockwise'
    pie.pointerLabelMode  = 'LeftRight'
    # for i in range(len(labels)):
    #     pie.slices[i].fontName = 'song' #设置中文
    for i, col in enumerate(use_colors):
         pie.slices[i].fillColor  = col
    return pie
Ejemplo n.º 2
0
    def __add_graph(self):
        drawing = Drawing(200, 100)
        data = list()
        labels = list()

        self.c.drawString(370, 730, 
            'Distribucion en pesos'.encode('utf-8'))

        for acc in self.accounts:
            balance = acc.balance
            if acc.currency == 'USD':
                balance = balance * self.dolar

            data.append(balance)
            labels.append(acc.name)

        pie = Pie()
        pie.x = 280
        pie.y = 630
        pie.height = 100
        pie.width = 100
        pie.data = data
        pie.labels = labels
        pie.simpleLabels = 1
        pie.slices.strokeWidth = 1
        pie.slices.strokeColor = black
        pie.slices.label_visible = 0

        legend = Legend()
        legend.x = 400
        legend.y = 680
        legend.dx              = 8
        legend.dy              = 8
        legend.fontName        = 'Helvetica'
        legend.fontSize        = 7
        legend.boxAnchor       = 'w'
        legend.columnMaximum   = 10
        legend.strokeWidth     = 1
        legend.strokeColor     = black
        legend.deltax          = 75
        legend.deltay          = 10
        legend.autoXPadding    = 5
        legend.yGap            = 0
        legend.dxTextSpace     = 5
        legend.alignment       = 'right'
        legend.dividerLines    = 1|2|4
        legend.dividerOffsY    = 4.5
        legend.subCols.rpad    = 30
        n = len(pie.data)
        self.__setItems(n,pie.slices,
            'fillColor',self.pdf_chart_colors)

        legend.colorNamePairs = [(pie.slices[i].fillColor, 
            (pie.labels[i][0:20],'$%0.2f' % pie.data[i])) for i in xrange(n)]


        drawing.add(pie)
        drawing.add(legend)
        x, y = 0, 0
        renderPDF.draw(drawing, self.c, x, y, showBoundary=False)
Ejemplo n.º 3
0
def simple_pie_chart_label_customization():
    data = [10, 20, 30, 40]
    drawing = Drawing()
    pie = Pie()

    pie.x = 150
    pie.y = 65
    pie.data = data
    pie.labels = [letter for letter in 'abcd']

    # enable label customization
    pie.simpleLabels = 0

    # add some customization
    pie.slices[0].label_angle = 45
    pie.slices[0].label_text = 'foobar'

    # normal pie properties
    pie.slices.strokeWidth = 0.5
    pie.slices[3].popout = 20
    pie.slices[3].strokeDashArray = [1, 1]
    drawing.add(pie)
    drawing.save(formats=['pdf'],
                 outDir='.',
                 fnRoot='simple_pie_chart_label_customization')
Ejemplo n.º 4
0
def create_pie_chart(data, labels, legend=False):
    from reportlab.graphics.charts.piecharts import Pie
    from reportlab.graphics.shapes import Drawing

    d = Drawing(250, 275)
    pie = Pie()
    # required by Auto
    pie._seriesCount = len(data)

    pie.x = 175
    pie.y = 100
    pie.width = 150
    pie.height = 150
    pie.data = data
    pie.labels = labels
    pie.simpleLabels = 0
    pie.sideLabels = True
    pie.slices.strokeWidth = 0.5

    for i in range(0, len(colores)):
        pie.slices[i].fillColor = colores[i]

    if legend:
        add_legend(d, pie, data)

    d.add(pie)

    #d.save(formats=['pdf'], outDir='.', fnRoot='test-pie')
    return d
Ejemplo n.º 5
0
def makePie(pdf, SeBAz_contents, total):
    pdf.saveState()
    d = Drawing(A4[0] * 6 / 12, A4[1] * 5 / 17)
    pc = Pie()
    pc.x = A4[0] * 4 / 12
    pc.y = A4[1] * 5 / 17
    pc.width = A4[0] * 4 / 12
    pc.height = A4[0] * 4 / 12
    passd = len([s for s in SeBAz_contents if len(s) > 3 and s[2] == 'PASS'])
    faild = len([s for s in SeBAz_contents if len(s) > 3 and s[2] == 'FAIL'])
    check = len([s for s in SeBAz_contents if len(s) > 3 and s[2] == 'CHEK'])
    excluded = total - (passd + faild + check)
    pc.data = [passd, faild, excluded, check]
    pc.simpleLabels = 0
    pc.slices.strokeWidth = 0.5
    pc.slices.fontName = 'SF-Pro-Rounded-Heavy'
    pc.slices.fontSize = 8
    pc.slices.labelRadius = 1.1
    pc.slices[0].popout = 10
    pc.slices[0].fillColor = Color(colorPass[0] / 256, colorPass[1] / 256,
                                   colorPass[2] / 256, 1)
    pc.slices[0].fontColor = Color(colorPass[0] / 256, colorPass[1] / 256,
                                   colorPass[2] / 256, 1)
    pc.slices[
        0].label_text = 'which is {:.3f}% of all tests\n{} of {} tests ({:.3f}%) Passed,'.format(
            (passd / total) * 100, passd, (passd + faild + check),
            (passd / (passd + faild + check)) * 100)
    pc.slices[1].fillColor = Color(colorFail[0] / 256, colorFail[1] / 256,
                                   colorFail[2] / 256, 1)
    pc.slices[1].fontColor = Color(colorFail[0] / 256, colorFail[1] / 256,
                                   colorFail[2] / 256, 1)
    pc.slices[
        1].label_text = 'which is {:.3f}% of all tests\n{} of {} tests ({:.3f}%) Failed,'.format(
            (faild / total) * 100, faild, (passd + faild + check),
            (faild / (passd + faild + check)) * 100)
    pc.slices[2].fillColor = darkgray
    pc.slices[2].fontColor = darkgray
    pc.slices[2].label_text = '{} of {} tests ({:.0f}%) Excluded'.format(
        excluded, total, (excluded / total) * 100)
    pc.slices[3].fillColor = Color(colorWarn[0] / 256, colorWarn[1] / 256,
                                   colorWarn[2] / 256, 1)
    pc.slices[3].fontColor = Color(colorWarn[0] / 256, colorWarn[1] / 256,
                                   colorWarn[2] / 256, 1)
    pc.slices[
        3].label_text = 'which is {:.3f}% of all tests\n{} of {} tests ({:.3f}%) are to be Checked,'.format(
            (check / total) * 100, check, (passd + faild + check),
            (check / (passd + faild + check)) * 100)
    d.add(pc)
    renderPDF.draw(d, pdf, -A4[0] / 52, -A4[1] / 6)
    pdf.restoreState()
Ejemplo n.º 6
0
def plpleg(i=None):
    from reportlab.lib.colors import pink, red, green
    pie = Pie()
    pie.x = 0
    pie.y = 0
    pie.pointerLabelMode='LeftAndRight'
    pie.slices.label_boxStrokeColor      = red
    pie.simpleLabels = 0
    pie.sameRadii = 1
    pie.data = [1, 0.1, 1.7, 4.2,0,0]
    pie.labels = ['abcdef', 'b', 'c', 'd','e','fedcba']
    pie.strokeWidth=1
    pie.strokeColor=green
    pie.slices.label_pointer_piePad      = 6
    pie.width = 160
    pie.direction = 'clockwise'
    pie.pointerLabelMode  = 'LeftRight'
    return autoLegender(i,pie,pie.slices,None)
Ejemplo n.º 7
0
def plpleg(i=None):
    from reportlab.lib.colors import pink, red, green
    pie = Pie()
    pie.x = 0
    pie.y = 0
    pie.pointerLabelMode='LeftAndRight'
    pie.slices.label_boxStrokeColor      = red
    pie.simpleLabels = 0
    pie.sameRadii = 1
    pie.data = [1, 0.1, 1.7, 4.2,0,0]
    pie.labels = ['abcdef', 'b', 'c', 'd','e','fedcba']
    pie.strokeWidth=1
    pie.strokeColor=green
    pie.slices.label_pointer_piePad      = 6
    pie.width = 160
    pie.direction = 'clockwise'
    pie.pointerLabelMode  = 'LeftRight'
    return autoLegender(i,pie,pie.slices,None)
def plpleg(i=None):
    from reportlab.lib.colors import pink, red, green

    pie = Pie()
    pie.x = 0
    pie.y = 0
    pie.pointerLabelMode = "LeftAndRight"
    pie.slices.label_boxStrokeColor = red
    pie.simpleLabels = 0
    pie.sameRadii = 1
    pie.data = [1, 0.1, 1.7, 4.2, 0, 0]
    pie.labels = ["abcdef", "b", "c", "d", "e", "fedcba"]
    pie.strokeWidth = 1
    pie.strokeColor = green
    pie.slices.label_pointer_piePad = 6
    pie.width = 160
    pie.direction = "clockwise"
    pie.pointerLabelMode = "LeftRight"
    return autoLegender(i, pie, pie.slices, None)
Ejemplo n.º 9
0
    def pdf_drawPie(self,
                    pie_data,
                    pie_lable=[],
                    colors=[],
                    x=805,
                    y=1650,
                    width=900,
                    height=900,
                    innerRadiusFraction=0.5):
        '''
            @example:
                    chart_data = [1212,66,585,225,36]
                    lable = ['dfd','sdd','trtr','rrrr','ytytyt']
                    self.pdf_drawPie(chart_data,lable)
        '''

        if len(pie_data) == 0 or sum(pie_data) == 0:
            return
        d = Drawing(200, 100)
        pc = Pie()
        pc.x = 65
        pc.y = 65
        pc.width = width
        pc.height = height
        pc.data = pie_data
        pc.labels = pie_lable
        pc.startAngle = 0
        pc.sideLabels = 1
        pc.simpleLabels = 0
        if len(colors) != len(pie_data):
            colors = []
            for i in range(len(pie_data)):
                colors.append(HexColor(self.randomcolor()))
        for i in range(0, len(pie_data)):
            pc.slices[i].fontSize = 40
            pc.slices[i].fontName = FONT
            pc.slices[i].fillColor = colors[i]
            pc.slices[i].strokeColor = colors[i]
            pc.innerRadiusFraction = innerRadiusFraction
        d.add(pc)
        d.drawOn(self.pdf_page_object, x, y)
Ejemplo n.º 10
0
def torta(datos):
    tamanio = datos["tamanio"]
    data = datos["data"]
    labels = datos["labels"]
    colores = datos["colores"]
    titulo = datos["titulo"]

    grafico = Pie()
    grafico.x = 10
    grafico.y = 10
    grafico.startAngle = 45
    grafico.width = tamanio
    grafico.height = tamanio
    grafico.data = data
    grafico.labels = labels
    grafico.slices.fontName = "Helvetica"
    grafico.slices.fontSize = FONTSIZE
    grafico.simpleLabels = False
    grafico.sideLabels = 1
    grafico.sideLabelsOffset = 0.075
    grafico.slices.label_simple_pointer = False
    grafico.slices.label_pointer_elbowLength = 0.5
    grafico.slices.label_pointer_piePad = 3
    grafico.slices.label_pointer_edgePad = 3
    grafico.slices.label_pointer_strokeColor = colors.black
    grafico.slices.label_pointer_strokeWidth = 0.75
    grafico.slices.strokeWidth=1.5
    grafico.slices.strokeColor=colors.white
    for i in range(len(colores)):
        grafico.slices[i].fillColor = colors.HexColor(colores[i])
    retorno = crearDrawing(grafico)
    if titulo:
        etiqueta = Label()
        etiqueta.fontSize = FONTSIZE
        etiqueta.fontName = "Helvetica-Bold"
        etiqueta.setText(titulo)
        etiquetar(retorno, etiqueta, tamanio/2.0, 0.0)
    return retorno
Ejemplo n.º 11
0
    def __get_tags_statistics(self):
        monto_categorias = dict()
        for tra in self.transactions:
            if len(tra.tags) > 0:
                for tag in tra.tags:
                    if tag in monto_categorias.keys():
                        monto_categorias[tag] += tra.amount
                    else:
                        monto_categorias[tag] = tra.amount

        labels = [lab.encode('utf-8') for lab in monto_categorias.keys()]
        data = monto_categorias.values()

        p = PageBreak()
        p.drawOn(self.c, 0, 1000)
        self.c.showPage()
        self.l = 600

        self.c.setFont('Courier', 14)
        self.c.drawString(30, 800, 'Categorias')

        drawing = Drawing(200, 200)

        pie = Pie()
        pie.x = 30
        pie.y = self.l - 130
        pie.height = 300
        pie.width = 300
        pie.data = data
        pie.labels = labels
        pie.simpleLabels = 1
        pie.slices.strokeWidth = 1
        pie.slices.strokeColor = black
        pie.slices.label_visible = 0

        legend = Legend()
        legend.x = 400
        legend.y = self.l
        legend.dx              = 8
        legend.dy              = 8
        legend.fontName        = 'Helvetica'
        legend.fontSize        = 7
        legend.boxAnchor       = 'w'
        legend.columnMaximum   = 10
        legend.strokeWidth     = 1
        legend.strokeColor     = black
        legend.deltax          = 75
        legend.deltay          = 10
        legend.autoXPadding    = 5
        legend.yGap            = 0
        legend.dxTextSpace     = 5
        legend.alignment       = 'right'
        legend.dividerLines    = 1|2|4
        legend.dividerOffsY    = 4.5
        legend.subCols.rpad    = 30
        n = len(pie.data)
        self.__setItems(n,pie.slices,
            'fillColor',self.pdf_chart_colors)

        legend.colorNamePairs = [(pie.slices[i].fillColor, 
            (pie.labels[i][0:20],'$%0.2f' % pie.data[i])) for i in xrange(n)]

        drawing.add(pie)
        drawing.add(legend)
        x, y = 0, 10

        renderPDF.draw(drawing, self.c, x, y, showBoundary=False)
Ejemplo n.º 12
0
    def __per_account_statistic(self):

        for acc in self.accounts:
            p = PageBreak()
            p.drawOn(self.c, 0, 1000)
            self.c.showPage()
            self.l = 760

            self.c.setFont('Courier', 14)
            self.c.drawString(30, 800, 'Cuenta: %s' % \
                acc.name)

            header = ['Fecha', 'Tipo', 'Monto', 'Description']
            data   = [header]
            g_data = list()
            g_labe = list()
            total  = 0

            for tra in self.transactions:
                if tra.account == acc.name:
                    if tra.t_type in ['expense', 'transfer']:
                        tipo = self.__translate_type(tra.t_type)
                        data.append([tra.date, tipo.upper(),
                            '$%2.f' % tra.amount, tra.description])
                        total += tra.amount

                        g_data.append(tra.amount)
                        g_labe.append(tra.description.encode('utf-8'))

            data.append(['TOTAL', '', '$%.2f' % total, ''])

            if len(g_data) == 0 or len(g_labe) == 0:
                self.c.setFont('Courier', 12)
                self.c.drawString(30, 770, 'Sin movimientos negativos')
                continue
 
            from_title = 35
            if len(data) != 2:
                self.l -= ((len(data) * len(data)) + len(data)) + from_title

            t = Table(data)
            t.setStyle(TableStyle([('INNERGRID', (0,0), (-1,-1), 0.25, black),
                ('BOX', (0,0), (-1,-1), 0.25, black),
                ('FONTNAME', (0,0), (-1,0), 'Courier-Bold'),
                ('BACKGROUND', (0,0), (-1,0), HexColor('#efeded')),
                ('BACKGROUND', (0,0), (0,-1), HexColor('#efeded')),
                ('FONTSIZE', (0,0), (-1,0), 12),
                ('FONTSIZE', (0,1), (-1,-1), 8),
                ('FONTNAME', (0,1), (-1,-1), 'Courier'),
                ('BACKGROUND', (0,-1), (-1,-1), red),
                ('TEXTCOLOR', (0,-1), (-1,-1), white)]))

            t.wrapOn(self.c, 30, self.l)
            t.drawOn(self.c, 30, self.l)

            drawing = Drawing(200, 100)

            pie = Pie()
            pie.x = 30
            pie.y = self.l - 300
            pie.height = 200
            pie.width = 200
            pie.data = g_data
            pie.labels = g_labe
            pie.simpleLabels = 1
            pie.slices.strokeWidth = 1
            pie.slices.strokeColor = black
            pie.slices.label_visible = 0
            pie.slices.popout        = 1
            #pie.labels   = map(str, pie.data)

            
            legend = Legend()
            legend.x = 250
            legend.y = self.l - 250
            legend.dx              = 8
            legend.dy              = 8
            legend.fontName        = 'Helvetica'
            legend.fontSize        = 7
            legend.boxAnchor       = 'w'
            legend.columnMaximum   = 10
            legend.strokeWidth     = 1
            legend.strokeColor     = black
            legend.deltax          = 75
            legend.deltay          = 10
            legend.autoXPadding    = 5
            legend.yGap            = 0
            legend.dxTextSpace     = 5
            legend.alignment       = 'right'
            legend.dividerLines    = 1|2|4
            legend.dividerOffsY    = 4.5
            legend.subCols.rpad    = 30
            n = len(pie.data)
            self.__setItems(n,pie.slices,
                'fillColor',self.pdf_chart_colors)

            legend.colorNamePairs = [(pie.slices[i].fillColor, 
                (pie.labels[i][0:20],'$%0.2f' % pie.data[i])) for i in xrange(n)]
            

            drawing.add(pie)
            drawing.add(legend)
            x, y = 0, 10

            renderPDF.draw(drawing, self.c, x, y, showBoundary=False)