Browse Source

Merge branch 'master' into donut_label_with_links

pull/78/head
Florian Mounier 11 years ago
parent
commit
0e1a7259db
  1. 1
      pygal/graph/__init__.py
  2. 101
      pygal/graph/supranationalworldmap.py
  3. 16
      pygal/i18n.py
  4. 6
      pygal/test/test_config.py
  5. 5
      pygal/test/test_graph.py
  6. 3
      pygal/util.py

1
pygal/graph/__init__.py

@ -38,5 +38,6 @@ CHARTS_NAMES = [
'Gauge',
'DateY',
'Worldmap',
'SupranationalWorldmap',
'Histogram'
]

101
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 <http://www.gnu.org/licenses/>.
"""
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

16
pygal/i18n.py

@ -185,6 +185,22 @@ 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']
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, 'eur': EUR}
def set_countries(countries):
global COUNTRIES

6
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 <http://www.gnu.org/licenses/>.
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

5
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 or Chart == pygal.SupranationalWorldmap:
v = list(map(lambda x: x, i18n.COUNTRIES))
chart.add('Serie with metadata', [
v[0],
@ -91,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'))

3
pygal/util.py

@ -352,7 +352,8 @@ 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')
raw_value = dict(raw_value)
value = raw_value.pop('value', None)
metadata[index] = raw_value
else:
value = raw_value

Loading…
Cancel
Save