Skip to content

Commit 2a40570

Browse files
committed
Add stroke_dash_array option to TimeSliderChoropleth
1 parent 3922b31 commit 2a40570

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

folium/plugins/time_slider_choropleth.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
3535
`[-L, L-1]`, where `L` is the maximum number of time stamps in
3636
`styledict`. For example, use `-1` to initialize the slider to the
3737
latest timestamp.
38+
stroke_opacity: float, default 1
39+
Opacity of the stroke drawn around each feature.
40+
stroke_width: float, default 0.8
41+
Width of the stroke drawn around each feature.
42+
stroke_color: str, default "#FFFFFF"
43+
Color of the stroke drawn around each feature.
44+
stroke_dash_array: str, default "5,5"
45+
SVG `stroke-dasharray` for the stroke around each feature. The default
46+
"5,5" draws a dashed line; pass "0" (or "none") for a solid line.
3847
"""
3948

4049
_template = Template("""
@@ -150,7 +159,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
150159
d3.selectAll('path')
151160
.attr('stroke', '{{ this.stroke_color }}')
152161
.attr('stroke-width', {{ this.stroke_width }})
153-
.attr('stroke-dasharray', '5,5')
162+
.attr('stroke-dasharray', '{{ this.stroke_dash_array }}')
154163
.attr('stroke-opacity', {{ this.stroke_opacity }})
155164
.attr('fill-opacity', 0);
156165
@@ -191,6 +200,7 @@ def __init__(
191200
stroke_opacity=1,
192201
stroke_width=0.8,
193202
stroke_color="#FFFFFF",
203+
stroke_dash_array="5,5",
194204
):
195205
super().__init__(name=name, overlay=overlay, control=control, show=show)
196206
self.data = GeoJson.process_data(GeoJson({}), data)
@@ -200,6 +210,7 @@ def __init__(
200210
self.stroke_opacity = stroke_opacity
201211
self.stroke_width = stroke_width
202212
self.stroke_color = stroke_color
213+
self.stroke_dash_array = stroke_dash_array
203214

204215
if not isinstance(styledict, dict):
205216
raise ValueError(f"styledict must be a dictionary, got {styledict!r}")

tests/plugins/test_time_slider_choropleth.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,43 @@ def test_slider_overlays_map():
120120
rendered = plugin._template.module.script(plugin)
121121

122122
assert '.style("position", "absolute")' in rendered
123+
124+
125+
def _single_feature_plugin(**kwargs):
126+
geojson = json.dumps(
127+
{
128+
"type": "FeatureCollection",
129+
"features": [
130+
{
131+
"type": "Feature",
132+
"id": "0",
133+
"properties": {},
134+
"geometry": {
135+
"type": "Polygon",
136+
"coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
137+
},
138+
}
139+
],
140+
}
141+
)
142+
styledict = {"0": {"1420070400": {"color": "#ff0000", "opacity": 0.5}}}
143+
144+
m = folium.Map((0, 0), zoom_start=2)
145+
plugin = TimeSliderChoropleth(geojson, styledict, **kwargs)
146+
plugin.add_to(m)
147+
return plugin
148+
149+
150+
def test_stroke_dash_array_default():
151+
# the default keeps the previous dashed stroke, so existing maps are unchanged
152+
plugin = _single_feature_plugin()
153+
rendered = plugin._template.module.script(plugin)
154+
assert ".attr('stroke-dasharray', '5,5')" in rendered
155+
156+
157+
def test_stroke_dash_array_custom():
158+
# "0" gives a solid line instead of the dashed default
159+
plugin = _single_feature_plugin(stroke_dash_array="0")
160+
rendered = plugin._template.module.script(plugin)
161+
assert ".attr('stroke-dasharray', '0')" in rendered
162+
assert ".attr('stroke-dasharray', '5,5')" not in rendered

0 commit comments

Comments
 (0)