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.util import cut
from pygal.graph import CHARTS_NAMES
from pygal.style import styles
from pygal.style import styles, parametric_styles
from base64 import (
urlsafe_b64encode as b64encode,
urlsafe_b64decode as b64decode)
@ -84,7 +84,9 @@ def create_app():
@app.route("/")
def index():
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)
@app.route("/svg/<type>/<series>/<config>")
@ -95,19 +97,30 @@ def create_app():
return graph.render_response()
@app.route("/sparkline/<style>")
def sparkline(style):
line = pygal.Line(style=styles[style], pretty_print=True)
@app.route("/sparkline/parameric/<style>/<color>")
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)])
return Response(
line.render_sparkline(height=40), mimetype='image/svg+xml')
@app.route("/all")
@app.route("/all/style=<style>")
@app.route("/all/<style>")
@app.route("/all/<style>/<color>")
@app.route("/all/interpolate=<interpolate>")
def all(style='default', interpolate=None):
def all(style='default', color=None, interpolate=None):
width, height = 600, 400
data = 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)
other_series = []
for title, values in xy_series:
@ -121,7 +134,7 @@ def create_app():
config.fill = bool(random.randrange(0, 2))
config.human_readable = True
config.interpolate = interpolate
config.style = styles[style]
config.style = style
config.x_labels = [random_label() for i in range(data)]
svgs = []
for chart in pygal.CHARTS:

10
demo/moulinrouge/templates/index.jinja2

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

29
pygal/style.py

@ -21,6 +21,8 @@ Charts styling
"""
from __future__ import division
from pygal.util import cycle_fill
from pygal import colors
import sys
class Style(object):
@ -130,3 +132,30 @@ styles = {'default': DefaultStyle,
'clean': CleanStyle,
'dark_solarized': DarkSolarizedStyle,
'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