Browse Source

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
pull/63/head
Jean-Marc Martins 11 years ago
parent
commit
a2448a4171
  1. 19
      pygal/ghost.py
  2. 13
      pygal/i18n.py

19
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)

13
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

Loading…
Cancel
Save