Browse Source

Add parametric styles

pull/39/head
Florian Mounier 12 years ago
parent
commit
769a5e962e
  1. 27
      demo/moulinrouge/__init__.py
  2. 10
      demo/moulinrouge/templates/index.jinja2
  3. 29
      pygal/style.py

27
demo/moulinrouge/__init__.py

@ -21,7 +21,7 @@ import pygal
from pygal.config import Config from pygal.config import Config
from pygal.util import cut from pygal.util import cut
from pygal.graph import CHARTS_NAMES from pygal.graph import CHARTS_NAMES
from pygal.style import styles from pygal.style import styles, parametric_styles
from base64 import ( from base64 import (
urlsafe_b64encode as b64encode, urlsafe_b64encode as b64encode,
urlsafe_b64decode as b64decode) urlsafe_b64decode as b64decode)
@ -84,7 +84,9 @@ def create_app():
@app.route("/") @app.route("/")
def index(): def index():
return render_template( return render_template(
'index.jinja2', styles=styles, 'index.jinja2', styles=styles, parametric_styles=parametric_styles,
parametric_colors=(
'#ff5995', '#b6e354', '#feed6c', '#8cedff', '#9e6ffe'),
links=links, charts_name=CHARTS_NAMES) links=links, charts_name=CHARTS_NAMES)
@app.route("/svg/<type>/<series>/<config>") @app.route("/svg/<type>/<series>/<config>")
@ -95,19 +97,30 @@ def create_app():
return graph.render_response() return graph.render_response()
@app.route("/sparkline/<style>") @app.route("/sparkline/<style>")
def sparkline(style): @app.route("/sparkline/parameric/<style>/<color>")
line = pygal.Line(style=styles[style], pretty_print=True) def sparkline(style, color=None):
if color is None:
style = styles[style]
else:
style = parametric_styles[style](color)
line = pygal.Line(style=style, pretty_print=True)
line.add('_', [random.randrange(0, 10) for _ in range(25)]) line.add('_', [random.randrange(0, 10) for _ in range(25)])
return Response( return Response(
line.render_sparkline(height=40), mimetype='image/svg+xml') line.render_sparkline(height=40), mimetype='image/svg+xml')
@app.route("/all") @app.route("/all")
@app.route("/all/style=<style>") @app.route("/all/<style>")
@app.route("/all/<style>/<color>")
@app.route("/all/interpolate=<interpolate>") @app.route("/all/interpolate=<interpolate>")
def all(style='default', interpolate=None): def all(style='default', color=None, interpolate=None):
width, height = 600, 400 width, height = 600, 400
data = random.randrange(1, 10) data = random.randrange(1, 10)
order = random.randrange(1, 10) order = random.randrange(1, 10)
if color is None:
style = styles[style]
else:
style = parametric_styles[style](color)
xy_series = _random(data, order) xy_series = _random(data, order)
other_series = [] other_series = []
for title, values in xy_series: for title, values in xy_series:
@ -121,7 +134,7 @@ def create_app():
config.fill = bool(random.randrange(0, 2)) config.fill = bool(random.randrange(0, 2))
config.human_readable = True config.human_readable = True
config.interpolate = interpolate config.interpolate = interpolate
config.style = styles[style] config.style = style
config.x_labels = [random_label() for i in range(data)] config.x_labels = [random_label() for i in range(data)]
svgs = [] svgs = []
for chart in pygal.CHARTS: for chart in pygal.CHARTS:

10
demo/moulinrouge/templates/index.jinja2

@ -3,12 +3,22 @@
{% block section %} {% block section %}
<h1>Moulin Rouge</h1> <h1>Moulin Rouge</h1>
<ul> <ul>
<li>Styles</li>
{% for style in styles %} {% for style in styles %}
<li> <li>
<embed src="{{ url_for('sparkline', style=style) }}" type="image/svg+xml" /> <embed src="{{ url_for('sparkline', style=style) }}" type="image/svg+xml" />
<a href="{{ url_for('all', style=style) }}">{{ style | title }}</a> <a href="{{ url_for('all', style=style) }}">{{ style | title }}</a>
</li> </li>
{% endfor %} {% endfor %}
<li>Parametric Styles</li>
{% for color in parametric_colors %}
<li><embed src="{{ url_for('sparkline', style='RotateStyle', color=color) }}" type="image/svg+xml" /></li>
{% for style in parametric_styles %}
<li>
<a href="{{ url_for('all', style=style, color=color) }}">{{ style }} for {{ color }}</a>
</li>
{% endfor %}
{% endfor %}
</ul> </ul>
<a href="{{ url_for('interpolation') }}">Interpolation</a> <a href="{{ url_for('interpolation') }}">Interpolation</a>
<a href="{{ url_for('rotation') }}">Rotations test</a> <a href="{{ url_for('rotation') }}">Rotations test</a>

29
pygal/style.py

@ -21,6 +21,8 @@ Charts styling
""" """
from __future__ import division from __future__ import division
from pygal.util import cycle_fill from pygal.util import cycle_fill
from pygal import colors
import sys
class Style(object): class Style(object):
@ -130,3 +132,30 @@ styles = {'default': DefaultStyle,
'clean': CleanStyle, 'clean': CleanStyle,
'dark_solarized': DarkSolarizedStyle, 'dark_solarized': DarkSolarizedStyle,
'light_solarized': LightSolarizedStyle} 'light_solarized': LightSolarizedStyle}
parametric_styles = {}
for op in ('lighten', 'darken', 'saturate', 'desaturate', 'rotate'):
name = op.capitalize() + 'Style'
def get_style_for(op_):
operation = getattr(colors, op_)
def parametric_style(color, step=10, max_=None, **kwargs):
if max_ is None:
max__ = 50 if op_ != 'rotate' else 360
else:
max__ = max_
def modifier(index):
percent = max__ * index / (step - 1)
return operation(color, percent)
colors = list(map(modifier, range(0, max(2, step))))
return Style(colors=colors, **kwargs)
return parametric_style
style = get_style_for(op)
parametric_styles[name] = style
setattr(sys.modules[__name__], name, style)

Loading…
Cancel
Save