Built with blockbuilder.org
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
div.tooltip {
color: #222;
background-color: #fff;
padding: .5em;
text-shadow: #f5f5f5 0 1px 0;
border-radius: 2px;
opacity: 0.9;
position: absolute;
}
path {
stroke: black;
stroke-width="1"
}
</style>
</head>
<body>
<div>
<input id="slider" type="range" value="1" min="1" max="100" step="1" />
<span id="week">week</span>
</div>
<script>
var width = 700,
height = 580;
var svg = d3.select( "body" )
.append( "svg" )
.attr( "width", width )
.attr( "height", height );
var projection = d3.geo.conicConformal()
.center([2.454071, 46.79229])
.scale(3000)
.translate([width/2,height/2]);
var path = d3.geo.path()
.projection(projection);
var tooltip = d3.select('body').append('div')
.attr('class', 'hidden tooltip');
var json={},data={};
var min=0,max=0;
var months=[];
// On definie une echelle de couleur
var color = d3.scale.quantile()
.range(["rgb(237,248,233)",
"rgb(186,228,179)",
"rgb(116,196,118)",
"rgb(49,163,84)",
"rgb(0,109,44)"]);
d3.select("#slider").on("input", function() {
updateViz(+this.value);
});
function updateViz(num){
d3.select('#week').html(months[num]);
drawMap(json,num);
}
function drawMap(json, currentWeek) {
carte = svg.selectAll("path")
.data(json.features);
// code en cas de mise a jour de la carte / de changement de semaine
carte
.attr("class", "update")
.style("fill", function(d) {
var value = d.properties.value;
if (value) {
return color(value[currentWeek]);
} else {
// si pas de valeur alors en gris
return "#000000";
}
})
.on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] - 35) + 'px')
.html(function(){
var value = d.properties.value?d.properties.value[currentWeek]:null;
return "région :" + d.properties.nom +"<br>"+"value :"+ value;
});
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
})
.style("fill", function(d) {
var value = d.properties.value;
if (value) {
return color(value[currentWeek]);
} else {
// si pas de valeur alors en gris
return "#000000";
}
});
// code pour la creation de la carte quand les donnees sont chargees la 1e fois.
carte.enter()
.append("path")
.attr("class", "enter")
.attr("d", path)
.style("fill", function(d) {
var value = d.properties.value;
if (value) {
return color(value[currentWeek]);
} else {
// si pas de valeur alors en gris
return "#000000";
}
}).on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] - 35) + 'px')
.html(function(){
var value = d.properties.value?d.properties.value[currentWeek]:null;
return "région :" + d.properties.nom +"<br>"+"value :"+ value;
});
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
})
.style("fill", function(d) {
var value = d.properties.value;
if (value) {
return color(value[currentWeek]);
} else {
// si pas de valeur alors en gris
return "#000000";
}
});
}
function processData(error,francejson,grippedata) {
data = grippedata;
json = francejson;
nb_month = Object.keys(grippedata[0]).length-2;
for(i=1;i<=nb_month;i++){
months.push(Object.keys(grippedata[0])[i]);
}
d3.select('#slider').attr("max", months.length-1);
for (var i = 0; i < data.length; i++) {
//Nom de l'etat
var nomRegion = data[i].region;
//Valeur associee a l'etat
var dataValue = months.map(function(key) {
return data[i][key];
});
dataValue.forEach(function(el){
max = Math.max(max,el);
});
//Recherche de l'etat dans le GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.nom;
if (nomRegion == jsonState) {
//On injecte la valeur de l'Etat dans le json
json.features[j].properties.value = dataValue;
//Pas besoin de chercher plus loin
break;
}
}
}
color.domain([min,max]);
console.log("min:"+min);
console.log("max:"+max);
drawMap(json,0);
// draw legend
var legend = svg.selectAll(".legend")
.data(color.quantiles())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
// draw legend colored rectangles
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
// draw legend text
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d;});
}
queue() // permet de charger les fichiers de manière asynchrone
.defer(d3.json, "region.geojson")
.defer(d3.csv, "GrippeFrance2014.csv")
.await(processData); // une fois les fichiers chargé, la fonction processData
// est appelée avec les fichiers en arguments
</script>
</body>
Modified http://d3js.org/queue.v1.min.js to a secure url
https://d3js.org/d3.v3.min.js
https://d3js.org/queue.v1.min.js