D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jadiehm
Full window
Github gist
FBGovernmentRequests_D3BarChart
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Facebook Data Requests by Country</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> h1 { font-family: sans-serif; margin-bottom:0; } p { font-family: sans-serif; font-size: .75em; color:gray; margin-bottom:0; } svg { background-color: white; } rect:hover { fill: #00664C; } .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>Requests for Facebook User Data by Government Officials</h1> <p>July 2014 – Dec. 2014 | Source: <a href="https://govtrequests.facebook.com/">Facebook</a></p> <script type="text/javascript"> var w = 750; //sets the width variable var h = 1400; //sets the height variable var padding = [0, 10, 60, 120]; //top, right, bottom, left var widthScale = d3.scale.linear() //.domain([0,15000]) input values .range([0,w - padding[1] - padding[3]]); //pixel values var heightScale = d3.scale.ordinal() .rangeRoundBands([padding[0], h - padding[2]], 0.12); //rounds to nearest pixel and adds space in between 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("FBGovernmentRequests.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a.dataRequests, +b.dataRequests); }); widthScale.domain([0, d3.max(data, function(d){ return +d.dataRequests; }) ]); heightScale.domain(data.map(function(d) {return d.Country;})); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d, i) { return heightScale(d.Country); }) .attr("width", function(d) { return widthScale(d.dataRequests); }) .attr("height", heightScale.rangeBand()) .attr("fill", "#00CC99") .append("title") .text(function(d) { return d.Country + " requested data from " + d.dataRequests + " users"; }); 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