diff options
author | Martin Dengler <martin@martindengler.com> | 2009-02-22 10:11:02 (GMT) |
---|---|---|
committer | Martin Dengler <martin@martindengler.com> | 2009-02-22 10:11:02 (GMT) |
commit | 6ade3b7b4a99f2a921c0dd6462d90b0ca50c78a4 (patch) | |
tree | 856a32a57d6d8b6c4da0216f959edc6fa3e2105d | |
parent | a73b28f5b4934d0f64756e440401a5fc7bb0c490 (diff) |
refactor drawing methods
-rwxr-xr-x | wirelessgraph.py | 158 |
1 files changed, 82 insertions, 76 deletions
diff --git a/wirelessgraph.py b/wirelessgraph.py index 803f078..2aa2d2a 100755 --- a/wirelessgraph.py +++ b/wirelessgraph.py @@ -54,13 +54,13 @@ class Graph(gtk.Image): def __expose_event_cb(self, widget, event): """sets up a unit-height canvas for the drawing method""" - cr = self.__get_cairo_region() + cr = self._get_cairo_region() self._draw_frame(cr) if len(self.series[0].data) > 0: self._draw_lines(cr) self._draw_metrics(cr) - def __get_cairo_region(self): + def _get_cairo_region(self): cr = self.window.cairo_create() x, y, w, h = self.allocation cr.translate(x, y) @@ -80,99 +80,105 @@ class Graph(gtk.Image): cr.stroke() def _draw_lines(self, cr): - def _plot_line(color, line_width, data, offset=0.0): - dx = (self.width - (2 * self.padding_x)) / float(len(data)) - last_x = 0.0 - last_y = 1.0 - data[0] - offset - - cr.set_line_width(line_width) - cr.set_source_rgba(*color) - cr.move_to(last_x, last_y) - for idx in range(1, len(data[1:])): - cur_y = 1.0 - data[idx] - offset - dy = cur_y - last_y - cr.rel_line_to(dx, dy) - last_x += dx - last_y += dy - cr.stroke() - - def _tick_mark(color, tick_width, line_width, datum, tick_x_offset=0.0, - label=None): - cr.set_source_rgba(*color) - cr.set_line_width(line_width) - tick_y = 1.0 - datum - tick_x = self.width - (2.0 * self.padding_x) + tick_x_offset - cr.move_to(tick_x, tick_y) - cr.line_to(tick_x + tick_width, tick_y) - if label is not None: - cr.set_font_size(0.035) - cr.move_to(tick_x + tick_width, tick_y + line_width) - cr.show_text(label) - cr.stroke() - tick_line_width = 0.005 for series in self.series: data = series.data means = series.moments[1] colors = series.colors - _plot_line(colors[0], self.line_width, data) - _plot_line(colors[1], tick_line_width, means) + self._plot_line(cr, colors[0], self.line_width, data) + self._plot_line(cr, colors[1], tick_line_width, means) line1_tick_offset = -(self.padding_x / 1.99) - _tick_mark(colors[0], - self.padding_x / 2.0, - self.line_width, data[-1], - tick_x_offset=line1_tick_offset) - _tick_mark(colors[1], - self.padding_x, - tick_line_width, - means[-1], - label="%i" % (means[-1] * 100)) + self._tick_mark(cr, + colors[0], + self.padding_x / 2.0, + self.line_width, data[-1], + tick_x_offset=line1_tick_offset) + self._tick_mark(cr, + colors[1], + self.padding_x, + tick_line_width, + means[-1], + label="%i" % (means[-1] * 100)) min_datum = min(data) max_datum = max(data) - _tick_mark(colors[2], - self.padding_x, - tick_line_width, - min_datum, - tick_x_offset=line1_tick_offset, - label="%i" % (min_datum * 100)) - _tick_mark(colors[2], - self.padding_x, - tick_line_width, - max_datum, - tick_x_offset=line1_tick_offset, - label="%i" % (max_datum * 100)) + self._tick_mark(cr, + colors[2], + self.padding_x, + tick_line_width, + min_datum, + tick_x_offset=line1_tick_offset, + label="%i" % (min_datum * 100)) + self._tick_mark(cr, + colors[2], + self.padding_x, + tick_line_width, + max_datum, + tick_x_offset=line1_tick_offset, + label="%i" % (max_datum * 100)) def _draw_metrics(self, cr): cr.set_font_size(0.09) - - for series in self.series: + if len(self.series) > 0: + series = self.series[0] colors = series.colors data = series.data means = series.moments[1] - def _draw_outlined_text(text, color, pos, font_size=0.09): - x, y = pos - cr.set_source_rgba(*black) - cr.move_to(x - (0.005 * x), y + (0.005 * y)) - cr.set_font_size(font_size * 1.005) - cr.show_text(text) - - cr.set_source_rgba(*color) - cr.move_to(x, y) - cr.set_font_size(font_size) - cr.show_text(text) - - _draw_outlined_text("Link quality: %i" % (data[-1] * 100), - colors[0], - (0.0, 0.9)) - - _draw_outlined_text("Mean: %i" % (means[-1] * 100), - colors[1], - (0.79, 0.9)) + self._draw_outlined_text(cr, + "Link quality: %i" % (data[-1] * 100), + colors[0], + (0.0, 0.9)) + + self._draw_outlined_text(cr, + "Mean: %i" % (means[-1] * 100), + colors[1], + (0.79, 0.9)) + + def _plot_line(self, cr, color, line_width, data, offset=0.0): + dx = (self.width - (2 * self.padding_x)) / float(len(data)) + last_x = 0.0 + last_y = 1.0 - data[0] - offset + + cr.set_line_width(line_width) + cr.set_source_rgba(*color) + cr.move_to(last_x, last_y) + for idx in range(1, len(data[1:])): + cur_y = 1.0 - data[idx] - offset + dy = cur_y - last_y + cr.rel_line_to(dx, dy) + last_x += dx + last_y += dy + cr.stroke() + + def _tick_mark(self, cr, color, tick_width, line_width, datum, + tick_x_offset=0.0, label=None): + cr.set_source_rgba(*color) + cr.set_line_width(line_width) + tick_y = 1.0 - datum + tick_x = self.width - (2.0 * self.padding_x) + tick_x_offset + cr.move_to(tick_x, tick_y) + cr.line_to(tick_x + tick_width, tick_y) + if label is not None: + cr.set_font_size(0.035) + cr.move_to(tick_x + tick_width, tick_y + line_width) + cr.show_text(label) + cr.stroke() + + def _draw_outlined_text(self, cr, text, color, pos, font_size=0.09): + x, y = pos + cr.set_source_rgba(*black) + cr.move_to(x - (0.005 * x), y + (0.005 * y)) + cr.set_font_size(font_size * 1.005) + cr.show_text(text) + + cr.set_source_rgba(*color) + cr.move_to(x, y) + cr.set_font_size(font_size) + cr.show_text(text) |