From 505a117fe459882f9ba829813a3c0b96e845c8d2 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Wed, 21 Jan 2015 12:34:43 +0100 Subject: [PATCH] Add a way to specify style/color on nodes. References: #184 --- demo/moulinrouge/tests.py | 13 +++++++++++++ pygal/util.py | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/demo/moulinrouge/tests.py b/demo/moulinrouge/tests.py index ea9da49..9d10abd 100644 --- a/demo/moulinrouge/tests.py +++ b/demo/moulinrouge/tests.py @@ -535,4 +535,17 @@ def get_test_routes(app): graph.legend_at_bottom = True return graph.render_response() + @app.route('/test/custom_metadata/') + def test_custom_metadata_for(chart): + c = CHARTS_BY_NAME[chart]() + c.add('1', [ + {'style': 'fill: red', 'value': 1}, + {'color': 'blue', 'value': 2}, + {'style': 'fill: red; stroke: yellow', 'value': 3}]) + c.add('2', [ + {'value': 4}, + {'color': 'green', 'value': 5}, + 6]) + return c.render_response() + return list(sorted(filter(lambda x: x.startswith('test'), locals()))) diff --git a/pygal/util.py b/pygal/util.py index 2548c1d..f08cec1 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -239,11 +239,20 @@ def decorate(svg, node, metadata): xlink = {'href': xlink, 'target': '_blank'} node = svg.node(node, 'a', **xlink) + if 'color' in metadata: + color = metadata.pop('color') + node.attrib['style'] = 'fill: %s; stroke: %s' % ( + color, color) + + if 'style' in metadata: + node.attrib['style'] = metadata.pop('style') + for key, value in metadata.items(): if key == 'xlink' and isinstance(value, dict): value = value.get('href', value) if value: svg.node(node, 'desc', class_=key).text = to_unicode(value) + return node