Browse Source

Merge branch 'master' of github.com:Kozea/pygal

Conflicts:
	pygal/style.py
pull/39/head
Anaèle Digoin 12 years ago
parent
commit
032cf5206b
  1. 27
      demo/moulinrouge/__init__.py
  2. 10
      demo/moulinrouge/templates/index.jinja2
  3. 98
      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>

98
pygal/style.py

@ -21,28 +21,9 @@ Charts styling
"""
from __future__ import division
from pygal.util import cycle_fill
from colorsys import rgb_to_hls, hls_to_rgb
def darken(color, percent):
assert color[0] == '#', '#rrggbb and #rgb format are supported'
color = color[1:]
assert len(color) in (3, 6), '#rrggbb and #rgb format are supported'
if len(color) == 3:
color = [a for b in zip(color, color) for a in b]
return '#%02x%02x%02x' % tuple(
map(lambda x: 255 * x,
hls_to_rgb(*(
lambda h, l, s: (h, max(0, min(1, l - percent / 100)), s))(
*rgb_to_hls(*map(
lambda x: int(''.join(x), 16) / 255,
zip(color[::2], color[1::2])))
))))
def lighten(color, percent):
return darken(color, -percent)
from pygal import colors
from pygal.colors import darken, lighten
import sys
class Style(object):
@ -96,6 +77,8 @@ class Style(object):
DefaultStyle = Style(opacity_hover='.4', opacity='.8')
LightStyle = Style(
background='white',
plot_background='rgba(0, 0, 255, 0.1)',
@ -105,10 +88,14 @@ LightStyle = Style(
colors=('#242424', '#9f6767', '#92ac68',
'#d0d293', '#9aacc3', '#bb77a4',
'#77bbb5', '#777777'))
NeonStyle = Style(
opacity='.1',
opacity_hover='.75',
transition='1s ease-out')
CleanStyle = Style(
background='transparent',
plot_background='rgba(240, 240, 240, 0.7)',
@ -119,10 +106,12 @@ CleanStyle = Style(
'rgb(12,55,149)', 'rgb(117,38,65)', 'rgb(228,127,0)', 'rgb(159,170,0)',
'rgb(149,12,12)'))
solarized_colors = (
'#b58900', '#cb4b16', '#dc322f', '#d33682',
'#6c71c4', '#268bd2', '#2aa198', '#859900')
DarkSolarizedStyle = Style(
background='#073642',
plot_background='#002b36',
@ -134,6 +123,7 @@ DarkSolarizedStyle = Style(
transition='500ms ease-in',
colors=solarized_colors)
LightSolarizedStyle = Style(
background='#fdf6e3',
plot_background='#eee8d5',
@ -145,6 +135,7 @@ LightSolarizedStyle = Style(
transition='500ms ease-in',
colors=solarized_colors)
RedBlueStyle = Style(
background=lighten('#e6e7e9', 7),
plot_background=lighten('#e6e7e9', 10),
@ -158,9 +149,10 @@ RedBlueStyle = Style(
lighten('#d94e4c', 10), darken('#39929a', 15), lighten('#e5884f', 17),
darken('#d94e4c', 10), '#234547'))
LightColorizedStyle = Style (
LightColorizedStyle = Style(
background='#f8f8f8',
plot_background= lighten('#f8f8f8', 3),
plot_background=lighten('#f8f8f8', 3),
foreground='#333',
foreground_light='#666',
foreground_dark='rgba(255, 255 , 255, 0.5)',
@ -172,9 +164,10 @@ LightColorizedStyle = Style (
darken('#fe9592', 15), lighten('#534f4c', 15), lighten('#3ac2c0', 15),
lighten('#a2a7a1', 15), lighten('#fe9592', 15), darken('#3ac2c0', 10)))
DarkColorizedStyle = Style(
background=darken('#3a2d3f', 5),
plot_background=lighten('#3a2d3f',2),
plot_background=lighten('#3a2d3f', 2),
foreground='rgba(255, 255, 255, 0.9)',
foreground_light='rgba(255, 255, 255, 0.9)',
foreground_dark='rgba(255, 255 , 255, 0.5)',
@ -186,6 +179,7 @@ DarkColorizedStyle = Style(
darken('#c900fe', 20), darken('#01b8fe', 15), darken('#59f500', 20),
darken('#ff00e4', 15), lighten('#f9fa00', 20)))
TurquoiseStyle = Style(
background=darken('#1b8088', 15),
plot_background=darken('#1b8088', 17),
@ -197,11 +191,12 @@ TurquoiseStyle = Style(
transition='250ms ease-in',
colors=(
'#93d2d9', '#ef940f', '#8C6243', '#fff',
darken('#93d2d9', 20), lighten('#ef940f', 15), lighten('#8c6243', 15),'#1b8088'))
darken('#93d2d9', 20), lighten('#ef940f', 15),
lighten('#8c6243', 15), '#1b8088'))
GreenLightStyle = Style(
background= lighten('#f3f3f3', 3),
background=lighten('#f3f3f3', 3),
plot_background='#fff',
foreground='#333333',
foreground_light='#666',
@ -211,12 +206,13 @@ GreenLightStyle = Style(
transition='250ms ease-in',
colors=(
'#7dcf30', '#247fab', lighten('#7dcf30', 10), '#ccc',
darken('#7dcf30', 15), '#ddd', lighten('#247fab', 10), darken('#247fab', 15)))
darken('#7dcf30', 15), '#ddd', lighten('#247fab', 10),
darken('#247fab', 15)))
GreenDarkStyle = Style(
background=darken('#251e01', 3),
plot_background=darken('#251e01',1),
plot_background=darken('#251e01', 1),
foreground='rgba(255, 255, 255, 0.9)',
foreground_light='rgba(255, 255, 255, 0.9)',
foreground_dark='rgba(255, 255, 255, 0.6)',
@ -227,8 +223,9 @@ GreenDarkStyle = Style(
'#adde09', '#6e8c06', '#4a5e04', '#fcd202', '#C1E34D',
lighten('#fcd202', 25)))
GreenBlueDarkStyle = Style(
background= '#000',
background='#000',
plot_background=lighten('#000', 8),
foreground='rgba(255, 255, 255, 0.9)',
foreground_light='rgba(255, 255, 255, 0.9)',
@ -236,8 +233,10 @@ GreenBlueDarkStyle = Style(
opacity='.55',
opacity_hover='.9',
transition='250ms ease-in',
colors=(lighten('#34B8F7', 15), '#7dcf30', '#247fab', darken('#7dcf30', 10),
lighten('#247fab', 10), lighten('#7dcf30', 10), darken('#247fab', 10), '#fff'))
colors=(lighten('#34B8F7', 15), '#7dcf30', '#247fab',
darken('#7dcf30', 10), lighten('#247fab', 10),
lighten('#7dcf30', 10), darken('#247fab', 10), '#fff'))
BlueStyle = Style(
background=darken('#f8f8f8', 3),
@ -251,6 +250,7 @@ BlueStyle = Style(
colors=('#00b2f0', '#43d9be', '#0662ab', '#ffd541', lighten('#43d9be', 20),
lighten('#7dcf30', 10), darken('#0662ab', 15), '#7dcf30', darken('#ffd541', 20)))
styles = {'default': DefaultStyle,
'light': LightStyle,
'neon': NeonStyle,
@ -265,3 +265,37 @@ styles = {'default': DefaultStyle,
'dark_green': GreenDarkStyle,
'dark_green_blue': GreenBlueDarkStyle,
'blue_colorized': BlueStyle}
parametric_styles = {}
for op in ('lighten', 'darken', 'saturate', 'desaturate', 'rotate'):
name = op.capitalize() + 'Style'
def get_style_for(op_name):
operation = getattr(colors, op_name)
def parametric_style(color, step=10, max_=None, **kwargs):
if max_ is None:
violency = {
'darken': 50,
'lighten': 50,
'saturate': 100,
'desaturate': 100,
'rotate': 360
}
max__ = violency[op_name]
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