def sample4a(): '''Satish Darade failure''' from reportlab.graphics.charts.legends import LegendSwatchCallout from reportlab.graphics import shapes class LSwatchCallout(LegendSwatchCallout): def __init__(self, texts, fontName, fontSize): self._texts = texts self._fontName = fontName self._fontSize = fontSize def __call__(self, legend, g, thisx, y, i, colName, swatch): g.add( shapes.String(swatch.x - 2, y, self._texts[i], textAnchor='end', fontName=self._fontName, fontSize=self._fontSize)) d = Drawing(200, 100) legend = LineLegend() d.add(legend, 'legend') items = 'red green blue yellow pink black white' sw_names = items.upper().split() items = items.split() legend.colorNamePairs = [(getattr(colors, i), i) for i in items] legend.x = 20 legend.y = 90 d.legend.swatchCallout = LSwatchCallout(sw_names, 'Helvetica', 12) return d
def __init__(self, width=400, height=200, data=[], labels=[], legends=[], *args, **kw): Drawing.__init__(self, width, height, *args, **kw) self.add(HorizontalLineChart(), name='chart') self.add(LineLegend(), 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.angle = 45 for i in range(len(data)): self.chart.lines[i].strokeColor = 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 = 1 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 sample3a(): "Make sample legend with line swatches and dasharrays on the lines." d = Drawing(200, 100) legend = LineLegend() legend.alignment = 'right' legend.x = 20 legend.y = 90 legend.deltax = 60 legend.dxTextSpace = 10 legend.columnMaximum = 4 items = 'red green blue yellow pink black white'.split() darrays = ([2, 1], [2, 5], [2, 2, 5, 5], [1, 2, 3, 4], [4, 2, 3, 4], [1, 2, 3, 4, 5, 6], [1]) cnp = [] for i in range(0, len(items)): l = LineSwatch() l.strokeColor = getattr(colors, items[i]) l.strokeDashArray = darrays[i] cnp.append((l, items[i])) legend.colorNamePairs = cnp d.add(legend, 'legend') return d
def add_legend1(draw_obj, chart, data, doc): legend = LineLegend() legend.alignment = 'right' legend.x = doc.leftMargin legend.y = doc.bottomMargin - 30 legend.colorNamePairs = Auto(obj=chart) draw_obj.add(legend)
def __init__(self, data=None, legend_data=None): Drawing.__init__(self, width=530, height=160) self._add(self, MyChartFrame(x=0, y=0, width=530, height=160, title='Volume'), name='frame') # chart self._add(self, LinePlot(), name='chart') self.chart.y = 20 self.chart.x = 34 self.chart.width = 423 self.chart.height = 110 # line styles self.chart.lines.symbol = makeMarker('Circle', size=2) colors = map(toColor, get_n_random_colors(len(legend_data))) for (i, _) in enumerate(legend_data): self.chart.lines[i].strokeColor = colors[i] # x axis self.chart.xValueAxis = NormalDateXValueAxis() self.chart.xValueAxis.xLabelFormat = '{dd} {MMM}' self.chart.xValueAxis.loLLen = 8 self.chart.xValueAxis.hiLLen = 5 self.chart.xValueAxis.labels.fontName = 'Lato' self.chart.xValueAxis.labels.fontSize = 9 # y axis self.chart.yValueAxis = YValueAxis() self.chart.yValueAxis.visibleGrid = 1 self.chart.yValueAxis.visibleAxis = 0 self.chart.yValueAxis.strokeWidth = 0.25 self.chart.yValueAxis.labels.rightPadding = 5 self.chart.yValueAxis.labels.fontName = 'Lato' self.chart.yValueAxis.labels.fontSize = 9 self.chart.yValueAxis.rangeRound = 'both' self.chart.yValueAxis.tickLeft = 7.5 self.chart.yValueAxis.minimumTickSpacing = 0.5 self.chart.yValueAxis.maximumTicks = 8 self.chart.yValueAxis.avoidBoundFrac = 0.1 # legend self._add(self, LineLegend(), name='legend') self.legend.alignment = 'right' self.legend.y = 110 self.legend.x = 462 self.legend.dxTextSpace = 5 self.legend.columnMaximum = 4 self.legend.fontName = 'Lato' self.legend.fontSize = 9 self.legend.colorNamePairs = zip(colors, legend_data) # Data... self.chart.data = data
def sample3(): "Make sample legend with line swatches." d = Drawing(200, 100) legend = LineLegend() legend.alignment = 'right' legend.x = 20 legend.y = 90 legend.deltax = 60 legend.dxTextSpace = 10 legend.columnMaximum = 4 items = 'red green blue yellow pink black white'.split() items = [(getattr(colors, i), i) for i in items] legend.colorNamePairs = items d.add(legend, 'legend') return d
def render_graph(self, request_opts, graph_opts): def label_fmt(x): print "@@@", x return str(x) ld = len(graph_opts["data"]) palette = [Color(*x) for x in get_float_pallete(ld)] w = graph_opts["width"] h = graph_opts["height"] drawing = Drawing(w, h) # Legend legend = LineLegend() legend.colorNamePairs = [ (palette[i], graph_opts["data"][i].name) for i in range(ld) ] legend.boxAnchor = "sw" legend.columnMaximum = 2 legend.alignment = "right" drawing.add(legend) lh = legend._calcHeight() + self.X_PADDING / 2 # Plot lp = LinePlot() lfs = lp.xValueAxis.labels.fontSize lp.x = self.X_PADDING lp.y = self.Y_PADDING + lh + lfs lp.width = w - 2 * self.X_PADDING lp.height = h - self.Y_PADDING - lp.y lp.data = [ [(t, v) for v, t in ts] for ts in graph_opts["data"] ] for i in range(ld): lp.lines[i].strokeColor = palette[i] drawing.add(lp) # Render cdata = drawing.asString(format="png") response = self.render_plain_text( cdata, mimetype="image/png" ) if not request_opts["noCache"]: cache.set(request_opts["requestKey"], response, request_opts["cacheTimeout"]) return response
def __init__(self, series_colors_cmyk, box_width, box_height, chart_width, chart_height, width=550, height=215, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) Drawing.hAlign = 'CENTER' font_name = 'Helvetica' # self.width = 550 # self.height = 240 self.width = box_width self.height = box_height self._add(self, LinePlot(), name='chart', validate=None, desc=None) self._add(self, LineLegend(), name='legend', validate=None, desc=None) # self.chart.width = 490 # self.chart.height = 150 self.chart.width = chart_width self.chart.height = chart_height self.chart.y = 60 self.chart.x = 45 self.chart.strokeWidth = 1 for color in series_colors_cmyk: index = series_colors_cmyk.index(color) self.chart.lines[index].strokeColor = PCMYKColor(color[0], color[1], color[2], color[3], alpha=color[4]) self.chart.lines[index].symbol = makeMarker('FilledCircle') self.chart.lines[index].symbol.strokeColor = PCMYKColor( color[0], color[1], color[2], color[3], alpha=color[4]) self.chart.lines[index].symbol.size = 5 self.chart.lines.strokeWidth = 2 self.legend.colorNamePairs = Auto(obj=self.chart) self.legend.x = 5 self.legend.y = 30 # set size of swatches self.legend.dx = 0 self.legend.dy = 0 self.legend.fontName = font_name self.legend.fontSize = 8 self.legend.alignment = 'right' self.legend.columnMaximum = 2 self.legend.dxTextSpace = 4 self.legend.variColumn = 1 self.legend.boxAnchor = 'nw' self.legend.deltay = 15 self.legend.autoXPadding = 20 self.background = Rect(0, 0, self.width, self.height, strokeWidth=0, fillColor=PCMYKColor(0, 0, 10, 0)) self.background.strokeColor = black self.background.fillOpacity = 0.25 self.background.strokeWidth = 0 self.background.x = 0 self.background.fillColor = PCMYKColor(16, 12, 13, 0, alpha=30)
def __init__(self, data, font, font_bold, title, x_axis_params, y_axis_params, series_names, series_colors_cmyk, box_width, box_height, chart_width, chart_height, width=550, height=215, *args, **kw): logger.debug("Generating line chart with data:\n{0}\n".format( json.dumps(data, indent=2))) Drawing.__init__(self, width, height, *args, **kw) Drawing.hAlign = "CENTER" # self.width = 550 # self.height = 240 self.width = box_width self.height = box_height self._add(self, LinePlot(), name="chart", validate=None, desc=None) self._add(self, LineLegend(), name="legend", validate=None, desc=None) # self.chart.width = 490 # self.chart.height = 150 self.chart.width = chart_width self.chart.height = chart_height self.chart.y = 60 self.chart.x = 45 self.chart.strokeWidth = 1 for color in series_colors_cmyk: index = series_colors_cmyk.index(color) self.chart.lines[index].strokeColor = PCMYKColor(color[0], color[1], color[2], color[3], alpha=color[4]) self.chart.lines[index].symbol = makeMarker("FilledCircle") self.chart.lines[index].symbol.strokeColor = PCMYKColor( color[0], color[1], color[2], color[3], alpha=color[4]) self.chart.lines[index].symbol.size = 5 self.chart.lines.strokeWidth = 2 self.legend.colorNamePairs = Auto(obj=self.chart) self.legend.x = 10 * ( len(series_names) // 5 ) # adjust how far to the left/right the legend labels are self.legend.y = 12 * (len(series_names) // 5 ) # adjust how far up/down the legend labels are # set size of swatches self.legend.dx = 0 self.legend.dy = -5 self.legend.fontName = font self.legend.fontSize = 100 // len(series_names) self.legend.alignment = "right" self.legend.columnMaximum = ( len(series_names) // 5) + 1 # adjust number of ROWS allowed in legend self.legend.dxTextSpace = 4 self.legend.variColumn = 1 self.legend.boxAnchor = "nw" self.legend.deltay = 10 # adjust the space between legend rows self.legend.autoXPadding = 15 * ( (len(series_names) // 5) + 1 ) # adjust the space between legend columns self.background = Rect(0, 0, self.width, self.height, strokeWidth=0, fillColor=PCMYKColor(0, 0, 10, 0)) self.background.strokeColor = black self.background.fillOpacity = 0.25 self.background.strokeWidth = 0 self.background.x = 0 self.background.fillColor = PCMYKColor(16, 12, 13, 0, alpha=30) self.make_title(title, font=font_bold) self.make_data(data) self.make_x_axis(*x_axis_params, font=font) self.make_y_axis(*y_axis_params, font=font) self.make_series_labels(series_names)
def __init__(self, width=600, height=400, *args, **kw): apply(Drawing.__init__, (self, width, height) + args, kw) self.add(LinePlot(), name='chart') self.add(String(200, 180, ''), name='title') #set any shapes, fonts, colors you want here. We'll just #set a title font and place the chart within the drawing. #pick colors for all the lines, do as many as you need self.chart.x = 20 self.chart.y = 30 self.chart.width = self.width - 100 self.chart.height = self.height - 75 self.chart.lines[0].strokeColor = colors.blue self.chart.lines[1].strokeColor = colors.red self.chart.fillColor = colors.white self.title.fontName = 'Times-Roman' self.title.fontSize = 18 #self.chart.data = [((0, 50), (100,100), (200,200), (250,210), (300,300), (400,500))] self.chart.xValueAxis.labels.fontSize = 12 self.chart.xValueAxis.forceZero = 0 self.chart.xValueAxis.gridEnd = self.width - self.width * 0.60 self.chart.yValueAxis.gridEnd = self.height - self.height * 0.2 self.chart.xValueAxis.tickDown = 3 self.chart.xValueAxis.visibleGrid = 1 self.chart.yValueAxis.visibleGrid = 1 self.chart.yValueAxis.tickLeft = 3 self.chart.yValueAxis.labels.fontName = 'Times-Roman' self.chart.yValueAxis.labels.fontSize = 12 self.title.x = self.width / 2 self.title.y = 0 self.title.textAnchor = 'middle' self.add(LineLegend(), name='Legend') self.Legend.fontName = 'Times-Roman' self.Legend.fontSize = 12 self.Legend.x = self.width - 10 self.Legend.y = 70 self.Legend.dxTextSpace = 5 self.Legend.dy = 5 self.Legend.dx = 5 self.Legend.deltay = 5 self.Legend.alignment = 'right' self.Legend.colorNamePairs = [(colors.blue, 'Tiempo Estimado')] self.add(Label(), name='XLabel') self.XLabel.fontName = 'Times-Roman' self.XLabel.fontSize = 12 self.XLabel.x = 100 self.XLabel.y = 5 self.XLabel.textAnchor = 'middle' self.XLabel.height = 20 self.XLabel._text = "que tal" self.add(Label(), name='YLabel') self.YLabel.fontName = 'Times-Roman' self.YLabel.fontSize = 12 self.YLabel.x = -10 self.YLabel.y = 100 self.YLabel.angle = 90 self.YLabel.textAnchor = 'middle' self.YLabel._text = "Hola" self.chart.yValueAxis.forceZero = 1 self.chart.xValueAxis.forceZero = 1 self.chart.lines[0].symbol = makeMarker('FilledCircle') self.chart.lines[1].symbol = makeMarker('Circle')
def drawsystemdailyefficiency(can, resultsbuffer): can.setFont("Helvetica", 24) sec_title = "Chiller Plant System Daily Efficiency" can.drawString(25, 750, sec_title) desc_text = "The chiller plant system daily efficiency demonstrates the total efficiency including every" \ " parts of the system across a series of days. The range can be by default (say, 2 days) or" \ " specified by administrators." stylesheet = getSampleStyleSheet() paragraph = Paragraph(desc_text, stylesheet['Normal']) aW, aH = 500, 600 w, h = paragraph.wrap(aW, aH) if w <= aW and h <= aH: paragraph.drawOn(can, 25, 700) # Draw the chart drawing = Drawing(600, 400) # font fontName = 'Helvetica' fontSize = 7 # chart lp = LinePlot() lp.y = 16 lp.x = 32 lp.width = 400 lp.height = 200 # line styles lp.lines.strokeWidth = 0 lp.lines.symbol = makeMarker('FilledSquare') # x axis lp.xValueAxis = NormalDateXValueAxis() lp.xValueAxis.labels.fontName = fontName lp.xValueAxis.labels.fontSize = fontSize - 1 lp.xValueAxis.forceEndDate = 1 lp.xValueAxis.forceFirstDate = 1 lp.xValueAxis.labels.boxAnchor = 'autox' lp.xValueAxis.xLabelFormat = '{d}-{MMM}' lp.xValueAxis.maximumTicks = 5 lp.xValueAxis.minimumTickSpacing = 0.5 lp.xValueAxis.niceMonth = 0 lp.xValueAxis.strokeWidth = 1 lp.xValueAxis.loLLen = 5 lp.xValueAxis.hiLLen = 5 lp.xValueAxis.gridEnd = drawing.width lp.xValueAxis.gridStart = lp.x - 10 # y axis # self.chart.yValueAxis = AdjYValueAxis() lp.yValueAxis.visibleGrid = 1 lp.yValueAxis.visibleAxis = 0 lp.yValueAxis.labels.fontName = fontName lp.yValueAxis.labels.fontSize = fontSize - 1 lp.yValueAxis.labelTextFormat = '%0.2f%%' lp.yValueAxis.strokeWidth = 0.25 lp.yValueAxis.visible = 1 lp.yValueAxis.labels.rightPadding = 5 # self.chart.yValueAxis.maximumTicks = 6 lp.yValueAxis.rangeRound = 'both' lp.yValueAxis.tickLeft = 7.5 lp.yValueAxis.minimumTickSpacing = 0.5 lp.yValueAxis.maximumTicks = 8 lp.yValueAxis.forceZero = 0 lp.yValueAxis.avoidBoundFrac = 0.1 # legend ll = LineLegend() ll.fontName = fontName ll.fontSize = fontSize ll.alignment = 'right' ll.dx = 5 # sample data lp.data = [[(19010706, 3.3900000000000001), (19010806, 3.29), (19010906, 3.2999999999999998), (19011006, 3.29), (19011106, 3.3399999999999999), (19011206, 3.4100000000000001), (19020107, 3.3700000000000001), (19020207, 3.3700000000000001), (19020307, 3.3700000000000001), (19020407, 3.5), (19020507, 3.6200000000000001), (19020607, 3.46), (19020707, 3.3900000000000001)], [(19010706, 3.2000000000000002), (19010806, 3.1200000000000001), (19010906, 3.1400000000000001), (19011006, 3.1400000000000001), (19011106, 3.1699999999999999), (19011206, 3.23), (19020107, 3.1899999999999999), (19020207, 3.2000000000000002), (19020307, 3.1899999999999999), (19020407, 3.3100000000000001), (19020507, 3.4300000000000002), (19020607, 3.29), (19020707, 3.2200000000000002)]] lp.lines[0].strokeColor = PCMYKColor(0, 100, 100, 40, alpha=100) lp.lines[1].strokeColor = PCMYKColor(100, 0, 90, 50, alpha=100) lp.xValueAxis.strokeColor = PCMYKColor(100, 60, 0, 50, alpha=100) ll.colorNamePairs = [(PCMYKColor(0, 100, 100, 40, alpha=100), '01-Mar-2017'), (PCMYKColor(100, 0, 90, 50, alpha=100), '02-Mar-2017')] lp.lines.symbol.x = 0 lp.lines.symbol.strokeWidth = 0 lp.lines.symbol.arrowBarbDx = 5 lp.lines.symbol.strokeColor = PCMYKColor(0, 0, 0, 0, alpha=100) lp.lines.symbol.fillColor = None lp.lines.symbol.arrowHeight = 5 ll.dxTextSpace = 7 ll.boxAnchor = 'nw' ll.subCols.dx = 0 ll.subCols.dy = -2 ll.subCols.rpad = 0 ll.columnMaximum = 1 ll.deltax = 1 ll.deltay = 0 ll.dy = 5 ll.y = 240 ll.x = 300 lp.lines.symbol.kind = 'FilledCross' lp.lines.symbol.size = 5 lp.lines.symbol.angle = 45 drawing.add(lp) drawing.add(ll) # drawing.title.text = "Jurong Point System Efficiency" # drawing.title.fondSize = 16 drawing.drawOn(can, 100, 450) can.showPage()
def __init__(self, width=258, height=150, *args, **kw): Drawing.__init__(self, width, height, *args, **kw) # font fontName = 'Helvetica' fontSize = 7 # chart self._add(self, LinePlot(), name='chart', validate=None, desc=None) self.chart.y = 16 self.chart.x = 32 self.chart.width = 212 self.chart.height = 90 # line styles self.chart.lines.strokeWidth = 0 self.chart.lines.symbol = makeMarker('FilledSquare') # x axis self.chart.xValueAxis = NormalDateXValueAxis() self.chart.xValueAxis.labels.fontName = fontName self.chart.xValueAxis.labels.fontSize = fontSize - 1 self.chart.xValueAxis.forceEndDate = 1 self.chart.xValueAxis.forceFirstDate = 1 self.chart.xValueAxis.labels.boxAnchor = 'autox' self.chart.xValueAxis.xLabelFormat = '{d}-{MMM}' self.chart.xValueAxis.maximumTicks = 5 self.chart.xValueAxis.minimumTickSpacing = 0.5 self.chart.xValueAxis.niceMonth = 0 self.chart.xValueAxis.strokeWidth = 1 self.chart.xValueAxis.loLLen = 5 self.chart.xValueAxis.hiLLen = 5 self.chart.xValueAxis.gridEnd = self.width self.chart.xValueAxis.gridStart = self.chart.x - 10 # y axis #self.chart.yValueAxis = AdjYValueAxis() self.chart.yValueAxis.visibleGrid = 1 self.chart.yValueAxis.visibleAxis = 0 self.chart.yValueAxis.labels.fontName = fontName self.chart.yValueAxis.labels.fontSize = fontSize - 1 self.chart.yValueAxis.labelTextFormat = '%0.2f%%' self.chart.yValueAxis.strokeWidth = 0.25 self.chart.yValueAxis.visible = 1 self.chart.yValueAxis.labels.rightPadding = 5 #self.chart.yValueAxis.maximumTicks = 6 self.chart.yValueAxis.rangeRound = 'both' self.chart.yValueAxis.tickLeft = 7.5 self.chart.yValueAxis.minimumTickSpacing = 0.5 self.chart.yValueAxis.maximumTicks = 8 self.chart.yValueAxis.forceZero = 0 self.chart.yValueAxis.avoidBoundFrac = 0.1 # legend self._add(self, LineLegend(), name='legend', validate=None, desc=None) self.legend.fontName = fontName self.legend.fontSize = fontSize self.legend.alignment = 'right' self.legend.dx = 5 # sample data self.chart.data = [[(19010706, 3.3900000000000001), (19010806, 3.29), (19010906, 3.2999999999999998), (19011006, 3.29), (19011106, 3.3399999999999999), (19011206, 3.4100000000000001), (19020107, 3.3700000000000001), (19020207, 3.3700000000000001), (19020307, 3.3700000000000001), (19020407, 3.5), (19020507, 3.6200000000000001), (19020607, 3.46), (19020707, 3.3900000000000001)], [(19010706, 3.2000000000000002), (19010806, 3.1200000000000001), (19010906, 3.1400000000000001), (19011006, 3.1400000000000001), (19011106, 3.1699999999999999), (19011206, 3.23), (19020107, 3.1899999999999999), (19020207, 3.2000000000000002), (19020307, 3.1899999999999999), (19020407, 3.3100000000000001), (19020507, 3.4300000000000002), (19020607, 3.29), (19020707, 3.2200000000000002)]] self.chart.lines[0].strokeColor = PCMYKColor(0, 100, 100, 40, alpha=100) self.chart.lines[1].strokeColor = PCMYKColor(100, 0, 90, 50, alpha=100) self.chart.xValueAxis.strokeColor = PCMYKColor(100, 60, 0, 50, alpha=100) self.legend.colorNamePairs = [(PCMYKColor(0, 100, 100, 40, alpha=100), 'Bovis Homes'), (PCMYKColor(100, 0, 90, 50, alpha=100), 'HSBC Holdings')] self.chart.lines.symbol.x = 0 self.chart.lines.symbol.strokeWidth = 0 self.chart.lines.symbol.arrowBarbDx = 5 self.chart.lines.symbol.strokeColor = PCMYKColor(0, 0, 0, 0, alpha=100) self.chart.lines.symbol.fillColor = None self.chart.lines.symbol.arrowHeight = 5 self.legend.dxTextSpace = 7 self.legend.boxAnchor = 'nw' self.legend.subCols.dx = 0 self.legend.subCols.dy = -2 self.legend.subCols.rpad = 0 self.legend.columnMaximum = 1 self.legend.deltax = 1 self.legend.deltay = 0 self.legend.dy = 5 self.legend.y = 135 self.legend.x = 120 self.chart.lines.symbol.kind = 'FilledCross' self.chart.lines.symbol.size = 5 self.chart.lines.symbol.angle = 45