def draw_coluor_bars(self, cx, cy, rotate, name, parent, bbox): group = parent.add(inkex.Group(id=name)) group.transform = inkex.Transform(translate=(cx, cy)) * inkex.Transform(rotate=rotate) loc = 0 if bbox: loc = min(self.mark_size / 3, max(bbox.width, bbox.height) / 45) for bar in [{'c': '*', 'stroke': '#000', 'x': 0, 'y': -(loc + 1)}, {'c': 'r', 'stroke': '#0FF', 'x': 0, 'y': 0}, {'c': 'g', 'stroke': '#F0F', 'x': (loc * 11) + 1, 'y': -(loc + 1)}, {'c': 'b', 'stroke': '#FF0', 'x': (loc * 11) + 1, 'y': 0} ]: i = 0 while i <= 1: color = inkex.Color('white') if bar['c'] == 'r' or bar['c'] == '*': color.red = 255 * i if bar['c'] == 'g' or bar['c'] == '*': color.green = 255 * i if bar['c'] == 'b' or bar['c'] == '*': color.blue = 255 * i r_att = {'fill': str(color), 'stroke': bar['stroke'], 'stroke-width': loc/8, 'x': str((loc * i * 10) + bar['x']), 'y': str(bar['y']), 'width': loc, 'height': loc} rect = Rectangle() for att, value in r_att.items(): rect.set(att, value) group.add(rect) i += 0.1
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 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 generate(self): """Generate the actual svg from the coding""" string = self.encode(self.text) if string == 'ERROR': return name = self.get_id('barcode') # use an svg group element to contain the barcode barcode = Group() barcode.set('id', name) barcode.set('style', 'fill: black;') barcode.transform.add_translate(self.pos_x, self.pos_y) if self.scale: barcode.transform.add_scale(self.scale) bar_id = 1 bar_offset = 0 tops = set() for datum in self.graphical_array(string): # Datum 0 tells us what style of bar is to come next style = self.get_style(int(datum[0])) # Datum 1 tells us what width in units, # style tells us how wide a unit is width = int(datum[1]) * int(style['width']) if style['write']: tops.add(style['top']) rect = Rectangle() rect.set('x', str(bar_offset)) rect.set('y', str(style['top'])) if self.pos_text == TEXT_POS_TOP: rect.set('y', str(style['top'] + self.font_size)) rect.set('id', "{}_bar{:d}".format(name, bar_id)) rect.set('width', str(width)) rect.set('height', str(style['height'])) barcode.append(rect) bar_offset += width bar_id += 1 for extra in self._extra: if extra is not None: barcode.append(extra) bar_width = bar_offset # Add text at the bottom of the barcode text = TextElement() text.set('x', str(int(bar_width / 2))) text.set('y', str(min(tops) + self.font_size - 1)) if self.pos_text == TEXT_POS_BOTTOM: text.set('y', str(self.height + max(tops) + self.font_size)) text.set('style', TEXT_TEMPLATE % self.font_size) text.set('xml:space', 'preserve') text.set('id', '{}_text'.format(name)) text.text = str(self.text) barcode.append(text) return barcode
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)