Skip to content

Commit

Permalink
Merge pull request Kozea#154 from blakev/master
Browse files Browse the repository at this point in the history
Add line stroke customization parameters to style.py
  • Loading branch information
paradoxxxzero committed Nov 21, 2014
2 parents ebbab47 + 48f7112 commit 18ee2f6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pygal/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
fill-opacity: {{ style.opacity_hover }};
}

{{ id }}.series {
stroke-width: {{ style.stroke_width }};
stroke-linejoin: {{ style.stroke_style }};
stroke-linecap: {{ style.stroke_style }};
stroke-dasharray: {{ style.stroke_dasharray }};
}

{{ id }}.series text {
fill: {{ style.foreground_light }};
}
Expand Down
30 changes: 30 additions & 0 deletions pygal/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
from pygal import colors
from pygal.colors import darken, lighten
import sys
import re

re_dasharray_delimiters = re.compile(r'[\.|,|x|\||\- ]+', re.I)

class Style(object):
"""Styling class containing colors for the css generation"""
Expand All @@ -38,6 +40,9 @@ def __init__(
font_family='monospace', # Monospaced font is highly encouraged
opacity='.8',
opacity_hover='.9',
stroke_width='1',
stroke_style='round',
stroke_dasharray=(0,0),
transition='250ms',
colors=(
'#ff5995', '#b6e354', '#feed6c', '#8cedff', '#9e6ffe',
Expand All @@ -52,9 +57,34 @@ def __init__(
self.font_family = font_family
self.opacity = opacity
self.opacity_hover = opacity_hover
self.stroke_width = stroke_width
self.stroke_style = stroke_style
self.stroke_dasharray = stroke_dasharray
self.transition = transition
self.colors = colors

self.validate_stroke_values()

def validate_stroke_values(self):
# stroke_width
self.stroke_width = float(self.stroke_width)

# stroke_style
self.stroke_style = self.stroke_style.lower().strip()
if not self.stroke_style in ['round', 'bevel', 'miter']:
self.stroke_style = 'round'

# stroke_dasharray
if isinstance(self.stroke_dasharray, (list, tuple)):
self.stroke_dasharray = '%d,%d' % self.stroke_dasharray

if isinstance(self.stroke_dasharray, str):
self.stroke_dasharray = re.sub(re_dasharray_delimiters, ',', self.stroke_dasharray)

if not isinstance(self.stroke_dasharray, str):
raise ValueError('stroke_dasharray not in proper form: tuple(int,int)')


def get_colors(self, prefix):
"""Get the css color list"""

Expand Down
33 changes: 33 additions & 0 deletions pygal/test/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
from pygal.style import Style
from pygal import Line
from pygal.style import (
LightStyle,
Expand All @@ -42,3 +43,35 @@ def test_parametric_styles_with_parameters():
line.add('_', [1, 2, 3])
line.x_labels = 'abc'
assert line.render()

def test_stroke_style():
s = Style(stroke_style = 'round')
assert s.stroke_style == 'round'
s = Style(stroke_style = 'bevel')
assert s.stroke_style == 'bevel'
s = Style(stroke_style = 'miter')
assert s.stroke_style == 'miter'
s = Style(stroke_style = 'rounded')
assert s.stroke_style == 'round'
s = Style(stroke_style = 'invalid derp')
assert s.stroke_style == 'round'

def test_stroke_dasharray():
s = Style(stroke_dasharray = (0,0))
assert s.stroke_dasharray == '0,0'
s = Style(stroke_dasharray = (.5,.5))
assert s.stroke_dasharray == '0,0'
s = Style(stroke_dasharray = (.9,.9))
assert s.stroke_dasharray == '0,0'
s = Style(stroke_dasharray = (1.9,1.9))
assert s.stroke_dasharray == '1,1'

def test_stroke_dasharray_input_types():
s = Style(stroke_dasharray = (0,0))
assert s.stroke_dasharray == '0,0'
s = Style(stroke_dasharray = '0,0')
assert s.stroke_dasharray == '0,0'
s = Style(stroke_dasharray = '0x0')
assert s.stroke_dasharray == '0,0'
s = Style(stroke_dasharray = '0 0')
assert s.stroke_dasharray == '0,0'

0 comments on commit 18ee2f6

Please sign in to comment.