From f8b9b84728a12b9b783c25e17c341c42062636dd Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Thu, 19 Sep 2013 15:29:58 +0200 Subject: [PATCH 01/10] Fix error if the raw_values is empty --- pygal/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygal/util.py b/pygal/util.py index 7cc2b33..d2cc7b1 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -346,7 +346,7 @@ def prepare_values(raw, config, cls): (width - len(raw_values)) * [None] # aligning values if len(raw_values) < width else [])): if isinstance(raw_value, dict): - value = raw_value.pop('value') + value = raw_value.pop('value', None) metadata[index] = raw_value else: value = raw_value From 699422c131187861988ebe24caab685f962e29ec Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Fri, 20 Sep 2013 11:04:37 +0200 Subject: [PATCH 02/10] Fixes tests on the worldmap --- pygal/test/test_graph.py | 3 +++ pygal/util.py | 1 + 2 files changed, 4 insertions(+) diff --git a/pygal/test/test_graph.py b/pygal/test/test_graph.py index 0040526..77df284 100644 --- a/pygal/test/test_graph.py +++ b/pygal/test/test_graph.py @@ -20,6 +20,7 @@ import os import pygal import uuid import sys +from pygal import i18n from pygal.util import cut from pygal._compat import u from pygal.test import pytest_generate_tests, make_data @@ -69,6 +70,8 @@ def test_metadata(Chart): v = range(7) if Chart == pygal.XY: v = list(map(lambda x: (x, x + 1), v)) + elif Chart == pygal.Worldmap: + v = list(map(lambda x: x, i18n.COUNTRIES)) chart.add('Serie with metadata', [ v[0], diff --git a/pygal/util.py b/pygal/util.py index d2cc7b1..8ab6958 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -346,6 +346,7 @@ def prepare_values(raw, config, cls): (width - len(raw_values)) * [None] # aligning values if len(raw_values) < width else [])): if isinstance(raw_value, dict): + raw_value = dict(raw_value) value = raw_value.pop('value', None) metadata[index] = raw_value else: From 3dead6e0a86e406e3e4df48f88fc4924a7a171e9 Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Fri, 20 Sep 2013 15:30:53 +0200 Subject: [PATCH 03/10] Adds supranational areas to pygal available areas are defined in the i18n file it can be used like this: - add("Legend", "eur") - add("Legend", ["eur", "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 From a2448a41714275f1d08ec76189ca154848444818 Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Fri, 20 Sep 2013 15:30:53 +0200 Subject: [PATCH 04/10] 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 From f781b56a7a1f92fcd26b2e387d221441c900686e Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Mon, 23 Sep 2013 13:49:57 +0200 Subject: [PATCH 05/10] Fixes supranational code leaving ghost generic --- pygal/ghost.py | 19 ------------------- pygal/graph/__init__.py | 1 + pygal/i18n.py | 11 +++++++---- pygal/test/test_config.py | 6 ++++-- pygal/test/test_graph.py | 4 ++-- pygal/util.py | 10 ++++++---- 6 files changed, 20 insertions(+), 31 deletions(-) diff --git a/pygal/ghost.py b/pygal/ghost.py index a991867..44f81ce 100644 --- a/pygal/ghost.py +++ b/pygal/ghost.py @@ -29,7 +29,6 @@ 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 @@ -66,29 +65,11 @@ 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/graph/__init__.py b/pygal/graph/__init__.py index d6418ce..d464591 100644 --- a/pygal/graph/__init__.py +++ b/pygal/graph/__init__.py @@ -38,5 +38,6 @@ CHARTS_NAMES = [ 'Gauge', 'DateY', 'Worldmap', + 'SupranationalWorldmap', 'Histogram' ] diff --git a/pygal/i18n.py b/pygal/i18n.py index d97651f..3b0b714 100644 --- a/pygal/i18n.py +++ b/pygal/i18n.py @@ -185,9 +185,12 @@ 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'] +EUROPE = ['at', 'be', 'bg', 'hr', 'cy', 'cz', 'dk', 'ee', 'fi', 'fr', 'de', + 'gr', 'hu', 'ie', 'it', 'lv', 'lt', 'lu', 'mt', 'nl', 'pl', 'pt', + 'ro', 'sk', 'si', 'es', 'se', 'gb'] + +EUR = ['be', 'de', 'ie', 'gr', 'es', 'fr', 'it', 'cy', 'lu', 'mt', 'nl', 'at', + 'pt', 'si', 'sk', 'fi', 'ee'] 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', @@ -196,7 +199,7 @@ OECD = ['au', 'at', 'be', 'ca', 'cl', 'cz', 'dk', 'ee', 'fi', 'fr', 'de', 'gr', NAFTA = ['ca', 'mx', 'us'] -SUPRANATIONAL = {'europe': EUROPE, 'oecd': OECD, 'nafta': NAFTA} +SUPRANATIONAL = {'europe': EUROPE, 'oecd': OECD, 'nafta': NAFTA, 'eur': EUR} def set_countries(countries): diff --git a/pygal/test/test_config.py b/pygal/test/test_config.py index a8b83df..1f688bf 100644 --- a/pygal/test/test_config.py +++ b/pygal/test/test_config.py @@ -17,7 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see . from pygal import ( - Line, Dot, Pie, Radar, Config, Bar, Funnel, Worldmap, Histogram, Gauge) + Line, Dot, Pie, Radar, Config, Bar, Funnel, Worldmap, + SupranationalWorldmap, Histogram, Gauge) from pygal._compat import u from pygal.test.utils import texts from pygal.test import pytest_generate_tests, make_data @@ -259,7 +260,8 @@ def test_no_data(): def test_include_x_axis(Chart): chart = Chart() - if Chart in (Pie, Radar, Funnel, Dot, Gauge, Worldmap, Histogram): + if Chart in (Pie, Radar, Funnel, Dot, Gauge, Worldmap, + SupranationalWorldmap, Histogram): return if not chart.cls._dual: data = 100, 200, 150 diff --git a/pygal/test/test_graph.py b/pygal/test/test_graph.py index 77df284..aa2931f 100644 --- a/pygal/test/test_graph.py +++ b/pygal/test/test_graph.py @@ -70,7 +70,7 @@ def test_metadata(Chart): v = range(7) if Chart == pygal.XY: v = list(map(lambda x: (x, x + 1), v)) - elif Chart == pygal.Worldmap: + elif Chart == pygal.Worldmap or Chart == pygal.SupranationalWorldmap: v = list(map(lambda x: x, i18n.COUNTRIES)) chart.add('Serie with metadata', [ @@ -94,7 +94,7 @@ def test_metadata(Chart): if Chart == pygal.Pie: # Slices with value 0 are not rendered assert len(v) - 1 == len(q('.tooltip-trigger').siblings('.value')) - elif Chart != pygal.Worldmap: + elif Chart != pygal.Worldmap and Chart != pygal.SupranationalWorldmap: # Tooltip are not working on worldmap assert len(v) == len(q('.tooltip-trigger').siblings('.value')) diff --git a/pygal/util.py b/pygal/util.py index 8ab6958..0111cd2 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -300,9 +300,10 @@ def prepare_values(raw, config, cls): from pygal.graph.datey import DateY from pygal.graph.histogram import Histogram from pygal.graph.worldmap import Worldmap + from pygal.graph.supranationalworldmap import SupranationalWorldmap if config.x_labels is None and hasattr(cls, 'x_labels'): config.x_labels = cls.x_labels - if config.zero == 0 and issubclass(cls, Worldmap): + if config.zero == 0 and issubclass(cls, (Worldmap, SupranationalWorldmap)): config.zero = 1 for key in ('x_labels', 'y_labels'): @@ -332,7 +333,7 @@ def prepare_values(raw, config, cls): metadata = {} values = [] if isinstance(raw_values, dict): - if issubclass(cls, Worldmap): + if issubclass(cls, (Worldmap, SupranationalWorldmap)): raw_values = list(raw_values.items()) else: value_list = [None] * width @@ -364,8 +365,9 @@ def prepare_values(raw, config, cls): value = (None, None) elif not is_list_like(value): value = (value, config.zero) - if issubclass(cls, DateY) or issubclass(cls, Worldmap): - value = (adapter(value[0]), value[1]) + if issubclass(cls, DateY) or issubclass( + cls, (Worldmap, SupranationalWorldmap)): + value = (adapter(value[0]), value[1]) else: value = list(map(adapter, value)) else: From a3ba1dacdf4e03312f00bdac846948849ca56eec Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Mon, 23 Sep 2013 14:58:07 +0200 Subject: [PATCH 06/10] Stupid forgotten file --- pygal/graph/supranationalworldmap.py | 101 +++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 pygal/graph/supranationalworldmap.py diff --git a/pygal/graph/supranationalworldmap.py b/pygal/graph/supranationalworldmap.py new file mode 100644 index 0000000..dd24c1d --- /dev/null +++ b/pygal/graph/supranationalworldmap.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +# This file is part of pygal +# +# A python svg graph plotting library +# Copyright © 2012-2013 Kozea +# +# This library is free software: you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with pygal. If not, see . +""" +Supranational Worldmap chart + +""" + +from __future__ import division +from pygal.graph.worldmap import Worldmap +from pygal.i18n import SUPRANATIONAL +from pygal.util import cut, decorate +from lxml import etree +import os + +with open(os.path.join( + os.path.dirname(__file__), + 'worldmap.svg')) as file: + MAP = file.read() + + +class SupranationalWorldmap(Worldmap): + """SupranationalWorldmap graph""" + def _plot(self): + map = etree.fromstring(MAP) + map.set('width', str(self.view.width)) + map.set('height', str(self.view.height)) + + for i, serie in enumerate(self.series): + safe_vals = list(filter( + lambda x: x is not None, cut(serie.values, 1))) + if not safe_vals: + continue + min_ = min(safe_vals) + max_ = max(safe_vals) + serie.values = self.replace_supranationals(serie.values) + for j, (country_code, value) in enumerate(serie.values): + if value is None: + continue + if max_ == min_: + ratio = 1 + else: + ratio = .3 + .7 * (value - min_) / (max_ - min_) + country = map.find('.//*[@id="%s"]' % country_code) + if country is None: + continue + cls = country.get('class', '').split(' ') + cls.append('color-%d' % i) + country.set('class', ' '.join(cls)) + country.set( + 'style', 'fill-opacity: %f' % ( + ratio)) + + metadata = serie.metadata.get(j) + if metadata: + parent = country.getparent() + node = decorate(self.svg, country, metadata) + if node != country: + country.remove(node) + index = parent.index(country) + parent.remove(country) + node.append(country) + parent.insert(index, node) + + last_node = len(country) > 0 and country[-1] + if last_node is not None and last_node.tag == 'title': + title_node = last_node + text = title_node.text + '\n' + else: + title_node = self.svg.node(country, 'title') + text = '' + title_node.text = text + '[%s] %s: %d' % ( + serie.title, + self.country_names[country_code], value) + + self.nodes['plot'].append(map) + + def replace_supranationals(self, values): + """Replaces the values if it contains a supranational code.""" + for i, (code, value) in enumerate(values[:]): + for suprakey in SUPRANATIONAL.keys(): + if suprakey == code: + values.extend( + [(country, value) for country in SUPRANATIONAL[code]]) + values.remove((code, value)) + return values From 3422462511d638a2b93372cf16bdeb940aa384c6 Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Mon, 23 Sep 2013 15:05:41 +0200 Subject: [PATCH 07/10] Supranations --- pygal/ghost.py | 19 +++++ pygal/graph/__init__.py | 1 - pygal/graph/supranationalworldmap.py | 101 +++++++++++++++++++++++++++ pygal/i18n.py | 11 ++- pygal/test/test_config.py | 6 +- pygal/test/test_graph.py | 4 +- pygal/util.py | 10 ++- 7 files changed, 132 insertions(+), 20 deletions(-) create mode 100644 pygal/graph/supranationalworldmap.py 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/graph/__init__.py b/pygal/graph/__init__.py index d464591..d6418ce 100644 --- a/pygal/graph/__init__.py +++ b/pygal/graph/__init__.py @@ -38,6 +38,5 @@ CHARTS_NAMES = [ 'Gauge', 'DateY', 'Worldmap', - 'SupranationalWorldmap', 'Histogram' ] diff --git a/pygal/graph/supranationalworldmap.py b/pygal/graph/supranationalworldmap.py new file mode 100644 index 0000000..dd24c1d --- /dev/null +++ b/pygal/graph/supranationalworldmap.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +# This file is part of pygal +# +# A python svg graph plotting library +# Copyright © 2012-2013 Kozea +# +# This library is free software: you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with pygal. If not, see . +""" +Supranational Worldmap chart + +""" + +from __future__ import division +from pygal.graph.worldmap import Worldmap +from pygal.i18n import SUPRANATIONAL +from pygal.util import cut, decorate +from lxml import etree +import os + +with open(os.path.join( + os.path.dirname(__file__), + 'worldmap.svg')) as file: + MAP = file.read() + + +class SupranationalWorldmap(Worldmap): + """SupranationalWorldmap graph""" + def _plot(self): + map = etree.fromstring(MAP) + map.set('width', str(self.view.width)) + map.set('height', str(self.view.height)) + + for i, serie in enumerate(self.series): + safe_vals = list(filter( + lambda x: x is not None, cut(serie.values, 1))) + if not safe_vals: + continue + min_ = min(safe_vals) + max_ = max(safe_vals) + serie.values = self.replace_supranationals(serie.values) + for j, (country_code, value) in enumerate(serie.values): + if value is None: + continue + if max_ == min_: + ratio = 1 + else: + ratio = .3 + .7 * (value - min_) / (max_ - min_) + country = map.find('.//*[@id="%s"]' % country_code) + if country is None: + continue + cls = country.get('class', '').split(' ') + cls.append('color-%d' % i) + country.set('class', ' '.join(cls)) + country.set( + 'style', 'fill-opacity: %f' % ( + ratio)) + + metadata = serie.metadata.get(j) + if metadata: + parent = country.getparent() + node = decorate(self.svg, country, metadata) + if node != country: + country.remove(node) + index = parent.index(country) + parent.remove(country) + node.append(country) + parent.insert(index, node) + + last_node = len(country) > 0 and country[-1] + if last_node is not None and last_node.tag == 'title': + title_node = last_node + text = title_node.text + '\n' + else: + title_node = self.svg.node(country, 'title') + text = '' + title_node.text = text + '[%s] %s: %d' % ( + serie.title, + self.country_names[country_code], value) + + self.nodes['plot'].append(map) + + def replace_supranationals(self, values): + """Replaces the values if it contains a supranational code.""" + for i, (code, value) in enumerate(values[:]): + for suprakey in SUPRANATIONAL.keys(): + if suprakey == code: + values.extend( + [(country, value) for country in SUPRANATIONAL[code]]) + values.remove((code, value)) + return values diff --git a/pygal/i18n.py b/pygal/i18n.py index 3b0b714..d97651f 100644 --- a/pygal/i18n.py +++ b/pygal/i18n.py @@ -185,12 +185,9 @@ COUNTRIES = { 'zw': 'Zimbabwe' } -EUROPE = ['at', 'be', 'bg', 'hr', 'cy', 'cz', 'dk', 'ee', 'fi', 'fr', 'de', - 'gr', 'hu', 'ie', 'it', 'lv', 'lt', 'lu', 'mt', 'nl', 'pl', 'pt', - 'ro', 'sk', 'si', 'es', 'se', 'gb'] - -EUR = ['be', 'de', 'ie', 'gr', 'es', 'fr', 'it', 'cy', 'lu', 'mt', 'nl', 'at', - 'pt', 'si', 'sk', 'fi', 'ee'] +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', @@ -199,7 +196,7 @@ OECD = ['au', 'at', 'be', 'ca', 'cl', 'cz', 'dk', 'ee', 'fi', 'fr', 'de', 'gr', NAFTA = ['ca', 'mx', 'us'] -SUPRANATIONAL = {'europe': EUROPE, 'oecd': OECD, 'nafta': NAFTA, 'eur': EUR} +SUPRANATIONAL = {'europe': EUROPE, 'oecd': OECD, 'nafta': NAFTA} def set_countries(countries): diff --git a/pygal/test/test_config.py b/pygal/test/test_config.py index 1f688bf..a8b83df 100644 --- a/pygal/test/test_config.py +++ b/pygal/test/test_config.py @@ -17,8 +17,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see . from pygal import ( - Line, Dot, Pie, Radar, Config, Bar, Funnel, Worldmap, - SupranationalWorldmap, Histogram, Gauge) + Line, Dot, Pie, Radar, Config, Bar, Funnel, Worldmap, Histogram, Gauge) from pygal._compat import u from pygal.test.utils import texts from pygal.test import pytest_generate_tests, make_data @@ -260,8 +259,7 @@ def test_no_data(): def test_include_x_axis(Chart): chart = Chart() - if Chart in (Pie, Radar, Funnel, Dot, Gauge, Worldmap, - SupranationalWorldmap, Histogram): + if Chart in (Pie, Radar, Funnel, Dot, Gauge, Worldmap, Histogram): return if not chart.cls._dual: data = 100, 200, 150 diff --git a/pygal/test/test_graph.py b/pygal/test/test_graph.py index aa2931f..77df284 100644 --- a/pygal/test/test_graph.py +++ b/pygal/test/test_graph.py @@ -70,7 +70,7 @@ def test_metadata(Chart): v = range(7) if Chart == pygal.XY: v = list(map(lambda x: (x, x + 1), v)) - elif Chart == pygal.Worldmap or Chart == pygal.SupranationalWorldmap: + elif Chart == pygal.Worldmap: v = list(map(lambda x: x, i18n.COUNTRIES)) chart.add('Serie with metadata', [ @@ -94,7 +94,7 @@ def test_metadata(Chart): if Chart == pygal.Pie: # Slices with value 0 are not rendered assert len(v) - 1 == len(q('.tooltip-trigger').siblings('.value')) - elif Chart != pygal.Worldmap and Chart != pygal.SupranationalWorldmap: + elif Chart != pygal.Worldmap: # Tooltip are not working on worldmap assert len(v) == len(q('.tooltip-trigger').siblings('.value')) diff --git a/pygal/util.py b/pygal/util.py index 0111cd2..8ab6958 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -300,10 +300,9 @@ def prepare_values(raw, config, cls): from pygal.graph.datey import DateY from pygal.graph.histogram import Histogram from pygal.graph.worldmap import Worldmap - from pygal.graph.supranationalworldmap import SupranationalWorldmap if config.x_labels is None and hasattr(cls, 'x_labels'): config.x_labels = cls.x_labels - if config.zero == 0 and issubclass(cls, (Worldmap, SupranationalWorldmap)): + if config.zero == 0 and issubclass(cls, Worldmap): config.zero = 1 for key in ('x_labels', 'y_labels'): @@ -333,7 +332,7 @@ def prepare_values(raw, config, cls): metadata = {} values = [] if isinstance(raw_values, dict): - if issubclass(cls, (Worldmap, SupranationalWorldmap)): + if issubclass(cls, Worldmap): raw_values = list(raw_values.items()) else: value_list = [None] * width @@ -365,9 +364,8 @@ def prepare_values(raw, config, cls): value = (None, None) elif not is_list_like(value): value = (value, config.zero) - if issubclass(cls, DateY) or issubclass( - cls, (Worldmap, SupranationalWorldmap)): - value = (adapter(value[0]), value[1]) + if issubclass(cls, DateY) or issubclass(cls, Worldmap): + value = (adapter(value[0]), value[1]) else: value = list(map(adapter, value)) else: From be5ad45048895728b64dd2d08ec7ac0db22b63e7 Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Mon, 23 Sep 2013 15:08:57 +0200 Subject: [PATCH 08/10] Adds supranations --- pygal/i18n.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pygal/i18n.py b/pygal/i18n.py index d97651f..3b0b714 100644 --- a/pygal/i18n.py +++ b/pygal/i18n.py @@ -185,9 +185,12 @@ 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'] +EUROPE = ['at', 'be', 'bg', 'hr', 'cy', 'cz', 'dk', 'ee', 'fi', 'fr', 'de', + 'gr', 'hu', 'ie', 'it', 'lv', 'lt', 'lu', 'mt', 'nl', 'pl', 'pt', + 'ro', 'sk', 'si', 'es', 'se', 'gb'] + +EUR = ['be', 'de', 'ie', 'gr', 'es', 'fr', 'it', 'cy', 'lu', 'mt', 'nl', 'at', + 'pt', 'si', 'sk', 'fi', 'ee'] 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', @@ -196,7 +199,7 @@ OECD = ['au', 'at', 'be', 'ca', 'cl', 'cz', 'dk', 'ee', 'fi', 'fr', 'de', 'gr', NAFTA = ['ca', 'mx', 'us'] -SUPRANATIONAL = {'europe': EUROPE, 'oecd': OECD, 'nafta': NAFTA} +SUPRANATIONAL = {'europe': EUROPE, 'oecd': OECD, 'nafta': NAFTA, 'eur': EUR} def set_countries(countries): From 921352c0933cece12e7b74d153b5281c96b72c7a Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Mon, 23 Sep 2013 15:11:41 +0200 Subject: [PATCH 09/10] Removes from ghost --- pygal/ghost.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/pygal/ghost.py b/pygal/ghost.py index a991867..44f81ce 100644 --- a/pygal/ghost.py +++ b/pygal/ghost.py @@ -29,7 +29,6 @@ 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 @@ -66,29 +65,11 @@ 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) From e226aa540519de3377faa5419d82a4ca354315f4 Mon Sep 17 00:00:00 2001 From: Jean-Marc Martins Date: Mon, 23 Sep 2013 15:20:41 +0200 Subject: [PATCH 10/10] Fighting versus git --- pygal/graph/__init__.py | 1 + pygal/test/test_config.py | 6 ++++-- pygal/test/test_graph.py | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pygal/graph/__init__.py b/pygal/graph/__init__.py index d6418ce..d464591 100644 --- a/pygal/graph/__init__.py +++ b/pygal/graph/__init__.py @@ -38,5 +38,6 @@ CHARTS_NAMES = [ 'Gauge', 'DateY', 'Worldmap', + 'SupranationalWorldmap', 'Histogram' ] diff --git a/pygal/test/test_config.py b/pygal/test/test_config.py index a8b83df..1f688bf 100644 --- a/pygal/test/test_config.py +++ b/pygal/test/test_config.py @@ -17,7 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see . from pygal import ( - Line, Dot, Pie, Radar, Config, Bar, Funnel, Worldmap, Histogram, Gauge) + Line, Dot, Pie, Radar, Config, Bar, Funnel, Worldmap, + SupranationalWorldmap, Histogram, Gauge) from pygal._compat import u from pygal.test.utils import texts from pygal.test import pytest_generate_tests, make_data @@ -259,7 +260,8 @@ def test_no_data(): def test_include_x_axis(Chart): chart = Chart() - if Chart in (Pie, Radar, Funnel, Dot, Gauge, Worldmap, Histogram): + if Chart in (Pie, Radar, Funnel, Dot, Gauge, Worldmap, + SupranationalWorldmap, Histogram): return if not chart.cls._dual: data = 100, 200, 150 diff --git a/pygal/test/test_graph.py b/pygal/test/test_graph.py index 77df284..aa2931f 100644 --- a/pygal/test/test_graph.py +++ b/pygal/test/test_graph.py @@ -70,7 +70,7 @@ def test_metadata(Chart): v = range(7) if Chart == pygal.XY: v = list(map(lambda x: (x, x + 1), v)) - elif Chart == pygal.Worldmap: + elif Chart == pygal.Worldmap or Chart == pygal.SupranationalWorldmap: v = list(map(lambda x: x, i18n.COUNTRIES)) chart.add('Serie with metadata', [ @@ -94,7 +94,7 @@ def test_metadata(Chart): if Chart == pygal.Pie: # Slices with value 0 are not rendered assert len(v) - 1 == len(q('.tooltip-trigger').siblings('.value')) - elif Chart != pygal.Worldmap: + elif Chart != pygal.Worldmap and Chart != pygal.SupranationalWorldmap: # Tooltip are not working on worldmap assert len(v) == len(q('.tooltip-trigger').siblings('.value'))