Skip to content

Commit 92d8456

Browse files
committed
Add a date_option (format string) parameter
1 parent dc84732 commit 92d8456

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

folium/plugins/time_slider_choropleth.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
1919
styledict: dict
2020
A dictionary where the keys are the geojson feature ids and the values are
2121
dicts of `{time: style_options_dict}`
22+
date_options: str, default "ddd MMM DD YYYY"
23+
A format string to render the currently active time in the control.
2224
highlight: bool, default False
2325
Whether to show a visual effect on mouse hover and click.
2426
name : string, default None
@@ -44,6 +46,11 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
4446
let styledict = {{ this.styledict|tojson }};
4547
let current_timestamp = timestamps[{{ this.init_timestamp }}];
4648
49+
function formatDate(date) {
50+
var newdate = new moment(date);
51+
return newdate.format({{this.date_format|tojson}});
52+
}
53+
4754
let slider_body = d3.select("body").insert("div", "div.folium-map")
4855
.attr("id", "slider_{{ this.get_name() }}");
4956
$("#slider_{{ this.get_name() }}").hide();
@@ -64,7 +71,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
6471
.attr("step", "1")
6572
.style('align', 'center');
6673
67-
let datestring = new Date(parseInt(current_timestamp)*1000).toDateString();
74+
let datestring = formatDate(parseInt(current_timestamp)*1000);
6875
d3.select("#slider_{{ this.get_name() }} > output").text(datestring);
6976
7077
let fill_map = function(){
@@ -84,7 +91,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
8491
8592
d3.select("#slider_{{ this.get_name() }} > input").on("input", function() {
8693
current_timestamp = timestamps[this.value];
87-
var datestring = new Date(parseInt(current_timestamp)*1000).toDateString();
94+
let datestring = formatDate(parseInt(current_timestamp)*1000);
8895
d3.select("#slider_{{ this.get_name() }} > output").text(datestring);
8996
fill_map();
9097
});
@@ -155,12 +162,19 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
155162
"""
156163
)
157164

158-
default_js = [("d3v4", "https://d3js.org/d3.v4.min.js")]
165+
default_js = [
166+
("d3v4", "https://d3js.org/d3.v4.min.js"),
167+
(
168+
"moment",
169+
"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js",
170+
),
171+
]
159172

160173
def __init__(
161174
self,
162175
data,
163176
styledict,
177+
date_options: str = "ddd MMM DD YYYY",
164178
highlight: bool = False,
165179
name=None,
166180
overlay=True,
@@ -173,21 +187,21 @@ def __init__(
173187
):
174188
super().__init__(name=name, overlay=overlay, control=control, show=show)
175189
self.data = GeoJson.process_data(GeoJson({}), data)
190+
self.date_format = date_options
176191
self.highlight = highlight
177192

178193
self.stroke_opacity = stroke_opacity
179194
self.stroke_width = stroke_width
180195
self.stroke_color = stroke_color
181196

182197
if not isinstance(styledict, dict):
183-
raise ValueError(
184-
f"styledict must be a dictionary, got {styledict!r}"
185-
) # noqa
198+
raise ValueError(f"styledict must be a dictionary, got {styledict!r}")
199+
186200
for val in styledict.values():
187201
if not isinstance(val, dict):
188202
raise ValueError(
189203
f"Each item in styledict must be a dictionary, got {val!r}"
190-
) # noqa
204+
)
191205

192206
# Make set of timestamps.
193207
timestamps_set = set()

0 commit comments

Comments
 (0)