def simple_horizontal_bar_chart(): d = Drawing(280, 250) bar = HorizontalBarChart() bar.x = 50 bar.y = 85 bar.height = 225 bar.width = 250 data = [[1, 2, 3, None, None], [10, 5, 2, 6, 8], [5, 7, 2, 8, 8], [2, 10, 2, 1, 8], ] bar.data = data bar.categoryAxis.categoryNames = ['Year1', 'Year2', 'Year3', 'Year4', 'Year5', 'Year6', 'Year7'] bar.bars[0].fillColor = colors.green bar.bars[1].fillColor = colors.blue bar.bars[2].fillColor = colors.red bar.bars[3].fillColor = colors.purple bar.categoryAxis.labels.angle = 45 bar.categoryAxis.labels.dx = -15 d.add(bar, '') doc = SimpleDocTemplate('18.simple_horizontal_bar_chart.pdf') story = [] story.append(d) doc.build(story)
def generate_histogram(data, left=True): chart = HorizontalBarChart() drawing = Drawing(300, 400) chart.bars.strokeColor = None chart.bars[0].fillColor = chartreuse if left else coral chart.x = 20 if left else 0 chart.y = 0 chart.width = 245 chart.height = 400 chart.data = [[data['hist'][c] for c in data['hist']]] chart.strokeColor = None chart.fillColor = None chart.valueAxis.valueMin = 0 chart.valueAxis.valueMax = max(chart.data[0]) * 1.1 # chart.valueAxis.visibleTicks = False # chart.valueAxis.valueStep = 10 chart.valueAxis.reverseDirection = 1 if left else 0 chart.categoryAxis.labels.boxAnchor = 'nw' if left else 'ne' chart.categoryAxis.labels.dx = 6 if left else -6 chart.categoryAxis.labels.dy = 8 chart.categoryAxis.categoryNames = [c for c in data['hist']] chart.categoryAxis.joinAxisMode = 'right' if left else 'left' chart.categoryAxis.reverseDirection = 1 chart.categoryAxis.visibleTicks = False chart.categoryAxis.visibleAxis = False chart.categoryAxis.strokeColor = None drawing.add(chart) return drawing
def barChart(daten,Versuch,Phaenomene,path=None,vMin=1,vMax=6): """ Plots data to a Drawing and returns the Drawing """ #Festlegen der Gesamtgröße in Pixel d = Drawing(500,160) #Daten für das Diagramm #daten = [(10,6,8)] #Anlegen des Diagramms diagramm = HorizontalBarChart() #Positionierung und Größe des Diagramms diagramm.x = 10 diagramm.y = 30 diagramm.height = 100 diagramm.width = 400 #Hinzufügen der Daten #diagramm.reversePlotOrder = 1 diagramm.data = daten #Y-Achse (in ReportLab „valueAxis“) formatieren diagramm.valueAxis.valueMin = vMin diagramm.valueAxis.valueMax = vMax diagramm.valueAxis.valueStep = 1 #X-Achse (in ReportLab „categoryAxis“) formatieren diagramm.categoryAxis.categoryNames = Phaenomene #Diagramm zeichnen d.add(diagramm) if not path == None: Versuch = path + Versuch renderPM.drawToFile(d, Versuch + ".png", 'PNG') #d = Paragraph(d, centered) d.scale(0.8,0.8) return d
def bar_chart(self, pdf, userdjango): usuariomodelo = Usuario.objects.get(usuario=userdjango) formularios = usuariomodelo.formulario_set.all() nombre = [] #labels ponderado = [] #porcetaje for datos in formularios: nombre.append(datos.nombreFormulario) ponderado.append(datos.ponderado) nuevo_ponderado = [float(i) for i in ponderado] drawing = Drawing(50, 150) data = [nuevo_ponderado] bc = HorizontalBarChart() bc.x = 100 bc.y = 40 bc.height = 470 bc.width = 600 bc.data = data #bc.strokeColor = colors.black bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 60 bc.valueAxis.valueStep = 10 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = -2 bc.categoryAxis.labels.dy = -2 bc.categoryAxis.labels.angle = 0 bc.categoryAxis.labels.fontSize = 7 bc.categoryAxis.categoryNames = nombre drawing.add(bc) return drawing
def tercero_chart(self, pdf, userdjango): usuariomodelo = Usuario.objects.get(usuario=userdjango) formularios = usuariomodelo.formulario_set.all() nivel = [] ponderado = [] for datos in formularios: nivel.append(datos.nivel) ponderado.append(datos.ponderado) nuevo_ponderado = [float(i) for i in ponderado] drawing = Drawing(50, 150) data = [nuevo_ponderado] bc = HorizontalBarChart() bc.x = 100 bc.y = 40 bc.height = 520 bc.width = 600 bc.data = data bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 60 bc.valueAxis.valueStep = 10 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = -2 bc.categoryAxis.labels.dy = -2 bc.categoryAxis.labels.angle = 0 bc.categoryAxis.labels.fontSize = 7 bc.categoryAxis.categoryNames = nivel drawing.add(bc) return drawing
def horizontal_bar_graph(self): """ Draws a horizontal bar chart :return: horizontal bar chart """ drawing = Drawing(200, 50) graph = HorizontalBarChart() graph.x = self.graph_x graph.y = self.graph_y graph.width = self.width graph.height = self.height graph.valueAxis.valueMin = self.value_min graph.valueAxis.valueMax = self.value_max graph.valueAxis.valueStep = self.value_step graph.data = self.data graph.categoryAxis.categoryNames = self.categories graph.barLabels.nudge = 15 graph.barLabelFormat = '%d' graph.barLabels.dx = 0 graph.barLabels.dy = 7 graph.barLabels.boxAnchor = 'n' graph.barLabels.fontName = 'Vera' graph.barLabels.fontSize = 10 graph.bars[0].fillColor = PCMYKColor(45, 45, 0, 0, alpha=85) graph.bars[1].fillColor = PCMYKColor(64, 75, 0, 18, alpha=95) graph.bars.fillColor = PCMYKColor(64, 62, 0, 18, alpha=85) drawing.add(graph, '') return [drawing]
def __init__(self, drawing=None, title=None, data=None, x=45, y=25, width=170, height=130): if len(data) > 1: y = y + 22 height = height - 22 bars = HorizontalBarChart() bars.x = x bars.y = y bars.data = [[value for (_, value) in category] for category in data] bars.width = width bars.height = height bars.valueAxis.forceZero = 1 bars.valueAxis.labels.fontName = 'Lato' bars.valueAxis.labels.fontSize = 9 bars.valueAxis.strokeColor = white bars.valueAxis.visibleGrid = 1 bars.bars[0].fillColor = toColor(my_color_func()) bars.bars.strokeColor = white bars.categoryAxis.categoryNames = [key for (key, _) in data[0]] bars.categoryAxis.tickRight = 0 bars.categoryAxis.tickLeft = 0 #bars.categoryAxis.strokeColor = white bars.categoryAxis.labels.fontName = 'Lato' bars.categoryAxis.labels.fontSize = 9 legend = Legend() legend.y = 25 legend.x = 95 legend.strokeColor = white legend.alignment = 'right' legend.fontName = 'Lato' legend.fontSize = 9 legend.dx = 6 legend.dy = 6 legend.dxTextSpace = 5 legend.deltay = 10 legend.strokeWidth = 0 legend.strokeColor = white colors = map(toColor, get_n_random_colors(len(data))) for (i, color) in enumerate(colors): bars.bars[i].fillColor = color if len(data) > 1: legend_data = (title, 'Benchmark') legend.colorNamePairs = zip(colors, legend_data) drawing.add(legend) drawing.add(bars)
def vendorcreatedperquarter(vendor_data): vendor_data['Period'] = vendor_data['Created Date'].dt.to_period('M') vendor_data['Qtr'] = (vendor_data['Period']).dt.quarter vendor_data['Year'] = vendor_data['Created Date'].map(lambda x: 1 * x.year) vendor_data["Quarter"] = vendor_data["Year"].map(str) + "Q" + vendor_data["Qtr"].map(str) # raw_ven_data = vendor_data[['Vendor ID', 'Quarter']] act_raw_ven_data = vendor_data[vendor_data['Status'] == 'Active'][['Vendor ID', 'Quarter']] inact_raw_ven_data = vendor_data[vendor_data['Status'] == 'In Active'][['Vendor ID', 'Quarter']] group_data = vendor_data[['Vendor ID', 'Quarter']].groupby(['Quarter']).count() act_group_ven_data = act_raw_ven_data.groupby(['Quarter']).count() inact_group_ven_data = inact_raw_ven_data.groupby(['Quarter']).count() act_data = act_group_ven_data['Vendor ID'].tolist() inact_data = inact_group_ven_data['Vendor ID'].tolist() y_data = group_data.index.tolist() drawing = Drawing(200, 100) list_data = [] list_data.append(act_data) list_data.append(inact_data) data = list_data lc = HorizontalBarChart() lc.x = 10 lc.y = -150 lc.height = 250 lc.width = 450 lc.data = data lc.categoryAxis.categoryNames = y_data lc.bars[0].fillColor = colors.lightblue lc.bars[1].fillColor = colors.lightgreen # lc.lines.symbol = makeMarker('Circle') # name1 = 'Active' # name2 = 'Inactive' # # swatches = Legend() # swatches.alignment = 'right' # swatches.x = 80 # swatches.y = 160 # swatches.deltax = 60 # swatches.dxTextSpace = 10 # swatches.columnMaximum = 4 # items = [(colors.lightblue, name1), (colors.lightgreen, name2)] # swatches.colorNamePairs = items drawing.add(lc) return drawing
def get_studentPRChart(studentEvaluation, showPercentValue=False): pr_items = _get_pr_items(studentEvaluation) row_data = [] row_barLabel = [] categories = [] '''这是一个Hack,因为Chart绘制的顺序和预期的不同。''' pr_items = reversed(pr_items) for item in pr_items: row_data.append(item.p_value) if showPercentValue: barLabel = '%s%%(%s %s)' % (item.p_value, item.e_value, item.e_unit) else: barLabel = '%s%%' % (item.p_value, ) row_barLabel.append(barLabel) categories.append(item.name) data = [row_data] labels = [row_barLabel] drawing = Drawing(14 * cm, 6 * cm) bc = HorizontalBarChart() bc.x = 1.5 * cm bc.y = 0 bc.width = drawing.width - bc.x bc.height = drawing.height bc.data = data bc.bars.strokeColor = None bc.bars[0].fillColor = colors.HexColor('#7fd8ff') bc.barLabelFormat = 'values' bc.barLabelArray = labels bc.barLabels.boxAnchor = 'w' bc.barLabels.fixedEnd = LabelOffset() bc.barLabels.fixedEnd.posMode = 'low' bc.barLabels.fontName = 'Microsoft-YaHei-Light' bc.barLabels.fontSize = 8 bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 100 bc.valueAxis.valueStep = 10 bc.valueAxis.labels.fontName = 'Microsoft-YaHei-Light' bc.valueAxis.labels.fontSize = 8 bc.categoryAxis.categoryNames = categories bc.categoryAxis.labels.boxAnchor = 'w' bc.categoryAxis.labels.dx = -1.5 * cm bc.categoryAxis.labels.fontName = 'Microsoft-YaHei-Light' bc.categoryAxis.labels.fontSize = 8 bc.categoryAxis.visibleAxis = 0 bc.categoryAxis.visibleTicks = 0 drawing.add(bc) return drawing
def vendorcreatedperquarter(vendor_data): vendor_data['Period'] = vendor_data['Created Date'].dt.to_period('M') vendor_data['Qtr'] = (vendor_data['Period']).dt.quarter vendor_data['Year'] = vendor_data['Created Date'].map(lambda x: 1 * x.year) vendor_data["Quarter"] = vendor_data["Year"].map( str) + "Q" + vendor_data["Qtr"].map(str) # raw_ven_data = vendor_data[['Vendor ID', 'Quarter']] act_raw_ven_data = vendor_data[vendor_data['Status'] == 'Active'][[ 'Vendor ID', 'Quarter' ]] inact_raw_ven_data = vendor_data[vendor_data['Status'] == 'In Active'][[ 'Vendor ID', 'Quarter' ]] group_data = vendor_data[['Vendor ID', 'Quarter']].groupby(['Quarter']).count() act_group_ven_data = act_raw_ven_data.groupby(['Quarter']).count() inact_group_ven_data = inact_raw_ven_data.groupby(['Quarter']).count() act_data = act_group_ven_data['Vendor ID'].tolist() inact_data = inact_group_ven_data['Vendor ID'].tolist() y_data = group_data.index.tolist() drawing = Drawing(200, 100) list_data = [] list_data.append(act_data) list_data.append(inact_data) data = list_data lc = HorizontalBarChart() lc.x = 10 lc.y = -200 lc.height = 300 lc.width = 450 lc.data = data lc.categoryAxis.categoryNames = y_data lc.bars[0].fillColor = colors.lightblue lc.bars[1].fillColor = colors.lightgreen # lc.lines.symbol = makeMarker('Circle') drawing.add(lc) return drawing
def __init__(self, width=400, height=200, *args, **kw): Drawing.__init__(*(self, width, height) + args, **kw) bc = HorizontalBarChart() bc.x = 50 bc.y = 50 bc.height = 125 bc.width = 300 bc.data = dataSample5 bc.strokeColor = colors.black bc.useAbsolute = 1 bc.barWidth = 10 bc.groupSpacing = 20 bc.barSpacing = 10 bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 60 bc.valueAxis.valueStep = 15 bc.categoryAxis.labels.boxAnchor = 'e' bc.categoryAxis.categoryNames = ['Ying', 'Yang'] self.add(bc, name='HBC')
def add_vertical_bar_chart(self, data, categories, dataLabel): if len(categories) == 0: return if len(data) == 0: return drawing = Drawing(self.content_width(), len(categories)*15 + 50) maxValue = 1 for value in data: if value > maxValue: maxValue = value bc = HorizontalBarChart() bc.height = len(categories)*15 bc.width = self.content_width() - 300 bc.x = 150 bc.y = 25 bc.groupSpacing = 2 bc.data = [data] bc.strokeColor = None bc.bars[0].fillColor = CYAN bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = maxValue bc.valueAxis.valueStep = max(maxValue/5, 1) bc.valueAxis.labelTextFormat = dataLabel bc.categoryAxis.labels.boxAnchor = 'e' bc.categoryAxis.labels.dx = -8 bc.categoryAxis.labels.dy = 0 bc.categoryAxis.categoryNames = categories drawing.add(bc) self.add(drawing)
def generate_page1(c): #Image---------------------------------------------------------------------- im1 = Image.open("left.jpg") c.drawInlineImage(im1, 10, 15, width=546 / 2.3, height=1290 / 2.3) im2 = Image.open("shinken.jpg") c.drawInlineImage(im2, 259, height - 183, width=2434 / 4.4, height=725 / 4.4) im3 = Image.open("skor.jpg") c.drawInlineImage(im3, 259, height - 251, width=2335 / 4.2, height=271 / 4.2) im4 = Image.open("diagram.jpg") c.drawInlineImage(im4, 259, 15, width=2339 / 4.2, height=1367 / 4.2) #grafik1---------------------------------------------------------------------- from reportlab.graphics.shapes import Drawing from reportlab.graphics.charts.barcharts import HorizontalBarChart drawing = Drawing(500, 250) data = [(N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, N11, N12, N13, N14, N15, N16, N17)] bc = HorizontalBarChart() bc.x = 100 bc.y = 100 bc.height = 250 bc.width = 300 bc.data = data bc.strokeColor = None bc.fillColor = None # bc.bars[0].fillColor = PCMYKColor(92,47,0,33,alpha=95) # bc.bars[0].strokeColor = PCMYKColor(92,47,0,33,alpha=95) # bc.bars[0].fillColor = PCMYKColor(92,32,0,33,alpha=95) # bc.bars[0].strokeColor = PCMYKColor(92,32,0,33,alpha=95) bc.bars[0].fillColor = PCMYKColor(92, 16, 0, 33, alpha=95) bc.bars[0].strokeColor = PCMYKColor(92, 16, 0, 33, alpha=95) # bc.bars[0].fillColor = PCMYKColor(92,7,0,33,alpha=95) # bc.bars[0].strokeColor = PCMYKColor(92,7,0,33,alpha=95) bc.barWidth = 15 bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 100 bc.valueAxis.valueStep = 10 bc.valueAxis.visibleAxis = False bc.valueAxis.visibleGrid = False bc.valueAxis.visibleTicks = False bc.valueAxis.forceZero = True bc.valueAxis.visibleLabels = 0 bc.categoryAxis.visibleGrid = False bc.categoryAxis.visibleTicks = False # hidding the ticks remove the label bc.categoryAxis.tickLeft = 0 # a workaround is to set the tick length bc.categoryAxis.tickRight = 0 # to zero. bc.categoryAxis.strokeWidth = 0.25 bc.categoryAxis.labelAxisMode = 'low' bc.categoryAxis.labels.textAnchor = 'end' bc.categoryAxis.labels.angle = 0 bc.categoryAxis.labels.fontName = 'Designosaur-Regular' #bc.categoryAxis.labels.fontColor = PCMYKColor(0,65,100,0,alpha=90) bc.categoryAxis.labels.boxAnchor = 'e' bc.categoryAxis.labels.dx = -5 bc.categoryAxis.labels.dy = 0 bc.categoryAxis.labels.angle = 0 bc.categoryAxis.reverseDirection = 1 bc.categoryAxis.joinAxisMode = 'left' bc.categoryAxis.categoryNames = [ NN1, NN2, NN3, NN4, NN5, NN6, NN7, NN8, NN9, NN10, NN11, NN12, NN13, NN14, NN15, NN16, NN17 ] bc.barLabels.fontName = 'Designosaur-Regular' bc.barLabels.fontSize = 10 bc.barLabels.angle = 0 bc.barLabelFormat = "%.00f%%" bc.barLabels.boxAnchor = 'w' bc.barLabels.boxFillColor = None bc.barLabels.boxStrokeColor = None bc.barLabels.dx = 10 bc.barLabels.dy = 0 #bc.barLabels.dy = -1 bc.barLabels.boxTarget = 'hi' drawing.add(bc) drawing.wrapOn(c, width, height) drawing.drawOn(c, 350, height - 650) #Table--------------------------------------------------------------------- styleBH = styles["Normal"] styleBH.alignment = TA_CENTER styleBH.fontSize = 12 styleBH.fontName = 'Designosaur-Regular' #styleBH.textColor = PCMYKColor(92,7,0,33,alpha=100) isi1 = Paragraph(nama, styleBH) isitabel1 = [[isi1]] table1 = Table(isitabel1, colWidths=[150]) table1.wrapOn(c, width, height) table1.drawOn(c, 54, height - 352) isi2 = Paragraph(kelas, styleBH) isitabel2 = [[isi2]] table2 = Table(isitabel2, colWidths=[150]) table2.wrapOn(c, width, height) table2.drawOn(c, 54, height - 386) isi3 = Paragraph(sekolah, styleBH) isitabel3 = [[isi3]] table3 = Table(isitabel3, colWidths=[150]) table3.wrapOn(c, width, height) table3.drawOn(c, 54, height - 421) isi4 = Paragraph(outlet, styleBH) isitabel4 = [[isi4]] table4 = Table(isitabel4, colWidths=[150]) table4.wrapOn(c, width, height) table4.drawOn(c, 54, height - 455) #Text---------------------------------------------------------------------- #nilai total c.setFillColor(white) c.setStrokeColor(white) if (NTotal == '10.0'): c.setFont("Designosaur-Regular", 35) c.drawString(352, height - 230, NTotal + "/") c.setFont("Designosaur-Regular", 20) c.drawString(395, height - 235, "10") else: c.setFont("Designosaur-Regular", 35) c.drawString(338, height - 230, NTotal + "/") c.setFont("Designosaur-Regular", 20) c.drawString(415, height - 235, "10") #Yang paling tidak dikuasai siswa c.setFont("Designosaur-Regular", 9) c.drawString( 576, height - 205, "Topik yang paling tidak dikuasai peserta Try-Out II :" ) c.drawString(576, height - 216, "1. Mean, Median, dan Modus") c.drawString(576, height - 227, "2. Debit Air") c.drawString(576, height - 238, "3. Bangun Ruang")
""" Exemplo do gerador de gráficos automático do reportlab """ drawing = Drawing(400, 200) data = [ (1, 2, 3, 3, 5, 4, 2, 3) ] bc = HorizontalBarChart() bc.x = 50 bc.y = 50 bc.height = 125 bc.width = 150 bc.data = data bc.strokeColor = colors.black bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 5 bc.valueAxis.valueStep = 1 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = -5 bc.categoryAxis.labels.dy = 8 bc.categoryAxis.labels.angle = 0 bc.categoryAxis.categoryNames = ['Liderança','Comunicação','Empreendedorismo', 'Detalhe','Decisão racional','Cumprimento à normas','Criatividade','Energia'] drawing.add(bc) story.append(drawing)
int(round(row[14])), int(round(row[15])), int(round(row[16])), int(round(row[17])), int(round(row[18])), int(round(row[19])), int(round(row[20])), int(round(row[21])) ]] valors_qualitatius = ["Competència %s" % i for i in range(1, 13)] bc.x = 85 bc.y = 0 bc.height = 180 bc.width = 300 bc.data = dades_dibuix bc.strokeColor = colors.whitesmoke bc.fillColor = colors.whitesmoke bc.valueAxis.valueMin = 0.75 bc.valueAxis.valueMax = 4 bc.valueAxis.valueStep = 1 for i in range(10, 22): if int(round(row[i])) == 1: bc.bars[(0, i - 10)].fillColor = colors.orangered elif int(round(row[i])) == 2: bc.bars[(0, i - 10)].fillColor = colors.orange elif int(round(row[i])) == 3: bc.bars[(0, i - 10)].fillColor = colors.yellowgreen else:
doc = SimpleDocTemplate("pdfs/extra-071.pdf") story = [] """ Exemplo do gerador de gráficos automático do reportlab """ drawing = Drawing(400, 200) data = [(1, 2, 3, 3, 5, 4, 2, 3)] bc = HorizontalBarChart() bc.x = 50 bc.y = 50 bc.height = 125 bc.width = 150 bc.data = data bc.strokeColor = colors.black bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 5 bc.valueAxis.valueStep = 1 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = -5 bc.categoryAxis.labels.dy = 8 bc.categoryAxis.labels.angle = 0 bc.categoryAxis.categoryNames = [ 'Liderança', 'Comunicação', 'Empreendedorismo', 'Detalhe', 'Decisão racional', 'Cumprimento à normas', 'Criatividade', 'Energia' ] drawing.add(bc)