Browse Source

added a constant interpolation, i.e., a step function

pull/427/head
Hendrik Schawe 7 years ago
parent
commit
b6128aa917
  1. 9
      docs/documentation/configuration/interpolations.rst
  2. 17
      pygal/interpolate.py
  3. 7
      pygal/test/test_interpolate.py

9
docs/documentation/configuration/interpolations.rst

@ -50,6 +50,15 @@ trigonometric
chart.add('line', [1, 5, 17, 12, 5, 10]) chart.add('line', [1, 5, 17, 12, 5, 10])
constant
~~~~~~~~
.. pygal-code::
chart = pygal.Line(interpolate='constant')
chart.add('line', [1, 5, 17, 12, 5, 10])
hermite hermite
~~~~~~~ ~~~~~~~

17
pygal/interpolate.py

@ -230,12 +230,26 @@ def trigonometric_interpolate(x, y, precision=250, **kwargs):
yield X, s yield X, s
def constant_interpolate(x, y, precision=250, **kwargs):
n = len(x) - 1
delta_x = [x2 - x1 for x1, x2 in zip(x, x[1:])]
for i in range(n + 1):
yield x[i], y[i]
if i == n or delta_x[i] == 0:
continue
for s in range(1, precision):
X = x[i] + s * delta_x[i] / precision
yield X, y[i]
INTERPOLATIONS = { INTERPOLATIONS = {
'quadratic': quadratic_interpolate, 'quadratic': quadratic_interpolate,
'cubic': cubic_interpolate, 'cubic': cubic_interpolate,
'hermite': hermite_interpolate, 'hermite': hermite_interpolate,
'lagrange': lagrange_interpolate, 'lagrange': lagrange_interpolate,
'trigonometric': trigonometric_interpolate 'trigonometric': trigonometric_interpolate,
'constant': constant_interpolate
} }
if __name__ == '__main__': if __name__ == '__main__':
@ -247,6 +261,7 @@ if __name__ == '__main__':
xy.add('cubic', cubic_interpolate(*zip(*points))) xy.add('cubic', cubic_interpolate(*zip(*points)))
xy.add('lagrange', lagrange_interpolate(*zip(*points))) xy.add('lagrange', lagrange_interpolate(*zip(*points)))
xy.add('trigonometric', trigonometric_interpolate(*zip(*points))) xy.add('trigonometric', trigonometric_interpolate(*zip(*points)))
xy.add('constant', constant_interpolate(*zip(*points)))
xy.add( xy.add(
'hermite catmul_rom', 'hermite catmul_rom',
hermite_interpolate(*zip(*points), type='catmul_rom') hermite_interpolate(*zip(*points), type='catmul_rom')

7
pygal/test/test_interpolate.py

@ -60,6 +60,13 @@ def test_trigonometric(Chart, datas):
assert chart.render() assert chart.render()
def test_constant(Chart, datas):
"""Test constant interpolation"""
chart = Chart(interpolate='constant')
chart = make_data(chart, datas)
assert chart.render()
def test_hermite(Chart, datas): def test_hermite(Chart, datas):
"""Test hermite interpolation""" """Test hermite interpolation"""
chart = Chart(interpolate='hermite') chart = Chart(interpolate='hermite')

Loading…
Cancel
Save