Skip to content

Commit 3ff3117

Browse files
committed
Fixes #341: Added Highlight switch
1 parent c6c9f04 commit 3ff3117

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

folium/features.py

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

332332
if style_function is None:
333333
def style_function(x):
334-
return {}
334+
return {}
335+
335336
self.style_function = style_function
336337

338+
self.highlight = highlight_function is not None
339+
340+
if highlight_function is None:
341+
def highlight_function(x):
342+
return {}
343+
337344
self.highlight_function = highlight_function
338345

339346
self.smooth_factor = smooth_factor
340347

341348
self._template = Template(u"""
342349
{% macro script(this, kwargs) %}
343350
344-
{% if this.highlight_function is not none %}
351+
{% if this.highlight %}
345352
{{this.get_name()}}_onEachFeature = function onEachFeature(feature, layer) {
346353
layer.on({
347354
mouseout: function(e) {
@@ -356,13 +363,13 @@ def style_function(x):
356363
357364
var {{this.get_name()}} = L.geoJson(
358365
{% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %}
359-
{% if this.smooth_factor is not none or this.highlight_function is not none %}
366+
{% if this.smooth_factor is not none or this.highlight %}
360367
, {
361368
{% if this.smooth_factor is not none %}
362-
smoothFactor:{{this.smooth_factor}}}
369+
smoothFactor:{{this.smooth_factor}}
363370
{% endif %}
364371
365-
{% if this.highlight_function is not none %}
372+
{% if this.highlight %}
366373
{% if this.smooth_factor is not none %}
367374
,
368375
{% 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)