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=400, height=200, data=[], labels=[], legends=[], *args, **kw): Drawing.__init__(self, width, height, *args, **kw) self.add(VerticalBarChart(), name='chart') self.add(Legend(), name='legend') self.chart.x = 15 self.chart.y = 15 self.chart.width = self.width - 20 self.chart.height = self.height - 40 self.chart.data = data self.chart.categoryAxis.categoryNames = labels self.chart.categoryAxis.labels.boxAnchor = 'ne' self.chart.categoryAxis.labels.angle = 30 for i in range(len(data)): self.chart.bars[i].fillColor = color_list[i] self.legend.alignment = 'right' self.legend.x = self.width 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=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 autoLegender(i,chart,styleObj,sym='symbol'): if sym: setattr(styleObj[0],sym, makeMarker('Diamond',size=6)) setattr(styleObj[1],sym,makeMarker('Square')) width = 300 height = 150 legend = Legend() legend.x = width-5 legend.y = 5 legend.dx = 20 legend.dy = 5 legend.deltay = 0 legend.boxAnchor = 'se' if i=='col auto': legend.colorNamePairs[0]=(Auto(chart=chart),'auto chart=self.chart') legend.colorNamePairs[1]=(Auto(obj=chart,index=1),'auto chart=self.chart index=1') elif i=='full auto': legend.colorNamePairs=Auto(chart=chart) elif i=='swatch set': legend.swatchMarker=makeMarker('Circle') legend.swatchMarker.size = 10 elif i=='swatch auto': legend.swatchMarker=Auto(chart=chart) d = Drawing(width,height) d.background = Rect(0,0,width,height,strokeWidth=1,strokeColor=colors.red,fillColor=None) m = makeMarker('Cross') m.x = width-5 m.y = 5 m.fillColor = colors.red m.strokeColor = colors.yellow d.add(chart) d.add(legend) d.add(m) 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 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 addAutoLegend(self,chart,offset,num,side='bottom'): from reportlab.lib.validators import Auto width = 300 height = 150 legend = Legend() if side == 'bottom': legend.x = offset[0]-15 legend.dx = 20 legend.dy = 5 legend.y = offset[1]-25 legend.deltax = None legend.autoXPadding = 35 legend.deltay = 5 legend.boxAnchor = 'nw' legend.dxTextSpace = 5 legend.columnMaximum = (num/3) if num%3 >0: legend.columnMaximum += 1 legend.variColumn = True elif side == 'right': legend.x = offset[0]+offset[2]+40 legend.dx = 20 legend.dy = 5 legend.y = offset[1]+offset[3]-15 legend.deltax = None legend.deltay = 5 legend.boxAnchor = 'nw' legend.dxTextSpace = 5 legend.columnMaximum = 9 legend.colorNamePairs=Auto(chart=chart) return legend
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 make_legend(self, drawing, chart): if not self.legend_labels: return # Get legend labels labels = self.get_legend_labels() # Legend object legend = Legend() legend.colorNamePairs = zip(self.colors[:len(labels)], labels) legend.columnMaximum = len(legend.colorNamePairs) legend.deltay = 5 legend.alignment = 'right' legend.x = drawing.width + 40 legend.y = drawing.height - (self.title and self.title.get('height', DEFAULT_TITLE_HEIGHT) or 0) # Sets legend extra attributes if legend_labels is a dictionary if isinstance(self.legend_labels, dict): for k,v in self.legend_labels.items(): if k != 'labels' and v: setattr(legend, k, v) drawing.add(legend) return legend
def draw_bar(bar_data=[], ax=[], items=[]): drawing = Drawing(500, 250) bc = VerticalBarChart() bc.x = 35 bc.y = 100 bc.height = 120 bc.width = 350 bc.data = bar_data bc.strokeColor = colors.black bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = 100 bc.valueAxis.valueStep = 10 bc.categoryAxis.labels.dx = 8 bc.categoryAxis.labels.dy = -10 bc.categoryAxis.labels.angle = 20 bc.categoryAxis.categoryNames = ax # 图示 leg = Legend() leg.fontName = 'song' leg.alignment = 'right' leg.boxAnchor = 'ne' leg.x = 465 leg.y = 220 leg.dxTextSpace = 10 leg.columnMaximum = 3 leg.colorNamePairs = items drawing.add(leg) drawing.add(bc) return drawing
def legend_draw(self, labels, chart, **kwargs): legend = Legend() chart_type = kwargs['type'] legend.fontName = 'RobotoBold' legend.fontSize = 13 legend.strokeColor = None if 'x' in kwargs: legend.x = kwargs['x'] if 'y' in kwargs: legend.y = kwargs['y'] legend.alignment = 'right' if 'boxAnchor' in kwargs: legend.boxAnchor = kwargs['boxAnchor'] if 'columnMaximum' in kwargs: legend.columnMaximum = kwargs['columnMaximum'] legend.deltax = 0 lcolors = legendcolors if chart_type == 'line': lcolors = [colors.red, colors.blue] legend.colorNamePairs = zip(lcolors, labels) for i, color in enumerate(lcolors): if chart_type == 'line': chart.lines[i].fillColor = color elif chart_type == 'pie': chart.slices[i].fillColor = color elif chart_type == 'bar': chart.bars[i].fillColor = color return legend
def insert_legend(self): """ Insert legend into graphic """ self._add(self, Legend(), name='legend') self.legend.deltay = 8 self.legend.fontName = 'Helvetica' self.legend.fontSize = 5 self.legend.strokeWidth = 0.5 self.legend.strokeColor = PCMYKColor(0, 0, 0, 100) self.legend.alignment = 'right' self.legend.columnMaximum = 3 self.legend.boxAnchor = 'sw' self.legend.y = 75 self.legend.x = 24 self.legend.dx = 8 self.legend.dy = 5 self.legend.dxTextSpace = 5 self.legend.deltax = 0 self.legend.colorNamePairs = Auto(obj=self.chart) self.chart.bars[0].fillColor = self.random_color() self.chart.bars[1].fillColor = self.random_color() self.chart.bars[2].fillColor = self.random_color() self.chart.bars[3].fillColor = self.random_color()
def legend_draw(self, labels, chart, **kwargs): legend = Legend() chart_type = kwargs['type'] legend.fontName = 'FreeSans' legend.fontSize = 13 legend.strokeColor = None if 'x' in kwargs: legend.x = kwargs['x'] if 'y' in kwargs: legend.y = kwargs['y'] legend.alignment = 'right' if 'boxAnchor' in kwargs: legend.boxAnchor = kwargs['boxAnchor'] if 'columnMaximum' in kwargs: legend.columnMaximum = kwargs['columnMaximum'] # x-distance between neighbouring swatche\s legend.deltax = 0 lcolors = legendcolors if chart_type == 'pie': lcolors = [colors.green, colors.red] legend.colorNamePairs = list(zip(lcolors, labels)) for i, color in enumerate(lcolors): ## if chart_type == 'line': ## chart.lines[i].fillColor = color if chart_type == 'pie': chart.slices[i].fillColor = color ## elif chart_type == 'bar': ## chart.bars[i].fillColor = color return legend
def show_figure(data, y_max, x_category, legend_category): drawing = Drawing(400, 200) bc = VerticalBarChart() bc.x = 50 bc.y = 50 bc.height = 150 bc.width = 450 bc.data = data bc.strokeColor = colors.black bc.valueAxis.valueMin = 0 bc.valueAxis.valueMax = y_max # bc.valueAxis.valueStep = 15 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = len(data[0]) # bc.categoryAxis.labels.dy = -2 # bc.categoryAxis.labels.angle = 30 bc.categoryAxis.categoryNames = x_category drawing.add(bc) # add legend from here swatches = Legend() swatches.alignment = 'right' swatches.x = 80 swatches.y = 190 swatches.deltax = 30 swatches.dxTextSpace = 10 swatches.columnMaximum = 1 color_list = (colors.red, colors.green, colors.blue, colors.pink, colors.yellow) items = [] for index, item in enumerate(legend_category): items.append((color_list[index], item)) swatches.colorNamePairs = items drawing.add(swatches, 'legend') return drawing
def add_legend(draw_obj, chart, data): legend = Legend() legend.alignment = 'right' legend.x = 10 legend.y = 70 legend.colorNamePairs = Auto(obj=chart) draw_obj.add(legend)
def __init__(self,width=200,height=150,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) self._add(self,ScatterPlot(),name='chart',validate=None,desc="The main chart") self.chart.width = 115 self.chart.height = 80 self.chart.x = 30 self.chart.y = 40 self.chart.lines[0].strokeColor = color01 self.chart.lines[1].strokeColor = color02 self.chart.lines[2].strokeColor = color03 self.chart.lines[3].strokeColor = color04 self.chart.lines[4].strokeColor = color05 self.chart.lines[5].strokeColor = color06 self.chart.lines[6].strokeColor = color07 self.chart.lines[7].strokeColor = color08 self.chart.lines[8].strokeColor = color09 self.chart.lines[9].strokeColor = color10 self.chart.fillColor = backgroundGrey self.chart.lineLabels.fontName = 'Helvetica' self.chart.xValueAxis.labels.fontName = 'Helvetica' self.chart.xValueAxis.labels.fontSize = 7 self.chart.xValueAxis.forceZero = 0 self.chart.data = [((100,100), (200,200), (250,210), (300,300), (400,500)), ((100,200), (200,300), (250,200), (300,400), (400, 600))] self.chart.xValueAxis.avoidBoundFrac = 1 self.chart.xValueAxis.gridEnd = 115 self.chart.xValueAxis.tickDown = 3 self.chart.xValueAxis.visibleGrid = 1 self.chart.yValueAxis.tickLeft = 3 self.chart.yValueAxis.labels.fontName = 'Helvetica' self.chart.yValueAxis.labels.fontSize = 7 self._add(self,Label(),name='Title',validate=None,desc="The title at the top of the chart") self.Title.fontName = 'Helvetica-Bold' self.Title.fontSize = 7 self.Title.x = 100 self.Title.y = 135 self.Title._text = 'Chart Title' self.Title.maxWidth = 180 self.Title.height = 20 self.Title.textAnchor ='middle' self._add(self,Legend(),name='Legend',validate=None,desc="The legend or key for the chart") self.Legend.colorNamePairs = [(color01, 'Widgets'), (color02, 'Sprockets')] self.Legend.fontName = 'Helvetica' self.Legend.fontSize = 7 self.Legend.x = 153 self.Legend.y = 85 self.Legend.dxTextSpace = 5 self.Legend.dy = 5 self.Legend.dx = 5 self.Legend.deltay = 5 self.Legend.alignment ='right' self.chart.lineLabelFormat = None self.chart.xLabel = 'X Axis' self.chart.y = 30 self.chart.yLabel = 'Y Axis' self.chart.yValueAxis.labelTextFormat = '%d' self.chart.yValueAxis.forceZero = 1 self.chart.xValueAxis.forceZero = 1 self._add(self,0,name='preview',validate=None,desc=None)
def SetLegend(self, colorsAndLabels, x=None, y=None, dxChartSpace=36, legendAlign='right', fontname="Helvetica-Bold", fontsize=10, dxTextSpace=10, dy=10, dx=10, dySpace=15, maxCols=2): self._add(self, Legend(), name='Legend', validate=None, desc="The pie legend object") self.Legend.fontName = fontname self.Legend.fontSize = fontsize self.Legend.dxTextSpace = dxTextSpace self.Legend.dy = dy self.Legend.dx = dx self.Legend.deltay = dySpace self.Legend.alignment = legendAlign self.Legend.columnMaximum = maxCols self.Legend.colorNamePairs = colorsAndLabels if None in (x, y): x = self.chart.x + self.chart.width + dxChartSpace legendY1, legendY2 = self.Legend.getBounds()[1::2] legendH = abs(legendY2 - legendY1) figH = self.height y = figH / 2 + legendH / 2 # drawing is relative to document template flowable space --> self.x,self.y=0,0 (although these are real attributes) self.Legend.x = x self.Legend.y = y
def add_trends_new_resolved_findings_chart(): drawing = Drawing(200, 200) data, month = get_new_resolved_trends() max_val_new_findings = max(data[0]) max_val_resolved_findings = max(data[1]) maxVal = max(max_val_new_findings, max_val_resolved_findings) if (maxVal > 1000): multiplier = 1000 step = 4 * multiplier else: multiplier = 100 step = 4 * multiplier value_step = int(ceil(maxVal / step)) * multiplier if (value_step < 10): value_step = 1 bar = VerticalBarChart() bar.x = 25 bar.y = -35 bar.height = 100 bar.width = doc.width bar.barWidth = 2 bar.data = data bar.valueAxis.valueMin = 0 bar.valueAxis.valueMax = int( maxVal * 2) ## graph displa twice as much as max violation bar.valueAxis.valueStep = value_step ## Convert to neartest step bar.categoryAxis.categoryNames = month bar.bars[0].strokeColor = None bar.bars[1].strokeColor = None bar.bars[0].fillColor = HexColor("#E57300") bar.bars[1].fillColor = HexColor("#408F00") chartLabel = Label() chartLabel.setText("Trends - New Findings") chartLabel.fontSize = 10 chartLabel.fillColor = HexColor("#737373") chartLabel.fontName = 'Helvetica-Bold' chartLabel.dx = 250 chartLabel.dy = 90 legend = Legend() legend.alignment = 'right' legend.colorNamePairs = [[HexColor("#E57300"), "New Findings"], [HexColor("#408F00"), "Resolved Findings"]] legend.columnMaximum = 2 legend.x = 400 legend.y = 120 drawing.add(legend) drawing.add(chartLabel) drawing.add(bar) fields.append(drawing)
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, 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, 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 add_legend(draw_obj, chart, data): #chart=Pie(); legend = Legend() legend.alignment = 'left' legend.x = 10 legend.y = 70 draw_obj = chart legend.colorNamePairs = Auto(draw_obj=chart)
def __init__(self, width, height, x, y, categoryXlables, datas, categoryYlables, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) # font fontName = 'msyh' # chart # self._add(self,VerticalBarChart(),name='chart',validate=None,desc=None) # self.chart.width = width-x # self.chart.height = height-15 # # chart bars # self.chart.bars.strokeColor = None # self.chart.bars.strokeWidth = 0 # self.chart.barSpacing = 4 # self.chart.barLabels.fontName = fontName # self.chart.barLabels.fontSize = 5 # self.chart.data = datas # self.chart.categoryAxis.categoryNames=categoryXlables # # categoy axis # # self.chart.categoryAxis.labelAxisMode='low' # self.chart.categoryAxis.labels.boxAnchor = 'ne' # self.chart.categoryAxis.labels.dy = 5 # self.chart.categoryAxis.labels.fillColor = PCMYKColor(0,0,0,100) # self.chart.categoryAxis.labels.fontName = fontName # self.chart.categoryAxis.labels.fontSize = 5 # # self.chart.categoryAxis.labels.angle=25 # self.chart.categoryAxis.labels.textAnchor='start' # self.chart.categoryAxis.strokeWidth = 0 # self.chart.categoryAxis.style = 'stacked' # # value axis # self.chart.valueAxis.labels.fontName = fontName # self.chart.valueAxis.labels.fontSize = 8 # self.chart.valueAxis.strokeWidth = 0 # self.chart.valueAxis.visibleGrid = True # self.chart.valueAxis.visibleTicks = True # self.chart.valueAxis.visibleAxis = True # legend self._add(self, Legend(), name='legend', validate=None, desc=None) self.legend.alignment = 'right' self.legend.boxAnchor = 'sw' self.legend.fontSize = 5 self.legend.fontName = fontName self.legend.strokeColor = None self.legend.strokeWidth = 0 self.legend.subCols.minWidth = 55 self.legend.columnMaximum = 1 self.legend.deltay = 1 self.legend.colorNamePairs = zip( task_type_color[0:len(categoryYlables)], categoryYlables) # self.legend.colorNamePairs = Auto(obj=self.chart) # for i in range(len(categoryYlables)): # self.chart.bars[i].name = categoryYlables[i] # self.chart.bars[i].fillColor = task_type_color[i] # self.chart.x = x # self.chart.y = y self.legend.x = x
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 __init__(self, width=200, height=150, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) self._add(self, SpiderChart(), name='chart', validate=None, desc="The main chart") self.chart.width = 90 self.chart.height = 90 self.chart.x = 45 self.chart.y = 25 self.chart.strands[0].fillColor = color01 self.chart.strands[1].fillColor = color02 self.chart.strands[2].fillColor = color03 self.chart.strands[3].fillColor = color04 self.chart.strands[4].fillColor = color05 self.chart.strands[5].fillColor = color06 self.chart.strands[6].fillColor = color07 self.chart.strands[7].fillColor = color08 self.chart.strands[8].fillColor = color09 self.chart.strands[9].fillColor = color10 self.chart.strandLabels.fontName = 'Helvetica' self.chart.strandLabels.fontSize = 6 self.chart.fillColor = backgroundGrey self.chart.data = [(125, 180, 200), (100, 150, 180)] self.chart.labels = ['North', 'South', 'Central'] self._add(self, Label(), name='Title', validate=None, desc="The title at the top of the chart") self.Title.fontName = 'Helvetica-Bold' self.Title.fontSize = 7 self.Title.x = 100 self.Title.y = 135 self.Title._text = 'Chart Title' self.Title.maxWidth = 180 self.Title.height = 20 self.Title.textAnchor = 'middle' self._add(self, Legend(), name='Legend', validate=None, desc="The legend or key for the chart") self.Legend.colorNamePairs = [(color01, 'Widgets'), (color02, 'Sprockets')] self.Legend.fontName = 'Helvetica' self.Legend.fontSize = 7 self.Legend.x = 153 self.Legend.y = 85 self.Legend.dxTextSpace = 5 self.Legend.dy = 5 self.Legend.dx = 5 self.Legend.deltay = 5 self.Legend.alignment = 'right' self._add(self, 0, name='preview', validate=None, desc=None)
def get2PieChart(): legend = Legend() a = df2.iloc[i, 13] b = df2.iloc[i, 14] c = df2.iloc[i, 15] d = df2.iloc[i, 16] e = df2.iloc[i, 17] da = [a, b, c, d, e] x = 0 y = 0 for i5 in da: if i5 == "Attempted": x = x + 1 else: y = y + 1 data = [x, y] u = round(x * 100 / 5) v = round(y * 100 / 5) h = [u, v] d = [] l = ["%.2f" % i5 for i5 in h] for i5 in l: k = i5.split(".") d.append(k[0]) e = [] j = 0 for i5 in d: #w=i5+"%" j = j + 1 w = i5 + "%" if j == 1: w = w + " (Attempted)" if j == 2: w = w + " (Unattempted)" e.append(w) drawing = Drawing(width=400, height=200) my_title = String(170, 40, 'Attempts', fontSize=14) pie = Pie() pie.sideLabels = True pie.slices.popout = 3 pie.x = 140 pie.y = 60 pie.data = data pie.labels = [letter for letter in e] pie.slices.strokeWidth = 0.5 drawing.add(my_title) n = len(pie.data) setItems(n, pie.slices, 'fillColor', pdf_chart_colors) legend.colorNamePairs = [(pie.slices[i5].fillColor, (pie.labels[i5][0:20], '%0.2f' % pie.data[i5])) for i5 in range(n)] drawing.add(pie) add_legend(drawing, pie, data) return drawing
def __init__(self,width=200,height=150,*args,**kw): Drawing.__init__(self,width,height,*args,**kw) self._add(self,Pie(),name='chart',validate=None,desc="The main chart") self.chart.width = 100 self.chart.height = 100 self.chart.x = 25 self.chart.y = 25 self.chart.slices[0].fillColor = color01 self.chart.slices[1].fillColor = color02 self.chart.slices[2].fillColor = color03 self.chart.slices[3].fillColor = color04 self.chart.slices[4].fillColor = color05 self.chart.slices[5].fillColor = color06 self.chart.slices[6].fillColor = color07 self.chart.slices[7].fillColor = color08 self.chart.slices[8].fillColor = color09 self.chart.slices[9].fillColor = color10 self.chart.data = (100, 150, 180) self.chart.startAngle = -90 self._add(self,Label(),name='Title',validate=None,desc="The title at the top of the chart") self.Title.fontName = 'Helvetica-Bold' self.Title.fontSize = 7 self.Title.x = 100 self.Title.y = 135 self.Title._text = 'Chart Title' self.Title.maxWidth = 180 self.Title.height = 20 self.Title.textAnchor ='middle' self._add(self,Legend(),name='Legend',validate=None,desc="The legend or key for the chart") self.Legend.colorNamePairs = [(color01, 'North'), (color02, 'South'), (color03, 'Central')] self.Legend.fontName = 'Helvetica' self.Legend.fontSize = 7 self.Legend.x = 160 self.Legend.y = 85 self.Legend.dxTextSpace = 5 self.Legend.dy = 5 self.Legend.dx = 5 self.Legend.deltay = 5 self.Legend.alignment ='right' self.Legend.columnMaximum = 10 self.chart.slices.strokeWidth = 1 self.chart.slices.fontName = 'Helvetica' self.background = ShadedRect() self.background.fillColorStart = backgroundGrey self.background.fillColorEnd = backgroundGrey self.background.numShades = 1 self.background.strokeWidth = 0.5 self.background.x = 20 self.background.y = 20 self.chart.slices.popout = 5 self.background.height = 110 self.background.width = 110 self._add(self,0,name='preview',validate=None,desc=None)
def __init__(self, drawing=None, data=None): #Drawing.__init__(self, width=100, height=175) pie = Pie() pie.strokeColor = white pie.slices.strokeColor = white pie.slices.popout = 1 pie.width = 100 pie.height = 100 pie.y = 50 pie.x = 80 legend = Legend() legend.columnMaximum = 99 legend.alignment = 'right' legend.boxAnchor = 'c' legend.dx = 6 legend.dy = 6 legend.dxTextSpace = 5 legend.deltay = 10 legend.strokeWidth = 0 legend.strokeColor = white legend.subCols[0].minWidth = 75 legend.subCols[0].align = 'left' legend.subCols[1].minWidth = 25 legend.subCols[1].align = 'right' legend.y = 20 legend.x = 128 legend.fontName = 'Lato' legend.fontSize = 9 pie.data = data pie.slices[0].fillColor = lightgrey pie.slices[1].fillColor = limegreen pie.slices[1].popout = 6 pie.slices[2].fillColor = red #pie.slices[2].popout = 1 neutral_pct = positive_pct = negative_pct = 0 total = sum(data) if total > 0: neutral_pct = (data[0] / total) * 100.0 positive_pct = (data[1] / total) * 100.0 negative_pct = (data[2] / total) * 100.0 legend.colorNamePairs = [ (limegreen, ('Positive', '%.1f%%' % positive_pct)), (lightgrey, ('Neutral', '%.1f%%' % neutral_pct)), (red, ('Negative', '%.1f%%' % negative_pct)) ] drawing.add(pie) drawing.add(legend)
def create_pie_chart(self, data_list, label_list, user_color=None): # print data_list # print label_list label_list = map(lambda item: item.upper(), label_list) data = [(item / (sum(data_list) * 1.0)) * 100 for item in data_list] if user_color != None: usage_color = user_color else: random_range = [randint(0, 100) for i in range(len(data_list))] usage_color = map( lambda item: PCMYKColor(randint(0, item), randint(0, item), randint(0, item), randint(0, item)), random_range) print user_color # u_color = [colors.lawngreen, colors.red, colors.gray] # color = [colors.lawngreen, colors.red, colors.gray] # u_master = [randint(0, 100) for i in range(4)] # u_color = [PCMYKColor(randint(0, u_master[0]), randint(0, u_master[1]), randint(0, u_master[2]), randint(0, u_master[3])) for i in range(3)] # print u_color # color = u_color d = Drawing() pie = Pie() pie.x = 200 pie.y = 85 pie.data = data pie.labels = label_list for i, color in enumerate(usage_color): pie.slices[i].fillColor = color pie.slices.strokeWidth = 0.5 pie.slices.popout = 1.5 pie._seriesCount = 3 pie.sideLabels = 1 legend = Legend() legend.alignment = 'right' legend.x = 0 legend.y = 75 legend.colorNamePairs = [ (z, (x, ' {val:.2f}%'.format(val=y))) for x, y, z in zip(pie.labels, data, usage_color) ] d.add(legend) d.add(pie) self.flowables.append(d)