D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
eplunkett
Full window
Github gist
Cleaning chart
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Indices of Multiple Deprivation 2010 Score by Local Authority</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: ; font-family: Arial } svg { background-color: white; } rect { fill: darkorange } h1 { color: darkblue } p { color: darkslateblue } rect:hover { fill: darkblue; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: Arial; font-size: 11px; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>Measuring Deprivation in England</h1> <p>English indices of multiple deprivation 2010 scores by Local Authority area (higher value = more deprived area)</p> <p><em>Source:</em> <a href="https://www.gov.uk/government/statistics/english-indices-of-deprivation-2010">DCLG 2010</a></p> <script type="text/javascript"> var w = 700; var h = 3500; var padding = [ 0, 100, 220, 200 ]; //Top, right, bottom, left var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.5); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("IMD2010Data-byLA.csv", function(data) { //console.log(data); data.sort(function(a, b) { return d3.descending(+a.imdscore, +b.imdscore); //If your numeric values aren't sorting properly, //try commenting out the line above, and instead using: // //return d3.descending(+a.lifeSatisfaction, +b.lifeSatisfaction); // //Data coming in from the CSV is saved as strings (text), //so the + signs here force JavaScript to treat those //strings instead as numeric values, thereby fixing the //sort order (hopefully!). }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.imdscore; }) ]); heightScale.domain(data.map(function(d) { return d.LocalAuthority; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.LocalAuthority); }) .attr("width", function(d) { return widthScale(d.imdscore); }) .attr("height", 8) //.attr("height", heightScale.rangeBand()) .attr("fill", "steelblue") .append("title") .text(function(d) { return d.LocalAuthority + " has an IMD 2010 score of " + d.imdscore; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); 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