def generateCircle(self, x, y, r, strokeWidth, stroke, fill, name): circle = Circle() circle.center = (x, y) circle.radius = r circle.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': fill } circle.label = name return circle
def test_circle_with_stroke(self): r = 5 cx = 10 cy = 20 stroke_half_width = 1.0 circle = Circle(r=str(r), cx=str(cx), cy=str(cy)) circle.style = Style("stroke-width:{};stroke:red".format(stroke_half_width * 2)) self.assert_bounding_box_is_equal(circle, (cx - (r + stroke_half_width), cx + (r + stroke_half_width)), (cy - (r + stroke_half_width), cy + (r + stroke_half_width)))
def test_circle_with_stroke_scaled(self): r = 5 cx = 10 cy = 20 scale_x = 2 scale_y = 3 stroke_half_width = 1.0 circle = Circle(r=str(r), cx=str(cx), cy=str(cy)) circle.style = Style("stroke-width:{};stroke:red".format(stroke_half_width * 2)) circle.transform = Transform(scale=(scale_x, scale_y)) self.assert_bounding_box_is_equal(circle, (scale_x * (cx - (r + stroke_half_width)), scale_x * (cx + (r + stroke_half_width))), (scale_y * (cy - (r + stroke_half_width)), scale_y * (cy + (r + stroke_half_width))))
def render_pie(self, keys, values, pie_abs=False): """Draw pie chart""" pie_radius = self.options.pie_radius # Iterate all values to draw the different slices color = 0 x = float(self.width) / 2 y = float(self.height) / 2 # Create the shadow first (if it should be created): if self.blur: shadow = Circle(cx=str(x), cy=str(y)) shadow.set('r', str(pie_radius)) shadow.style = self.blur + inkex.Style(fill='#000000') yield shadow # Add a grey background circle with a light stroke background = Circle(cx=str(x), cy=str(y)) background.set("r", str(pie_radius)) background.set("style", "stroke:#ececec;fill:#f9f9f9") yield background # create value sum in order to divide the slices try: valuesum = sum(values) except ValueError: valuesum = 0 if pie_abs: valuesum = 100 # Set an offsetangle offset = 0 # Draw single slices for cnt, value in enumerate(values): # Calculate the PI-angles for start and end angle = (2 * 3.141592) / valuesum * float(value) start = offset end = offset + angle # proper overlapping if self.options.segment_overlap: if cnt != len(values) - 1: end += 0.09 # add a 5° overlap if cnt == 0: start -= 0.09 # let the first element overlap into the other direction # then add the slice pieslice = inkex.PathElement() pieslice.set('sodipodi:type', 'arc') pieslice.set('sodipodi:cx', x) pieslice.set('sodipodi:cy', y) pieslice.set('sodipodi:rx', pie_radius) pieslice.set('sodipodi:ry', pie_radius) pieslice.set('sodipodi:start', start) pieslice.set('sodipodi:end', end) pieslice.set( "style", "fill:" + self.get_color() + ";stroke:none;fill-opacity:1") ang = angle / 2 + offset # If text is given, draw short paths and add the text if keys: elem = inkex.PathElement() elem.path = [ Move( (self.width / 2) + pie_radius * math.cos(ang), (self.height / 2) + pie_radius * math.sin(ang), ), line( (self.options.text_offset - 2) * math.cos(ang), (self.options.text_offset - 2) * math.sin(ang), ), ] elem.style = { 'fill': 'none', 'stroke': self.options.font_color, 'stroke-width': self.options.stroke_width, 'stroke-linecap': 'butt', } yield elem label = keys[cnt] if self.options.show_values: label += ' ({}{})'.format(str(value), ('', '%')[pie_abs]) # check if it is right or left of the Pie anchor = 'start' if math.cos(ang) > 0 else 'end' text = self.draw_text(label, anchor=anchor) off = pie_radius + self.options.text_offset text.set("x", (self.width / 2) + off * math.cos(ang)) text.set("y", (self.height / 2) + off * math.sin(ang) + self.fontoff) yield text # increase the rotation-offset and the colorcycle-position offset = offset + angle color = (color + 1) % 8 # append the objects to the extension-layer yield pieslice yield self.draw_header(self.width / 2 - pie_radius)