def addChromosomes(drawing, chrNames, chrSizes, xmap, ymap, w=0.1*DPI, fillColor=colors.skyblue, strokeColor=colors.skyblue): for i,chrom in enumerate(chrNames): x = xmap(i+1) y = ymap(chrSizes[chrom]) h = ymap(1)-ymap(chrSizes[chrom]) chromosome = Rect(x,y,w,h, strokeColor=strokeColor, fillColor=fillColor) drawing.add(chromosome) topCap = Wedge(x+0.5*w, y+h, 0.5*w, 0, 180, strokeColor=strokeColor, fillColor=fillColor) bottomCap = Wedge(x+0.5*w, y, 0.5*w, 180, 0, strokeColor=strokeColor, fillColor=fillColor) drawing.add(topCap) drawing.add(bottomCap) label = Label() label.setOrigin(xmap(i+1)+w/2, ymap(0)) label.boxAnchor = 's' label.textAnchor = 'middle' label.dx = 0 label.dy = DPI/10 label.setText(chrom) label.fontSize = 36 label.fontName = 'Helvetica' drawing.add(label) chrLength = Label() chrLength.setOrigin(xmap(i+1)+w/2, ymap(chrSizes[chrom])) chrLength.boxAnchor = 'n' chrLength.textAnchor = 'middle' chrLength.dx = 0 chrLength.dy = -DPI/10 chrLength.setText('%iMb' % int(chrSizes[chrom]/1e6)) chrLength.fontSize = 24 chrLength.fontName = 'Helvetica' drawing.add(chrLength)
def drawing_chinese(): from reportlab.graphics.charts.lineplots import LinePlot from reportlab.graphics.charts.textlabels import Label from reportlab.graphics import renderPDF from reportlab.graphics.widgets.markers import makeMarker data = [((1, 100), (2, 200), (3, 300), (4, 400), (5, 500)), ((1, 50), (2, 80), (3, 400), (4, 40), (5, 70))] drawing = Drawing(500, 300) lp = LinePlot() lp.x = 50 #������������ lp.y = 30 lp.height = 250 lp.width = 400 lp.data = data lp.joinedLines = 1 lp.lines.symbol = makeMarker('FilledCircle') lp.xValueAxis.valueMin = 1 lp.xValueAxis.valueMax = 5 lp.xValueAxis.valueStep = 1 lp.yValueAxis.valueMin = 0 lp.yValueAxis.valueMax = 500 lp.yValueAxis.valueStep = 100 drawing.add(lp) title = Label() #����Ҫ��ʾ���ģ���Ҫ��ע��һ���������� title.fontName = "msyh" title.fontSize = 12 title_text = u'你好吗' #title_text = "abc" title._text = title_text title.x = 250 title.y = 280 title.textAnchor = 'middle' drawing.add(title) Xlabel = Label() Xlabel._text = 'x' Xlabel.fontSize = 12 Xlabel.x = 480 Xlabel.y = 30 Xlabel.textAnchor = 'middle' drawing.add(Xlabel) Ylabel = Label() Ylabel._text = "y" Ylabel.fontSize = 12 Ylabel.x = 40 Ylabel.y = 295 Ylabel.textAnchor = 'middle' drawing.add(Ylabel) try: drawing.save(formats=['gif'], outDir=".", fnRoot="abc") except: import traceback traceback.print_exc()
def addChromosomes(drawing, chrNames, chrSizes, xmap, ymap, w=0.1 * DPI, fillColor=colors.skyblue, strokeColor=colors.skyblue): for i, chrom in enumerate(chrNames): x = xmap(i + 1) y = ymap(chrSizes[chrom]) h = ymap(1) - ymap(chrSizes[chrom]) chromosome = Rect(x, y, w, h, strokeColor=strokeColor, fillColor=fillColor) drawing.add(chromosome) topCap = Wedge(x + 0.5 * w, y + h, 0.5 * w, 0, 180, strokeColor=strokeColor, fillColor=fillColor) bottomCap = Wedge(x + 0.5 * w, y, 0.5 * w, 180, 0, strokeColor=strokeColor, fillColor=fillColor) drawing.add(topCap) drawing.add(bottomCap) label = Label() label.setOrigin(xmap(i + 1) + w / 2, ymap(0)) label.boxAnchor = 's' label.textAnchor = 'middle' label.dx = 0 label.dy = DPI / 10 label.setText(chrom) label.fontSize = 36 label.fontName = 'Helvetica' drawing.add(label) chrLength = Label() chrLength.setOrigin(xmap(i + 1) + w / 2, ymap(chrSizes[chrom])) chrLength.boxAnchor = 'n' chrLength.textAnchor = 'middle' chrLength.dx = 0 chrLength.dy = -DPI / 10 chrLength.setText('%iMb' % int(chrSizes[chrom] / 1e6)) chrLength.fontSize = 24 chrLength.fontName = 'Helvetica' drawing.add(chrLength)
def draw_recent_week_pdf(filename, data_list): """ 画最近七天的流量计报表 :param filename: :param data_list :return: """ data = [] max_val = 0 for index in range(0, len(data_list)): data.append((index + 1, data_list[index])) max_val = max(max_val, data_list[index]) drawing = Drawing(500, 800) lp = LinePlot() lp.x = 50 lp.y = 80 lp.height = 600 lp.width = 400 lp.data = [data] lp.joinedLines = 1 lp.lines.symbol = makeMarker('FilledCircle') lp.xValueAxis.valueMin = 1 lp.xValueAxis.valueMax = 7 lp.xValueAxis.valueStep = 1 lp.yValueAxis.valueMin = 0 lp.yValueAxis.valueMax = (int(max_val / 100) + 1) * 100 lp.yValueAxis.valueStep = 100 drawing.add(lp) x_title = Label() # 若需要显示中文,需要先注册一个中文字体 pdfmetrics.registerFont(ttfonts.TTFont("haha", "simsun.ttc")) x_title.fontName = "haha" x_title.fontSize = 12 title_text = '用气量' x_title._text = title_text x_title.x = 20 x_title.y = 100 x_title.textAnchor = 'middle' drawing.add(x_title) y_title = Label() # 若需要显示中文,需要先注册一个中文字体 pdfmetrics.registerFont(ttfonts.TTFont("haha", "simsun.ttc")) y_title.fontName = "haha" y_title.fontSize = 12 title_text = '最近七天' y_title._text = title_text y_title.x = 80 y_title.y = 50 y_title.textAnchor = 'middle' drawing.add(y_title) drawing.save(formats=['pdf'], outDir=TMP_FILE_DIRECTORY_PATH, fnRoot=filename)
def addScale(drawing, xmap, y, start, end, tickLen=10, dx=3, dy=6, textAnchor='middle', boxAnchor='s', fontSize=12, strokeWidth=1, strokeColor=colors.black, scale=1.0, format='%ibp'): x1 = xmap(start) x2 = xmap(end) line = Line(x1+dx,y,x2-dx,y, strokeWidth=strokeWidth, strokeColor=strokeColor) drawing.add(line) leftTick = Line(x1+dx,y-0.5*tickLen,x1+dx,y+0.5*tickLen, strokeWidth=strokeWidth, strokeColor=strokeColor) drawing.add(leftTick) rightTick = Line(x2-dx,y-0.5*tickLen,x2-dx,y+0.5*tickLen, strokeWidth=strokeWidth, strokeColor=strokeColor) drawing.add(rightTick) label = Label() label.setOrigin(0.5*(x1+x2), y+dy) distance = float(end-start)/scale label.setText(format % (distance/scale)) label.fontSize = fontSize label.textAnchor = textAnchor label.boxAnchor = boxAnchor drawing.add(label)
def draw(self): # general widget bits w = float(self.length) h = float(self.height) g = shapes.Group() body = shapes.Polygon( [self.x-0.5*w, self.y-0.5*w, self.x-0.5*w, self.y+0.5*w, self.x+0.5*w, self.y], fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(body) if self.label: b = g.getBounds() s = Label() s.setText(self.label) s.setOrigin(self.x+0.5*w, self.y-h/2+b[3]-b[1]+4) s.boxAnchor = self.boxAnchor s.textAnchor = self.textAnchor s.fontName = 'Helvetica' s.fontSize = self.fontSize s.angle = self.labelAngle g.add(s) return g
def title_draw(self, x, y, text): chart_title = Label() chart_title.x = x chart_title.y = y chart_title.fontName = 'FreeSansBold' chart_title.fontSize = 16 chart_title.textAnchor = 'middle' chart_title.setText(text) return chart_title
def header_draw(self, x, y, text): chart_title = Label() chart_title.x = x chart_title.y = y chart_title.fontName = 'RobotoBold' chart_title.fontSize = 9 chart_title.textAnchor = 'middle' chart_title.setText(text) return chart_title
def title_draw(self, x, y, text): chart_title = Label() chart_title.x = x chart_title.y = y chart_title.fontName = 'FreeSansBold' chart_title.fontSize = 16 chart_title.textAnchor = 'middle' chart_title.setText(text) return chart_title
def draw(self): # general widget bits w = float(self.length) h = float(self.height) # print self.label,w,h # Set minimum size if abs(w)<self.wmin: xmid = self.x+0.5*w w = w/abs(w) * self.wmin self.x = xmid-0.5*w g = shapes.Group() if abs(w)>self.wNoTail: # arrow specific bits body = shapes.Rect(x=self.x, y=self.y-self.aspectRatio*h/2, width=2*(w/3), height=self.aspectRatio*h, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(body) head = shapes.Polygon( points=[self.x+w, self.y, self.x+2*(w/3), self.y+h/2, self.x+2*(w/3), self.y-h/2, self.x+w, self.y], fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(head) else: head = shapes.Polygon( points=[self.x+w, self.y, self.x, self.y+h/2, self.x, self.y-h/2, self.x+w, self.y], fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(head) if self.label: b = g.getBounds() s = Label() s.setText(self.label) s.setOrigin(self.x+0.5*w+self.labeldx, self.y-h/2+b[3]-b[1]+self.labeldy) s.boxAnchor = self.boxAnchor s.textAnchor = self.textAnchor s.fontName = 'Helvetica' s.fontSize = self.fontSize s.angle = self.labelAngle g.add(s) return g
def setDescription(self): desc = Label() desc.fontName = 'Helvetica' desc.fontSize = 12 desc.x = 230 desc.y = 10 desc._text = self._data_dict.get('description', '') desc.maxWidth = 280 desc.height = 20 desc.textAnchor ='middle' self.add(desc, name='Description')
def setTitle(self): title = Label() title.fontName = 'Helvetica-Bold' title.fontSize = 12 title.x = 300 title.y = 335 title._text = self._data_dict.get('title', '') title.maxWidth = 180 title.height = 20 title.textAnchor ='middle' self.add(title, name='Title')
def addLabels(self,drawing,title=None,xlabel=None,ylabel=None): from reportlab.graphics.charts.textlabels import Label if not title is None: Title = Label() Title.fontName = 'Helvetica-Bold' Title.fontSize = 7 Title.x = drawing.width/2 Title.y = drawing.height-25 Title._text = title Title.maxWidth = 180 Title.height = 20 Title.textAnchor ='middle' drawing.add(Title) if not xlabel is None: XLabel = Label() XLabel.fontName = 'Helvetica' XLabel.fontSize = 7 XLabel.x = drawing.width/2 XLabel.y = 10 XLabel.textAnchor ='middle' XLabel.maxWidth = 100 XLabel.height = 20 XLabel._text = xlabel drawing.add(XLabel) if not ylabel is None: YLabel = Label() YLabel.fontName = 'Helvetica' YLabel.fontSize = 7 YLabel.x = 12 YLabel.y = drawing.height/2 YLabel.angle = 90 YLabel.textAnchor ='middle' YLabel.maxWidth = 100 YLabel.height = 20 YLabel._text = ylabel drawing.add(YLabel)
def setAxesLabels(self): xlabel = Label() xlabel.fontName = 'Helvetica' xlabel.fontSize = 12 xlabel.x = 450 xlabel.y = 40 xlabel._text = self._data_dict.get('xlabel', '') xlabel.maxWidth = 180 xlabel.height = 20 xlabel.textAnchor ='middle' self.add(xlabel, name='xlabel') ylabel = Label() ylabel.fontName = 'Helvetica' ylabel.fontSize = 12 ylabel.x = 20 ylabel.y = 210 ylabel.angle = 90 ylabel._text = self._data_dict.get('ylabel', '') ylabel.maxWidth = 180 ylabel.height = 20 ylabel.textAnchor ='middle' self.add(ylabel, name='ylabel')
def genLayers(l0=["substrate", 30], l1=["p++", 20], l2=["p--", 50]): c = ['#ffeea0', '#ff88ff', '#bbd5e8'] length = 4 * cm d = Drawing(4 * cm, 100) xpos = 0 #*(width-length-4*cm)/2 # layer0 = 40 # l1[1] = 40 # l2[1]= 100-layer0-l1[1] l0[1], l1[1], l2[1] = layersize(l0[1], l1[1], l2[1]) d.add(Rect(xpos, 0, length, l0[1], fillColor=colors.HexColor(c[0]))) lab = Label() lab.textAnchor = 'middle' lab.setText(l0[0]) lab.setOrigin(xpos + 2 * cm, l0[1] / 2) d.add(lab) d.add(Rect(xpos, l0[1], length, l1[1], fillColor=colors.HexColor(c[1]))) lab = Label() lab.textAnchor = 'middle' lab.setText(l1[0]) lab.setOrigin(xpos + 2 * cm, l0[1] + l1[1] / 2) d.add(lab) d.add( Rect(xpos, l0[1] + l1[1], length, l2[1], fillColor=colors.HexColor(c[2]))) lab = Label() lab.textAnchor = 'middle' lab.setText(l2[0]) lab.setOrigin(xpos + 2 * cm, l0[1] + l1[1] + l2[1] / 2) d.add(lab) return d
def addLabel(drawing, x, y, text, fontName='Helvetica', fontSize=11, dy=0, angle=0, boxAnchor='sw', textAnchor='start'): """Add a label to the drawing. This interface here is inconsistent in that it requires pixel coords. FIX This just sets convenient defaults for Label.""" label = Label() label.setText(text) label.setOrigin(x, y) label.fontName = fontName label.fontSize = fontSize label.boxAnchor = boxAnchor label.textAnchor = textAnchor label.dy = dy label.angle = angle drawing.add(label)
def addPointyCompoundFeature(drawing, xmap, y, gene, strokeColor=None, fillColor=colors.blue, intronColor=colors.blue, glyph=PointyBlock, height=12, utrHeight=6, rise=8, labeldy=10, fontSize=10, textAnchor='middle', boxAnchor='s'): """Adds a pointy compound feature to the drawing. This is typically several exons joined by zig-zag lines with an arrow showing strand.""" if gene.strand=='+': x1,x2 = xmap(gene.start), xmap(gene.end) else: x2,x1 = xmap(gene.start), xmap(gene.end) y = y+height/2 y1 = y line = Line(x1,y1,x2,y1,strokeColor=intronColor) drawing.add(line) for exon in gene: if exon.strand=='+': x1,x2 = xmap(exon.start), xmap(exon.end) else: x2,x1 = xmap(exon.start), xmap(exon.end) g = glyph() g.x = x1 g.y = y if exon.kind.lower()=='utr': g.height = utrHeight else: g.height = height g.length = x2-x1 g.fillColor = fillColor if strokeColor: g.strokeColor = strokeColor else: g.strokeColor = fillColor g.fontSize = fontSize drawing.add(g) label = Label() label.setText(gene.name) x = 0.5*(gene.start+gene.end) label.setOrigin(x,y) label.dy = labeldy label.textAnchor = textAnchor label.boxAnchor = boxAnchor drawing.add(label)
def genLayers2(ls): #ls =(titre, taille) c = ['#ffeea0', '#ff88ff', '#bbd5e8'] length = 4 * cm # ls = np.asarray(ls) l = [] for i in range(len(ls)): l.append((ls[i][0], ls[i][1])) ls = np.asarray(l, dtype=np.str) d = Drawing(4 * cm, 100) xpos = 0 ls[:, 1] = layersize2(ls[:, 1]) tickness = ls[:, 1].astype(np.float32) for i in range(len(ls)): lab = Label() lab.textAnchor = 'middle' color = c[0] labelTxt = str(ls[:, 0][i]) if '+' in labelTxt: color = c[1] elif '-' in labelTxt: color = c[2] lab.setText(ls[:, 0][i]) if i > 0: d.add( Rect(xpos, np.cumsum(tickness)[i - 1], length, tickness[i], fillColor=colors.HexColor(color))) lab.setOrigin(xpos + 2 * cm, np.cumsum(tickness)[i - 1] + tickness[i] / 2) else: d.add( Rect(xpos, 0, length, tickness[i], fillColor=colors.HexColor(color))) lab.setOrigin(xpos + 2 * cm, tickness[i] / 2) d.add(lab) return d
def addAxis(drawing, xmap, y, strokeWidth=1, minorStrokeWidth=0.5, tickDir='down', autoTicks=False, nTicks=20, tickLen=5, fontSize=10, nMinorTicks=80, minorTickLen=2, angle=0, dx=0, dy=-2, textAnchor='middle', boxAnchor=None, scale=1.0, format='%i'): """Add a horizontal axis to the drawing. To do: Round tick positions """ line = Line(xmap.x0, y, xmap.x1, y, strokeWidth=strokeWidth) drawing.add(line) if not boxAnchor: if tickDir=='down': boxAnchor = 'n' else: boxAnchor = 's' signum = {'up': -1, 'down': 1}[tickDir] if nTicks>0: ticks = tick_generator(xmap.start, xmap.end, n=nTicks, convert=int) for p in ticks: x = xmap(p) line = Line(x, y, x, y-signum*tickLen, strokeWidth=strokeWidth) drawing.add(line) s = Label() s.setOrigin(x, y-signum*tickLen) s.setText(format % (p/scale)) s.dx = dx s.dy = signum*dy s.fontName = 'Helvetica' s.fontSize = fontSize s.textAnchor = textAnchor s.boxAnchor = boxAnchor s.angle = angle drawing.add(s) minorticks = tick_generator(xmap.start, xmap.end, n=nMinorTicks, convert=int) for p in minorticks: x = xmap(p) line = Line(x, y, x, y-signum*minorTickLen, strokeWidth=minorStrokeWidth) drawing.add(line)
def draw_line(self, line_data, *args, **kw): drawing = Drawing(400, 200) lp = LinePlot() lp.x = 30 lp.y = -20 lp.height = 200 lp.width = 400 lp.data = line_data lp.xValueAxis.labels.fontName = 'Helvetica' lp.xValueAxis.labels.fontSize = 7 lp.xValueAxis.forceZero = 0 lp.xValueAxis.avoidBoundFrac = 1 lp.xValueAxis.gridEnd = 115 lp.xValueAxis.tickDown = 3 lp.xValueAxis.labelTextFormat = lambda x: time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(int(x) / 1000)) lp.yValueAxis.tickLeft = 3 lp.yValueAxis.labels.fontName = 'Helvetica' lp.yValueAxis.labels.fontSize = 7 for i in range(len(line_data)): lp.lines[i].strokeColor = colors.toColor('hsl(%s,80%%,40%%)' % (i * 60)) drawing.add(lp) title = Label() title.fontName = 'Helvetica-Bold' title.fontSize = 14 title.x = 200 title.y = 180 title._text = 'Chart Title' title.maxWidth = 180 title.height = 20 title.textAnchor = 'middle' drawing.add(title) return drawing
def addAxis(drawing, xmap, y, fontSize=8, tickLen=4, minorTickLen=2, nTicks=20, strokeWidth=1, minorStrokeWidth=0.5): line = Line(xmap.x0, y, xmap.x1, y, strokeWidth=strokeWidth) drawing.add(line) ticks = tick_generator(xmap.start, xmap.end, n=nTicks, convert=int) for p in ticks: x = xmap(p) line = Line(x, y, x, y-tickLen, strokeWidth=strokeWidth) drawing.add(line) s = Label() s.setOrigin(x, y-tickLen) s.setText(str(p)) s.fontName = 'Helvetica' s.fontSize = fontSize s.textAnchor = 'middle' s.boxAnchor = 'n' drawing.add(s) minorticks = tick_generator(xmap.start, xmap.end, n=50, convert=int) for p in minorticks: x = xmap(p) line = Line(x, y, x, y-minorTickLen, strokeWidth=minorStrokeWidth) drawing.add(line)
def addCompoundFeature(drawing, xmap, y, gene, strokeColor=None, fillColor=colors.blue, intronColor=colors.blue, intronWidth=0.5, glyph=Block, height=12, utrHeight=6, labeldy=10, fontSize=10, textAnchor='middle', boxAnchor='s'): """Adds a compund feature to the drawing. A compound feature is typically several exons joined by zig-zag lines.""" rise = height + utrHeight intronStarts = [None] intronEnds = [] heights = [] for exon in gene: x1,x2 = xmap(exon.start), xmap(exon.end) kind = exon.kind.lower() if kind in ['exon', 'utr']: intronStarts.append(exon.end) intronEnds.append(exon.start) g = glyph() g.x = x1 g.y = y+height/2 if exon.kind.lower()=='exon': g.height = height heights.append(height) else: g.height = utrHeight heights.append(utrHeight) g.length = x2-x1 g.fillColor = fillColor if strokeColor: g.strokeColor = strokeColor else: g.strokeColor = fillColor g.fontSize = fontSize drawing.add(g) for i,(intronStart,intronEnd) in enumerate(zip(intronStarts[1:], intronEnds[1:])): x1 = xmap(intronStart) x2 = xmap(0.5*(intronStart+intronEnd)) x3 = xmap(intronEnd) # if abs(x3-x1)<3: continue # print intronStart,intronEnd,heights[i],heights[i+1] y1 = y+heights[i]/2+height/2 y2 = y+rise y3 = y+heights[i+1]/2+height/2 line1 = Line(x1,y1,x2,y2,strokeColor=intronColor,strokeWidth=intronWidth) line2 = Line(x2,y2,x3,y3,strokeColor=intronColor,strokeWidth=intronWidth) drawing.add(line1) drawing.add(line2) # Draw arrows if xmap.flipped: signum = -1 else: signum = 1 if gene.strand=='+': x1 = xmap(gene.end) x2 = x1 + signum*15 x3 = x1 + signum*10 y1 = y + 0.5*height y2 = y + 0.75*height y3 = y + 0.25*height line1 = Line(x1,y1,x2,y1,strokeColor=intronColor,strokeWidth=intronWidth) line2 = Line(x2,y1,x3,y2,strokeColor=intronColor,strokeWidth=intronWidth) line3 = Line(x2,y1,x3,y3,strokeColor=intronColor,strokeWidth=intronWidth) drawing.add(line1) drawing.add(line2) drawing.add(line3) else: x1 = xmap(gene.start) x2 = x1 - signum*15 x3 = x1 - signum*10 y1 = y + 0.5*height y2 = y + 0.75*height y3 = y + 0.25*height line1 = Line(x1,y1,x2,y1,strokeColor=intronColor,strokeWidth=intronWidth) line2 = Line(x2,y1,x3,y2,strokeColor=intronColor,strokeWidth=intronWidth) line3 = Line(x2,y1,x3,y3,strokeColor=intronColor,strokeWidth=intronWidth) drawing.add(line1) drawing.add(line2) drawing.add(line3) # if gene has attribute name... label = Label() label.setText(gene.name) pos = 0.5*(gene.start+gene.end) x = xmap(pos) label.setOrigin(x,y) label.dy = labeldy label.textAnchor = textAnchor label.boxAnchor = boxAnchor drawing.add(label)
def bar_chart(data, labels, **kw): """ :param data: contains a two dimentional array of values, e.g. [[d11, d21, x1], [d12, d22, x2]] :type data: list[list[numbers],...] :param labels: can contain, but must not ["xlabel", "ylabel", ["data label0", ...]] third item can also be an interger stating the iteration start as label :type lables: ??? :param ylim: limit the y axis to these values, e.g. (0, 100) :type ylim: ??? :param bars: list of colors we should use for the bars, refer to PDF_CHART_COLORS as an example :type bars: ??? :param size: size in pixels, e.g. (8 * cm, 4 * cm) or pixels, e.g. (400, 200) :type size: ??? :param title: title of bar chart :type title: string :param stacked: weather to do a stacked bar plot or std column plot :type stacked: ??? """ title = kw.pop('title', None) stacked = kw.pop('stacked', False) bars = kw.pop('bars', PDF_CHART_COLORS) size = kw.pop('plotSize', (18 * cm, 9 * cm)) ylim = kw.pop('ylim', None) # Create the drawing drawing = Drawing(size[0], size[1]) # Create the Chart chart = VerticalBarChart() for key, val in list(kw.items()): setattr(chart, key, val) if title is not None: drawing.add(String(20, size[1] - 20, title), name='title') chart.y -= 10 chart.width = drawing.width - 20 chart.height = drawing.height - 40 chart.data = data max_y = 0 min_y = maxsize for i in range(len(data)): chart.bars[i].fillColor = HexColor(bars[i % len(bars)]) max_y = max(data[i] + [max_y]) min_y = min(data[i] + [min_y]) chart.valueAxis.valueMax = max_y * 1.1 chart.valueAxis.valueMin = min_y * 0.9 if ylim is not None: chart.valueAxis.valueMin = ylim[0] chart.valueAxis.valueMax = ylim[1] if len(data) > 1: chart.barSpacing = 2 if labels is not None: if len(labels) > 0: xlabel = Label() xlabel._text = labels[0] # pylint: disable=W0212 xlabel.textAnchor = 'middle' xlabel.x = drawing.width / 2 xlabel.y = 0 chart.y += 15 drawing.add(xlabel, name="xlabel") if len(labels) > 1: ylabel = Label() ylabel._text = labels[1] # pylint: disable=W0212 xlabel.textAnchor = 'middle' ylabel.angle = 90 ylabel.x = 0 ylabel.y = drawing.height / 2 chart.x += 10 drawing.add(ylabel, name="ylabel") if len(labels) > 2: if len(labels[2]) == max([len(x) for x in data]): chart.categoryAxis.categoryNames = labels[2] chart.categoryAxis.labels.angle = 30 elif type(labels[2]) == int: chart.categoryAxis.categoryNames = range(labels[2], max([len(x) for x in data]) + labels[2]) if stacked: chart.categoryAxis.style = 'stacked' drawing.add(chart, name='chart') return drawing
def line_chart(data, labels, **kw): """ :param data: contains a three dimensional array of values (list of lists of points) or just a list of datapoint lists (it will be auto-transposed to start at 0) :type data: list :param labels: can contain, but must not ["xlabel", "ylabel", ["data label0", ...]] third item can also be an interger stating the iteration start as label when of same size as data, then a legend is added instead :type lables: ??? :param xlim: limit the x axis to these values, e.g. (0, 100) :type xlim: Tuple(Number, Number) :param ylim: limit the y axis to these values, e.g. (0, 50) :type ylim: Tuple(Number, Number) :param size: size in pixels, e.g. (18*cm, 9*cm) :type size: Tuple(Number, Number) :param title: title of bar chart :type title: string :param lines: list of colors we should use to paint lines :type lines: list[???] :param markers: list of markers we should use to draw markers :type markers: list[`PDF_LINE_MARKERS`,...] :param scatter: weather to do a scatter plot or line chart :type scatter: boolean """ # Get all arguments from the keywordargs title = kw.pop('title', None) scatter = kw.pop('scatter', False) size = kw.pop('plotSize', (18 * cm, 9 * cm)) lines = kw.pop('lines', PDF_CHART_COLORS) markers = kw.pop('markers', PDF_LINE_MARKERS) xlim = kw.pop('xlim', None) ylim = kw.pop('ylim', None) drawing = Drawing(size[0], size[1]) chart = None if(scatter): chart = ScatterPlot() else: chart = LinePlot() for key, val in list(kw.items()): setattr(chart, key, val) if title is not None: drawing.add(String(20, size[1] - 10, title), name='title') chart.y -= 10 chart.width = drawing.width - 20 chart.height = drawing.height - 40 chart.x = 10 chart.y = 10 chart.data = data if type(data[0][0]) in (tuple, list) else [list(zip(list(range(len(i))), i)) for i in data] max_y = 0 min_y = maxsize for i in range(len(data)): chart.lines[i].strokeColor = HexColor(lines[i % len(lines)]) if markers is not None: chart.lines[i].symbol = makeMarker(markers[i % len(markers)]) chart.lines[i].symbol.size = 3 max_y = max([k[1] for k in chart.data[i]] + [max_y]) min_y = min([k[1] for k in chart.data[i]] + [min_y]) chart.yValueAxis.valueMax = max_y * 1.1 chart.yValueAxis.valueMin = min_y * 0.9 chart.xValueAxis.visibleGrid = True chart.yValueAxis.visibleGrid = True if xlim is not None: chart.xValueAxis.valueMin = xlim[0] chart.xValueAxis.valueMax = xlim[1] if ylim is not None: chart.yValueAxis.valueMin = ylim[0] chart.yValueAxis.valueMax = ylim[1] if scatter: chart.xLabel = '' chart.yLabel = '' chart.y -= 10 chart.lineLabelFormat = None if labels is not None: if len(labels) > 0: xlabel = Label() xlabel._text = labels[0] # pylint: disable=W0212 xlabel.textAnchor = 'middle' xlabel.x = drawing.width / 2 xlabel.y = 5 chart.y += 15 drawing.add(xlabel, name="xlabel") if len(labels) > 1: ylabel = Label() ylabel._text = labels[1] # pylint: disable=W0212 xlabel.textAnchor = 'middle' ylabel.angle = 90 ylabel.x = 0 ylabel.y = drawing.height / 2 chart.x += 12 drawing.add(ylabel, name="ylabel") if len(labels) > 2: # when labels are of same size as max nr of data point, use as x axis labels if len(labels[2]) == max([len(x) for x in data]): chart.categoryAxis.categoryNames = labels[2] # pylint: disable=E1101 chart.xValueAxis.labels.angle = 30 # pylint: disable=E1101 # otherwise when integer use the counter elif type(labels[2]) == int: temp = range(labels[2], max([len(x) for x in data]) + labels[2]) chart.categoryAxis.categoryNames = temp # pylint: disable=E1101 # or we could add a legend when of same size as data elif len(labels[2]) == len(data): legend = Legend() chart.height -= 8 chart.y += 8 xlabel.y += 8 legend.boxAnchor = 'sw' legend.x = chart.x + 8 legend.y = -2 legend.columnMaximum = 1 legend.deltax = 50 legend.deltay = 0 legend.dx = 10 legend.dy = 1.5 legend.fontSize = 7 legend.alignment = 'right' legend.dxTextSpace = 5 legend.colorNamePairs = [(HexColor(lines[i]), labels[2][i]) for i in range(len(chart.data))] legend.strokeWidth = 0 drawing.add(legend, name='legend') drawing.add(chart, name='chart') return drawing
def draw(self): g = Group() #box g.add(Rect(self.x,self.y,len(self.xlabels)*self.gridDivWidth,len(self.ylabels)*self.gridDivWidth, strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=None)) #internal gridding for f in range (1,len(self.ylabels)): #horizontal g.add(Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1 = self.x, y1 = self.y+f*self.gridDivWidth, x2 = self.x+len(self.xlabels)*self.gridDivWidth, y2 = self.y+f*self.gridDivWidth)) for f in range (1,len(self.xlabels)): #vertical g.add(Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1 = self.x+f*self.gridDivWidth, y1 = self.y, x2 = self.x+f*self.gridDivWidth, y2 = self.y+len(self.ylabels)*self.gridDivWidth)) # draw the 'dot' g.add(Circle(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=self.dotColor, cx = self.x+(self.dotXPosition*self.gridDivWidth), cy = self.y+(self.dotYPosition*self.gridDivWidth), r = self.dotDiameter/2.0)) #used for centering y-labels (below) ascent=getFont(self.labelFontName).face.ascent if ascent==0: ascent=0.718 # default (from helvetica) ascent=ascent*self.labelFontSize # normalize #do y-labels if self.ylabels != None: for f in range (len(self.ylabels)-1,-1,-1): if self.ylabels[f]!= None: g.add(String(strokeColor=self.gridColor, text = self.ylabels[f], fontName = self.labelFontName, fontSize = self.labelFontSize, fillColor=_PCMYK_black, x = self.x-self.labelOffset, y = self.y+(f*self.gridDivWidth+(self.gridDivWidth-ascent)/2.0), textAnchor = 'end')) #do x-labels if self.xlabels != None: for f in range (0,len(self.xlabels)): if self.xlabels[f]!= None: l=Label() l.x=self.x+(f*self.gridDivWidth)+(self.gridDivWidth+ascent)/2.0 l.y=self.y+(len(self.ylabels)*self.gridDivWidth)+self.labelOffset l.angle=90 l.textAnchor='start' l.fontName = self.labelFontName l.fontSize = self.labelFontSize l.fillColor = _PCMYK_black l.setText(self.xlabels[f]) l.boxAnchor = 'sw' l.draw() g.add(l) return g
def draw(self): g = Group() #box g.add( Rect(self.x, self.y, len(self.xlabels) * self.gridDivWidth, len(self.ylabels) * self.gridDivWidth, strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=None)) #internal gridding for f in range(1, len(self.ylabels)): #horizontal g.add( Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1=self.x, y1=self.y + f * self.gridDivWidth, x2=self.x + len(self.xlabels) * self.gridDivWidth, y2=self.y + f * self.gridDivWidth)) for f in range(1, len(self.xlabels)): #vertical g.add( Line(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, x1=self.x + f * self.gridDivWidth, y1=self.y, x2=self.x + f * self.gridDivWidth, y2=self.y + len(self.ylabels) * self.gridDivWidth)) # draw the 'dot' g.add( Circle(strokeColor=self.gridColor, strokeWidth=self.strokeWidth, fillColor=self.dotColor, cx=self.x + (self.dotXPosition * self.gridDivWidth), cy=self.y + (self.dotYPosition * self.gridDivWidth), r=self.dotDiameter / 2.0)) #used for centering y-labels (below) ascent = getFont(self.labelFontName).face.ascent if ascent == 0: ascent = 0.718 # default (from helvetica) ascent = ascent * self.labelFontSize # normalize #do y-labels if self.ylabels != None: for f in range(len(self.ylabels) - 1, -1, -1): if self.ylabels[f] != None: g.add( String(strokeColor=self.gridColor, text=self.ylabels[f], fontName=self.labelFontName, fontSize=self.labelFontSize, fillColor=_PCMYK_black, x=self.x - self.labelOffset, y=self.y + (f * self.gridDivWidth + (self.gridDivWidth - ascent) / 2.0), textAnchor='end')) #do x-labels if self.xlabels != None: for f in range(0, len(self.xlabels)): if self.xlabels[f] != None: l = Label() l.x = self.x + (f * self.gridDivWidth ) + (self.gridDivWidth + ascent) / 2.0 l.y = self.y + (len(self.ylabels) * self.gridDivWidth) + self.labelOffset l.angle = 90 l.textAnchor = 'start' l.fontName = self.labelFontName l.fontSize = self.labelFontSize l.fillColor = _PCMYK_black l.setText(self.xlabels[f]) l.boxAnchor = 'sw' l.draw() g.add(l) return g
lp.yValueAxis.valueMin = 0 lp.yValueAxis.valueMax = 500 lp.yValueAxis.valueStep = 100 drawing.add(lp) title = Label() #若需要显示中文,需要先注册一个中文字体 pdfmetrics.registerFont(ttfonts.TTFont("haha", "simsun.ttc")) title.fontName = "haha" title.fontSize = 12 title_text = '你好' #title_text = "abc" title._text = title_text title.x = 250 title.y = 280 title.textAnchor = 'middle' drawing.add(title) Xlabel = Label() Xlabel._text = 'x' Xlabel.fontSize = 12 Xlabel.x = 480 Xlabel.y = 30 Xlabel.textAnchor = 'middle' drawing.add(Xlabel) Ylabel = Label() Ylabel._text = "y" Ylabel.fontSize = 12 Ylabel.x = 40 Ylabel.y = 295