Bubble chart avec D3js
2019-04-30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html class="no-js" lang=""> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Bubble chart avec D3js</title> | |
| <meta name="description" content=""> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet"> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> | |
| <style> | |
| body{overflow-x:hidden;font-family: 'Raleway', sans-serif;} | |
| div.tooltip {position: absolute;text-align: center;width: 190px;padding: 10px;font-size:0.9em;background: #96281b;border: 0px;border-radius: 8px;pointer-events: none;top:10px;left:10px;color:#FFFFFF;} | |
| svg{max-width:100%;overflow:hidden;top:60px;width:100%;position:relative;display:block;margin:0 auto;} | |
| #container{width:1000px;max-width:100%;margin:0 auto;} | |
| circle{cursor:pointer;} | |
| circle:hover{opacity:0.7;} | |
| h1{text-align:center;margin-bottom:-30px;font-size:26px;font-weight:bold;} | |
| </style> | |
| </head> | |
| <body> | |
| <div id="container"> | |
| <h1>Répartition de la population par département</h1> | |
| <svg width="1000" height="600" font-family="sans-serif" font-size="10" text-anchor="middle" | |
| viewBox="0 0 1000 600" | |
| preserveAspectRatio="xMinYMin meet"> | |
| </svg> | |
| </div> | |
| <script> | |
| var svg = d3.select("svg"), | |
| width = +svg.attr("width"), | |
| height = +svg.attr("height"); | |
| var div = d3.select("body").append("div") | |
| .attr("class", "tooltip") | |
| .style("opacity", 0); | |
| var pack = d3.pack() | |
| .size([width, height]) | |
| .padding(1.5); | |
| d3.csv("population.csv", function(d) { | |
| d.value = +d["habitants"]; | |
| d.departement = d["departement"] | |
| return d; | |
| }, function(error, data) { | |
| if (error) throw error; | |
| var color = d3.scaleOrdinal() | |
| .domain(data.map(function(d){ return d.departement;})) | |
| .range(['#26a65b','#03a678','#019875','#03c9a9']); | |
| var root = d3.hierarchy({children: data}) | |
| .sum(function(d) { return d.value; }) | |
| .sort(function(a, b) { return b.value - a.value; }); | |
| var node = svg.selectAll(".node") | |
| .data(pack(root).leaves()) | |
| .enter().append("g") | |
| .attr("class", "node") | |
| .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
| node.append("circle") | |
| .attr("id", function(d) { return d.id; }) | |
| .attr("r", function(d) { return d.r; }) | |
| .style("fill", function(d) { return color(d.data.departement); }) | |
| .on("mouseover", function(d) { | |
| div.transition() | |
| .duration(1200) | |
| .style("opacity", 1); | |
| var duration = 800; | |
| data.forEach(function(d, i) { | |
| console.log(d.value); | |
| node.transition().duration(duration).delay(i * duration) | |
| .attr("r", d.value); | |
| }); | |
| div.html(d.data.departement + ": <br>"+d.data.value ) | |
| .style("left", (d3.event.pageX) + "px") | |
| .style("top", (d3.event.pageY) + "px"); | |
| }) | |
| .on("mouseout", function(d) { | |
| div.transition() | |
| .duration(500) | |
| .style("opacity", 0); | |
| }); | |
| //node.append("text") | |
| //.text(function(d) { | |
| //return d.data.departement; | |
| //return "";}); | |
| //}); | |
| }); | |
| </script> | |
| </body> | |
| </html> |