D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
NaamaNaa
Full window
Github gist
TP4 Dataviz_HAMIZI
Built with
blockbuilder.org
<!DOCTYPE html> <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; } div.tooltip { color: #222; background-color: #fff; padding: .5em; text-shadow: #f5f5f5 0 1px 0; border-radius: 2px; opacity: 0.9; position: absolute; } .reg { fill: #000; stroke: black; stroke-width: 1px; } .reg:hover { opacity: 0.5; } .hidden { display: none; } </style> </head> <body> <!-- slider to change week --> <div> <input id="slider" type="range" value="1" min="1" max="52" step="1" /> <span id="week">week </span> </div> <script> // Color scale : 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 width = 700, height = 580; // map --> France var projection = d3.geoConicConformal().center([2.454071, 46.279229]).scale(2500); var path = d3.geoPath() // d3.geo.path avec d3 version 3 .projection(projection); var svg = d3.select( "body" ) .append( "svg" ) .attr( "width", width ) .attr( "height", height ); //counting number of sick people per area var tooltip = d3.select('body') .append('div') .attr('class', 'hidden tooltip'); d3.csv("GrippeFrance2014.csv", function(data) { var frJs; var listWeeks = Object.keys(data[0]); d3.json("regions.json", function(json) { // define the first value displayed next to the slider var first = listWeeks[1]; d3.select('#week').html(first); // updating weeks and displays (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++) { // finding region name in data file var dataState = data[i].region; for (var j = 0; j< json.features.length; j++){ // finding region name in json file var jsonState = json.features[j].properties.nom; if (dataState == jsonState) { json.features[j].properties.line = Object.values(data[i]); //stop because result found break; } } } svg.selectAll("path") .data(json.features) .enter() .append("path") .attr("d", path); frJs = json; drawMap(1); }); function updateViz(currentWeek){ d3.select('#week').html(listWeeks[currentWeek]); drawMap(currentWeek); }; // Draw function drawMap(currentWeek){ var value; //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(frJs.features); carte .on('mousemove', function(d) { if(d.properties.line == undefined){ value = "undefined"; }else { value = d.properties.line[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(value)); }) .on('mouseout', function() { tooltip.classed('hidden', true); }); carte .attr('class', function(d) { return 'reg ' + d.properties.code; }) .attr('d', path) .style("fill", function(d) { var line = d.properties.line; if (line) { return color(line[currentWeek]); } else { return "#ccc"; } }); carte.selectAll("path") .enter() .data(frJs.features) .append("path") .attr("class", "enter") .attr("d", path) .style("fill", function(d) { var line = d.properties.line; if (line) { return color(line[currentWeek]); } else { return "#ccc"; } }); }; }); </script> </body>
https://d3js.org/d3.v4.min.js
https://d3js.org/queue.v1.min.js