Browse Source

Fix sparktext

pull/39/head
Florian Mounier 12 years ago
parent
commit
9ae3e66ddb
  1. 24
      pygal/ghost.py
  2. 27
      pygal/test/test_sparktext.py

24
pygal/ghost.py

@ -23,6 +23,7 @@ It is used to delegate rendering to real objects but keeping config in place
""" """
from __future__ import division
import io import io
import sys import sys
from pygal.config import Config from pygal.config import Config
@ -111,7 +112,7 @@ class Ghost(object):
return cairosvg.svg2png( return cairosvg.svg2png(
bytestring=self.render(), write_to=filename, dpi=dpi) bytestring=self.render(), write_to=filename, dpi=dpi)
def render_sparktext(self): def render_sparktext(self, relative_to=None):
"""Make a mini text sparkline from chart""" """Make a mini text sparkline from chart"""
bars = u('▁▂▃▄▅▆▇█') bars = u('▁▂▃▄▅▆▇█')
if len(self.raw_series) == 0: if len(self.raw_series) == 0:
@ -121,23 +122,30 @@ class Ghost(object):
return u('') return u('')
chart = u('') chart = u('')
values = map(lambda x: max(x, 0), values)
vmax = max(values) vmax = max(values)
divisions = len(bars) if relative_to is None:
relative_to = min(values)
divisions = len(bars) - 1
for value in values: for value in values:
chart += bars[int(divisions * min(value, 0) / vmax)] chart += bars[int(divisions *
(value - relative_to) / (vmax - relative_to))]
return chart return chart
def render_sparkline(self, width=200, height=50): def render_sparkline(self, **kwargs):
return self.make_instance(dict( spark_options = dict(
width=width, width=200,
height=height, height=50,
show_dots=False, show_dots=False,
show_legend=False, show_legend=False,
show_y_labels=False, show_y_labels=False,
spacing=0, spacing=0,
margin=5, margin=5,
explicit_size=True explicit_size=True
)).render() )
spark_options.update(kwargs)
return self.make_instance(spark_options).render()
def _repr_png_(self): def _repr_png_(self):
"""Display png in IPython notebook""" """Display png in IPython notebook"""

27
pygal/test/test_sparktext.py

@ -23,27 +23,40 @@ from pygal._compat import u
def test_basic_sparktext(): def test_basic_sparktext():
chart = Line() chart = Line()
chart.add('_', [1, 5, 22, 13, 53]) chart.add('_', [1, 5, 22, 13, 53])
chart.render_sparktext() == u('▁▁▃▂▇') assert chart.render_sparktext() == u('▁▁▃▂█')
def test_all_sparktext():
chart = Line()
chart.add('_', range(8))
assert chart.render_sparktext() == u('▁▂▃▄▅▆▇█')
def test_shifted_sparktext():
chart = Line()
chart.add('_', map(lambda x: x + 10000, range(8)))
assert chart.render_sparktext() == u('▁▂▃▄▅▆▇█')
assert chart.render_sparktext(relative_to=0) == u('▇▇▇▇▇▇▇█')
def test_another_sparktext(): def test_another_sparktext():
chart = Line() chart = Line()
chart.add('_', [0, 30, 55, 80, 33, 150]) chart.add('_', [0, 30, 55, 80, 33, 150])
chart.render_sparktext() == u('▁▂▃▅▂▇') assert chart.render_sparktext() == u('▁▂▃▄▂█')
chart.render_sparktext() == chart.render_sparktext() assert chart.render_sparktext() == chart.render_sparktext()
chart2 = Bar() chart2 = Bar()
chart2.add('_', [0, 30, 55, 80, 33, 150]) chart2.add('_', [0, 30, 55, 80, 33, 150])
chart2.render_sparktext() == chart.render_sparktext() assert chart2.render_sparktext() == chart.render_sparktext()
def test_negative_and_float_and_no_data_sparktext(): def test_negative_and_float_and_no_data_sparktext():
chart = Line() chart = Line()
chart.add('_', [0.1, 0.2, 0.9, -0.5]) chart.add('_', [0.1, 0.2, 0.9, -0.5])
chart.render_sparktext() == u('▄▅█▁') assert chart.render_sparktext() == u('▁▂█▁')
chart2 = Line() chart2 = Line()
chart2.add('_', []) chart2.add('_', [])
chart2.render_sparktext() == u('') assert chart2.render_sparktext() == u('')
chart3 = Line() chart3 = Line()
chart3.render_sparktext() == u('') assert chart3.render_sparktext() == u('')

Loading…
Cancel
Save