viewof carte_tournages_annees = {
const years = Array.from(new Set(tournages.objects().map(d => d["Année du tournage"]))).sort();
const yearMin = Math.min(...years);
const yearMax = Math.max(...years);
const slider = Inputs.range([yearMin, yearMax], {
step: 1,
label: "Année de tournage",
value: yearMin
});
const playBtn = html`<button style="margin-left: 1em;">Play</button>`;
let playing = false;
let interval;
const chartDiv = html`<div style="width: 700px; height: 600px;"></div>`;
async function draw(year) {
const data = tournages.objects().filter(
d => d["Année du tournage"] === year && d.lon && d.lat
);
const spec = {
$schema: "https://vega.github.io/schema/vega-lite/v5.json",
width: 700,
height: 600,
projection: {type: "mercator"},
encoding: {
longitude: {field: "lon", type: "quantitative", scale: {zero: false}},
latitude: {field: "lat", type: "quantitative", scale: {zero: false}}
},
layer: [
{
data: {
url: "https://mjlobo.github.io/teaching/eivp/arrondissementswithid.json",
format: {type: "topojson", feature: "arrondissements"}
},
mark: {type: "geoshape", fill: "#f5f5f5", stroke: "white"}
},
{
data: {values: data},
mark: {type: "circle", color: "firebrick", opacity: 0.6},
encoding: {
longitude: {field: "lon", type: "quantitative"},
latitude: {field: "lat", type: "quantitative"},
tooltip: [
{field: "Titre", title: "Titre"},
{field: "Type de tournage", title: "Type"},
{field: "Réalisateur", title: "Réalisateur"},
{field: "Année du tournage", title: "Année"}
]
}
}
],
selection: {
zoom: {
type: "interval",
bind: "scales",
encodings: ["x", "y"]
}
}
};
chartDiv.innerHTML = "";
await embed(chartDiv, spec);
}
slider.addEventListener("input", () => {
draw(slider.value);
});
playBtn.onclick = () => {
playing = !playing;
playBtn.textContent = playing ? "Pause" : "Play";
if (playing) {
interval = setInterval(() => {
let next = slider.value + 1;
if (next > yearMax) next = yearMin;
slider.value = next;
slider.dispatchEvent(new Event("input"));
}, 2000);
} else {
clearInterval(interval);
}
};
slider.dispatchEvent(new Event("input"));
return html`<div>
<div style="margin-bottom: 10px;">${slider}${playBtn}</div>
${chartDiv}
</div>`;
}