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 ( from pygal import (
Bar, Gauge, Pyramid, Funnel, Dot, StackedBar, StackedLine, XY, Bar, Gauge, Pyramid, Funnel, Dot, StackedBar, StackedLine, XY,
CHARTS_BY_NAME, Config, Line, Worldmap, Histogram, Box, SwissMapCantons, 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 from pygal.style import styles, Style, RotateStyle
@ -425,6 +426,19 @@ def get_test_routes(app):
wmap.title = 'World Map !!' wmap.title = 'World Map !!'
return wmap.render_response() 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') @app.route('/test/frenchmapdepartments')
def test_frenchmapdepartments(): def test_frenchmapdepartments():
fmap = FrenchMapDepartments(style=choice(list(styles.values()))) fmap = FrenchMapDepartments(style=choice(list(styles.values())))
@ -434,7 +448,7 @@ def get_test_routes(app):
for _ in range(randint(1, 5))]) for _ in range(randint(1, 5))])
fmap.add('links', [{ fmap.add('links', [{
'value': ('69', 10), 'value': (69, 10),
'label': '\o/', 'label': '\o/',
'xlink': 'http://google.com?q=69' 'xlink': 'http://google.com?q=69'
}, { }, {

16
pygal/graph/frenchmap.py

@ -25,6 +25,8 @@ from __future__ import division
from collections import defaultdict from collections import defaultdict
from pygal.graph.map import BaseMap from pygal.graph.map import BaseMap
from pygal._compat import u from pygal._compat import u
from numbers import Number
import os import os
@ -174,7 +176,17 @@ with open(os.path.join(
DPT_MAP = file.read() 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""" """French department map"""
x_labels = list(DEPARTMENTS.keys()) x_labels = list(DEPARTMENTS.keys())
area_names = DEPARTMENTS area_names = DEPARTMENTS
@ -189,7 +201,7 @@ with open(os.path.join(
REG_MAP = file.read() REG_MAP = file.read()
class FrenchMapRegions(BaseMap): class FrenchMapRegions(IntCodeMixin, BaseMap):
"""French regions map""" """French regions map"""
x_labels = list(REGIONS.keys()) x_labels = list(REGIONS.keys())
area_names = REGIONS area_names = REGIONS

15
pygal/graph/map.py

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

19
pygal/graph/worldmap.py

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

Loading…
Cancel
Save