Skip to content

Instantly share code, notes, and snippets.

@sampathweb
Created December 21, 2015 07:16
Show Gist options
  • Save sampathweb/5f8a880323f42fdde415 to your computer and use it in GitHub Desktop.
Save sampathweb/5f8a880323f42fdde415 to your computer and use it in GitHub Desktop.
Bar Chart_beta
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.log()
.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(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.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 = +d.Total;
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment