From 41f1ecd11b95c0dc69a445b1a2ea9a0fcd011d61 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Fri, 13 Apr 2012 18:07:16 +0200 Subject: [PATCH] Cycle colors in css --- pygal/graph/radar.py | 5 ++-- pygal/style.py | 4 ++- pygal/test/test_style.py | 60 ++++++++++++++++++++++++++++++++++++++++ pygal/util.py | 11 ++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/pygal/graph/radar.py b/pygal/graph/radar.py index f6eb9e8..5c8494b 100644 --- a/pygal/graph/radar.py +++ b/pygal/graph/radar.py @@ -109,8 +109,9 @@ class Radar(Line): x_pos = [.5 * pi + i * delta for i in range(self._len + 1)] for serie in self.series: vals = list(serie.values) - vals.append(vals[0]) - serie.metadata.append(serie.metadata[0]) + # TODO: Fix metadata add here + # vals.append(vals[0]) + # serie.metadata.append(serie.metadata[0]) vals = [val if val != None else 0 for val in vals] serie.points = [ (v, x_pos[i]) diff --git a/pygal/style.py b/pygal/style.py index b06a6ec..36ed507 100644 --- a/pygal/style.py +++ b/pygal/style.py @@ -19,6 +19,7 @@ """ Charts styling """ +from pygal.util import cycle_fill class Style(object): @@ -50,6 +51,7 @@ class Style(object): @property def colors(self): """Get the css color list""" + def color(tupl): """Make a color css""" return ( @@ -57,7 +59,7 @@ class Style(object): ' stroke: {1};\n' ' fill: {1};\n' '}}\n'.format(*tupl)) - return '\n'.join(map(color, enumerate(self._colors))) + return '\n'.join(map(color, enumerate(cycle_fill(self._colors, 16)))) def to_dict(self): config = {} diff --git a/pygal/test/test_style.py b/pygal/test/test_style.py index 61e38ce..991e7a5 100644 --- a/pygal/test/test_style.py +++ b/pygal/test/test_style.py @@ -41,4 +41,64 @@ def test_colors(): stroke: rgb(12, 231, 3); fill: rgb(12, 231, 3); } + +.color-4 { + stroke: red; + fill: red; +} + +.color-5 { + stroke: #231A3b; + fill: #231A3b; +} + +.color-6 { + stroke: #ff0; + fill: #ff0; +} + +.color-7 { + stroke: rgb(12, 231, 3); + fill: rgb(12, 231, 3); +} + +.color-8 { + stroke: red; + fill: red; +} + +.color-9 { + stroke: #231A3b; + fill: #231A3b; +} + +.color-10 { + stroke: #ff0; + fill: #ff0; +} + +.color-11 { + stroke: rgb(12, 231, 3); + fill: rgb(12, 231, 3); +} + +.color-12 { + stroke: red; + fill: red; +} + +.color-13 { + stroke: #231A3b; + fill: #231A3b; +} + +.color-14 { + stroke: #ff0; + fill: #ff0; +} + +.color-15 { + stroke: rgb(12, 231, 3); + fill: rgb(12, 231, 3); +} ''' diff --git a/pygal/util.py b/pygal/util.py index 133251d..599fe76 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -24,6 +24,7 @@ Various utils from __future__ import division from decimal import Decimal from math import floor, pi, log, log10, ceil +from itertools import cycle ORDERS = u"yzafpnµm kMGTPEZY" @@ -189,6 +190,7 @@ def get_texts_box(texts, fs): def decorate(svg, node, metadata): + """Add metedata next to a node""" if hasattr(metadata, 'xlink'): xlink = metadata.xlink if not isinstance(xlink, dict): @@ -202,6 +204,15 @@ def decorate(svg, node, metadata): return node +def cycle_fill(short_list, max_len): + """Fill a list to max_len using a cycle of it""" + short_list = list(short_list) + list_cycle = cycle(short_list) + while len(short_list) < 16: + short_list.append(list_cycle.next()) + return short_list + + # Stolen from brownie http://packages.python.org/Brownie/ class cached_property(object): """Optimize a static property"""