Built with blockbuilder.org
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.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; }
.province:hover {
opacity: 0.5;
}
.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>
<input id="slider" type="range" value="1" min="1" max="52" 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 );
// French map
var projection = d3.geoConicConformal().center([2.454071, 46.279229]).scale(2500);
// On definie une echelle de couleur
var color = d3.scaleQuantize()
.range(["rgb(213,239,204)",
"rgb(169,221,160)",
"rgb(94,186,97)",
"rgb(42,141,71)",
"rgb(0,91,36)"]);
var tooltip = d3.select('body')
.append('div')
.attr('class', 'hidden tooltip');
var path = d3.geoPath() // d3.geo.path avec d3 version 3
.projection(projection);
d3.csv("GrippeFrance2014.csv", function(data) {
var france;
var listWeeks = Object.keys(data[0]);
d3.json("regions.json", function(json) {
// first value for slider
var first = listWeeks[1];
d3.select('#week').html(first);
// update the week in slider
d3.select("#slider").on("input", function() {
updateViz(+this.value);
});
//On fusionne les donnees avec le GeoJSON des regions
for (var i = 0; i < data.length; i++) {
// Find the name of the region in csv file
var dataState = data[i].region;
for (var j = 0; j< json.features.length; j++){
var jsonState = json.features[j].properties.nom;
if (dataState == jsonState) {
json.features[j].properties.tab = Object.values(data[i]);
//stop because result found
break;
}
}
}
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path);
france = json;
drawMap(1);
}); // End of "d3.json("regions.json", function(json){"
//update elements
function updateViz(val){
d3.select('#week').html(listWeeks[val]);
drawMap(val);
};
// Draw the map
function drawMap(currentWeek){
var nbr;
//Set input domain for color scale
color.domain([
d3.min(data, function(x) { return parseFloat(Object.values(x)[currentWeek]);}),
d3.max(data, function(x) { return parseFloat(Object.values(x)[currentWeek]);})
]);
carte = svg.selectAll("path")
.data(france.features);
carte
.on('mousemove', function(d) {
if(d.properties.tab == undefined){
nbr = "undefined";
}else {
nbr = d.properties.tab[currentWeek];
}
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(d.properties.nom + ":" + parseFloat(nbr));
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
});
//code in case of update of the map / change of week
carte
.attr('class', function(d) {
return 'province ' + d.properties.code;
})
.attr('d', path)
.style("fill", function(d) {
// get value found above
var tab = d.properties.tab;
if (tab) {
return color(tab[currentWeek]);
} else {
// if no value then color with Grey
return "#ccc";
}
});
// fist time of coloring the map
carte.selectAll("path")
.enter()
.data(france.features)
.append("path")
.attr("class", "enter")
.attr("d", path)
.style("fill", function(d) {
// get value found above
var tab = d.properties.tab;
if (tab) {
return color(tab[currentWeek]);
} else {
// if no value then color with Grey
return "#ccc";
}
});
};
});
</script>
</body>
https://d3js.org/d3.v4.min.js
https://d3js.org/queue.v1.min.js