Skip to content

Commit 62a46d3

Browse files
committed
Fixes #341: Added highlight_function functionality for choropleth maps
1 parent 6bb2d90 commit 62a46d3

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

folium/features.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ class GeoJson(Layer):
297297
>>> GeoJson(geojson, style_function=style_function)
298298
"""
299299
def __init__(self, data, style_function=None, name=None,
300-
overlay=True, control=True, smooth_factor=None):
300+
overlay=True, control=True, smooth_factor=None,
301+
highlight_function=None):
301302
super(GeoJson, self).__init__(name=name, overlay=overlay,
302303
control=control)
303304
self._name = 'GeoJson'
@@ -333,16 +334,45 @@ def style_function(x):
333334
return {}
334335
self.style_function = style_function
335336

337+
self.highlight_function = highlight_function
338+
336339
self.smooth_factor = smooth_factor
337340

338341
self._template = Template(u"""
339342
{% macro script(this, kwargs) %}
343+
344+
{% if this.highlight_function is not none %}
345+
{{this.get_name()}}_onEachFeature = function onEachFeature(feature, layer) {
346+
layer.on({
347+
mouseout: function(e) {
348+
e.target.setStyle(e.target.feature.properties.style);},
349+
mouseover: function(e) {
350+
e.target.setStyle(e.target.feature.properties.highlight);},
351+
click: function(e) {
352+
{{this._parent.get_name()}}.fitBounds(e.target.getBounds());}
353+
});
354+
};
355+
{% endif %}
356+
340357
var {{this.get_name()}} = L.geoJson(
341358
{% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %}
342-
{% if this.smooth_factor is not none %}
343-
, {smoothFactor:{{this.smooth_factor}}}
344-
{% endif %}).addTo({{this._parent.get_name()}});
359+
{% if this.smooth_factor is not none or this.highlight_function is not none %}
360+
, {
361+
{% if this.smooth_factor is not none %}
362+
smoothFactor:{{this.smooth_factor}}}
363+
{% endif %}
364+
365+
{% if this.highlight_function is not none %}
366+
{% if this.smooth_factor is not none %}
367+
,
368+
{% endif %}
369+
onEachFeature: {{this.get_name()}}_onEachFeature
370+
{% endif %}
371+
}
372+
{% endif %}
373+
).addTo({{this._parent.get_name()}});
345374
{{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;});
375+
346376
{% endmacro %}
347377
""") # noqa
348378

@@ -361,6 +391,7 @@ def style_data(self):
361391

362392
for feature in self.data['features']:
363393
feature.setdefault('properties', {}).setdefault('style', {}).update(self.style_function(feature)) # noqa
394+
feature.setdefault('properties', {}).setdefault('highlight', {}).update(self.highlight_function(feature)) # noqa
364395
return json.dumps(self.data, sort_keys=True)
365396

366397
def _get_self_bounds(self):

folium/folium.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
ClickForMarker, TopoJson, PolyLine, MultiPolyLine,
2323
)
2424

25-
2625
def initialize_notebook():
2726
"""Initialize the IPython notebook display elements."""
2827
warnings.warn("%s is deprecated and no longer required." %
2928
("initialize_notebook",),
3029
FutureWarning, stacklevel=2)
3130
pass
3231

33-
3432
class Map(LegacyMap):
3533
"""Create a Map with Folium and Leaflet.js
3634
@@ -673,12 +671,18 @@ def color_scale_fun(x):
673671
def style_function(x):
674672
return {
675673
"weight": line_weight,
676-
"opactiy": line_opacity,
674+
"opacity": line_opacity,
677675
"color": line_color,
678676
"fillOpacity": fill_opacity,
679677
"fillColor": color_scale_fun(x)
680678
}
681679

680+
def highlight_function(x):
681+
return {
682+
"weight": line_weight + 2,
683+
"fillOpacity": fill_opacity + .2
684+
}
685+
682686
if topojson:
683687
geo_json = TopoJson(
684688
geo_data,
@@ -689,7 +693,8 @@ def style_function(x):
689693
geo_json = GeoJson(
690694
geo_data,
691695
style_function=style_function,
692-
smooth_factor=smooth_factor)
696+
smooth_factor=smooth_factor,
697+
highlight_function=highlight_function)
693698

694699
self.add_child(geo_json)
695700

folium/map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
_default_js = [
2424
('leaflet',
25-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"),
25+
"https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.js"),
2626
('jquery',
2727
"https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"),
2828
('bootstrap',
@@ -37,7 +37,7 @@
3737

3838
_default_css = [
3939
("leaflet_css",
40-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css"),
40+
"https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.css"),
4141
("bootstrap_css",
4242
"https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"),
4343
("bootstrap_theme_css",

0 commit comments

Comments
 (0)