Browse Source

Add style class, style format and style overriding

pull/8/head
Florian Mounier 13 years ago
parent
commit
c3f16a2236
  1. 100
      pygal/css/graph.css
  2. 6
      pygal/line.py
  3. 30
      pygal/style.py
  4. 12
      pygal/svg.py
  5. 2
      pygal/test/test_line.py
  6. 26
      pygal/test/test_style.py
  7. 2
      relauncher

100
pygal/css/graph.css

@ -5,19 +5,19 @@ svg * {
} }
.graph > .background { .graph > .background {
fill: black; fill: {{ style.background }};
} }
.plot > .background { .plot > .background {
fill: #111; fill: {{ style.plot_background }};
} }
.graph { .graph {
fill: #999; fill: {{ style.foreground }};
} }
.title { .title {
fill: #eee; fill: {{ style.foreground_light }};
text-anchor: middle; text-anchor: middle;
alignment-baseline: baseline; alignment-baseline: baseline;
} }
@ -29,7 +29,7 @@ svg * {
} }
.legend rect { .legend rect {
stroke: #555; stroke: {{ style.foreground_dark }};
} }
.axis text { .axis text {
@ -48,11 +48,11 @@ svg * {
} }
.axis .line { .axis .line {
stroke: #ccc; stroke: {{ style.foreground_light }};
} }
.axis .guide.line { .axis .guide.line {
stroke: #555; stroke: {{ style.foreground_dark }};
stroke-dasharray: 5,5; stroke-dasharray: 5,5;
} }
@ -61,13 +61,13 @@ svg * {
} }
.axis .guides:hover .guide.line { .axis .guides:hover .guide.line {
stroke: #aaa; stroke: {{ style.foreground_light }};
opacity: 1; opacity: 1;
} }
.axis .guides:hover text { .axis .guides:hover text {
opacity: 1; opacity: 1;
fill: #aaa; fill: {{ style.foreground_light }};
} }
.series .dots .dot circle { .series .dots .dot circle {
@ -81,7 +81,7 @@ svg * {
text-anchor: middle; text-anchor: middle;
alignment-baseline: baseline; alignment-baseline: baseline;
stroke: none; stroke: none;
fill: #ccc; fill: {{ style.foreground_light }};
} }
.series .dots .dot:hover text { .series .dots .dot:hover text {
@ -98,82 +98,4 @@ svg * {
stroke-width: 2px; stroke-width: 2px;
} }
.color-0 { {{ style.colors }}
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;
}

6
pygal/line.py

@ -13,13 +13,13 @@ class Line(BaseGraph):
self.label_font_size = 12 self.label_font_size = 12
self.scale_int = scale_int self.scale_int = scale_int
self.series = [] self.series = []
self.x_labels = self.title = None self.x_labels = self.y_labels = self.title = None
def add(self, title, values): def add(self, title, values):
self.series.append( self.series.append(
Serie(title, values)) Serie(title, values))
def y_labels(self, ymin, ymax): def _y_labels(self, ymin, ymax):
step = (ymax - ymin) / 20. step = (ymax - ymin) / 20.
label = ymin label = ymin
labels = [] labels = []
@ -46,7 +46,7 @@ class Line(BaseGraph):
if self.x_labels: if self.x_labels:
x_labels = [Label(label, x_pos[i]) x_labels = [Label(label, x_pos[i])
for i, label in enumerate(self.x_labels)] 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] series_labels = [serie.title for serie in self.series]
margin.left += 10 + max( margin.left += 10 + max(
map(len, [l.label for l in y_labels])) * 0.6 * self.label_font_size map(len, [l.label for l in y_labels])) * 0.6 * self.label_font_size

30
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()

12
pygal/svg.py

@ -1,6 +1,7 @@
import os import os
from lxml import etree from lxml import etree
from pygal.view import View from pygal.view import View
from pygal.style import DefaultStyle
class Svg(object): class Svg(object):
@ -30,7 +31,16 @@ class Svg(object):
def add_style(self, css): def add_style(self, css):
style = self.node(self.defs, 'style', type='text/css') style = self.node(self.defs, 'style', type='text/css')
with open(css) as f: 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): def node(self, parent=None, tag='g', attrib=None, **extras):
if parent is None: if parent is None:

2
pygal/test/test_line.py

@ -1,3 +1,4 @@
from pygal import Serie, Margin, Label
from pygal.line import Line from pygal.line import Line
from math import cos, sin 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('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.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.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.title = "cos sin and cos - sin"
line._in_browser() line._in_browser()

26
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);
}
'''

2
relauncher

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/zsh
while inotifywait -e modify **/*.py >> ~/.log/inotifywait.log 2>&1; do while inotifywait -e modify **/*.py >> ~/.log/inotifywait.log 2>&1; do
py.test pygal/test py.test pygal/test
done done

Loading…
Cancel
Save