Browse Source

Improve Xy data detection. Fix #21

pull/26/merge
Florian Mounier 12 years ago
parent
commit
8a07288e21
  1. 71
      pygal/graph/xy.py

71
pygal/graph/xy.py

@ -22,58 +22,79 @@ XY Line graph
""" """
from __future__ import division from __future__ import division
from pygal.util import compute_scale from pygal.util import compute_scale, cached_property
from pygal.graph.line import Line from pygal.graph.line import Line
class XY(Line): class XY(Line):
"""XY Line graph""" """XY Line graph"""
def _get_value(self, values, i): @cached_property
return 'x=%s, y=%s' % tuple(map(self._format, values[i])) def xvals(self):
return [val[0]
def _compute(self):
xvals = [val[0]
for serie in self.all_series for serie in self.all_series
for val in serie.values for val in serie.values
if val[0] is not None] if val[0] is not None]
yvals = [val[1]
@cached_property
def yvals(self):
return [val[1]
for serie in self.series for serie in self.series
for val in serie.values for val in serie.values
if val[1] is not None] if val[1] is not None]
if xvals:
xmin = min(xvals) def _has_data(self):
xmax = max(xvals) """Check if there is any data"""
rng = (xmax - xmin) return sum(
map(len, map(lambda s: s.safe_values, self.series))) != 0 and any((
sum(map(abs, self.xvals)) != 0,
sum(map(abs, self.yvals)) != 0))
def _get_value(self, values, i):
return 'x=%s, y=%s' % tuple(map(self._format, values[i]))
def _compute(self):
if self.xvals:
xmin = min(self.xvals)
xmax = max(self.xvals)
xrng = (xmax - xmin)
else:
xrng = None
if self.yvals:
ymin = min(self.yvals)
ymax = max(self.yvals)
yrng = (ymax - ymin)
else: else:
rng = None yrng = None
for serie in self.all_series: for serie in self.all_series:
serie.points = serie.values serie.points = serie.values
if self.interpolate and rng: if self.interpolate and xrng:
vals = zip(*sorted( vals = zip(*sorted(
filter(lambda t: None not in t, filter(lambda t: None not in t,
serie.points), key=lambda x: x[0])) serie.points), key=lambda x: x[0]))
serie.interpolated = self._interpolate( serie.interpolated = self._interpolate(
vals[1], vals[0], xy=True, xy_xmin=xmin, xy_rng=rng) vals[1], vals[0], xy=True, xy_xmin=xmin, xy_rng=xrng)
if self.interpolate and rng: if self.interpolate and xrng:
xvals = [val[0] self.xvals = [val[0]
for serie in self.all_series for serie in self.all_series
for val in serie.interpolated] for val in serie.interpolated]
yvals = [val[1] self.yvals = [val[1]
for serie in self.series for serie in self.series
for val in serie.interpolated] for val in serie.interpolated]
if xvals: if self.xvals:
xmin = min(xvals) xmin = min(self.xvals)
xmax = max(xvals) xmax = max(self.xvals)
rng = (xmax - xmin) xrng = (xmax - xmin)
else: else:
rng = None xrng = None
if rng: if xrng:
self._box.xmin, self._box.xmax = min(xvals), max(xvals) self._box.xmin, self._box.xmax = min(self.xvals), max(self.xvals)
self._box.ymin, self._box.ymax = min(yvals), max(yvals) if yrng:
self._box.ymin, self._box.ymax = min(self.yvals), max(self.yvals)
x_pos = compute_scale( x_pos = compute_scale(
self._box.xmin, self._box.xmax, self.logarithmic, self.order_min) self._box.xmin, self._box.xmax, self.logarithmic, self.order_min)

Loading…
Cancel
Save