From a2448a41714275f1d08ec76189ca154848444818 Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Fri, 20 Sep 2013 15:30:53 +0200 Subject: [PATCH] Adds supranational areas to pygal available areas are defined in the i18n file it can be used like this: - add("Legend", "europe") - add("Legend", ["europe", "oecd"]) - or even add("Legend", {"nafta", 1000}) if you wish to add a value for the entire area --- pygal/ghost.py | 19 +++++++++++++++++++ pygal/i18n.py | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pygal/ghost.py b/pygal/ghost.py index 44f81ce..a991867 100644 --- a/pygal/ghost.py +++ b/pygal/ghost.py @@ -29,6 +29,7 @@ import sys from pygal.config import Config from pygal._compat import u, is_list_like from pygal.graph import CHARTS_NAMES +from pygal.i18n import SUPRANATIONAL from pygal.util import prepare_values from uuid import uuid4 @@ -65,11 +66,29 @@ class Ghost(object): """Add a serie to this graph""" if not is_list_like(values) and not isinstance(values, dict): values = [values] + values = self.replace_supranationals(values) if secondary: self.raw_series2.append((title, values)) else: self.raw_series.append((title, values)) + def replace_supranationals(self, values): + """Replaces the values if it contains a supranational code.""" + from pygal import Worldmap + if not isinstance(self, Worldmap): + return values + for suprakey in SUPRANATIONAL.keys(): + if suprakey in values: + if not isinstance(values, dict): + del values[values.index(suprakey)] + values.extend(SUPRANATIONAL[suprakey]) + else: + values.update( + dict([(code, values[suprakey]) + for code in SUPRANATIONAL[suprakey]])) + del values[suprakey] + return values + def make_series(self, series): return prepare_values(series, self.config, self.cls) diff --git a/pygal/i18n.py b/pygal/i18n.py index bec7147..d97651f 100644 --- a/pygal/i18n.py +++ b/pygal/i18n.py @@ -185,6 +185,19 @@ COUNTRIES = { 'zw': 'Zimbabwe' } +EUROPE = ['bg', 'cs', 'da', 'de', 'et', 'el', 'en', 'es', 'fr', 'ga', 'hr', + 'it', 'lt', 'lv', 'hu', 'mt', 'nl', 'pl', 'pt', 'ro', 'sk', 'sl', + 'fi', 'sv'] + +OECD = ['au', 'at', 'be', 'ca', 'cl', 'cz', 'dk', 'ee', 'fi', 'fr', 'de', 'gr', + 'hu', 'is', 'ie', 'il', 'it', 'jp', 'kr', 'lu', 'mx', 'nl', 'nz', 'no', + 'pl', 'pt', 'sk', 'si', 'es', 'se', 'ch', 'tr', 'gb', 'us'] + +NAFTA = ['ca', 'mx', 'us'] + + +SUPRANATIONAL = {'europe': EUROPE, 'oecd': OECD, 'nafta': NAFTA} + def set_countries(countries): global COUNTRIES