Skip to content

Commit b0493c4

Browse files
committed
Fixes #341: Added Highlight switch
1 parent b0120e8 commit b0493c4

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

folium/features.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,17 +333,24 @@ def __init__(self, data, style_function=None, name=None,
333333

334334
if style_function is None:
335335
def style_function(x):
336-
return {}
336+
return {}
337+
337338
self.style_function = style_function
338339

340+
self.highlight = highlight_function is not None
341+
342+
if highlight_function is None:
343+
def highlight_function(x):
344+
return {}
345+
339346
self.highlight_function = highlight_function
340347

341348
self.smooth_factor = smooth_factor
342349

343350
self._template = Template(u"""
344351
{% macro script(this, kwargs) %}
345352
346-
{% if this.highlight_function is not none %}
353+
{% if this.highlight %}
347354
{{this.get_name()}}_onEachFeature = function onEachFeature(feature, layer) {
348355
layer.on({
349356
mouseout: function(e) {
@@ -358,13 +365,13 @@ def style_function(x):
358365
359366
var {{this.get_name()}} = L.geoJson(
360367
{% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %}
361-
{% if this.smooth_factor is not none or this.highlight_function is not none %}
368+
{% if this.smooth_factor is not none or this.highlight %}
362369
, {
363370
{% if this.smooth_factor is not none %}
364-
smoothFactor:{{this.smooth_factor}}}
371+
smoothFactor:{{this.smooth_factor}}
365372
{% endif %}
366373
367-
{% if this.highlight_function is not none %}
374+
{% if this.highlight %}
368375
{% if this.smooth_factor is not none %}
369376
,
370377
{% endif %}

folium/folium.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
508508
data=None, columns=None, key_on=None, threshold_scale=None,
509509
fill_color='blue', fill_opacity=0.6, line_color='black',
510510
line_weight=1, line_opacity=1, legend_name="",
511-
topojson=None, reset=False, smooth_factor=None):
511+
topojson=None, reset=False, smooth_factor=None,
512+
highlight=None):
512513
"""
513514
Apply a GeoJSON overlay to the map.
514515
@@ -580,6 +581,8 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
580581
How much to simplify the polyline on each zoom level. More means
581582
better performance and smoother look, and less means more accurate
582583
representation. Leaflet defaults to 1.0.
584+
highlight: boolean, default False
585+
Enable highlight functionality when hovering over a GeoJSON area.
583586
584587
Returns
585588
-------
@@ -596,6 +599,12 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
596599
... threshold_scale=[0, 20, 30, 40, 50, 60])
597600
>>> m.choropleth(geo_path='countries.json',
598601
... topojson='objects.countries')
602+
>>> m.choropleth(geo_path='geo.json', data=df,
603+
... columns=['Data 1', 'Data 2'],
604+
... key_on='feature.properties.myvalue',
605+
... fill_color='PuBu',
606+
... threshold_scale=[0, 20, 30, 40, 50, 60],
607+
... highlight=True)
599608
600609
"""
601610
if threshold_scale and len(threshold_scale) > 6:
@@ -677,6 +686,7 @@ def style_function(x):
677686
"fillColor": color_scale_fun(x)
678687
}
679688

689+
680690
def highlight_function(x):
681691
return {
682692
"weight": line_weight + 2,
@@ -694,7 +704,7 @@ def highlight_function(x):
694704
geo_data,
695705
style_function=style_function,
696706
smooth_factor=smooth_factor,
697-
highlight_function=highlight_function)
707+
highlight_function=highlight_function if highlight else None)
698708

699709
self.add_child(geo_json)
700710

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://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.js"),
25+
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/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://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.css"),
40+
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/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)