From c3f16a2236764ed4267508b978e96954a680083c Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Mon, 6 Feb 2012 14:26:39 +0100 Subject: [PATCH] Add style class, style format and style overriding --- pygal/css/graph.css | 100 +++++---------------------------------- pygal/line.py | 6 +-- pygal/style.py | 30 ++++++++++++ pygal/svg.py | 12 ++++- pygal/test/test_line.py | 2 + pygal/test/test_style.py | 26 ++++++++++ relauncher | 2 +- 7 files changed, 84 insertions(+), 94 deletions(-) create mode 100644 pygal/style.py create mode 100644 pygal/test/test_style.py diff --git a/pygal/css/graph.css b/pygal/css/graph.css index 830dceb..d74622f 100644 --- a/pygal/css/graph.css +++ b/pygal/css/graph.css @@ -5,19 +5,19 @@ svg * { } .graph > .background { - fill: black; + fill: {{ style.background }}; } .plot > .background { - fill: #111; + fill: {{ style.plot_background }}; } .graph { - fill: #999; + fill: {{ style.foreground }}; } .title { - fill: #eee; + fill: {{ style.foreground_light }}; text-anchor: middle; alignment-baseline: baseline; } @@ -29,7 +29,7 @@ svg * { } .legend rect { - stroke: #555; + stroke: {{ style.foreground_dark }}; } .axis text { @@ -48,11 +48,11 @@ svg * { } .axis .line { - stroke: #ccc; + stroke: {{ style.foreground_light }}; } .axis .guide.line { - stroke: #555; + stroke: {{ style.foreground_dark }}; stroke-dasharray: 5,5; } @@ -61,13 +61,13 @@ svg * { } .axis .guides:hover .guide.line { - stroke: #aaa; + stroke: {{ style.foreground_light }}; opacity: 1; } .axis .guides:hover text { opacity: 1; - fill: #aaa; + fill: {{ style.foreground_light }}; } .series .dots .dot circle { @@ -81,7 +81,7 @@ svg * { text-anchor: middle; alignment-baseline: baseline; stroke: none; - fill: #ccc; + fill: {{ style.foreground_light }}; } .series .dots .dot:hover text { @@ -98,82 +98,4 @@ svg * { stroke-width: 2px; } -.color-0 { - stroke: #ff5995; - fill: #ff5995; -} - -.color-1 { - stroke: #b6e354; - fill: #b6e354; -} - -.color-2 { - stroke: #feed6c; - fill: #feed6c; -} - -.color-3 { - stroke: #8cedff; - fill: #8cedff; -} - -.color-4 { - stroke: #9e6ffe; - fill: #9e6ffe; -} - -.color-5 { - stroke: #899ca1; - fill: #899ca1; -} - -.color-6 { - stroke: #f8f8f2; - fill: #f8f8f2; -} - -.color-7 { - stroke: #808384; - fill: #808384; -} - -.color-8 { - stroke: #bf4646; - fill: #bf4646; -} - -.color-9 { - stroke: #516083; - fill: #516083; -} - -.color-10 { - stroke: #f92672; - fill: #f92672; -} - -.color-11 { - stroke: #82b414; - fill: #82b414; -} - -.color-12 { - stroke: #fd971f; - fill: #fd971f; -} - -.color-13 { - stroke: #56c2d6; - fill: #56c2d6; -} - -.color-14 { - stroke: #8c54fe; - fill: #8c54fe; -} - -.color-15 { - stroke: #465457; - fill: #465457; -} +{{ style.colors }} diff --git a/pygal/line.py b/pygal/line.py index f08a1ff..1466ff6 100644 --- a/pygal/line.py +++ b/pygal/line.py @@ -13,13 +13,13 @@ class Line(BaseGraph): self.label_font_size = 12 self.scale_int = scale_int self.series = [] - self.x_labels = self.title = None + self.x_labels = self.y_labels = self.title = None def add(self, title, values): self.series.append( Serie(title, values)) - def y_labels(self, ymin, ymax): + def _y_labels(self, ymin, ymax): step = (ymax - ymin) / 20. label = ymin labels = [] @@ -46,7 +46,7 @@ class Line(BaseGraph): if self.x_labels: x_labels = [Label(label, x_pos[i]) for i, label in enumerate(self.x_labels)] - y_labels = self.y_labels(ymin, ymax) + y_labels = self.y_labels or self._y_labels(ymin, ymax) series_labels = [serie.title for serie in self.series] margin.left += 10 + max( map(len, [l.label for l in y_labels])) * 0.6 * self.label_font_size diff --git a/pygal/style.py b/pygal/style.py new file mode 100644 index 0000000..a56018e --- /dev/null +++ b/pygal/style.py @@ -0,0 +1,30 @@ +class Style(object): + def __init__(self, + background='black', + plot_background='#111', + foreground='#999', + foreground_light='#eee', + foreground_dark='#555', + colors=( + '#ff5995', '#b6e354', '#feed6c', '#8cedff', '#9e6ffe', + '#899ca1', '#f8f8f2', '#808384', '#bf4646', '#516083', + '#f92672', '#82b414', '#fd971f', '#56c2d6', '#8c54fe', + '#465457')): + self.background = background + self.plot_background = plot_background + self.foreground = foreground + self.foreground_light = foreground_light + self.foreground_dark = foreground_dark + self._colors = colors + + @property + def colors(self): + def color(tupl): + return ( + '.color-{0} {{\n' + ' stroke: {1};\n' + ' fill: {1};\n' + '}}\n'.format(*tupl)) + return '\n'.join(map(color, enumerate(self._colors))) + +DefaultStyle = Style() diff --git a/pygal/svg.py b/pygal/svg.py index 639ec90..80d6760 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -1,6 +1,7 @@ import os from lxml import etree from pygal.view import View +from pygal.style import DefaultStyle class Svg(object): @@ -30,7 +31,16 @@ class Svg(object): def add_style(self, css): style = self.node(self.defs, 'style', type='text/css') with open(css) as f: - style.text = f.read() + style.text = ( + f.read() + # Lol + .replace('{{ ', '\x00') + .replace('{', '{{') + .replace('\x00', '{') + .replace(' }}', '\x00') + .replace('}', '}}') + .replace('\x00', '}') + .format(style=DefaultStyle)) def node(self, parent=None, tag='g', attrib=None, **extras): if parent is None: diff --git a/pygal/test/test_line.py b/pygal/test/test_line.py index f0adc71..cdf2dd4 100644 --- a/pygal/test/test_line.py +++ b/pygal/test/test_line.py @@ -1,3 +1,4 @@ +from pygal import Serie, Margin, Label from pygal.line import Line from math import cos, sin @@ -8,5 +9,6 @@ def test_simple_line(): line.add('test2', [sin(x / 10.) for x in range(-30, 30, 5)]) line.add('test3', [cos(x / 10.) - sin(x / 10.) for x in range(-30, 30, 5)]) line.x_labels = map(str, range(-30, 30, 5)) + # line.y_labels = [Label(str(lbl / 100.), lbl / 100.) for lbl in range(20)] line.title = "cos sin and cos - sin" line._in_browser() diff --git a/pygal/test/test_style.py b/pygal/test/test_style.py new file mode 100644 index 0000000..2167ebd --- /dev/null +++ b/pygal/test/test_style.py @@ -0,0 +1,26 @@ +from pygal.style import Style + + +def test_colors(): + style = Style(colors=['red', '#231A3b', '#ff0', 'rgb(12, 231, 3)']) + assert style.colors == '''\ +.color-0 { + stroke: red; + fill: red; +} + +.color-1 { + stroke: #231A3b; + fill: #231A3b; +} + +.color-2 { + stroke: #ff0; + fill: #ff0; +} + +.color-3 { + stroke: rgb(12, 231, 3); + fill: rgb(12, 231, 3); +} +''' diff --git a/relauncher b/relauncher index 05cf91b..265dcc6 100755 --- a/relauncher +++ b/relauncher @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/zsh while inotifywait -e modify **/*.py >> ~/.log/inotifywait.log 2>&1; do py.test pygal/test done