From 7f25b508633c166e9c069450c33b198440b5deba Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Mon, 6 Feb 2012 16:48:02 +0100 Subject: [PATCH] Handle one point --- pygal/line.py | 15 ++++++++++----- pygal/test/test_line.py | 10 ++++++++-- pygal/view.py | 12 ++++++++---- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/pygal/line.py b/pygal/line.py index 36e135a..16d04cc 100644 --- a/pygal/line.py +++ b/pygal/line.py @@ -21,14 +21,18 @@ class Line(BaseGraph): self.series.append( Serie(title, values)) + def _label(self, label): + return Label(('{:.%d%s}' % ( + self.precision, self.format)).format(label), label) + def _y_labels(self, ymin, ymax): step = (ymax - ymin) / 20. + if not step: + return [self._label(ymin)] label = ymin labels = [] - while label < ymax: - labels.append( - Label(('{:.%d%s}' % ( - self.precision, self.format)).format(label), label)) + while label <= ymax: + labels.append(self._label(label)) label += step return labels @@ -42,7 +46,8 @@ class Line(BaseGraph): def draw(self): self.validate() x_step = len(self.series[0].values) - x_pos = [x / float(x_step - 1) for x in range(x_step)] + x_pos = [x / float(x_step - 1) for x in range(x_step) + ] if x_step != 1 else [0] # Center if only one value vals = [val for serie in self.series for val in serie.values] margin = Margin(*(4 * [10])) ymin, ymax = min(vals), max(vals) diff --git a/pygal/test/test_line.py b/pygal/test/test_line.py index cf8bbe2..939dd14 100644 --- a/pygal/test/test_line.py +++ b/pygal/test/test_line.py @@ -1,14 +1,20 @@ from pygal.line import Line -from pygal.style import LightStyle from math import cos, sin def test_simple_line(): - line = Line(800, 600, style=LightStyle, precision=2, format='f') + line = Line(800, 600, precision=2, format='f') rng = range(-30, 30, 5) line.add('test1', [cos(x / 10.) for x in rng]) line.add('test2', [sin(x / 10.) for x in rng]) line.add('test3', [cos(x / 10.) - sin(x / 10.) for x in rng]) line.x_labels = map(str, rng) line.title = "cos sin and cos - sin" + line.render() + + +def test_one_dot(): + line = Line(800, 600) + line.add('one dot', [12]) + line.x_labels = ['one'] line._in_browser() diff --git a/pygal/view.py b/pygal/view.py index 8cb163d..9be51cf 100644 --- a/pygal/view.py +++ b/pygal/view.py @@ -12,7 +12,13 @@ class View(object): def __init__(self, width, height, xmin, xmax, ymin, ymax): self.width = width self.height = height - self.box = Box(xmin, ymin, xmax - xmin, ymax - ymin) + xrng = (xmax - xmin) or 1 + yrng = (ymax - ymin) or 1 + if xrng == 1: + xmin -= .5 + if yrng == 1: + ymin -= .5 + self.box = Box(xmin, ymin, xrng, yrng) def x(self, x): return self.width * (x - self.box.x) / float(self.box.width) @@ -23,6 +29,4 @@ class View(object): def __call__(self, xy): x, y = xy - return ( - self.x(x), - self.y(y)) + return (self.x(x), self.y(y))