Browse Source

Handle one point

pull/8/head
Florian Mounier 13 years ago
parent
commit
7f25b50863
  1. 15
      pygal/line.py
  2. 10
      pygal/test/test_line.py
  3. 12
      pygal/view.py

15
pygal/line.py

@ -21,14 +21,18 @@ class Line(BaseGraph):
self.series.append( self.series.append(
Serie(title, values)) Serie(title, values))
def _label(self, label):
return Label(('{:.%d%s}' % (
self.precision, self.format)).format(label), label)
def _y_labels(self, ymin, ymax): def _y_labels(self, ymin, ymax):
step = (ymax - ymin) / 20. step = (ymax - ymin) / 20.
if not step:
return [self._label(ymin)]
label = ymin label = ymin
labels = [] labels = []
while label < ymax: while label <= ymax:
labels.append( labels.append(self._label(label))
Label(('{:.%d%s}' % (
self.precision, self.format)).format(label), label))
label += step label += step
return labels return labels
@ -42,7 +46,8 @@ class Line(BaseGraph):
def draw(self): def draw(self):
self.validate() self.validate()
x_step = len(self.series[0].values) 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] vals = [val for serie in self.series for val in serie.values]
margin = Margin(*(4 * [10])) margin = Margin(*(4 * [10]))
ymin, ymax = min(vals), max(vals) ymin, ymax = min(vals), max(vals)

10
pygal/test/test_line.py

@ -1,14 +1,20 @@
from pygal.line import Line from pygal.line import Line
from pygal.style import LightStyle
from math import cos, sin from math import cos, sin
def test_simple_line(): 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) rng = range(-30, 30, 5)
line.add('test1', [cos(x / 10.) for x in rng]) line.add('test1', [cos(x / 10.) for x in rng])
line.add('test2', [sin(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.add('test3', [cos(x / 10.) - sin(x / 10.) for x in rng])
line.x_labels = map(str, rng) line.x_labels = map(str, rng)
line.title = "cos sin and cos - sin" 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() line._in_browser()

12
pygal/view.py

@ -12,7 +12,13 @@ class View(object):
def __init__(self, width, height, xmin, xmax, ymin, ymax): def __init__(self, width, height, xmin, xmax, ymin, ymax):
self.width = width self.width = width
self.height = height 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): def x(self, x):
return self.width * (x - self.box.x) / float(self.box.width) return self.width * (x - self.box.x) / float(self.box.width)
@ -23,6 +29,4 @@ class View(object):
def __call__(self, xy): def __call__(self, xy):
x, y = xy x, y = xy
return ( return (self.x(x), self.y(y))
self.x(x),
self.y(y))

Loading…
Cancel
Save