Skip to content

Commit

Permalink
Add defs config key.
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Nov 3, 2015
1 parent 70fdda1 commit 2dc3fc0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 1 deletion.
40 changes: 39 additions & 1 deletion demo/moulinrouge/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,4 +913,42 @@ def test_legend_link_for(chart):

return chart.render_response()

return list(sorted(filter(lambda x: x.startswith('test'), locals())))
@app.route('/test/gradient/<chart>')
def test_gradient_for(chart):

config = Config()
config.style = styles['dark']
config.defs.append('''
<linearGradient id="gradient-0" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#ff5995" />
<stop offset="100%" stop-color="#feed6c" />
</linearGradient>
''')
config.defs.append('''
<linearGradient id="gradient-1" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#b6e354" />
<stop offset="100%" stop-color="#8cedff" />
</linearGradient>
''')
config.css.append('''inline:
.color-0 {
fill: url(#gradient-0) !important;
stroke: url(#gradient-0) !important;
}''')
config.css.append('''inline:
.color-1 {
fill: url(#gradient-1) !important;
stroke: url(#gradient-1) !important;
}''')
chart = CHARTS_BY_NAME[chart](config)
chart.add('1', [1, 3, 12, 3, 4, None, 9])
chart.add('2', [7, -4, 10, None, 8, 3, 1])
chart.x_labels = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
chart.legend_at_bottom = True
chart.interpolate = 'cubic'
return chart.render_response()

return list(sorted(filter(
lambda x: x.startswith('test') and not x.endswith('_for'), locals()))
) + list(sorted(filter(
lambda x: x.startswith('test') and x.endswith('_for'), locals())))
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Changelog
* Fix labels rotation > 180 (#257)
* Fix secondary axis
* Don't forget secondary series in table rendering (#260)
* Add `defs` config option to allow adding gradients and patterns.

2.0.8
=====
Expand Down
40 changes: 40 additions & 0 deletions docs/documentation/configuration/rendering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,46 @@ Css can also specified inline by prepending `inline:` to the css:
css = ['inline:.rect { fill: blue; }']
defs
----

You can add defs like linearGradient, radialGradient, pattern to the defs config:


.. pygal-code::

config = pygal.Config()
config.style = pygal.style.DarkStyle
config.defs.append('''
<linearGradient id="gradient-0" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#ff5995" />
<stop offset="100%" stop-color="#feed6c" />
</linearGradient>
''')
config.defs.append('''
<linearGradient id="gradient-1" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#b6e354" />
<stop offset="100%" stop-color="#8cedff" />
</linearGradient>
''')
config.css.append('''inline:
.color-0 {
fill: url(#gradient-0) !important;
stroke: url(#gradient-0) !important;
}''')
config.css.append('''inline:
.color-1 {
fill: url(#gradient-1) !important;
stroke: url(#gradient-1) !important;
}''')
chart = pygal.Line(config)
chart.add('1', [1, 3, 12, 3, 4, None, 9])
chart.add('2', [7, -4, 10, None, 8, 3, 1])
chart.x_labels = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
chart.legend_at_bottom = True
chart.interpolate = 'cubic'


js
--

Expand Down
3 changes: 3 additions & 0 deletions pygal/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def normalize_float(f):

def rgb_to_hsl(r, g, b):
"""Convert a color in r, g, b to a color in h, s, l"""
r = r or 0
g = g or 0
b = b or 0
r /= 255
g /= 255
b /= 255
Expand Down
6 changes: 6 additions & 0 deletions pygal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ class Config(CommonConfig):
"It can be any uri from file:///tmp/style.css to //domain/style.css",
str)

defs = Key(
[],
list, "Misc", "Extraneous defs to be inserted in svg",
"Useful for adding gradients / patterns…",
str)

# Look #
title = Key(
None, str, "Look",
Expand Down
3 changes: 3 additions & 0 deletions pygal/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def __init__(self, graph):
self.title = self.node(tag='title')
self.title.text = graph.title or 'Pygal'

for def_ in self.graph.defs:
self.defs.append(etree.fromstring(def_))

def add_styles(self):
"""Add the css to the svg"""
colors = self.graph.style.get_colors(self.id, self.graph._order)
Expand Down

0 comments on commit 2dc3fc0

Please sign in to comment.