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; }
.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="0" max="100" step="1" />
<span id="week">semaine</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([0, 45.5])
.scale(3000)
.translate([width/2, height/2]);
var path = d3.geo.path()
.projection(projection);
var maxColor =0;
var minColor;
var color = d3.scale.quantize() .range(["rgb(237,248,233)", "rgb(186,228,179)","rgb(116,196,118)", "rgb(49,163,84)", "rgb(0,109,44)"]);
var tooltip = d3.select('body')
.append('div')
.attr('class', 'hidden tooltip');
var jsonGrippe ;
var semaineCourante = 0;
var semaines;
d3.select("#slider").on("input", function() {
updateViz(+this.value);
});
function updateViz(num){
d3.select('#week').html(semaine[num+1]);
semaineCourante = num;
drawMap(jsonGrippe,num);
}
function drawMap(json, week) {
carte = svg.selectAll("path").data(json.features);
carte.style("fill", function(d) {
//on prend la valeur recupere plus haut
if (d.properties.nombreRecherche && d.properties.nombreRecherche[semaineCourante]) {
return color(d.properties.nombreRecherche[semaineCourante]);
} else {
// si pas de valeur alors en gris
return "#ccc";
}
});
};
queue() // permet de charger les fichiers de manière asynchrone
.defer(d3.json, "regions.json")
.defer(d3.csv, "GrippeFrance2003-15.csv")
.await(processData);
function processData (error,francejson,grippedata){
for(var i=0;i<grippedata.length;i++){
var regionGrippe =grippedata[i].regions;
var nomColonnes = d3.keys(grippedata[0]);
semaine = nomColonnes;
var valeurSemaine = new Array();
d3.select('#slider').attr("max", (nomColonnes.length - 2));
for(var k=1; k< nomColonnes.length;k++){
var grippeValeur = parseInt(grippedata[i][nomColonnes[k]]);
valeurSemaine.push(grippeValeur);
if(grippeValeur > maxColor)
maxColor = grippeValeur;
if(minColor){
if (grippeValeur < minColor)
minColor = grippeValeur;
}
else
minColor = grippeValeur;
}
for(var j=0; j< francejson.features.length;j++){
var regionFrance = francejson.features[j].properties.nom;
if(regionGrippe == regionFrance){
francejson.features[j].properties.nombreRecherche = valeurSemaine;
break;
}
}
}
if(minColor >= maxColor)
minColor = maxColor - 1;
color.domain([0,maxColor]);
svg.selectAll("path")
.data(francejson.features)
.enter()
.append("path")
.attr("d", path)
// colorer la map
.style("fill", function(d) {
//on prend la valeur recupere plus haut
if (d.properties.nombreRecherche && d.properties.nombreRecherche[semaineCourante]) {
return color(d.properties.nombreRecherche[semaineCourante]);
} else {
// si pas de valeur alors en gris
return "#ccc";
}
})
.attr('class', function(d) {
return 'region ' + d;
})
.attr('d', path)
.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(d.properties.nom + " " + "</br>Grippe : " + (d.properties.nombreRecherche ? String(d.properties.nombreRecherche[semaineCourante]) : "Undefined"))
})
.on('mouseout', function() {
tooltip.classed('hidden', true);
});
jsonGrippe = francejson;
d3.select('#week').html(semaine[semaineCourante+1]);
};
</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