def draw_xy(self, root_element, x, y, *datapoint, **kwargs): ra = datapoint[2] dec = datapoint[3] link = kwargs.get('link', '') DEC_SIZE = 7 RA_SIZE = 12 # following is wrong: dec_pointer_dx = math.cos(math.pi * dec / 180) * DEC_SIZE dec_pointer_dy = -math.sin(math.pi * dec / 180) * DEC_SIZE # following is ok: ra_pointer_dx = math.sin(ra * math.pi / 12) * RA_SIZE ra_pointer_dy = -math.cos(ra * math.pi / 12) * RA_SIZE if link: root_element = ET.SubElement(root_element, 'a') root_element.set('xlink:href', link) h1 = ET.SubElement(root_element, 'line') h1.set('x1', '%.2f' % x) h1.set('y1', '%.2f' % y) h1.set('x2', '%.2f' % (x + dec_pointer_dx)) h1.set('y2', '%.2f' % (y + dec_pointer_dy)) h1.set('stroke-width', '3') h2 = ET.SubElement(root_element, 'line') h2.set('x1', '%.2f' % x) h2.set('y1', '%.2f' % y) h2.set('x2', '%.2f' % (x + ra_pointer_dx)) h2.set('y2', '%.2f' % (y + ra_pointer_dy)) if 'color' in kwargs: h1.set('stroke', kwargs['color']) h2.set('stroke', kwargs['color'])
def draw_xy(self, root_element, x, y, *datapoint, **kwargs): minx = kwargs.get('minx', x - 7) maxx = kwargs.get('maxx', x + 7) l1 = ET.SubElement(root_element, 'line') l1.set('x1', '%.2f' % maxx) l1.set('x2', '%.2f' % minx) l1.set('y1', '%.2f' % y) l1.set('y2', '%.2f' % y) # Add 'serifs' to error bar. l2 = ET.SubElement(root_element, 'line') l2.set('x1', '%.2f' % maxx) l2.set('x2', '%.2f' % maxx) l2.set('y1', '%.2f' % (y - 3)) l2.set('y2', '%.2f' % (y + 3)) l3 = ET.SubElement(root_element, 'line') l3.set('x1', '%.2f' % minx) l3.set('x2', '%.2f' % minx) l3.set('y1', '%.2f' % (y + 3)) l3.set('y2', '%.2f' % (y - 3)) if 'color' in kwargs: l1.set('stroke', kwargs['color']) l2.set('stroke', kwargs['color']) l3.set('stroke', kwargs['color'])
def draw_xy(self, root_element, x, y, *datapoint, **kwargs): link = kwargs.get('link', self.link) if link: root_element = ET.SubElement(root_element, 'a') root_element.set('xlink:href', link) p = ET.SubElement(root_element, 'circle') p.set('cx', '%.2f' % x) p.set('cy', '%.2f' % y) p.set('r', '%.2f' % self.size) p.set('fill', 'none') p.set('stroke', 'lime')
def draw(self, root_element, x_transform, y_transform): legend_height = len(self.entries) * 18 w = self.svg_bbox[2] - self.svg_bbox[0] - 2 * AXIS_SIZE legend_width = w / 2 - 20 # Calculate the SVG coordinates of the top left point of the rectangle # that encloses the legend: if self.position == TOPLEFT: x = self.svg_bbox[0] + AXIS_SIZE + 10 y = self.svg_bbox[1] + AXIS_SIZE + 10 elif self.position == TOPRIGHT: x = self.svg_bbox[0] + AXIS_SIZE + w / 2 + 10 y = self.svg_bbox[1] + AXIS_SIZE + 10 elif self.position == BOTTOMLEFT: x = self.svg_bbox[0] + AXIS_SIZE + 10 y = self.svg_bbox[3] - AXIS_SIZE - legend_height - 10 elif self.position == BOTTOMRIGHT: x = self.svg_bbox[0] + AXIS_SIZE + w / 2 + 10 y = self.svg_bbox[3] - AXIS_SIZE - legend_height - 10 elif self.position == UNDER_PLOT: x = self.svg_bbox[0] + AXIS_SIZE y = self.svg_bbox[3] legend_width = self.svg_bbox[2] - self.svg_bbox[0] - 2 * AXIS_SIZE if self.draw_box: legend = ET.SubElement(root_element, 'rect') legend.set('stroke', 'black') legend.set('fill', 'white') legend.set('fill-opacity', '0.8') legend.set('x', '%.2f' % x) legend.set('y', '%.2f' % y) legend.set('width', '%.2f' % legend_width) legend.set('height', '%.2f' % legend_height) # symbol + color + text for i, entry in enumerate(self.entries): symbols, text, color = entry # Some of the code below assumes that the font-size for entries in # the Legends is 18 (I futzed around a bit to find the off-sets # that look good). sx = x + 20 sy = y + 9 + i * 18 for symbol in symbols: symbol.draw_xy(root_element, sx, sy, color=color) t = ET.SubElement(root_element, 'text') t.set('font-size', str(LEGEND_FONT_SIZE)) t.set('x', '%.2f' % (sx + 20)) t.set('y', '%.2f' % (sy + LEGEND_FONT_SIZE / 2 - 2)) t.set('text-anchor', 'left') t.text = escape(text)
def draw(self, root_element, x_transform, y_transform): # only draw if the self.x and self.y lie within the data_bbox if (self.data_bbox[0] <= self.x <= self.data_bbox[2]) and \ (self.data_bbox[1] <= self.y <= self.data_bbox[3]): rw = 0.1 * (self.svg_bbox[2] - self.svg_bbox[0]) rh = 0.1 * (self.svg_bbox[3] - self.svg_bbox[1]) l1 = ET.SubElement(root_element, 'line') l1.set('x1', '%.2f' % x_transform(self.x)) l1.set('y1', '%.2f' % (y_transform(self.y) - 0.7 * rh)) l1.set('x2', '%.2f' % x_transform(self.x)) l1.set('y2', '%.2f' % (y_transform(self.y) - 0.3 * rh)) l1.set('stroke', self.color) l1.set('stroke-width', '2') l1 = ET.SubElement(root_element, 'line') l1.set('x1', '%.2f' % x_transform(self.x)) l1.set('y1', '%.2f' % (y_transform(self.y) + 0.3 * rh)) l1.set('x2', '%.2f' % x_transform(self.x)) l1.set('y2', '%.2f' % (y_transform(self.y) + 0.7 * rh)) l1.set('stroke', self.color) l1.set('stroke-width', '2') l2 = ET.SubElement(root_element, 'line') l2.set('x1', '%.2f' % (x_transform(self.x) - 0.7 * rw)) l2.set('y1', '%.2f' % y_transform(self.y)) l2.set('x2', '%.2f' % (x_transform(self.x) - 0.3 * rw)) l2.set('y2', '%.2f' % y_transform(self.y)) l2.set('stroke', self.color) l2.set('stroke-width', '2') l2 = ET.SubElement(root_element, 'line') l2.set('x1', '%.2f' % (x_transform(self.x) + 0.3 * rw)) l2.set('y1', '%.2f' % y_transform(self.y)) l2.set('x2', '%.2f' % (x_transform(self.x) + 0.7 * rw)) l2.set('y2', '%.2f' % y_transform(self.y)) l2.set('stroke', self.color) l2.set('stroke-width', '2') d = ET.SubElement(root_element, 'rect') d.set('x', '%.2f' % (x_transform(self.x) - 0.5 * rw)) d.set('y', '%.2f' % (y_transform(self.y) - 0.5 * rh)) d.set('width', '%.2f' % rw) d.set('height', '%.2f' % rh) d.set('stroke', self.color) d.set('fill', 'none') d.set('stroke-width', '2')
def draw_xy(self, root_element, x, y, *datapoint, **kwargs): size = kwargs.get('size', self.size) link = kwargs.get('link', self.link) if link: root_element = ET.SubElement(root_element, 'a') root_element.set('xlink:href', link) p = ET.SubElement(root_element, 'rect') p.set('x', '%.2f' % (x - size)) p.set('y', '%.2f' % (y - size)) p.set('width', '%.2f' % (2 * size)) p.set('height', '%.2f' % (2 * size)) if 'color' in kwargs: p.set('fill', kwargs['color'])
def draw(self, root_element, x_transform, y_transform, *args, **kwargs): '''Add linked rectangle.''' tmp = ET.SubElement(root_element, 'a') tmp.set('xlink:href', self.link) box = ET.SubElement(tmp, 'rect') box.set('fill', 'white') box.set('stroke', self.color) box.set('fill-opacity', '0.0') x = x_transform(self.bbox[0]) y = y_transform(self.bbox[3]) width = x_transform(self.bbox[2]) - x height = y_transform(self.bbox[1]) - y assert width >= 0 assert height >= 0 box.set('x', '%.2f' % x) box.set('y', '%.2f' % y) box.set('width', '%.2f' % width) box.set('height', '%.2f' % height)
def draw(self, root_element, x_transform, y_transform): if self.data_bbox[1] <= self.y_limit <= self.data_bbox[3]: l = ET.SubElement(root_element, 'line') l.set('y1', '%.2f' % y_transform(self.y_limit)) l.set('y2', '%.2f' % y_transform(self.y_limit)) l.set('x1', '%.2f' % (self.svg_bbox[0] + AXIS_SIZE + DATA_PADDING)) l.set('x2', '%.2f' % (self.svg_bbox[2] - AXIS_SIZE - DATA_PADDING)) l.set('stroke', self.color) if self.line_pattern: l.set('style', 'stroke-dasharray:%s' % self.line_pattern)
def draw_xy(self, root_element, x, y, *datapoint, **kwargs): l = ET.SubElement(root_element, 'line') l.set('x1', '%.2f' % (x - 15)) l.set('x2', '%.2f' % (x + 15)) l.set('y1', '%.2f' % y) l.set('y2', '%.2f' % y) if self.line_pattern: l.set('style', 'stroke-dasharray:%s' % (self.line_pattern)) if 'color' in kwargs: l.set('stroke', kwargs['color'])
def draw(self, root_element, x_transform, y_transform, **kwargs): N_STEPS = 400 interval_length = self.max_value - self.min_value d_value = interval_length / N_STEPS if self.orientation == 'vertical': x1, x2 = x_transform(0), x_transform(1) for i in range(N_STEPS): min_value = self.min_value + i * d_value max_value = min_value + d_value color = self.gradient.get_css_color((min_value + max_value) / 2) rect = ET.SubElement(root_element, 'rect') rect.set('x', '%.2f' % x1) rect.set('width', '%.2f' % (x2 - x1)) y1 = y_transform(min_value) y2 = y_transform(max_value) if y1 > y2: y2, y1 = y1, y2 rect.set('y', '%.2f' % y1) rect.set('height', '%.2f' % (y2 - y1)) rect.set('fill', color) rect.set('stroke', color) else: y1, y2 = y_transform(0), y_transform(1) for i in range(N_STEPS): min_value = self.min_value + i * d_value max_value = min_value + d_value color = self.gradient.get_css_color((min_value + max_value) / 2) rect = ET.SubElement(root_element, 'rect') rect.set('y', '%.2f' % y2) rect.set('height', '%.2f' % (y1 - y2)) x1 = x_transform(min_value) x2 = x_transform(max_value) if x1 > x2: x2, x1 = x1, x2 rect.set('x', '%.2f' % x1) rect.set('width', '%.2f' % (x2 - x1)) rect.set('fill', color) rect.set('stroke', color)
def rdraw(self, root_element, x_transform, y_transform, svg_bbox): width = svg_bbox[2] - svg_bbox[0] height = svg_bbox[1] - svg_bbox[3] assert width > 0 assert height > 0 im = Image.new('RGBA', (width, height), (255, 255, 255, 0)) imdraw = ImageDraw.Draw(im) # above should be hidden (not re-implemented in each subclass) if self.gradient and self.gradient_i is not None: for i, datapoint in enumerate(izip(*self.datapoints)): rgba_color = self.gradient.get_rgba_color( datapoint[self.gradient_i]) for s in self.symbols: s.rdraw(imdraw, x_transform, y_transform, *datapoint, rgba_color=rgba_color) elif self.colors: for i, datapoint in enumerate(izip(*self.datapoints)): for s in self.symbols: rgba_color = svg_color2rgba_color(self.colors[i]) s.rdraw(imdraw, x_transform, y_transform, *datapoint, rgba_color=rgba_color) else: for datapoint in izip(*self.datapoints): rgba_color = svg_color2rgba_color(self.color) for s in self.symbols: s.rdraw(imdraw, x_transform, y_transform, *datapoint, rgba_color=rgba_color) # below should be hidden (not re-implemented in each subclass) tmp = StringIO.StringIO() im.save(tmp, format='png') encoded_png = 'data:image/png;base64,\n' + encodestring(tmp.getvalue()) img = ET.SubElement(root_element, 'image') img.set('xlink:href', encoded_png) img.set('x', '%.2f' % min(svg_bbox[0], svg_bbox[2])) img.set('y', '%.2f' % min(svg_bbox[1], svg_bbox[3])) img.set('width', '%.2f' % width) img.set('height', '%.2f' % height) img.set('preserveAspectRatio', 'none')
def draw(self, root_element, x_transform, y_transform): '''Draw line plot.''' pl = ET.SubElement(root_element, 'polyline') pl.set('stroke', self.color) pl.set('fill', 'none') points = [] for x, y in zip(self.datapoints[0], self.datapoints[1]): points.append("%.2f,%.2f" % (x_transform(x), y_transform(y))) pl.set('points', ' '.join(points)) if self.line_pattern: pl.set('style', 'stroke-dasharray: %s' % self.line_pattern) if self.use_markers: super(LinePlotter, self).draw(root_element, x_transform, y_transform)
def draw_xy(self, root_element, x, y, *datapoint, **kwargs): # only draw if the self.x and self.y lie within the data_bbox W = 10 w = W / 2 H = 10 h = H / 2 d = ET.SubElement(root_element, 'rect') d.set('x', '%.2f' % (x - w)) d.set('y', '%.2f' % (y - h)) d.set('width', '%.2f' % W) d.set('height', '%.2f' % H) d.set('fill', 'none') d.set('stroke-width', '2') if 'color' in kwargs: d.set('stroke', kwargs['color'])
def draw(self, root_element, x_transform, y_transform): # NOTE : the following is a crude way of detecting logarithmic # transforms for the axes. Bit of a hack. try: x_transform(-1) y_transform(-1) except ValueError: msg = 'RasterPlotterMixin does not support logarithmic axes.' raise ValueError(msg) x = x_transform(self.img_bbox[0]) y = y_transform(self.img_bbox[3]) width = x_transform(self.img_bbox[2]) - x height = y_transform(self.img_bbox[1]) - y img = ET.SubElement(root_element, 'image') img.set('xlink:href', self.encoded_png) img.set('x', '%.2f' % x) img.set('y', '%.2f' % y) img.set('width', '%.2f' % width) img.set('height', '%.2f' % height) img.set('preserveAspectRatio', 'none')
def draw(self, root_element, x_transform, y_transform): '''Draw scatter plot.''' if self.links: L = self.links else: L = FakeList('') if self.gradient and self.gradient_i is not None: for i, datapoint in enumerate(izip(*self.datapoints)): for s in self.symbols: s.draw(root_element, x_transform, y_transform, *datapoint, color=self.gradient.get_css_color( datapoint[self.gradient_i]), link=L[i]) elif self.colors: for i, datapoint in enumerate(izip(*self.datapoints)): for s in self.symbols: s.draw(root_element, x_transform, y_transform, *datapoint, color=self.colors[i], link=L[i]) else: root_element = ET.SubElement(root_element, 'g') root_element.set('stroke', self.color) root_element.set('fill', self.color) for i, datapoint in enumerate(izip(*self.datapoints)): for s in self.symbols: s.draw(root_element, x_transform, y_transform, *datapoint, link=L[i])
def rdraw(self, root_element, x_transform, y_transform, svg_bbox): width = svg_bbox[2] - svg_bbox[0] height = svg_bbox[1] - svg_bbox[3] assert width > 0 assert height > 0 im = Image.new('RGBA', (width, height), (255, 255, 255, 0)) imdraw = ImageDraw.Draw(im) rgba_color = svg_color2rgba_color(self.color) # above should be hidden (not re-implemented in each subclass) if len(self.datapoints) > 1: tmp = [] for datapoint in izip(*self.datapoints): x = x_transform(datapoint[0]) y = y_transform(datapoint[1]) tmp.append(x) tmp.append(y) imdraw.line(tmp, fill=rgba_color, width=1) # below should be hidden (not re-implemented in each subclass) tmp = StringIO.StringIO() im.save(tmp, format='png') encoded_png = 'data:image/png;base64,\n' + encodestring(tmp.getvalue()) img = ET.SubElement(root_element, 'image') img.set('xlink:href', encoded_png) img.set('x', '%.2f' % min(svg_bbox[0], svg_bbox[2])) img.set('y', '%.2f' % min(svg_bbox[1], svg_bbox[3])) img.set('width', '%.2f' % width) img.set('height', '%.2f' % height) img.set('preserveAspectRatio', 'none') if self.use_markers: super(LinePlotter, self).rdraw(root_element, x_transform, y_transform, svg_bbox)
def draw_top_axis(root_element, x_offset, y_offset, width, x_transform, interval, use_log, **options): ''' Draw top axis. See the documentation for brp.svg.plotters.axes_procedural.draw_left_axis ''' color = options.get('color', 'black') axes_lines = ET.SubElement(root_element, 'g') axes_lines.set('stroke', color) axes_text = ET.SubElement(root_element, 'g') axes_text.set('fill', color) line = ET.SubElement(axes_lines, 'line') line.set('x1', '%.2f' % (x_offset + AXIS_SIZE)) line.set('y1', '%.2f' % (y_offset + AXIS_SIZE)) line.set('x2', '%.2f' % (x_offset + width - AXIS_SIZE)) line.set('y2', '%.2f' % (y_offset + AXIS_SIZE)) hide_tickmarks = options.get('hide_tickmarks', False) if not hide_tickmarks: tickmarks = get_tickmarks(width, interval, use_log, **options) hide_tickmarklabels = options.get('hide_tickmarklabels', False) for x, size in tickmarks: nx = x_transform(x) tm = ET.SubElement(axes_lines, 'line') tm.set('x1', '%.2f' % (nx)) tm.set('x2', '%.2f' % (nx)) tm.set('y1', '%.2f' % (y_offset + AXIS_SIZE)) tm.set('y2', '%.2f' % (y_offset + AXIS_SIZE + size * 0.7)) if hide_tickmarklabels: continue if size != 10: continue ticklabel = ET.SubElement(axes_text, 'text') ticklabel.set('font-size', str(FONT_SIZE)) ticklabel.set('text-anchor', 'middle') ticklabel.set( 'y', '%.2f' % (y_offset + AXIS_SIZE - TICKMARK_LABEL_SPACING)), ticklabel.set('x', '%.2f' % nx) ticklabel.text = escape(str(x)) hide_label = options.get('hide_label', False) label_link = options.get('label_link', None) if not hide_label: if label_link: axes_text = ET.SubElement(axes_text, 'a') axes_text.set('xlink:href', label_link) lbl = ET.SubElement(axes_text, 'text') lbl.set('font-size', str(FONT_SIZE)) lbl.set('text-anchor', 'middle') lbl.set( 'y', '%.2f' % (y_offset + AXIS_SIZE - TICKMARK_LABEL_SPACING - 2 * FONT_SIZE)), lbl.set('x', '%.2f' % (x_offset + 0.5 * width)) label = options.get('label', 'X LABEL') lbl.text = escape(label)
def draw_left_axis(root_element, x_offset, y_offset, height, y_transform, interval, use_log, **options): ''' Draw left axis. arguments: `root_element` --- ElementTree Element instance, receives the drawing commands. `x_offset` --- The horizontal offset in SVG screen coordinates of the axis. `y_offset` --- The vertical offset in SVG screen coordinates of the axis. `y_transform` --- Transformation function that transforms y data coordinates to y SVG screen coordinates. `interval` --- Interval in y data coordinates that the axis covers. `use_log` --- Boolean, True if axis is to be treated as logarithmic. key word arguments: `tickmarks` --- list of tickmarks. Tickmarks are tuples like: (position, size) with position and size numbers. `label` --- Text that labels this axis. `hide_label` --- Boolean, True if label should be hidden. `hide_tickmarks` --- Boolean, True if tickmarks should be hidden. `hide_tickmarklabels` --- Boolean, True if tickmarks and labels for this axis should be hidden. ''' color = options.get('color', 'black') axes_lines = ET.SubElement(root_element, 'g') axes_lines.set('stroke', color) axes_text = ET.SubElement(root_element, 'g') axes_text.set('fill', color) line = ET.SubElement(axes_lines, 'line') line.set('x1', '%.2f' % (x_offset + AXIS_SIZE)) line.set('y1', '%.2f' % (y_offset + AXIS_SIZE)) line.set('x2', '%.2f' % (x_offset + AXIS_SIZE)) line.set('y2', '%.2f' % (y_offset + height - AXIS_SIZE)) hide_tickmarks = options.get('hide_tickmarks', False) if not hide_tickmarks: tickmarks = get_tickmarks(height, interval, use_log, **options) hide_tickmarklabels = options.get('hide_tickmarklabels', False) for y, size in tickmarks: ny = y_transform(y) tm = ET.SubElement(axes_lines, 'line') tm.set('x1', '%.2f' % (x_offset + AXIS_SIZE)) tm.set('x2', '%.2f' % (x_offset + AXIS_SIZE + size * 0.7)) tm.set('y1', '%.2f' % (ny)) tm.set('y2', '%.2f' % (ny)) tm.set('stroke', color) if hide_tickmarklabels: continue if size != 10: continue ticklabel = ET.SubElement(axes_text, 'text') ticklabel.set('font-size', str(FONT_SIZE)) ticklabel.set('text-anchor', 'middle') ticklabel.set( 'x', '%.2f' % (x_offset + AXIS_SIZE - TICKMARK_LABEL_SPACING)), ticklabel.set('y', '%.2f' % ny) ticklabel.set( 'transform', 'rotate(%d %.2f %.2f)' % (-90, x_offset + AXIS_SIZE - TICKMARK_LABEL_SPACING, ny)) ticklabel.text = escape(str(y)) hide_label = options.get('hide_label', False) label_link = options.get('label_link', None) if not hide_label: if label_link: axes_text = ET.SubElement(axes_text, 'a') axes_text.set('xlink:href', label_link) lbl = ET.SubElement(axes_text, 'text') lbl.set('font-size', str(FONT_SIZE)) lbl.set('text-anchor', 'middle') lbl.set( 'x', '%.2f' % (x_offset + AXIS_SIZE - TICKMARK_LABEL_SPACING - 2 * FONT_SIZE)), lbl.set('y', '%.2f' % (y_offset + 0.5 * height)) lbl.set( 'transform', 'rotate(%d %.2f %.2f)' % (-90, x_offset + AXIS_SIZE - TICKMARK_LABEL_SPACING - 2 * FONT_SIZE, y_offset + 0.5 * height)) label = options.get('label', 'Y LABEL') lbl.text = escape(label)
def draw_right_axis(root_element, x_offset, y_offset, height, y_transform, interval, use_log, **options): ''' Draw right axis. See the documentation for brp.svg.plotters.axes_procedural.draw_left_axis ''' color = options.get('color', 'black') axes_lines = ET.SubElement(root_element, 'g') axes_lines.set('stroke', color) axes_text = ET.SubElement(root_element, 'g') axes_text.set('fill', color) line = ET.SubElement(axes_lines, 'line') line.set('x1', '%.2f' % (x_offset + AXIS_SIZE)) line.set('y1', '%.2f' % (y_offset + AXIS_SIZE)) line.set('x2', '%.2f' % (x_offset + AXIS_SIZE)) line.set('y2', '%.2f' % (y_offset + height - AXIS_SIZE)) hide_tickmarks = options.get('hide_tickmarks', False) if not hide_tickmarks: tickmarks = get_tickmarks(height, interval, use_log, **options) hide_tickmarklabels = options.get('hide_tickmarklabels', False) for y, size in tickmarks: ny = y_transform(y) tm = ET.SubElement(axes_lines, 'line') tm.set('x1', '%.2f' % (x_offset + AXIS_SIZE)) tm.set('x2', '%.2f' % (x_offset + AXIS_SIZE - size * 0.7)) tm.set('y1', '%.2f' % (ny)) tm.set('y2', '%.2f' % (ny)) if hide_tickmarklabels: continue if size != 10: continue ticklabel = ET.SubElement(axes_text, 'text') ticklabel.set('font-size', str(FONT_SIZE)) ticklabel.set('text-anchor', 'middle') ticklabel.set( 'x', '%.2f' % (x_offset + AXIS_SIZE + TICKMARK_LABEL_SPACING + 0.5 * FONT_SIZE)), ticklabel.set('y', '%.2f' % ny) ticklabel.set( 'transform', 'rotate(%d %.2f %.2f)' % (-90, x_offset + AXIS_SIZE + TICKMARK_LABEL_SPACING + 0.5 * FONT_SIZE, ny)) ticklabel.text = escape(str(y)) hide_label = options.get('hide_label', False) label_link = options.get('label_link', None) if not hide_label: if label_link: axes_text = ET.SubElement(axes_text, 'a') axes_text.set('xlink:href', label_link) lbl = ET.SubElement(axes_text, 'text') lbl.set('font-size', str(FONT_SIZE)) lbl.set('text-anchor', 'middle') lbl.set( 'x', '%.2f' % (x_offset + AXIS_SIZE + TICKMARK_LABEL_SPACING + 2.5 * FONT_SIZE)), lbl.set('y', '%.2f' % (y_offset + 0.5 * height)) lbl.set( 'transform', 'rotate(%d %.2f %.2f)' % (-90, x_offset + AXIS_SIZE + TICKMARK_LABEL_SPACING + 2.5 * FONT_SIZE, y_offset + 0.5 * height)) label = options.get('label', 'Y LABEL') lbl.text = escape(label)
def draw(self, root_element, x_transform, y_transform): L = len(self.binned_data) # Still needs the optimization for line segments that extend each other # (those need to be merged). points = [] for i in range(L): if i == 0: x1, x2, y = self.binned_data[0] if self.orientation == 'horizontal': points.append('%.2f,%.2f' % (x_transform(x1), y_transform(y))) else: points.append('%.2f,%.2f' % (x_transform(y), y_transform(x1))) else: x1, x2, y = self.binned_data[i] previous_x1, previous_x2, previous_y = self.binned_data[i - 1] if x1 == previous_x2: if self.orientation == 'horizontal': points.append('%.2f,%.2f' % (x_transform(x1), y_transform(previous_y))) if y != previous_y: points.append('%.2f,%.2f' % (x_transform(x1), y_transform(y))) else: points.append('%.2f,%.2f' % (x_transform(previous_y), y_transform(x1))) if y != previous_y: points.append('%.2f,%.2f' % (x_transform(y), y_transform(x1))) else: # a 'break' in the histogram if self.orientation == 'horizontal': points.append('%.2f,%.2f' % (x_transform(previous_x2), y_transform(previous_y))) else: points.append('%.2f,%.2f' % (x_transform(previous_y), y_transform(previous_x2))) pl = ET.SubElement(root_element, 'polyline') pl.set('stroke', self.color) pl.set('fill', 'none') pl.set('points', ' '.join(points)) if self.line_pattern: pl.set('style', 'stroke-dasharray:%s' % self.line_pattern) if self.orientation == 'horizontal': points = ['%.2f,%.2f' % (x_transform(x1), y_transform(y))] else: points = ['%.2f,%.2f' % (x_transform(y), y_transform(x1))] if i == L - 1: x1, x2, y = self.binned_data[-1] if self.orientation == 'horizontal': points.append('%.2f,%.2f' % (x_transform(x2), y_transform(y))) else: points.append('%.2f,%.2f' % (x_transform(y), y_transform(x2))) pl = ET.SubElement(root_element, 'polyline') pl.set('stroke', self.color) pl.set('fill', 'none') pl.set('points', ' '.join(points)) if self.line_pattern: pl.set('style', 'stroke-dasharray:%s' % self.line_pattern)