Skip to content

Instantly share code, notes, and snippets.

@malcolm-decuire
Last active December 21, 2015 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcolm-decuire/3e46da46186e1dc1a02e to your computer and use it in GitHub Desktop.
Save malcolm-decuire/3e46da46186e1dc1a02e to your computer and use it in GitHub Desktop.
Lobbying Spending 1998-2015
Sector Total
Misc 6,882,333,430.00
Health 6,758,960,636.00
Finance 6,717,470,377.00
Tech 5,436,881,666.00
Energy 4,948,702,039.00
Other 3,499,947,126.00
Transportation 3,399,834,908.00
Single-Issue 2,177,981,449.00
Agribusiness 2,009,717,638.00
Defense 1,931,435,109.00
Construction 735,143,342.00
Labor 661,117,619.00
Lawyers & Lobbyists 433,853,668.00
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], 0.1);
var y = d3.scale.linear()
//d3_format.format(",.2r")
.domain([-2e10, 9e10])
.range([height,0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.ticks(20, ",.1s")
.tickSize(6, 0)
.scale(y)
.orient("left")
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("data.tsv", type, function(error, data) {
if (error) throw error;
x.domain(data.map(function(d) { return d.Sector; }));
y.domain([0, d3.max(data, function(d) { return d.Total; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format("s")))
.call(d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10)
.tickFormat(d3.format("s")))
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 2)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Total");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.Sector); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.Total); })
.attr("height", function(d) { return height - y(d.Total); });
});
function type(d) {
d.Total = parseInt(d.Total.replace(/,/g,''));
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment