Browse Source

Better js integration in maps. Use the normal tooltip

pull/264/head
Florian Mounier 10 years ago
parent
commit
a1839a7d99
  1. 15
      demo/moulinrouge/tests.py
  2. 2
      docs/changelog.rst
  3. 8
      pygal/graph/dot.py
  4. 3
      pygal/graph/line.py
  5. 33
      pygal/graph/map.py
  6. 6
      pygal/graph/xy.py
  7. 3
      pygal/style.py

15
demo/moulinrouge/tests.py

@ -472,25 +472,30 @@ def get_test_routes(app):
@app.route('/test/worldmap') @app.route('/test/worldmap')
def test_worldmap(): def test_worldmap():
wmap = world.World(style=choice(list(styles.values()))) wmap = world.World(print_values=True, style=choice(list(styles.values())))
wmap.add('1st', [('fr', 100), {
wmap.add('1st', [('fr', 100), ('us', 10)]) 'value': ('us', 10),
'node': {'style': 'fill: red'}
}
])
wmap.add('2nd', [('jp', 1), ('ru', 7), ('uk', 0)]) wmap.add('2nd', [('jp', 1), ('ru', 7), ('uk', 0)])
wmap.add('3rd', ['ch', 'cz', 'ca', 'cn']) wmap.add('3rd', ['ch', 'cz', 'ca', 'cn'])
wmap.add('4th', {'br': 12, 'bo': 1, 'bu': 23, 'fr': 34}) wmap.add('4th', {'br': 12, 'bo': 1, 'bu': 23, 'fr': 34})
wmap.add('5th', [{ wmap.add('5th', [{
'value': ('tw', 10), 'value': ('tw', 10),
'label': 'First label', 'label': 'First label',
'xlink': 'http://google.com?q=tw' 'xlink': 'http://google.com?q=tw',
}, { }, {
'value': ('bw', 20), 'value': ('bw', 20),
'label': 'Second one', 'label': 'Second one',
'xlink': 'http://google.com?q=bw' 'xlink': 'http://google.com?q=bw',
'node': {'style': 'fill: blue'}
}, { }, {
'value': ('mw', 40), 'value': ('mw', 40),
'label': 'Last' 'label': 'Last'
}]) }])
wmap.title = 'World Map !!' wmap.title = 'World Map !!'
wmap.value_formatter = lambda x: '%d%%' % x
return wmap.render_response() return wmap.render_response()
@app.route('/test/supranational') @app.route('/test/supranational')

2
docs/changelog.rst

