Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion folium/plugins/time_slider_choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
`[-L, L-1]`, where `L` is the maximum number of time stamps in
`styledict`. For example, use `-1` to initialize the slider to the
latest timestamp.
stroke_opacity: float, default 1
Opacity of the stroke drawn around each feature.
stroke_width: float, default 0.8
Width of the stroke drawn around each feature.
stroke_color: str, default "#FFFFFF"
Color of the stroke drawn around each feature.
stroke_dash_array: str, default "5,5"
SVG `stroke-dasharray` for the stroke around each feature. The default
"5,5" draws a dashed line; pass "0" (or "none") for a solid line.
"""

_template = Template("""
Expand Down Expand Up @@ -150,7 +159,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
d3.selectAll('path')
.attr('stroke', '{{ this.stroke_color }}')
.attr('stroke-width', {{ this.stroke_width }})
.attr('stroke-dasharray', '5,5')
.attr('stroke-dasharray', '{{ this.stroke_dash_array }}')
.attr('stroke-opacity', {{ this.stroke_opacity }})
.attr('fill-opacity', 0);

Expand Down Expand Up @@ -191,6 +200,7 @@ def __init__(
stroke_opacity=1,
stroke_width=0.8,
stroke_color="#FFFFFF",
stroke_dash_array="5,5",
):
super().__init__(name=name, overlay=overlay, control=control, show=show)
self.data = GeoJson.process_data(GeoJson({}), data)
Expand All @@ -200,6 +210,7 @@ def __init__(
self.stroke_opacity = stroke_opacity
self.stroke_width = stroke_width
self.stroke_color = stroke_color
self.stroke_dash_array = stroke_dash_array

if not isinstance(styledict, dict):
raise ValueError(f"styledict must be a dictionary, got {styledict!r}")
Expand Down
40 changes: 40 additions & 0 deletions tests/plugins/test_time_slider_choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,43 @@ def test_slider_overlays_map():
rendered = plugin._template.module.script(plugin)

assert '.style("position", "absolute")' in rendered


def _single_feature_plugin(**kwargs):
geojson = json.dumps(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "0",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
},
}
],
}
)
styledict = {"0": {"1420070400": {"color": "#ff0000", "opacity": 0.5}}}

m = folium.Map((0, 0), zoom_start=2)
plugin = TimeSliderChoropleth(geojson, styledict, **kwargs)
plugin.add_to(m)
return plugin


def test_stroke_dash_array_default():
# the default keeps the previous dashed stroke, so existing maps are unchanged
plugin = _single_feature_plugin()
rendered = plugin._template.module.script(plugin)
assert ".attr('stroke-dasharray', '5,5')" in rendered


def test_stroke_dash_array_custom():
# "0" gives a solid line instead of the dashed default
plugin = _single_feature_plugin(stroke_dash_array="0")
rendered = plugin._template.module.script(plugin)
assert ".attr('stroke-dasharray', '0')" in rendered
assert ".attr('stroke-dasharray', '5,5')" not in rendered