Browse Source

More fix on maps

pull/212/head
Florian Mounier 10 years ago
parent
commit
7ce0703fad
  1. 18
      demo/moulinrouge/tests.py
  2. 16
      pygal/graph/frenchmap.py
  3. 15
      pygal/graph/map.py
  4. 19
      pygal/graph/worldmap.py

18
demo/moulinrouge/tests.py

@ -3,7 +3,8 @@
from pygal import (
Bar, Gauge, Pyramid, Funnel, Dot, StackedBar, StackedLine, XY,
CHARTS_BY_NAME, Config, Line, Worldmap, Histogram, Box, SwissMapCantons,
FrenchMapDepartments, FrenchMapRegions, Pie, Treemap, TimeLine, DateLine)
FrenchMapDepartments, FrenchMapRegions, Pie, Treemap, TimeLine, DateLine,
SupranationalWorldmap)
from pygal.style import styles, Style, RotateStyle
@ -425,6 +426,19 @@ def get_test_routes(app):
wmap.title = 'World Map !!'
return wmap.render_response()
@app.route('/test/supranational')
def test_supranational():
wmap = SupranationalWorldmap(style=choice(list(styles.values())))
# wmap.add('1st', [('nafta', 100), ('oecd', 10)])
wmap.add('2nd', [{
'value': ('europe', 10),
'label': 'EUROP3',
'xlink': 'http://google.com?q=tw'
}])
wmap.title = 'Supra World Map !!'
return wmap.render_response()
@app.route('/test/frenchmapdepartments')
def test_frenchmapdepartments():
fmap = FrenchMapDepartments(style=choice(list(styles.values())))
@ -434,7 +448,7 @@ def get_test_routes(app):
for _ in range(randint(1, 5))])
fmap.add('links', [{
'value': ('69', 10),
'value': (69, 10),
'label': '\o/',
'xlink': 'http://google.com?q=69'
}, {

16
pygal/graph/frenchmap.py

@ -25,6 +25,8 @@ from __future__ import division
from collections import defaultdict
from pygal.graph.map import BaseMap
from pygal._compat import u
from numbers import Number
import os
@ -174,7 +176,17 @@ with open(os.path.join(
DPT_MAP = file.read()
class FrenchMapDepartments(BaseMap):
class IntCodeMixin(object):
def adapt_code(self, area_code):
if isinstance(area_code, Number):
if area_code > 100:
return '%3d' % area_code
return '%2d' % area_code
return super(IntCodeMixin, self).adapt_code(area_code)
class FrenchMapDepartments(IntCodeMixin, BaseMap):
"""French department map"""
x_labels = list(DEPARTMENTS.keys())
area_names = DEPARTMENTS
@ -189,7 +201,7 @@ with open(os.path.join(
REG_MAP = file.read()
class FrenchMapRegions(BaseMap):
class FrenchMapRegions(IntCodeMixin, BaseMap):
"""French regions map"""
x_labels = list(REGIONS.keys())
area_names = REGIONS

15
pygal/graph/map.py

@ -21,7 +21,6 @@ from __future__ import division
from pygal.graph.graph import Graph
from pygal.util import cut, cached_property, decorate
from pygal.etree import etree
from numbers import Number
class BaseMap(Graph):
@ -36,8 +35,11 @@ class BaseMap(Graph):
for val in serie.values
if val[1] is not None]
def get_values(self, serie):
return serie.values
def enumerate_values(self, serie):
return enumerate(serie.values)
def adapt_code(self, area_code):
return area_code
def _plot(self):
map = etree.fromstring(self.svg_map)
@ -51,11 +53,8 @@ class BaseMap(Graph):
continue
min_ = min(safe_vals)
max_ = max(safe_vals)
for j, (area_code, value) in enumerate(self.get_values(serie)):
# TODO: Generalize
if isinstance(area_code, Number):
area_code = '%2d' % area_code
for j, (area_code, value) in self.enumerate_values(serie):
area_code = self.adapt_code(area_code)
if value is None:
continue
if max_ == min_:

19
pygal/graph/worldmap.py

@ -22,12 +22,12 @@ Worldmap chart
"""
from __future__ import division
from pygal.util import cut, cached_property, decorate
from pygal.util import cached_property
from pygal.graph.map import BaseMap
from pygal.i18n import COUNTRIES, SUPRANATIONAL
from pygal.etree import etree
import os
with open(os.path.join(
os.path.dirname(__file__), 'maps',
'worldmap.svg')) as file:
@ -61,15 +61,8 @@ class Worldmap(BaseMap):
class SupranationalWorldmap(Worldmap):
"""SupranationalWorldmap graph"""
def get_values(self, serie):
return self.replace_supranationals(serie.values)
def replace_supranationals(self, values):
def enumerate_values(self, serie):
"""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
for i, (code, value) in enumerate(serie.values):
for subcode in SUPRANATIONAL.get(code, []):
yield i, (subcode, value)

Loading…
Cancel
Save