D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
romsson
Full window
Github gist
Horizontal Bar Chart
<!DOCTYPE html> <html lang="en"> <meta charset="utf-8"> <head> <script src="https://d3js.org/d3.v3.min.js"></script> <style type="text/css"> rect { fill:teal; fill-opacity:.8; } </style> </head> <body> <script type="text/javascript"> var margin = {top: 50, bottom: 10, left:300, right: 40}; var width = 900 - margin.left - margin.right; var height = 900 - margin.top - margin.bottom; var xScale = d3.scale.linear().range([0, width]); var yScale = d3.scale.ordinal().rangeRoundBands([0, height], .8, 0); var svg = d3.select("body").append("svg") .attr("width", width+margin.left+margin.right) .attr("height", height+margin.top+margin.bottom); var g = svg.append("g") .attr("transform", "translate("+margin.left+","+margin.top+")"); d3.json("countries_2012.json", function(data) { var max = d3.max(data, function(d) { return d.population; } ); var min = 0; xScale.domain([min, max]); yScale.domain(data.map(function(d) { return d.name; })); var groups = g.append("g") .selectAll("text") .data(data) .enter() .append("g"); var bars = groups .append("rect") .attr("width", function(d) { return xScale(d.population); }) .attr("height", 5) .attr("x", xScale(min)) .attr("y", function(d) { return yScale(d.name); }) }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js