D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jonolave
Full window
Github gist
Homicide vs life satisfaction
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content=type" content="text/html"; charset="utf-8"> <title>Homicide vs life satisfaction</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js" charset="utf-8"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; color: #65c8d0; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } circle:hover { fill: #ba9bc9; } .axis path, .axis line { fill: none; stroke: #666666; shape-rendering: crispEdges; } .axis text { font-family: Helvetica, Arial, sans-serif; font-size: 12px; } </style> </head> <body> <h1>Homicide rate vs life satisfaction score in OECD countries </h1> <p>Homicide rate on the horizontal axis, life satisfaction score on the vertical axis. Data from <a href="https://www.oecdbetterlifeindex.org/about/better-life-initiative/">OECD</a>.</p> <script type="text/javascript"> // Define height and width of SVG box var w = 600; var h = 500; var padding = [ 30, 20, 40, 40 ]; // Paading 0 top, 1 right, 2 bottom, 3 left // Create scale pixel range for width var xScale = d3.scale.linear() .range([ padding[3], w - padding[1] ]); // Create scale pixel range for height, and percentage gap var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); // Create x axis at the bottom var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(12); // Create y axis to the left var yAxis = d3.svg.axis() .scale(yScale) .ticks(8) .orient("left"); // Create SVG with right height and width var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); // Import data from file d3.csv("oecd.csv", function(data) { // Sort data data.sort(function(a, b) { return d3.descending(+a.homicide, +b.homicide); }); // Set domain (input range) for x scale xScale.domain([ 0, d3.max(data, function(d) {return +d.homicide;}) ]); // Set domain (input range) for y scale yScale.domain([ d3.max(data, function(d) {return +d.satisfaction;}), d3.min(data, function(d) {return +d.satisfaction;}) ]); // create circles var circles = svg.selectAll("circle") .data(data) .enter() .append("circle") // Set attributes for circles, initial state circles.attr("cx", padding[3]) .attr("cy", h - padding[2]) .attr("r", 5) .attr("fill", "#ffffff") .append("title") .text(function(d) { return d.country + "'s satisfaction rate: " + d.satisfaction + " % and homicide rate: " + d.homicide ; }); // Set new attributes for transition circles.sort(function(a, b) { return d3.ascending(+a.homicide, +b.homicide); }) .transition() // Delay in ms pr item .delay(function(d, i) { return i * 50; }) .duration(1000) .attr("fill", "#65c8d0") .attr("cx", function(d){ return xScale(d.homicide); }) .attr("cy", function(d) { return yScale(d.satisfaction); }) ; // Append x axis as group, translate svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0" + "," + (h - padding[2]) + ")") .call(xAxis); // Append y axis as group, translate svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js