Browse Source

Add defs config key.

pull/290/head
Florian Mounier 9 years ago
parent
commit
2dc3fc06b9
  1. 40
      demo/moulinrouge/tests.py
  2. 1
      docs/changelog.rst
  3. 40
      docs/documentation/configuration/rendering.rst
  4. 3
      pygal/colors.py
  5. 6
      pygal/config.py
  6. 3
      pygal/svg.py

40
demo/moulinrouge/tests.py

@ -913,4 +913,42 @@ def get_test_routes(app):
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
docs/changelog.rst

@ -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
=====

40
docs/documentation/configuration/rendering.rst

@ -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
--

3
pygal/colors.py

@ -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

6
pygal/config.py

@ -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",

3
pygal/svg.py

@ -80,6 +80,9 @@ class Svg(object):
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)

Loading…
Cancel
Save