Built with blockbuilder.org
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body { margin: 0;position:fixed;top:0;right:0;bottom:0;left:0; }
.hidden {
display: none;
}
div.tooltip {
color: #222;
background-color: #fff;
padding: .5em;
text-shadow: #f5f5f5 0 1px 0;
border-radius: 2px;
opacity: 0.9;
position: absolute;
}
</style>
</head>
<body>
<div>
<span id="week">week</span>
</div>
<script>
var width = 960;
var height = 500
var currentWeek = 1;
var weeksArray = [];
var mydata;
var myjson;
var svg = d3.select("body")
.append("svg")
.attr("width", width )
.attr( "height", height );
var tooltip = d3.select('body').append('div')
.attr('class', 'hidden tooltip');
var projection = d3.geoConicConformal()
.center([2.454071, 46.279229])
.scale(2800);
var color = d3.scaleQuantize()
.range(["rgb(237,248,233)","rgb(186,228,179)",
"rgb(116,196,118)","rgb(49,163,84)","rgb(0,109,44)"]);
var path = d3.geoPath().projection(projection);
d3.csv("GrippeFrance2014.csv").then(function(data) {
// On va stocker toutes les valeurs de novembre
// pour pouvoir calculer le min et le max
regionValues = [];
//Valeur associee a l'etat
novemberValue = 0;
d3.json("regions.json").then(function(json) {
//On fusionne les donnees avec le GeoJSON
for (var i = 0; i < data.length; i++) {
//Nom de la region
var dataState = data[i].region;
//On calcule la somme des valeurs associee a la region pour novembre
novemberValue = parseInt(data[i]["02/11/14"])
+ parseInt(data[i]["09/11/14"])
+ parseInt(data[i]["16/11/14"])
+ parseInt(data[i]["23/11/14"])
+ parseInt(data[i]["30/11/14"]);
console.log("novemberValue : " + novemberValue);
// on ajoute la valeur à la liste de toutes les valeurs
regionValues.push(novemberValue);
//Recherche de l'etat dans le GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.nom;
if (dataState == jsonState) {
//On injecte les valeurs de la region dans le json
json.features[j].properties.value = novemberValue;
//Pas besoin de chercher plus loin
break;
}
}
}
var min = d3.min(regionValues);
var max = d3.max(regionValues);
color.domain([min, max]);
myjson = json;
drawMap();
});
});
function drawMap() {
console.log("drawMap : " + currentWeek);
carte = svg.selectAll("path")
.data(myjson.features);
carte.enter()
.append("path")
.attr("stroke", "#eee" )
.attr("d", path)
.style("fill", function(d) {
if(d.properties.value) {
var value = d.properties.value;
if (value) {
return color(value);
} else {
//si la valeur n'est pas définie
return "#444";
}
}
})
.on('mousemove', function(d) {
var mouse = d3.mouse(this);
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] - 35) + 'px')
.html(d.properties.nom + ": " + d.properties.value);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
});
}
</script>
</body>
https://d3js.org/d3.v5.min.js