Built with blockbuilder.org
forked from aurelient's block:
forked from MohamedZaki's block: Grippe en France
forked from MohamedZaki's block: Grippe en France With Slider
forked from MohamedZaki's block: Grippe en France 2014
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.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 {
fill: #000;
stroke: #fff;
stroke-width: 1px;
}
.province:hover {
fill: #666;
}
.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>
<br/> <br/>
<div>
<input id="slider" type="range" value="1" min="1" max="2" step="1" />
<span id="week">week</span>
</div>
<script>
var width = 700,
height = 580;
var min = Number.MAX_VALUE ;
var max = 0 ;
var weeks = 0 ;
var index = 0 ;
var weeksArray ;
var svg = d3.select( "body" )
.append( "svg" )
.attr( "width", width )
.attr( "height", height );
var projection = d3.geo.conicConformal()
.translate([width/2, height/2])
.center([2.454071, 46.279229]).scale(3000);
var path = d3.geo.path()
.projection(projection);
var tooltip = d3.select('body').append('div')
.attr('class', 'hidden tooltip');
queue() // permet de charger les fichiers de manière asynchrone
.defer(d3.json, "regions.json")
.defer(d3.csv, "grippe.csv")
.await(processData); // Appel de Await Data
function processData(error,francejson,grippedata) {
weeksArray = Object.keys(grippedata[0]);
for (var i = 0; i < grippedata.length; i++) {
data = grippedata[i];
for(var j = 1 ;j < data.length-1 ; j++){
if (max < grippedata[i][j]) max = grippedata[i][j] ;
if (min > grippedata[i][j]) min = grippedata[i][j] ;
}
color.domain([ min ,max ]);
var dataRegion = grippedata[i].region;
for (var j = 0; j < francejson.features.length; j++) {
var jsonState = francejson.features[j].properties.nom;
if (dataRegion == jsonState) {
//Injection des valeurs dans le Json
francejson.features[j].properties.value = Object.values(data);
break;
}
}
}
weeks = weeksArray.length-2 ;
d3.select('#slider').attr("max", weeks) ;
d3.select("#slider").on("input", function() {
index = this.value ;
updateViz(+this.value);
});
function updateViz(value){
d3.select('#week').html(weeksArray[index]);
drawMap(francejson,weeksArray[index]);
}
}
var color = d3.scale.quantize()
.range(['rgb(255,245,240)','rgb(254,224,210)','rgb(252,187,161)','rgb(252,146,114)','rgb(251,106,74)','rgb(239,59,44)','rgb(203,24,29)','rgb(165,15,21)','rgb(103,0,13)']);
function drawMap(json, currentWeek) {
map = svg.selectAll("path")
.data(json.features);
// Maj de la map avec changement des semines
map.attr("class", "update").attr("d", path).style("opacity",function(d) {
//on prend la valeur recupere plus haut
var value = d.properties.value;
if (value) {
return value[index]/250;
} else {
// si pas de valeur alors en gris
return "#ccc";
}
})
.style("fill", function(d) {
//on prend la valeur recupere plus haut
var value = d.properties.value;
if (value) {
return color(value[index]);
} else {
// si pas de valeur alors en gris
return "#ccc";
}
}).on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
var value = d.properties.value;
var somme = 0 ;
if (value) {
somme = d.properties.value[index] ;
} else {
somme = 0 ;
}
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 10) +
'px; top:' + (mouse[1] - 1) + 'px')
.html(d.properties.nom+" : "+somme);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
});
map.enter()
.append("path")
.attr("class", "enter");
}
</script>
</body>
Modified http://d3js.org/topojson.v1.min.js to a secure url
Modified http://d3js.org/queue.v1.min.js to a secure url
https://d3js.org/d3.v3.min.js
https://d3js.org/topojson.v1.min.js
https://d3js.org/queue.v1.min.js