function map_interactive3(d3, calendrierDebut, calendrierFin, france, covid, width, height, sexe_select) {
const mapWidth = width;
const mapHeight = height;
const hospValues = Array.from(new Set(covid.map(item => item.hosp)));
const sexes = Array.from(new Set(covid.map(d => d.sexe)));
const sexs = sexes.indexOf(sexe_select);
const svgElement = d3.create("svg")
.attr("viewBox", [0, 0, width, mapHeight])
.style("width", "100%")
.style("height", "auto")
.style("background-color", "white");
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("background", "white")
.style("border", "1px solid black")
.style("border-radius", "5px")
.style("padding", "5px")
.style("display", "none");
const mapGroup = svgElement.append('g').attr("class", "topg");
const foreground = svgElement.append("g").attr("class", "foreground");
const title = foreground.append("text")
.attr("x", mapWidth / 2)
.attr("y", 30)
.attr("text-anchor", "middle")
.style("font-size", "18px")
.style("font-weight", "bold")
.text("Carte des hospitalisations COVID par département");
mapGroup.selectAll('path')
.data(france.features)
.enter().append('path')
.attr('d', path)
.style('fill', d => {
for (let i = 0; i < france.features.length; i++) {
let totalHosp = 0;
let totalRea = 0;
let totalConv = 0;
let totalSoins = 0;
let departement = france.features[i].properties.code;
let data = covid.filter(d =>
d.dep == departement &&
d.sexe == sexs && // Filtre par sexe
new Date(d.jour) >= calendrierDebut && // Filtre date de début
new Date(d.jour) <= calendrierFin // Filtre date de fin
);
if (data.length > 0) {
totalHosp += data.reduce((sum, item) => sum + item.hosp, 0); // Somme des hosp pour le département
totalRea += data.reduce((sum, item) => sum + item.rea, 0);
totalConv += data.reduce((sum, item) => sum + item.HospConv, 0);
totalSoins += data.reduce((sum, item) => sum + item.SSR_USLD, 0);
}
france.features[i].properties.totalHosp = totalHosp // Stocke la somme pour chaque département
france.features[i].properties.totalRea = totalRea
france.features[i].properties.totalConv = totalConv
france.features[i].properties.totalSoins = totalSoins
}
const maxHosp = d3.max(france.features, d => d.properties.totalHosp);
const colorScale = maxHosp === undefined || maxHosp === 0 ?
d3.scaleSequential(d3.interpolateOrRd).domain([0, 1]) :
d3.scaleSequential(d3.interpolateOrRd).domain([0, maxHosp]);
if (d.properties.totalHosp > 0) {
return colorScale(d.properties.totalHosp);
} else {
return '#ccc';
}
})
.style('stroke', 'white')
.attr("stroke-width", 0.5)
.attr("stroke-opacity", 0.6)
.on("mouseover", function (event, d) {
tooltip.style("display", "block")
.html(`<div style="font-size: 14px; font-weight: bold;">${d.properties.nom}</div>` +
`${d.properties.totalHosp} hospitalisations <br/>` +
`${d.properties.totalRea} individus en réanimation <br/>` +
`${d.properties.totalConv} individus en convalescence <br/>` +
`${d.properties.totalSoins} individus en soins continus et en récupération`);
d3.select(this).style("stroke", "black");
mapGroup.selectAll('path').style("opacity", e => e === d ? 1 : 0.5);
})
.on("mousemove", function (event) {
tooltip.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px");
})
.on("mouseout", function () {
tooltip.style("display", "none");
d3.select(this).style("stroke", "none");
mapGroup.selectAll('path').style("opacity", 1);
});
const maxHosp = d3.max(france.features, d => d.properties.totalHosp);
const colorScale = maxHosp === undefined || maxHosp === 0 ?
d3.scaleSequential(d3.interpolateOrRd).domain([0, 1]) :
d3.scaleSequential(d3.interpolateOrRd).domain([0, maxHosp]);
const legend = svgElement.append("g")
.attr("transform", `translate(${mapWidth - 150}, ${mapHeight - 200})`);
const legendScale = d3.scaleLinear()
.domain(colorScale.domain())
.range([100, 0]);
const legendAxis = d3.axisRight(legendScale).ticks(5);
const gradient = svgElement.append("defs")
.append("linearGradient")
.attr("id", "legendGradient")
.attr("x1", "0%").attr("y1", "0%")
.attr("x2", "0%").attr("y2", "100%");
gradient.selectAll("stop")
.data(d3.range(0, 1.1, 0.1))
.enter().append("stop")
.attr("offset", d => `${d * 100}%`)
.attr("stop-color", d => colorScale(legendScale.invert(d * 100)));
legend.append("rect")
.attr("width", 15)
.attr("height", 100)
.style("fill", "url(#legendGradient)");
legend.append("g")
.attr("transform", "translate(15,0)")
.call(legendAxis);
const zoomBehavior = d3.zoom()
.scaleExtent([1, 8])
.on('zoom', event => mapGroup.attr('transform', event.transform));
svgElement.call(zoomBehavior);
return svgElement.node();
}