D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jonolave
Full window
Github gist
Unemployment rate with axes
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content=type" content="text/html"; charset="utf-8"> <title>Unemployment rate in Europe</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; } rect: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; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>Unemployment rate in Europe</h1> <p>In percentage per country, December 2014. Data from <a href="https://ec.europa.eu/eurostat/web/products-datasets/-/ei_lmhr_m">Eurostat (EU)</a>.</p> <script type="text/javascript"> // Define height and width of SVG box var w = 600; var h = 700; var padding = [ 10, 20, 40, 100 ]; // Paading 0 top, 1 right, 2 bottom, 3 left // Create scale pixel range for width var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); // Create scale pixel range for height, and percentage gap var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.2); // Create x axis at the bottom var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); // Create y axis to the left var yAxis = d3.svg.axis() .scale(heightScale) .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("unemployment.csv", function(data) { // Sort data data.sort(function(a, b) { return d3.descending(+a.n2014M12, +b.n2014M12); }); // Set domain (input range) for width scale widthScale.domain([ 0, d3.max(data, function(d) { return +d.n2014M12; }) ]); // Set domain (input range) for height scale based on number of countries heightScale.domain(data.map(function(d) { return d.country; } )); // create rectangles var rects = svg.selectAll("rect") .data(data) .enter() .append("rect") // Set attributes for rectangles rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.country); }) .attr("width", function(d) { return widthScale(d.n2014M12); }) .attr("height", heightScale.rangeBand()) .attr("fill", "#65c8d0") .append("title") .text(function(d) { return d.country + "'s unemployment rate was " + d.n2014M12 + " %" ; }); // Append x axis as group, translate svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (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