def draw_rect(x, y, w, h, stroke_width, fill, name): elem = Rectangle(x=str(x), y=str(y), width=str(w), height=str(h)) elem.style = { 'stroke': '#000000', 'stroke-width': str(stroke_width), 'fill': fill } elem.set('inkscape:label', name) return elem
def generateRectangle(self, x, y, w, h, strokeWidth, stroke, fill, name): rect = Rectangle(x=str(x), y=str(y), width=str(w), height=str(h)) rect.style = { 'stroke': stroke, 'stroke-width': strokeWidth, 'fill': fill } rect.label = name return rect
def test_regular_rectangle_with_stroke(self): x, y = 10, 20 w, h = 7, 20 stroke_half_width = 1 rect = Rectangle(width=str(w), height=str(h), x=str(x), y=str(y)) rect.style = Style("stroke-width:{};stroke:red".format(stroke_half_width * 2)) self.assert_bounding_box_is_equal(rect, (x - stroke_half_width, x + w + stroke_half_width), (y - stroke_half_width, y + h + stroke_half_width))
def draw_rectangle(self, x, y, width, height): """Draw a rectangle bar with optional shadow""" if self.blur: shadow = Rectangle(x=str(x + 1), y=str(y + 1), width=str(width), height=str(height)) shadow.style = self.blur yield shadow rect = Rectangle(x=str(x), y=str(y), width=str(width), height=str(height)) rect.set("style", "fill:" + self.get_color()) yield rect
def test_regular_rectangle_with_stroke_scaled(self): x, y = 10, 20 w, h = 7, 20 stroke_half_width = 1 scale_x = 2 scale_y = 3 rect = Rectangle(width=str(w), height=str(h), x=str(x), y=str(y)) rect.style = Style("stroke-width:{};stroke:red".format(stroke_half_width * 2)) rect.transform = Transform(scale=(scale_x, scale_y)) self.assert_bounding_box_is_equal(rect, (scale_x * (x - stroke_half_width), scale_x * (x + w + stroke_half_width)), (scale_y * (y - stroke_half_width), scale_y * (y + h + stroke_half_width)))
def render_stbar(self, keys, values): """Draw stacked bar chart""" # Iterate over all values to draw the different slices color = 0 # create value sum in order to divide the bars try: valuesum = sum(values) except ValueError: valuesum = 0.0 # Init offset offset = 0 width = self.options.bar_width height = self.options.bar_height x = self.width / 2 y = self.height / 2 if self.blur: if self.options.rotate: width, height = height, width shy = y else: shy = str(y - self.options.bar_height) # Create rectangle element shadow = Rectangle( x=str(x), y=str(shy), width=str(width), height=str(height), ) # Set shadow blur (connect to filter object in xml path) shadow.style = self.blur yield shadow # Draw Single bars for cnt, value in enumerate(values): # Calculate the individual heights normalized on 100units normedvalue = (self.options.bar_height / valuesum) * float(value) # Create rectangle element rect = Rectangle() # Set chart position to center of document. if not self.options.rotate: rect.set('x', str(self.width / 2)) rect.set('y', str(self.height / 2 - offset - normedvalue)) rect.set("width", str(self.options.bar_width)) rect.set("height", str(normedvalue)) else: rect.set('x', str(self.width / 2 + offset)) rect.set('y', str(self.height / 2)) rect.set("height", str(self.options.bar_width)) rect.set("width", str(normedvalue)) rect.set("style", "fill:" + self.get_color()) # If text is given, draw short paths and add the text # TODO: apply overlap workaround for visible gaps in between if keys: if not self.options.rotate: x1 = (self.width + self.options.bar_width) / 2 y1 = y - offset - (normedvalue / 2) x2 = self.options.bar_width / 2 + self.options.text_offset y2 = 0 txt = self.width / 2 + self.options.bar_width + self.options.text_offset + 1 tyt = y - offset + self.fontoff - (normedvalue / 2) else: x1 = x + offset + normedvalue / 2 y1 = y + self.options.bar_width / 2 x2 = 0 y2 = self.options.bar_width / 2 + (self.options.font_size \ * cnt) + self.options.text_offset txt = x + offset + normedvalue / 2 - self.fontoff tyt = (y) + self.options.bar_width + (self.options.font_size \ * (cnt + 1)) + self.options.text_offset elem = inkex.PathElement() elem.path = [Move(x1, y1), line(x2, y2)] elem.style = { 'fill': 'none', 'stroke': self.options.font_color, 'stroke-width': self.options.stroke_width, 'stroke-linecap': 'butt', } yield elem yield self.draw_text(keys[cnt], x=str(txt), y=str(tyt)) # Increase Offset and Color offset = offset + normedvalue color = (color + 1) % 8 # Draw rectangle yield rect yield self.draw_header(self.width / 2 + offset + normedvalue)