D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sporange
Full window
Github gist
HLE data for men
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Male HLE</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } h2 { font-size: 18px; margin: 0; } p { font-size: 12px; margin: 10px 0 0 0; } svg { background-color: white; } rect:hover { fill: #043639; } .axis path, .axis line { fill: none; stroke: gray; shape-rendering: crispEdges; } .axis text { font-family: Helvetica, Arial, sans-serif; font-size: 14px; font-weight: bold; color:#043639; } .y.axis path, .y.axis line { opacity: 0; } p.padding{ padding-left: 5.2cm; } </style> </head> <body> <h1>Healthy life expectancy for men</h1> <h2>by English region, 2010 to 2012<br> </h2> <p>Healthy life expectancy (HLE) is an estimate of the years of life that will be spent in good health, and is calculated for England by the Office for National Statistics.</p> <p>The chart here shows HLE for men for the nine regions in England in 2010-12. It shows about a six year difference between the highest (South East) and lowest (North East) regions.</p> <script type="text/javascript"> var w = 600; var h = 350; var padding = [ 20, 10, 30, 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.1); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom") .ticks(7); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("PHOF_LE_data_EngRegion.csv", function(data) { //added + to force numeric data.sort(function(a, b) { return d3.descending(+a.HealthyLE_Male_2010to12, +b.HealthyLE_Male_2010to12); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.HealthyLE_Male_2010to12; }) ]); heightScale.domain(data.map(function(d) { return d.region; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.region); }) .attr("width", function(d) { return widthScale(d.HealthyLE_Male_2010to12); }) .attr("height", heightScale.rangeBand()) .attr("fill", "#1aa5ad") .append("title") .text(function(d) { return d.region + "'s male healthy life expectancy in 2010-12 was " + d.HealthyLE_Male_2010to12 + " years."; }); 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> <p class="padding"><b>age in years</b></p> <p><b>Source:</b> <a href="https://www.phoutcomes.info/">Public Health Outcomes Framework</a></p> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js