Skip to content

Commit

Permalink
Add a date_option (format string) parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
hansthen committed Oct 6, 2024
1 parent dc84732 commit 92d8456
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions folium/plugins/time_slider_choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
styledict: dict
A dictionary where the keys are the geojson feature ids and the values are
dicts of `{time: style_options_dict}`
date_options: str, default "ddd MMM DD YYYY"
A format string to render the currently active time in the control.
highlight: bool, default False
Whether to show a visual effect on mouse hover and click.
name : string, default None
Expand All @@ -44,6 +46,11 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
let styledict = {{ this.styledict|tojson }};
let current_timestamp = timestamps[{{ this.init_timestamp }}];
function formatDate(date) {
var newdate = new moment(date);
return newdate.format({{this.date_format|tojson}});
}
let slider_body = d3.select("body").insert("div", "div.folium-map")
.attr("id", "slider_{{ this.get_name() }}");
$("#slider_{{ this.get_name() }}").hide();
Expand All @@ -64,7 +71,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
.attr("step", "1")
.style('align', 'center');
let datestring = new Date(parseInt(current_timestamp)*1000).toDateString();
let datestring = formatDate(parseInt(current_timestamp)*1000);
d3.select("#slider_{{ this.get_name() }} > output").text(datestring);
let fill_map = function(){
Expand All @@ -84,7 +91,7 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
d3.select("#slider_{{ this.get_name() }} > input").on("input", function() {
current_timestamp = timestamps[this.value];
var datestring = new Date(parseInt(current_timestamp)*1000).toDateString();
let datestring = formatDate(parseInt(current_timestamp)*1000);
d3.select("#slider_{{ this.get_name() }} > output").text(datestring);
fill_map();
});
Expand Down Expand Up @@ -155,12 +162,19 @@ class TimeSliderChoropleth(JSCSSMixin, Layer):
"""
)

default_js = [("d3v4", "https://d3js.org/d3.v4.min.js")]
default_js = [
("d3v4", "https://d3js.org/d3.v4.min.js"),
(
"moment",
"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js",
),
]

def __init__(
self,
data,
styledict,
date_options: str = "ddd MMM DD YYYY",
highlight: bool = False,
name=None,
overlay=True,
Expand All @@ -173,21 +187,21 @@ def __init__(
):
super().__init__(name=name, overlay=overlay, control=control, show=show)
self.data = GeoJson.process_data(GeoJson({}), data)
self.date_format = date_options
self.highlight = highlight

self.stroke_opacity = stroke_opacity
self.stroke_width = stroke_width
self.stroke_color = stroke_color

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

for val in styledict.values():
if not isinstance(val, dict):
raise ValueError(
f"Each item in styledict must be a dictionary, got {val!r}"
) # noqa
)

# Make set of timestamps.
timestamps_set = set()
Expand Down

0 comments on commit 92d8456

Please sign in to comment.