diff --git a/docs/documentation/configuration/interpolations.rst b/docs/documentation/configuration/interpolations.rst index b232cae..648d8e0 100644 --- a/docs/documentation/configuration/interpolations.rst +++ b/docs/documentation/configuration/interpolations.rst @@ -50,6 +50,15 @@ trigonometric 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 ~~~~~~~ diff --git a/pygal/interpolate.py b/pygal/interpolate.py index 58bc39a..37b57cc 100644 --- a/pygal/interpolate.py +++ b/pygal/interpolate.py @@ -230,12 +230,26 @@ def trigonometric_interpolate(x, y, precision=250, **kwargs): 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 = { 'quadratic': quadratic_interpolate, 'cubic': cubic_interpolate, 'hermite': hermite_interpolate, 'lagrange': lagrange_interpolate, - 'trigonometric': trigonometric_interpolate + 'trigonometric': trigonometric_interpolate, + 'constant': constant_interpolate } if __name__ == '__main__': @@ -247,6 +261,7 @@ if __name__ == '__main__': xy.add('cubic', cubic_interpolate(*zip(*points))) xy.add('lagrange', lagrange_interpolate(*zip(*points))) xy.add('trigonometric', trigonometric_interpolate(*zip(*points))) + xy.add('constant', constant_interpolate(*zip(*points))) xy.add( 'hermite catmul_rom', hermite_interpolate(*zip(*points), type='catmul_rom') diff --git a/pygal/test/test_interpolate.py b/pygal/test/test_interpolate.py index eb7bdc2..332e2f2 100644 --- a/pygal/test/test_interpolate.py +++ b/pygal/test/test_interpolate.py @@ -60,6 +60,13 @@ def test_trigonometric(Chart, datas): 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): """Test hermite interpolation""" chart = Chart(interpolate='hermite')