D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
AminaSGitHub
Full window
Github gist
projet_DataViz
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> h1 { text-align : center; } body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } svg { position: relative; left: 30px; border: 3px solid #B0E0E6; } </style> </head> <body> <p> <h1>Comparison of the quality of TER flows</h1> <div> <input id="slider" type="range" value="1" min="1" max="60" step="1" /> <span id="month">month </span> </div> <script> /* var width = 700, height = 580; var svg = d3.select( "body" ) .append( "svg" ) .attr( "width", width ) .attr( "height", height ); // On rajoute un groupe englobant toute la visualisation pour plus tard var g = svg.append( "g" ); // French map centered var projection = d3.geoConicConformal().center([2.454071, 46.279229]).scale(2300); var path = d3.geoPath() .projection(projection); // 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)"]); //// d3.csv("TER_France2013_2017.csv", function(data) { //Set input domain for color scale var colonne = "Nombre de trains programmés"; //console.log("t = " + parseFloat(data[2][colonne])); //var ligne = data[2]; // console.log(ligne["Date"]);/ color.domain([ //d3.min(data, function(d) { / *console.log("=>" + parseFloat(d[colonne]));* / // return parseFloat(d[colonne]); }), //d3.max(data, function(d) { return parseFloat(d[colonne]); }) 0,50 ]); d3.json("regions.json", function(json) { //On fusionne les donnees avec le GeoJSON des regions for (var i = 0; i < data.length; i++) { //Nom de l'etat var dataState = data[i]["Région"]; //var dataDate = data[i]["Date"]; //var year = dataDate.substring(0,4); if(dataDate == "2013-01"){ // console.log("nbrLate = " + nbrLate); // console.log("nbrCanceled = " + nbrCanceled); // console.log("nbrProvided = " + nbrProvided); // console.log("nbrCirculated = " + nbrCirculated); // On remarque que nbrProvided = nbrCirculated + nbrCanceled // Nous allons étudier nbrCanceled + nbrLate // X % ===> (nbrCirculated + nbrCanceled) // 100% ===> (nbrProvided ) // X = (nbrLate + nbrCanceled) * 100/nbrProvided var valuePourcentage = parseFloat((nbrLate + nbrCanceled) * 100/nbrProvided); ; console.log("pour% : " + valuePourcentage); var dataValue = valuePourcentage; //Recherche de l'etat dans le GeoJSON// for (var j = 0; j < json.features.length; j++) { var jsonState = json.features[j].properties.nom; if (dataState == jsonState) { console.log(dataState); ; //On injecte la valeur de l'Etat dans le json json.features[j].properties.value = dataValue; //Pas besoin de chercher plus loin break; } } } } g.selectAll("path") .data(json.features) .enter() .append("path") .attr("d", path) .style("fill", function(d) { var value = d.properties.value; if (value) { return color(value); } else { // si pas de valeur alors en gris return "#ccc"; } }); }); }); */ 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)"]); /*Area containing the name and the value of each region*/ var tooltip = d3.select('body') .append('div') .attr('class', 'hidden tooltip'); var path = d3.geoPath() // d3.geo.path avec d3 version 3 .projection(projection); // On rajoute un groupe englobant toute la visualisation pour plus tard //var g = svg.append( "g" ); d3.csv("TER_France2013_2017.csv", function(data) { var jsonF; var listMonths = Object.keys(data[0]); //Set input domain for color scale /* color.domain([ d3.min(data, function(d) { return parseFloat(d.somme2014); }), d3.max(data, function(d) { return parseFloat(d.somme2014); }) ]);*/ color.domain([ 0,200 ]); d3.json("regions.json", function(json) { // first value for slider var first = listMonths[1]; d3.select('#month').html(first); // update the month 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++){ // Find the name of the region in json file 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); jsonF = json; drawMap(1); }); // End of "d3.json("regions.json", function(json){" //update elements function updateViz(val){ d3.select('#month').html(listMonths[val]); drawMap(val); }; // Draw the map function drawMap(currentMonth){ var nbr; //Set input domain for color scale color.domain([ //console.log("====> x : " + Object.values(x)[currentMonth]); d3.min(data, function(x) { return parseFloat(Object.values(x)[currentMonth]);}), d3.max(data, function(x) { return parseFloat(Object.values(x)[currentMonth]);}) ]); carte = svg.selectAll("path") .data(jsonF.features); carte .on('mousemove', function(d) { if(d.properties.tab == undefined){ nbr = "undefined"; }else { nbr = d.properties.tab[currentMonth]; } 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 month 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[currentMonth]); } else { // if no value then color with Grey return "#ccc"; } }); // fist time of coloring the map carte.selectAll("path") .enter() .data(jsonF.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[currentMonth]); } else { // if no value then color with Grey return "#ccc"; } }); }; // End of drawMap }); // End of "d3.csv("GrippeFrance2014.csv", function(data){" </script> </body>
https://d3js.org/d3.v4.min.js