def draw(self): g = shapes.Group() for attr, info in filter(lambda x: x[1]["visible"], self.render_info.items()): if hasattr(self, attr): val = getattr(self, attr) if val is not None: fs = info["font_size"] * self.baseheight rel = info["offset_relative"] y = self.y if rel == "self": x = self.x elif rel == "edge" and self.parent: x = self.x - (self.x - self.parent.x) * 0.5 dx = info["x_offset"] * fs dy = info["y_offset"] * fs label = shapes.String(x + dx, y + dy, info["format"] % val, fontName=info["font"], fontSize=fs, fillColor=info["font_color"], textAnchor=info["text_anchor"]) g.add(label) if self.parent: pl = shapes.PolyLine([ self.x, self.y, self.parent.x, self.y, self.parent.x, self.parent.y ], strokeWidth=max(self.calc_strokewidth(), 1.0), strokeColor=self.stroke_color, fillColor=None) g.add(pl) ## thicken strongly supported branches if self.thicken: pl = shapes.PolyLine( [ self.x, self.y, self.parent.x - self.parent.calc_strokewidth() / 2.0, self.y ], strokeWidth=max(self.calc_strokewidth(), 1.0) * 4, strokeColor=self.stroke_color, fillColor=None) g.add(pl) for d in self.drawables: g.add(d) return g
def testPolygon(self): points = [0, 0, 10, 30, 25, 15] s = shapes.Polygon(points) assert s.getBounds() == (0, 0, 25, 30) s = shapes.PolyLine(points) assert s.getBounds() == (0, 0, 25, 30)
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # tickbox specific bits box = shapes.Rect(self.x + 1, self.y + 1, s - 2, s - 2, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=2) g.add(box) tickLine = shapes.PolyLine(points=[ self.x + (s * 0.15), self.y + (s * 0.35), self.x + (s * 0.35), self.y + (s * 0.15), self.x + (s * 0.35), self.y + (s * 0.15), self.x + (s * 0.85), self.y + (s * 0.85) ], fillColor=self.tickColor, strokeColor=self.tickColor, strokeWidth=self.tickwidth) g.add(tickLine) return g
def drawField(self, X, Y, width, lineNum, flip=1, color=blue): with open("field.json", "r") as inputfile: iso = json.loads(inputfile.read()) preparedCurves = [] for curve in iso: x = curve[0] y = curve[1] scale = width / (y[-1] - y[0]) xp = x[::-1] + x yp = map(lambda x: -x, y[::-1]) + y x0 = xp[0] y0 = yp[0] xpp = map(lambda x: flip * scale * (x - x0) + X, xp) ypp = map(lambda x: scale * (x - y0) + Y, yp) preparedCurves += [[xpp, ypp]] xp = preparedCurves[lineNum][0] yp = preparedCurves[lineNum][1] coords = [] for x, y in zip(xp, yp): coords += [x * mm, y * mm] p = shapes.PolyLine(coords) p.strokeColor = color p.strokeWidth = self.strokeWidth return p
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # SmileyFace specific bits g.add( shapes.Circle(cx=self.x + (s / 2), cy=self.y + (s / 2), r=s / 2, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=max(s / 38., self.strokeWidth))) for i in (1, 2): g.add( shapes.Ellipse(self.x + (s / 3) * i, self.y + (s / 3) * 2, s / 30, s / 10, fillColor=self.strokeColor, strokeColor=self.strokeColor, strokeWidth=max(s / 38., self.strokeWidth))) # calculate a pointslist for the mouth # THIS IS A HACK! - don't use if there is a 'shapes.Arc' centerx = self.x + (s / 2) centery = self.y + (s / 2) radius = s / 3 yradius = radius xradius = radius startangledegrees = 200 endangledegrees = 340 degreedelta = 1 pointslist = [] a = pointslist.append from math import sin, cos, pi degreestoradians = pi / 180.0 radiansdelta = degreedelta * degreestoradians startangle = startangledegrees * degreestoradians endangle = endangledegrees * degreestoradians while endangle < startangle: endangle = endangle + 2 * pi angle = startangle while angle < endangle: x = centerx + cos(angle) * radius y = centery + sin(angle) * yradius a(x) a(y) angle = angle + radiansdelta # make the mouth smile = shapes.PolyLine(pointslist, fillColor=self.strokeColor, strokeColor=self.strokeColor, strokeWidth=max(s / 38., self.strokeWidth)) g.add(smile) return g
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() strokeColor = self.strokeColor # not=allowed specific bits outerCircle = shapes.Circle(cx=(self.x + (s / 2)), cy=(self.y + (s / 2)), r=(s / 2) - (s / 10), fillColor=self.fillColor, strokeColor=strokeColor, strokeWidth=s / 10.) g.add(outerCircle) centerx = self.x + s centery = self.y + (s / 2) - (s / 6) radius = s - (s / 6) yradius = radius / 2 xradius = radius / 2 startangledegrees = 100 endangledegrees = -80 degreedelta = 90 pointslist = [] a = pointslist.append from math import sin, cos, pi degreestoradians = pi / 180.0 radiansdelta = degreedelta * degreestoradians startangle = startangledegrees * degreestoradians endangle = endangledegrees * degreestoradians while endangle < startangle: endangle = endangle + 2 * pi angle = startangle while angle < endangle: x = centerx + cos(angle) * radius y = centery + sin(angle) * yradius a(x) a(y) angle = angle + radiansdelta crossbar = shapes.PolyLine(pointslist, fillColor=strokeColor, strokeColor=strokeColor, strokeWidth=s / 10.) g.add(crossbar) return g
(2007, 12, 107.3, 115.3, 99.3), (2008, 1, 105.2, 114.2, 96.2), (2008, 2, 104.1, 114.1, 94.1), (2008, 3, 99.9, 110.9, 88.9), (2008, 4, 94.8, 106.8, 82.8), (2008, 5, 91.2, 104.2, 78.2), ] drawing = rgs.Drawing(200, 150) predict = [row[2] - 40 for row in data] # 使用列表推导获得一列的值 high = [row[3] - 40 for row in data] low = [row[4] - 40 for row in data] times = [200 * ((row[0] + row[1] / 12.0) - 2007) - 110 for row in data] drawing.add(rgs.PolyLine(list(zip(times, predict)), strokeColor=colors.blue)) # 添加折线 drawing.add(rgs.PolyLine(list(zip(times, high)), strokeColor=colors.red)) drawing.add(rgs.PolyLine(list(zip(times, low)), strokeColor=colors.green)) drawing.add(rgs.String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red)) renderPDF.drawToFile(drawing, 'report.pdf', 'Sunspots') # ### reportlab.graphics.shapes.PolyLine # 用于绘制折线; # PolyLine对象的第一个参数是一个坐标列表,其中每对(x, y)坐标都指定了折线上的一个点; # # ### 编写原型的作用 # - 了解如何使用ReportLab进行基本绘图 # - 确认如何提取数据并展现;
def _draw_polyline(self, coords, color=blue): mmcoords = map(lambda x: x * mm, coords) p = shapes.PolyLine(mmcoords) p.strokeColor = color p.strokeWidth = self.strokeWidth return p