Skip to content

Commit 0946383

Browse files
jjbenesConengmo
andauthored
Add initial timestamp parameter to TimeSliderChoropleth (#1435)
* added optional inital position * Update time_slider_choropleth.py simplified javascript and python code * Update folium/plugins/time_slider_choropleth.py Co-authored-by: Frank <[email protected]> * fix implementation * Add entry to example notebook Co-authored-by: Frank <[email protected]>
1 parent aa6260d commit 0946383

File tree

2 files changed

+426
-67
lines changed

2 files changed

+426
-67
lines changed

examples/TimeSliderChoropleth.ipynb

Lines changed: 412 additions & 62 deletions
Large diffs are not rendered by default.

folium/plugins/time_slider_choropleth.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,24 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
2424
Whether the Layer will be included in LayerControls.
2525
show: bool, default True
2626
Whether the layer will be shown on opening (only for overlays).
27-
27+
init_timestamp: int, default 0
28+
Initial time-stamp index on the slider. Must be in the range
29+
`[-L, L-1]`, where `L` is the maximum number of time stamps in
30+
`styledict`. For example, use `-1` to initialize the slider to the
31+
latest timestamp.
2832
"""
2933
_template = Template(u"""
3034
{% macro script(this, kwargs) %}
3135
var timestamps = {{ this.timestamps|tojson }};
3236
var styledict = {{ this.styledict|tojson }};
33-
var current_timestamp = timestamps[0];
34-
37+
var current_timestamp = timestamps[{{ this.init_timestamp }}];
3538
// insert time slider
3639
d3.select("body").insert("p", ":first-child").append("input")
3740
.attr("type", "range")
3841
.attr("width", "100px")
3942
.attr("min", 0)
4043
.attr("max", timestamps.length - 1)
41-
.attr("value", 0)
44+
.attr("value", {{ this.init_timestamp }})
4245
.attr("id", "slider")
4346
.attr("step", "1")
4447
.style('align', 'center');
@@ -135,7 +138,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
135138
]
136139

137140
def __init__(self, data, styledict, name=None, overlay=True, control=True,
138-
show=True):
141+
show=True, init_timestamp=0):
139142
super(TimeSliderChoropleth, self).__init__(name=name, overlay=overlay,
140143
control=control, show=show)
141144
self.data = GeoJson.process_data(GeoJson({}), data)
@@ -157,3 +160,9 @@ def __init__(self, data, styledict, name=None, overlay=True, control=True,
157160

158161
self.timestamps = timestamps
159162
self.styledict = styledict
163+
assert -len(timestamps) <= init_timestamp < len(timestamps), (
164+
'init_timestamp must be in the range [-{}, {}) but got {}'.format(
165+
len(timestamps), len(timestamps), init_timestamp))
166+
if init_timestamp < 0:
167+
init_timestamp = len(timestamps) + init_timestamp
168+
self.init_timestamp = init_timestamp

0 commit comments

Comments
 (0)