def __init__(self, width=400, height=200, data=[], labels=[], legends=[], *args, **kw): Drawing.__init__(self, width, height, *args, **kw) self.add(Pie(), name='chart') self.add(Legend(), name='legend') self.chart.x = self.width / 4 self.chart.y = 15 self.chart.width = self.width - 150 self.chart.height = self.height - 40 self.chart.data = data for i in range(len(data)): self.chart.slices[i].fillColor = color_list[i] self.legend.alignment = 'right' self.legend.x = self.width - 20 self.legend.y = self.height - self.height / 4 self.legend.dx = 8 self.legend.dy = 8 self.legend.deltay = 10 self.legend.dxTextSpace = 3 self.legend.columnMaximum = 10 self.legend.colorNamePairs = [(color_list[i], legends[i]) for i in range(len(legends))]
def __init__(self, width=A4[0], height=A4[0] / 2, left_margin=0, right_margin=0, top_margin=0, bottom_margin=0, label_font_size=10, label_font_name='simsun'): self.label_font_size = label_font_size self.label_font_name = label_font_name self.width = width self.height = height # self.chart_width = self.width # self.chart_height = self.height self.chart_width = self.chart_height = min( self.width - left_margin - right_margin, self.height - top_margin - bottom_margin) self.chart = Pie() # self.chart.x = 0 # self.chart.y = 0 self.chart.x = (self.width - self.chart_width) / 2 self.chart.y = (self.height - self.chart_height) / 2 self.chart.width = self.chart_width self.chart.height = self.chart_height self.title = Label()
def test0(self, isFast=0): """Hello World, on a rectangular background. The rectangle's fillColor is yellow. The string's fillColor is red. """ reportlab.rl_config.shapeChecking = not isFast pdfPath = outputfile('test_graphics_speed_fast.pdf') c = Canvas(pdfPath) t0 = time.time() d = Drawing(400, 200) num = 100 for i in range(num): pc = Pie() pc.x = 150 pc.y = 50 pc.data = [10, 20, 30, 40, 50, 60] pc.labels = ['a', 'b', 'c', 'd', 'e', 'f'] pc.slices.strokeWidth = 0.5 pc.slices[3].popout = 20 pc.slices[3].strokeWidth = 2 pc.slices[3].strokeDashArray = [2, 2] pc.slices[3].labelRadius = 1.75 pc.slices[3].fontColor = colors.red d.add(pc) d.drawOn(c, 80, 500) t1 = time.time() result = 'drew %d pie charts in %0.4f' % (num, t1 - t0) open(outputfile('test_graphics_speed_test%s.log' % (isFast + 1)), 'w').write(result)
def build_pdf(): fruit = {"elderberries": 1,"figs": 1,"apples": 2,"durians": 3,"bananas": 5,"cherries": 8,"grapes": 13} report = SimpleDocTemplate("/home/scottdavis/eclipse-workspace/module1/report.pdf") styles = getSampleStyleSheet() report_title = Paragraph("A Complete Inventory of My Fruit", styles["h1"]) table_data = [] for k, v in fruit.items(): table_data.append([k, v]) table_style = [('GRID', (0,0), (-1,-1), 1, colors.black)] report_table = Table(data=table_data, style=table_style, hAlign="LEFT") report_pie = Pie() #width=3*inch, height=3*inch report_pie.width = 180 report_pie.height = 180 report_pie.data = [] report_pie.labels = [] for fruit_name in sorted(fruit): report_pie.data.append(fruit[fruit_name]) report_pie.labels.append(fruit_name) report_chart = Drawing() report_chart.add(report_pie) report.build([report_title, report_table, report_chart])
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
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
def main(argv): """Process the JSON data and generate a full report out of it.""" data = load_data("car_sales.json") summary = process_data(data) print(summary) # My code starts here #VVV########################################### # Turn this into a PDF report summary_paragraph = summary[0] + "<br/>" + summary[1] + "<br/>" + summary[2] car_table = cars_dict_to_table(data) # sorts table by sales from most to least car_table = [car_table[0]] + sorted( car_table[1:], key=lambda x: x[3], reverse=True) # Adds a pie chart where each slice represents a table row (it's dense) report_pie = Pie(width=3, height=3) report_pie.data = [] #report_pie.labels = [] # It's dense enough without the labels report_path = "/tmp/cars.pdf" for item in car_table[1:]: report_pie.data.append(item[3]) #report_pie.labels.append(item[1]) # It's dense enough without the labels report_chart = Drawing() report_chart.add(report_pie) reports.generate(report_path, "Sales summary for last month", summary_paragraph, report_chart, car_table) # Send the PDF report as an email attachment message = emails.generate( "*****@*****.**", "{}@example.com".format(os.environ.get('USER')), "Sales summary for last month", summary_paragraph.replace( "<br/>", "\n"), # Instead of calling summary[0:3] one by one again report_path) emails.send(message)
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')
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 getPieChart(): data = [3, 18, 20] chart = Pie() chart.data = data chart.x = 50 chart.y = 5 chart.labels = ['A', 'B', 'C'] title = String(50, 110, 'Pie Chart', fontSize=14) chart.sideLabels = True chart.slices[0].fillColor = colors.red chart.slices[0].popout = 8 legend = Legend() legend.x = 180 legend.y = 80 legend.alignment = 'right' legend.colorNamePairs = Auto(obj=chart) drawing = Drawing(240, 120) drawing.add(title) drawing.add(chart) drawing.add(legend) return drawing
def sample4pie(): width = 300 height = 150 d = Drawing(width, height) pc = Pie() pc.x = 150 pc.y = 50 pc.data = [1, 50, 100, 100, 100, 100, 100, 100, 100, 50] pc.labels = u'0 \xe4 b c d e f g h i'.split() pc.slices.strokeWidth = 0.5 pc.slices[3].popout = 20 pc.slices[3].strokeWidth = 2 pc.slices[3].strokeDashArray = [2, 2] pc.slices[3].labelRadius = 1.75 pc.slices[3].fontColor = colors.red pc.slices[1].fontName = fontName d.add(pc) legend = Legend() legend.x = width - 5 legend.y = height - 5 legend.dx = 20 legend.dy = 5 legend.deltax = 0 legend.boxAnchor = 'nw' legend.colorNamePairs = Auto(chart=pc) d.add(legend) return d
def monthly_spending_cat_pie(c, category, h, w, v=False): d = Drawing() pie = Pie() pie.sideLabels = 1 pie._seriesCount = 10 spent = 0 my_data = [] for i in range(1, month + 1): spent += int(data.month_code(category, i, year)) print(spent, category) my_data.append(spent) my_data.append(data.code_budget(category) - spent) labels = [f'{category} spending', 'budget left'] if sum(my_data) == 0: return pie.x = 0 pie.y = 0 pie.width = 50 pie.height = pie.width pie.data = my_data pie.slices.fontName = serif_font pie.slices.fontSize = 11 pie.labels = labels pie.slices.strokeWidth = 0.5 #pie.slices[3].popout = 20 d.add(pie) d.drawOn(c, h, w)
def category_budget_left_pie(c, category, h, w): my_data = [] for x in range(1, month + 1): my_data.append(data.category_data(category, x, year)) labels = month_name[1:month + 1] labels.append('Budget') my_data.append(data.category_budget(category) - sum(my_data)) d = Drawing() pie = Pie() pie.sideLabels = 1 pie._seriesCount = 10 #if legend: # add_legend(d, pie, data) pie.x = 0 pie.y = 0 pie.width = 100 pie.height = pie.width pie.data = my_data pie.slices.fontName = serif_font pie.slices.fontSize = 11 pie.labels = labels pie.slices.strokeWidth = 0.5 #pie.slices[3].popout = 20 d.add(pie) d.drawOn(c, h, w)
def overview_budget_spent(c, w, h): my_data = data.monthly_total(year) my_data = my_data[0:month] budget = data.budget() - sum(my_data) my_data.append(budget) my_label = [] label = [ 'January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] for j in range(0, month): my_label.append(label[j]) my_label.append('Budget remaining') d = Drawing() pie = Pie() pie.sideLabels = 1 pie._seriesCount = 10 pie.x = 0 pie.y = 0 pie.width = 100 pie.height = pie.width pie.data = my_data pie.slices.fontName = serif_font pie.slices.fontSize = 11 pie.labels = my_label pie.slices.strokeWidth = 0.5 pie.slices[-1].popout = 20 d.add(pie) d.drawOn(c, w, h)
def monthly_spending_breakdown_pie(c, category, h, w, v=False): d = Drawing() pie = Pie() pie.sideLabels = 1 pie._seriesCount = 10 output = [] my_data = [] output = data.cat_ind_data(category, month, year) my_data = output[1] labels = output[0] if sum(my_data) == 0: return pie.x = 0 pie.y = 0 pie.width = 100 pie.height = pie.width pie.data = my_data pie.slices.fontName = serif_font pie.slices.fontSize = 11 pie.labels = labels pie.slices.strokeWidth = 0.5 #pie.slices[3].popout = 20 d.add(pie) d.drawOn(c, h, w)
def pie_chart_draw(self, values, llabels): d = Drawing(10, 150) # chart pc = Pie() pc.x = 0 pc.y = 50 # set data pc.data = values # set labels percentage = [] for value in values: v = round(value, 2) percentage.append(str(v) + " %") pc.labels = percentage # set the link line between slice and it's label pc.sideLabels = 1 # set width and color for slices pc.slices.strokeWidth = 0 pc.slices.strokeColor = None d.add(self.title_draw(250, 180, 'Student Attendance Percentage')) d.add(pc) d.add( self.legend_draw(llabels, pc, x=300, y=150, boxAnchor='ne', columnMaximum=12, type='pie')) return d
def overview_month_spending_breakdown_pie(c, h, w): categories = [ 'Clothing', 'Transport', 'Insurance', 'Food', 'Medical and Dental', 'Housing', 'Utilities', 'Education', 'Gifts', 'Entertainment' ] d = Drawing() pie = Pie() pie.sideLabels = 1 pie._seriesCount = 10 my_data = [] for x in categories: my_data.append(data.category_data(x.lower(), month, 2019)) pie.x = 0 pie.y = 0 pie.width = 100 pie.height = pie.width pie.data = my_data pie.slices.fontName = serif_font pie.slices.fontSize = 11 pie.labels = tuple(categories) pie.slices.strokeWidth = 0.5 #pie.slices[3].popout = 20 d.add(pie) d.drawOn(c, h, w)
def __init__(self, width=200, height=150, pieType="Pie", bShowBoundary=False, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) if pieType == "Pie": self._add(self, Pie(), name='chart', validate=None, desc="The pie object") else: #default to Pie3d self._add(self, Pie3d(), name='chart', validate=None, desc="The pie object") if bShowBoundary: self.background = ShadedRect() self.background.fillColorStart = colors.white self.background.fillColorEnd = colors.white self.background.numShades = 1 self.background.strokeWidth = 0.5 self.background.height = height self.background.width = width
def __init__(self, drawing=None, data=None, x=70, y=25, width=115, height=115): pie = Pie() pie.strokeColor = white pie.slices.strokeColor = white pie.slices.popout = 1 pie.width = width pie.height = height pie.y = y pie.x = x pie.sideLabels = 1 pie.slices.fontName = 'Lato' pie.slices.fontSize = 9 colors = get_n_random_colors(len(data)) for i in range(0, len(data)): pie.slices[i].fillColor = toColor(colors[i]) pie.data = [value for (_, value) in data] pie.labels = [key for (key, _) in data] drawing.add(pie)
def __init__(self, width=400, height=200, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) # Width and height of chart. self.width = 772 self.height = 280 # Add title self._add(self, Label(), name='title', validate=None, desc=None) self.title._text = 'Simple Pie Chart' self.title.fontSize = 24 self.title.fontName = "Calibri" # Position Title self.title.x = 386 self.title.y = 240 # Add Bar chart to the Drawing. self._add(self, Pie(), name="chart", validate=None, desc=None) # Bar Chart Dimensions self.chart.width = 150 self.chart.height = 150 self.chart.slices[2].popout = 15 # Bar Chart Position self.chart.x = (self.width - self.chart.width) / 2 self.chart.y = 20 # Data will be sent from RML. However, defaults can be initialized here. self.chart.data = [] # Sector Labels. Data will be sent from RML. However, defaults can be initialized here. self.chart.labels = [] # Start angle for Pie Chart. 90 indicates 12'o Clock position. self.chart.startAngle = 90 # Pie Chart direction. self.chart.direction = 'clockwise'
def test_24_PieCharts(self): from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib import colors from reportlab.lib.units import inch from reportlab.platypus import SimpleDocTemplate, Spacer, Paragraph from reportlab.pdfbase import pdfmetrics from reportlab.graphics.shapes import Drawing from reportlab.pdfbase.ttfonts import TTFont from reportlab.graphics.charts.piecharts import Pie pdfmetrics.registerFont(TTFont('chsFont', 'STHeiti Light.ttc')) stylesheet = getSampleStyleSheet() elements = [] doc = SimpleDocTemplate("demo.pdf") elements.append( Paragraph('<font name="chsFont">JY.zenist.song - 俊毅</font>', stylesheet['Title'])) elements.append(Spacer(1, 1 * inch)) d = Drawing(400, 200) data = [13, 5, 20, 22, 37, 45] pc = Pie() pc.x = 65 pc.y = 15 pc.width = 150 pc.height = 150 pc.data = data pc.labels = ['a', 'b', 'c', 'd', 'e', 'f'] d.add(pc) elements.append(d) doc.build(elements)
def __init__(self, width=400, height=200, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) fontSize = 8 fontName = 'Helvetica' #pie self._add(self, Pie(), name='pie', validate=None, desc=None) self.pie.strokeWidth = 1 self.pie.slices.strokeColor = PCMYKColor(0, 0, 0, 0) self.pie.slices.strokeWidth = 1 #legend self._add(self, Legend(), name='legend', validate=None, desc=None) self.legend.columnMaximum = 99 self.legend.alignment = 'right' self.legend.dx = 6 self.legend.dy = 6 self.legend.dxTextSpace = 5 self.legend.deltay = 10 self.legend.strokeWidth = 0 self.legend.subCols[0].minWidth = 75 self.legend.subCols[0].align = 'left' self.legend.subCols[1].minWidth = 25 self.legend.subCols[1].align = 'right' # sample data colors = [ PCMYKColor(100, 67, 0, 23, alpha=100), PCMYKColor(70, 46, 0, 16, alpha=100), PCMYKColor(50, 33, 0, 11, alpha=100), PCMYKColor(30, 20, 0, 7, alpha=100), PCMYKColor(20, 13, 0, 4, alpha=100), PCMYKColor(10, 7, 0, 3, alpha=100), PCMYKColor(0, 0, 0, 100, alpha=100), PCMYKColor(0, 0, 0, 70, alpha=100), PCMYKColor(0, 0, 0, 50, alpha=100), PCMYKColor(0, 0, 0, 30, alpha=100), PCMYKColor(0, 0, 0, 20, alpha=100), PCMYKColor(0, 0, 0, 10, alpha=100) ] self.pie.data = [56.0, 12.199999999999999, 28.5, 3.3999999999999999] for i in range(len(self.pie.data)): self.pie.slices[i].fillColor = colors[i] self.height = 200 self.legend.boxAnchor = 'c' self.legend.y = 100 self.pie.strokeColor = PCMYKColor(0, 0, 0, 0, alpha=100) self.pie.slices[1].fillColor = PCMYKColor(100, 60, 0, 50, alpha=100) self.pie.slices[2].fillColor = PCMYKColor(0, 100, 100, 40, alpha=100) self.pie.slices[3].fillColor = PCMYKColor(66, 13, 0, 22, alpha=100) self.pie.slices[0].fillColor = PCMYKColor(100, 0, 90, 50, alpha=100) self.legend.colorNamePairs = [ (PCMYKColor(100, 0, 90, 50, alpha=100), ('BP', '56.0%')), (PCMYKColor(100, 60, 0, 50, alpha=100), ('BT', '12.2%')), (PCMYKColor(0, 100, 100, 40, alpha=100), ('Tesco', '28.5%')), (PCMYKColor(66, 13, 0, 22, alpha=100), ('Persimmon', '3.4%')) ] self.width = 400 self.legend.x = 350 self.pie.width = 150 self.pie.height = 150 self.pie.y = 25 self.pie.x = 25
def pie_chart_draw(self, values, llabels): d = Drawing(10, 150) # chart pc = Pie() pc.x = 0 pc.y = 50 # set data pc.data = values # set labels pc.labels = get_percentage(values) # set the link line between slice and it's label pc.sideLabels = 1 # set width and color for slices pc.slices.strokeWidth = 0 pc.slices.strokeColor = None d.add( self.title_draw(250, 180, _('Precipitation probability statistics'))) d.add(pc) d.add( self.legend_draw(llabels, pc, x=300, y=150, boxAnchor='ne', columnMaximum=12, type='pie')) return d
def sample4pie(): width = 300 height = 150 d = Drawing(width, height) pc = Pie() pc.x = 150 pc.y = 50 pc.data = [1, 50, 100, 100, 100, 100, 100, 100, 100, 50] pc.labels = ['0','a','b','c','d','e','f','g','h','i'] pc.slices.strokeWidth=0.5 pc.slices[3].popout = 20 pc.slices[3].strokeWidth = 2 pc.slices[3].strokeDashArray = [2,2] pc.slices[3].labelRadius = 1.75 pc.slices[3].fontColor = colors.red d.add(pc) legend = Legend() legend.x = width-5 legend.y = height-5 legend.dx = 20 legend.dy = 5 legend.deltax = 0 legend.boxAnchor = 'nw' legend.colorNamePairs=Auto(chart=pc) d.add(legend) return d
def __init__(self, pie_data, width=400, height=200): apply(Drawing.__init__, (self, width, height)) self._add(self, Pie(), name='chart', validate=None, desc=None) self.chart.x = 20 self.chart.y = (self.height - self.chart.height) / 2 self.chart.slices.strokeWidth = 1 self.chart.slices.popout = 1 self.chart.direction = 'clockwise' self.chart.width = self.chart.height self.chart.startAngle = 90 self.chart.slices[0].popout = 10 self._add(self, Legend(), name='legend', validate=None, desc=None) self.legend.x = width - 20 self.legend.y = 0 self.legend.boxAnchor = 'se' self.legend.subCols[1].align = 'right' # these data can be read from external sources data = [] categories = [] for d in pie_data.keys(): categories.append(d) data.append(pie_data.get(d, 0)) #data = (9, 7, 6, 4, 2.5, 1.0) #categories = ('A','B','C','D','E','F',) colors = [ PCMYKColor(0, 1, 550, x) for x in (1030, 2080, 3060, 440, 4520, 2225) ] self.chart.data = data self.chart.labels = map(str, self.chart.data) self.legend.colorNamePairs = zip(colors, categories) for i, color in enumerate(colors): self.chart.slices[i].fillColor = color
def on_btnInforme_clicked(self, boton): """ Función que controla el comportamiento del botón "btnInforme". Genera un informe general sobre cuantos clientes hay registrados en cada servicio usando ReportLab. :param boton: :return: """ # añadimos un nuevo dibujo d2 = Drawing(300, 200) tarta = Pie() tarta.x = 65 tarta.y = 15 tarta.height = 170 tarta.width = 170 # tarta.data = [10.456, 20.234, 30.567, 40, 50] tarta.labels = ['Seguro Coche', 'Seguro Moto', 'Sin Seguro'] # porciones # tarta.slices.strokeWidth = 0.5 # tarta.slices[3].popout = 50 # tarta.slices[3].strokeWidth = 5 # tarta.slices[3].strokeDashArray = [5, 2] # pixels de la linea (tamaño) # tarta.slices[3].labelRadius = 1.75 # tarta.slices[3].fontColor = colors.red tarta.sideLabels = 0 cores = [colors.blue, colors.red, colors.green] # coge cada elemento y le asigna un numero for i, color in enumerate(cores): tarta.slices[i].fillColor = color d2.add(tarta) lenda = Legend() lenda.x = 270 lenda.y = 0 lenda.dx = 8 lenda.dy = 8 # espacio entre lineas (?) lenda.fontName = "Helvetica" lenda.fontSize = 8 lenda.boxAnchor = 'n' lenda.columnMaximum = 3 lenda.strokeColor = colors.black lenda.deltax = 75 lenda.deltay = 10 lenda.autoXPadding = 5 lenda.yGap = 0 lenda.dxTextSpace = 5 lenda.alignment = 'right' lenda.dividerLines = 1 | 2 | 4 # lenda.dividerOffsY = 4.5 lenda.subCols.rpad = 30 d2.add(lenda) doc = SimpleDocTemplate("informeGrafica.pdf", pagesize=A4) doc.build([d2])
def __init__(self, labels, data, width=400, height=200, *args, **kw): 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"), ] 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.data = data # self.pie.labels = ['Financials', 'Energy', 'Health Care', 'Telecoms', 'Consumer', 'Consumer 2', 'Industrials', # 'Materials', 'Other', 'Liquid Assets'] self.pie.labels = labels 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) self.set_items(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)]
def __init__(self, width=155, height=138, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) fontName = 'Helvetica' fontSize = 6 self._add(self, Pie(), name='pie', validate=None, desc=None) self._add(self, Legend(), name='legend', validate=None, desc=None) colors = [ PCMYKColor(40, 0, 7, 40, alpha=100), PCMYKColor(0, 12, 9, 87, alpha=100), PCMYKColor(30, 0, 6, 33, alpha=100), PCMYKColor(0, 4, 4, 69, alpha=100), PCMYKColor(21, 0, 5, 25, alpha=100), PCMYKColor(0, 2, 2, 52, alpha=100) ] self.pie.data = [ 30.600000000000001, 8.5, 39.299999999999997, 7.7000000000000002, 11.6, 2.2000000000000002 ] self.pie.sameRadii = 1 for i in range(len(self.pie.data)): self.pie.slices[i].fillColor = colors[i] self.pie.slices.strokeColor = PCMYKColor(0, 0, 0, 0) self.pie.slices.strokeWidth = 0.5 self.legend.alignment = 'right' self.legend.fontName = fontName self.legend.fontSize = fontSize self.legend.dx = 6.5 self.legend.dy = 6.5 self.legend.yGap = 0 self.legend.deltax = 10 self.legend.deltay = 10 self.legend.strokeColor = PCMYKColor(0, 0, 0, 0) self.legend.strokeWidth = 0 self.legend.columnMaximum = 4 self.width = 400 self.height = 200 self.legend.y = 100 self.legend.boxAnchor = 'c' self.legend.x = 325 self.pie.x = 25 self.pie.y = 25 self.pie.height = 150 self.pie.width = 150 self.pie.slices[0].fillColor = PCMYKColor(23, 51, 0, 4, alpha=100) self.pie.slices[1].fillColor = PCMYKColor(0, 100, 100, 40, alpha=100) self.pie.slices[5].fillColor = PCMYKColor(0, 61, 22, 25, alpha=100) self.pie.slices[2].fillColor = PCMYKColor(100, 60, 0, 50, alpha=100) self.pie.slices[3].fillColor = PCMYKColor(100, 0, 90, 50, alpha=100) self.pie.slices[4].fillColor = PCMYKColor(66, 13, 0, 22, alpha=100) self.legend.colorNamePairs = [ (PCMYKColor(23, 51, 0, 4, alpha=100), u'Europe: 30.6'), (PCMYKColor(0, 100, 100, 40, alpha=100), u'Japan: 8.5'), (PCMYKColor(100, 60, 0, 50, alpha=100), u'US/Canada: 39.3'), (PCMYKColor(100, 0, 90, 50, alpha=100), u'Asia ex Japan: 7.7'), (PCMYKColor(66, 13, 0, 22, alpha=100), u'Latin\nAmerica/Other: 11.6'), (PCMYKColor(0, 61, 22, 25, alpha=100), u'Australia/New\nZealand: 2.2') ]
def __init__(self, width=100, height=100, *args, **kwargs): Drawing.__init__(self, width, height, *args, **kwargs) self.add(Pie(), name='chart') for i in range(10): self.chart.slices[i].fillColor = PIE_CHART_COLORS[i] self.chart.slices[i].labelRadius = 1.4 self.chart.slices[i].fontName = 'Helvetica' self.chart.slices[i].fontSize = 7
def pie_chart(data_for_pie): d = Drawing(400, 400) pc = Pie() pc.x = 100 pc.y = 210 pc.width = 170 pc.height = 170 pc.sideLabels = True pc.sideLabelsOffset = 0.05 car_name_lst = [] car_sales_lst = [] car_price_lst = [] data_for_pie = data_for_pie[1:] data_for_pie = sorted(data_for_pie, key=itemgetter(3), reverse=True) # so i can show 10 most popular cars for i in data_for_pie: car_name = i[1] car_name_lst.append(car_name) car_sales = i[3] car_sales_lst.append(car_sales) car_price = float(i[2].strip( "$")) #by default all the prices are in '', for example '18731.76' car_price_lst.append(car_price) pc.data = car_sales_lst[:10] pc.labels = list(set( car_name_lst[:10])) # by using set i wont have similar items in list d.add(pc, '') """have to done this because in task i have to calculate revenue on Xaxis and car name on Yaxis""" revenue_calculation_1 = car_sales_lst[:10] revenue_calculation_2 = car_price_lst[:10] car_revenue_list = [] for i in range(len(car_price_lst[:10])): reven = revenue_calculation_1[i] * revenue_calculation_2[i] car_revenue_list.append( int(reven)) #using int because its many digits after "," """bar chart """ data = [tuple(car_revenue_list) ] #for some reason bar chart accepts only [[]] or [()] bc = VerticalBarChart() bc.x = 50 bc.y = 0 bc.height = 125 bc.width = 300 bc.data = data bc.strokeColor = colors.black bc.valueAxis.valueMin = 7000000 bc.valueAxis.valueMax = 23000000 bc.valueAxis.valueStep = 1000000 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = 8 bc.categoryAxis.labels.dy = -2 bc.categoryAxis.labels.angle = 30 bc.categoryAxis.categoryNames = list(set(car_name_lst[:10])) d.add(bc) return d