Ejemplo n.º 1
0
def get_pie_image(width, height, x, y, datas, lables, _colors):
    """
        ��ɱ�״ͼ
    @param width: ͼƬ���
    @param height: ͼƬ�Ŀ��
    @param x: ͼƬ��x���
    @param y: ͼƬ��y���
    @param datas: ���ͼƬ�����
    @param lables: ��״ͼ���������
    """

    from reportlab.graphics.charts.piecharts import Pie
    drawing = Drawing(width, height)
    pc = Pie()
    pc.width = 80
    pc.height = 80
    pc.x = x
    pc.y = y
    pc.data = datas
    pc.labels = lables
    pc.slices.strokeWidth = 0.5
    pc.startAngle = 90
    pc.checkLabelOverlap = True
    pc.sideLabels = True
    pc.sideLabelsOffset = 0.1
    pc.direction = 'clockwise'
    for i in range(len(lables)):
        pc.slices[i].fontName = "msyh"
        pc.slices[i].fontSize = 3
        pc.slices[i].labelRadius = 3
        pc.slices[i].popout = 5
        pc.slices[i].fillColor = _colors[i]
    drawing.add(pc)
    return drawing
def generate(filename, title, additional_info, table_data):

    styles = getSampleStyleSheet()
    report = SimpleDocTemplate(filename)
    report_title = Paragraph(title, styles["h1"])
    report_info = Paragraph(additional_info, styles["BodyText"])
    table_style = [('GRID', (0, 0), (-1, -1), 1, colors.black),
                   ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
                   ('ALIGN', (0, 0), (-1, -1), 'CENTER')]
    report_table = Table(data=table_data, style=table_style, hAlign="LEFT")
    empty_line = Spacer(1, 20)

    sale_per_model = {}
    #print(table_data)
    for data in table_data[1:]:
        #print(data)
        if data[1].split()[0] not in sale_per_model:
            sale_per_model[data[1].split()[0]] = data[3]
        else:
            sale_per_model[data[1].split()[0]] += int(data[3])
    #print(sale_per_model)
    sorted_sales = sorted(sale_per_model.items(),
                          key=lambda kv: (kv[1], kv[0]))
    report_pie = Pie()
    report_pie.sideLabels = 1
    report_pie.checkLabelOverlap = 0
    report_pie.width = 300
    report_pie.height = 200
    report_pie.x = 120
    report_pie.y = 100

    report_pie.data = []
    report_pie.labels = []
    #print('sorted_sale '*20)
    #print(sorted_sales)
    for item in sorted_sales:
        report_pie.data.append(item[1])
        report_pie.labels.append(item[0])

    print(len(set(report_pie.labels)))
    report_chart = Drawing(500, 500)
    report_chart.add(report_pie)
    report.build([
        report_title, empty_line, report_info, empty_line, report_table,
        report_chart
    ])