D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Lampadaire
Full window
Github gist
Weapons export worldwide_step4
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Data on weapons exports in the world</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> h1,h2,p { font-family:"Helvetica"; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>Who is exporting weapons worldwide?</h1> <h2>Data about weapons exports</h2> <script type="text/javascript"> //Defines a bunch of variables to be used later in the chart, makes life easier when wanting to change some stuff //1 width and height of the svg element var w = 700; var h = 1800; //2 padding variables, defines various white spaces in the chart var padding = [ 20, 10, 30, 120 ]; //Top, right, bottom, left //3 creates the scale for axis, the scale needs to be created before the axis itself //linear scale, basically a scale for continuous numbers,range defines the length of the scale var widthScale = d3.scale.linear() .range([ 0, w-padding[1]-padding[3]]); //console.log(widthScale); //ordinal scale, basically a scale for categories,rangeRoundBands defines the position of the top and bottom cat. of the scale, O.1 the space var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2]], 0.1); //4 creates both axis var xAxis = d3.svg.axis() .scale(widthScale) .orient("top"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); //creates svg element named box var svg = d3.select("body").append("svg").attr("width",w).attr("height",h); //load the data and bind it to rectangles d3.csv("WeaponsExport19952014.csv", function(data) { var bars = svg.selectAll("rect").data(data).enter().append("rect"); //defines the input domain for the linear scale widthScale.domain([ 0,d3.max(data, function(d) { return +d.Total; })]); heightScale.domain(data.map(function(d) { return d.Country; } )); bars.attr("x",padding[3]) .attr("y",function(d) { return heightScale(d.Country); }) .attr("width", function(d) { return widthScale(d.Total); }) //.attr("width",100) .attr("height",heightScale.rangeBand()) .attr("fill","#9933FF") .append("title") .text(function(d) { return d.Country + "'s total weapons exports for the period 1995-2014 is " + d.Total + " TIV"; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + padding[0] + ")") .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