|
<!DOCTYPE html> |
|
<head> |
|
<meta charset="utf-8"> |
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<!-- UNNECESSARY <script src="https://d3js.org/topojson.v1.min.js"></script> --> |
|
<style> |
|
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } |
|
/* Add div.tooltip which contains info upon hovering*/ |
|
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> |
|
<!-- Add weekly slider --> |
|
<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 ) |
|
// Add white boundry to region territories |
|
.style("stroke", "#fff") |
|
.style("stroke-width", "1px"); |
|
|
|
// Add var tooltip |
|
// and append it to body |
|
// and make tool tip contain .hidden tooltip |
|
var tooltip = d3.select('body').append('div') |
|
.attr("class", "hidden tooltip"); |
|
|
|
// d3.geo.albersUsa changed to d3.geoConicConformal() |
|
var projection = d3.geoConicConformal() |
|
.translate([width/2, height/2]) |
|
// Reposition and rescale of the map base layer |
|
.center([2.454071, 46.279229]).scale(2800); |
|
|
|
// On definie une echelle de couleur |
|
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() // d3.geo.path avec d3 version 3 |
|
.projection(projection); |
|
|
|
// Chargement des donnees |
|
d3.csv("GrippeFrance2014.csv", function(data) { |
|
console.log(data) // Check link element denomination: .region |
|
|
|
// Adjust colour range related to shown vairation scale |
|
color.domain([0, 200]); |
|
|
|
// OBSOLETE Set input domain for color scale |
|
// OBSOLETE Link data min and max values to colour scheme |
|
// OBSOLETE color.domain([ |
|
// OBSOLETE d3.min(data, function(d) { return d.value; }), |
|
// OBSOLETE d3.max(data, function(d) { return d.value; }) |
|
// OBSOLETE ]); |
|
|
|
// Inject data values in CSV table to D3 JavaScript object |
|
d3.json("regions.json", function(json) { |
|
console.log(json) // Check link element denomination: .properties.nom |
|
|
|
for (var i = 0; i < data.length; i++) { |
|
// Fetch all .region values which refer to region name |
|
// and put them in var dataRegion |
|
var dataRegion = data[i].region; |
|
|
|
// OBSOLETE Fetch all values of column 'somme2014' |
|
// OBSOLETE and put them in var dataValue |
|
// OBSOLETEvar dataValue = parseFloat(data[i].somme2014); |
|
// Fetch all values of columns concerning Nov 2014 |
|
// add them |
|
// and put them in var dataValue |
|
// OBSOLETE var dataValue = parseFloat(data[i]["02/11/14"]) + parseFloat(data[i]["09/11/14"]) + parseFloat(data[i]["16/11/14"]) + parseFloat(data[i]["23/11/14"]) + parseFloat(data[i]["30/11/14"]); |
|
|
|
var dataValue = Object.values(data[i]); |
|
// console.log(Object.values(data[i])) |
|
// console.log(data[i]) |
|
var weeksArray = Object.keys(data[0]); |
|
// console.log(Object.keys(data[0])) |
|
|
|
//Recherche de l'etat dans le GeoJSON |
|
for (var j = 0; j < json.features.length; j++) { |
|
var jsonRegion = json.features[j].properties.nom; |
|
if (dataRegion == jsonRegion) { |
|
//On injecte la valeur de l'Etat dans le json |
|
// OBSOLETE json.features[j].properties.value = dataValue; |
|
// OBSOLETE console.log(dataValue) |
|
json.features[j].properties.value = dataValue; |
|
console.log(dataValue) |
|
|
|
//Pas besoin de chercher plus loin |
|
break; |
|
|
|
} |
|
} |
|
|
|
|
|
} |
|
|
|
// OBSOLETE MAP Paint <PATH> |
|
/* svg.selectAll("path") |
|
.data(json.features) |
|
.enter() |
|
.append("path") |
|
.attr("d", path) |
|
.style("fill", function(d) { |
|
//on prend la valeur recupere plus haut |
|
var value = d.properties.value; |
|
|
|
|
|
if (value) { |
|
return color(value); |
|
} else { |
|
// si pas de valeur alors en gris |
|
return "#ccc"; |
|
} |
|
})*/ |
|
|
|
// NEW MAP |
|
// Shaped under fx drawMap() |
|
function drawMap(currentWeek) { |
|
var 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"); |
|
|
|
// update the elements |
|
function updateViz(value) { |
|
///console.log("update " + "value"); |
|
d3.select('#week').html(weeksArray[value]); |
|
drawMap(value); |
|
}; |
|
|
|
// 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) { |
|
//on prend la valeur recupere plus haut |
|
var value = d.properties.value; |
|
|
|
if (value) { |
|
return color(value); |
|
} else { |
|
// si pas de valeur alors en gris |
|
return "#ccc"; |
|
} |
|
}) |
|
|
|
// When mouse hovers over a region |
|
.on('mousemove', function(d) { |
|
console.log(d) // Check element to show: .properties.nom AND .properties.value |
|
var mouse = d3.mouse(svg.node()).map(function(d) { |
|
// console.log(d) |
|
return parseInt(d); |
|
}); |
|
tooltip.classed('hidden', false) |
|
// CSS modification |
|
.attr('style', 'left:' + (mouse[0] + 15) + 'px; top:' + (mouse[1] - 35) + 'px') |
|
// Add HTML string which concatenates needed text |
|
// e.g. Bretagne: 71 |
|
.html(d.properties.nom + ": " + d.properties.value); |
|
}) |
|
.on('mouseout', function() { |
|
tooltip.classed('hidden', true); |
|
}); |
|
|
|
// Slider eventListener |
|
d3.select("#slider").on("input", function() { |
|
updateViz(+this.value); |
|
}); |
|
} |
|
|
|
// Call fx drawMap() |
|
drawMap(); |
|
}); |
|
}); |
|
</script> |
|
</body> |
|
|