Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial timestamp parameter to TimeSliderChoropleth #1435

Merged
merged 6 commits into from
Nov 10, 2022
Merged
Changes from 2 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
18 changes: 13 additions & 5 deletions folium/plugins/time_slider_choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
Whether the Layer will be included in LayerControls.
show: bool, default True
Whether the layer will be shown on opening (only for overlays).

init_timestamp: int, default 0
jjbenes marked this conversation as resolved.
Show resolved Hide resolved
Initial time-stamp index on the slider. Must be in the range
`[-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.
"""
_template = Template(u"""
{% macro script(this, kwargs) %}
var timestamps = {{ this.timestamps|tojson }};
var styledict = {{ this.styledict|tojson }};
var current_timestamp = timestamps[0];

var current_timestamp = timestamps[
jjbenes marked this conversation as resolved.
Show resolved Hide resolved
(timestamps.length+{{ this.init_timestamp}}) % timestamps.length];
// insert time slider
d3.select("body").insert("p", ":first-child").append("input")
.attr("type", "range")
.attr("width", "100px")
.attr("min", 0)
.attr("max", timestamps.length - 1)
.attr("value", 0)
.attr("value", current_timestamp)
jjbenes marked this conversation as resolved.
Show resolved Hide resolved
.attr("id", "slider")
.attr("step", "1")
.style('align', 'center');
Expand Down Expand Up @@ -137,7 +141,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
]

def __init__(self, data, styledict, name=None, overlay=True, control=True,
show=True):
show=True, init_timestamp=0):
super(TimeSliderChoropleth, self).__init__(name=name, overlay=overlay,
control=control, show=show)
self.data = GeoJson.process_data(GeoJson({}), data)
Expand All @@ -156,3 +160,7 @@ def __init__(self, data, styledict, name=None, overlay=True, control=True,

self.timestamps = timestamps
self.styledict = styledict
assert -len(timestamps) <= init_timestamp < len(timestamps), (
'init_timestamp must be in the range [-{}, {}) but got {}'.format(
len(timestamps), len(timestamps), init_timestamp))
self.init_timestamp = init_timestamp