Skip to content

Commit

Permalink
Fix interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Sep 14, 2012
1 parent c18dc1f commit 554e5f4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 49 deletions.
2 changes: 2 additions & 0 deletions pygal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class Config(object):
truncate_label = None
#: Pretty print the svg
pretty_print = False
#: If True don't try to adapt / filter wrong values
strict = False

def __init__(self, **kwargs):
"""Can be instanciated with config kwargs"""
Expand Down
28 changes: 8 additions & 20 deletions pygal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def __init__(self, config, series):
self._box = Box()
self.view = None

if self.series and self._has_data():
self._draw()
else:
self.svg.draw_no_data()

self.svg.pre_render()

def __getattr__(self, attr):
"""Search in config, then in self"""
if attr in dir(self.config):
Expand Down Expand Up @@ -129,11 +136,6 @@ def _max(self):
"""Getter for the maximum series value"""
return (self.range and self.range[1]) or max(self._values)

@cached_property
def _cumul_max(self):
"""Getter for the maximum sum of series value"""
return max(map(sum, cut(self.series, 'safe_values')))

@cached_property
def _order(self):
"""Getter for the maximum series value"""
Expand All @@ -148,27 +150,13 @@ def _draw(self):

def _has_data(self):
"""Check if there is any data"""
if self._order == 0:
return False
if sum(map(len, map(lambda s: s.values, self.series))) == 0:
return False
return True

def _render(self):
"""Make the graph internally"""
if self.series and self._has_data():
self._draw()
self.svg.pre_render(False)
else:
self.svg.pre_render(True)
return sum(map(len, map(lambda s: s.values, self.series))) != 0

def render(self, is_unicode):
"""Render the graph, and return the svg string"""
self._render()
return self.svg.render(
is_unicode=is_unicode, pretty_print=self.pretty_print)

def render_tree(self):
"""Render the graph, and return lxml tree"""
self._render()
return self.svg.root
14 changes: 8 additions & 6 deletions pygal/graph/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def _values(self):
return [
val[1]
for serie in self.series
for val in serie.points
for val in (serie.interpolated
if self.interpolate else serie.points)
if val[1] is not None and (not self.logarithmic or val[1] > 0)]

def _fill(self, values):
Expand Down Expand Up @@ -81,6 +82,8 @@ def line(self, serie_node, serie):
y + self.value_font_size)

if self.stroke:
if self.interpolate:
view_values = map(self.view, serie.interpolated)
if self.fill:
view_values = self._fill(view_values)
self.svg.line(
Expand All @@ -93,12 +96,11 @@ def _compute(self):
] if self._len != 1 else [.5] # Center if only one value

for serie in self.series:
serie.points = [
(x_pos[i], v)
for i, v in enumerate(serie.values)]
if self.interpolate:
serie.points = self._interpolate(serie.values, x_pos)
else:
serie.points = [
(x_pos[i], v)
for i, v in enumerate(serie.values)]
serie.interpolated = self._interpolate(serie.values, x_pos)

if self.include_x_axis:
self._box.ymin = min(self._min, 0)
Expand Down
11 changes: 5 additions & 6 deletions pygal/graph/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _get_value(self, values, i):
def _values(self):
if self.interpolate:
return [val[0] for serie in self.series
for val in serie.points]
for val in serie.interpolated]
else:
return super(Line, self)._values

Expand Down Expand Up @@ -109,6 +109,9 @@ def _compute(self):
delta = 2 * pi / self._len
x_pos = [.5 * pi + i * delta for i in range(self._len + 1)]
for serie in self.series:
serie.points = [
(v, x_pos[i])
for i, v in enumerate(serie.values)]
if self.interpolate:
extend = 2
extended_x_pos = (
Expand All @@ -119,12 +122,8 @@ def _compute(self):
extended_vals = (serie.values[-extend:] +
serie.values +
serie.values[:extend])
serie.point = self._interpolate(
serie.interpolated = self._interpolate(
extended_vals, extended_x_pos, polar=True)
else:
serie.points = [
(v, x_pos[i])
for i, v in enumerate(serie.values)]

self._box.margin *= 2
self._box.xmin = self._box.ymin = - self._max
Expand Down
9 changes: 4 additions & 5 deletions pygal/graph/stackedline.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ def _compute(self):
accumulation = [0] * self._len
for serie in self.series:
accumulation = map(sum, zip(accumulation, serie.values))
serie.points = [
(x_pos[i], v)
for i, v in enumerate(accumulation)]
if self.interpolate:
serie.points = self._interpolate(accumulation, x_pos)
else:
serie.points = [
(x_pos[i], v)
for i, v in enumerate(accumulation)]
serie.interpolated = self._interpolate(accumulation, x_pos)

return super(StackedLine, self)._compute()
9 changes: 4 additions & 5 deletions pygal/graph/xy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ def _compute(self):
rng = (xmax - xmin)

for serie in self.series:
serie.points = serie.values
if self.interpolate:
vals = zip(*sorted(serie.points, key=lambda x: x[0]))
serie.points = self._interpolate(
serie.interpolated = self._interpolate(
vals[1], vals[0], xy=True, xy_xmin=xmin, xy_rng=rng)
else:
serie.points = serie.values

if self.interpolate:
xvals = [val[0]
for serie in self.series
for val in serie.points]
for val in serie.interpolated]
yvals = [val[1]
for serie in self.series
for val in serie.points]
for val in serie.interpolated]

self._box.xmin, self._box.xmax = min(xvals), max(xvals)
self._box.ymin, self._box.ymax = min(yvals), max(yvals)
Expand Down
15 changes: 8 additions & 7 deletions pygal/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def slice(
self.graph._tooltip_data(node, val, x, y, classes="centered")
self.graph._static_value(serie_node, val, x, y)

def pre_render(self, no_data=False):
def pre_render(self):
"""Last things to do before rendering"""
self.add_styles()
self.add_scripts()
Expand All @@ -181,12 +181,13 @@ def pre_render(self, no_data=False):
if self.graph.explicit_size:
self.root.set('width', str(self.graph.width))
self.root.set('height', str(self.graph.height))
if no_data:
no_data = self.node(self.root, 'text',
x=self.graph.width / 2,
y=self.graph.height / 2,
class_='no_data')
no_data.text = self.graph.no_data_text

def draw_no_data(self):
no_data = self.node(self.root, 'text',
x=self.graph.width / 2,
y=self.graph.height / 2,
class_='no_data')
no_data.text = self.graph.no_data_text

def render(self, is_unicode=False, pretty_print=False):
"""Last thing to do before rendering"""
Expand Down

0 comments on commit 554e5f4

Please sign in to comment.