@ -38,6 +38,8 @@ Changelog
* Add auto ``print_value`` color + a configurable ``value_colors`` list in style * Add auto ``print_value`` color + a configurable ``value_colors`` list in style
* Add ``guide_stroke_dasharray`` and ``guide_stroke_dasharray`` in style to customize guides (#242) (thanks cbergmiller) * Add ``guide_stroke_dasharray`` and ``guide_stroke_dasharray`` in style to customize guides (#242) (thanks cbergmiller)
* Refactor label processing in a ``_compute_x_labels`` and ``_compute_y_labels`` method. Handle both string and numbers for all charts. Create a ``Dual`` base chart for dual axis charts. (#236) * Refactor label processing in a ``_compute_x_labels`` and ``_compute_y_labels`` method. Handle both string and numbers for all charts. Create a ``Dual`` base chart for dual axis charts. (#236)
* Better js integration in maps. Use the normal tooltip.
1.7.0 1.7.0
===== =====

8
pygal/graph/dot.py

@ -23,11 +23,13 @@ the bigger the dot
""" """
from __future__ import division from __future__ import division
from pygal.util import decorate, cut, safe_enumerate, cached_property, alter
from pygal.graph.graph import Graph
from pygal.view import View, ReverseView
from math import log10 from math import log10
from pygal.graph.graph import Graph
from pygal.util import alter, cached_property, decorate, safe_enumerate
from pygal.view import ReverseView, View
class Dot(Graph): class Dot(Graph):

3
pygal/graph/line.py

@ -23,8 +23,9 @@ connected by straight segments
""" """
from __future__ import division from __future__ import division
from pygal.graph.graph import Graph from pygal.graph.graph import Graph
from pygal.util import cached_property, compute_scale, decorate, alter, cut from pygal.util import alter, cached_property, decorate
class Line(Graph): class Line(Graph):

33
pygal/graph/map.py

@ -24,9 +24,10 @@ https://github.com/Kozea/pygal_maps_world
""" """
from __future__ import division from __future__ import division
from pygal.graph.graph import Graph
from pygal.util import cut, cached_property, decorate
from pygal.etree import etree from pygal.etree import etree
from pygal.graph.graph import Graph
from pygal.util import alter, cached_property, cut, decorate
class BaseMap(Graph): class BaseMap(Graph):
@ -51,6 +52,10 @@ class BaseMap(Graph):
"""Hook to change the area code""" """Hook to change the area code"""
return area_code return area_code
def _get_value(self, value):
"""Get the value formatted for tooltip"""
return '%s: %s' % (self.area_names[value[0]], self._format(value[1]))
def _plot(self): def _plot(self):
"""Insert a map in the chart and apply data on it""" """Insert a map in the chart and apply data on it"""
map = etree.fromstring(self.svg_map) map = etree.fromstring(self.svg_map)
@ -88,10 +93,13 @@ class BaseMap(Graph):
for area in areae: for area in areae:
cls = area.get('class', '').split(' ') cls = area.get('class', '').split(' ')
cls.append('color-%d' % i) cls.append('color-%d' % i)
cls.append('serie-%d' % i)
cls.append('series')
area.set('class', ' '.join(cls)) area.set('class', ' '.join(cls))
area.set('style', 'fill-opacity: %f' % (ratio)) area.set('style', 'fill-opacity: %f' % (ratio))
metadata = serie.metadata.get(j) metadata = serie.metadata.get(j)
if metadata: if metadata:
node = decorate(self.svg, area, metadata) node = decorate(self.svg, area, metadata)
if node != area: if node != area:
@ -104,17 +112,16 @@ class BaseMap(Graph):
node.append(area) node.append(area)
g.insert(index, node) g.insert(index, node)
last_node = len(area) > 0 and area[-1] for node in area:
if last_node is not None and last_node.tag == 'title': cls = node.get('class', '').split(' ')
title_node = last_node cls.append('reactive')
text = title_node.text + '\n' cls.append('tooltip-trigger')
else: cls.append('map-area')
title_node = self.svg.node(area, 'title') node.set('class', ' '.join(cls))
text = '' alter(node, metadata)
title_node.text = text + '[%s] %s: %s' % ( value = self._get_value((area_code, value))
serie.title, self._tooltip_data(area, value, 0, 0, classes='auto')
self.area_names[area_code], self._format(value))
self.nodes['plot'].append(map) self.nodes['plot'].append(map)

6
pygal/graph/xy.py

@ -23,10 +23,12 @@ straight segments.
""" """
from __future__ import division from __future__ import division
from functools import reduce from functools import reduce
from pygal.util import compute_scale, cached_property, compose, ident, cut
from pygal.graph.line import Line
from pygal.graph.dual import Dual from pygal.graph.dual import Dual
from pygal.graph.line import Line
from pygal.util import cached_property, compose, ident
class XY(Line, Dual): class XY(Line, Dual):

3
pygal/style.py

@ -140,7 +140,8 @@ class Style(object):
if i < len(self.value_colors) and self.value_colors[i] is not None: if i < len(self.value_colors) and self.value_colors[i] is not None:
value_colors.append(self.value_colors[i]) value_colors.append(self.value_colors[i])
else: else:
value_colors.append('white' if is_foreground_light(self.colors[i]) else 'black') value_colors.append('white' if is_foreground_light(
colors[i]) else 'black')
return '\n'.join(chain( return '\n'.join(chain(
map(color, enumerate(colors)), map(color, enumerate(colors)),

Loading…
Cancel
Save