Skip to content

Instantly share code, notes, and snippets.

@danasilver
Last active August 29, 2015 13:57
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 danasilver/9530403 to your computer and use it in GitHub Desktop.
Save danasilver/9530403 to your computer and use it in GitHub Desktop.
GitHub Language Bar Chart
<!DOCTYPE html>
<meta charset="utf8">
<style>
rect {
fill: #0086b3;
}
rect:hover {
fill: brown;
}
text {
font: 10px sans-serif;
text-anchor: middle;
}
.bar text {
fill: #FFF;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 10, right: 20, bottom: 20, left: 60},
width = 600 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.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.json("https://api.github.com/repos/danasilver/danasilver.org/languages", function(error, response) {
var data = [];
for (lang in response) {
data.push({"lang": lang, "bytes": response[lang]});
}
x.domain(data.map(function(d) { return d.lang; }));
y.domain([0, d3.max(data, function(d) { return d.bytes })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
d3.select("svg").append("text")
.style("text-anchor", "end")
.attr("transform", "translate(15," + height / 2 + "), rotate(-90)")
.text("Bytes");
var bar = svg.selectAll(".bar")
.data(data)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(" + x(d.lang) + ",0)"; });
bar.append("rect")
.attr("class", "bar")
.attr("y", function(d) { return y(d.bytes); })
.attr("height", function(d) { return height - y(d.bytes); })
.attr("width", x.rangeBand());
bar.append("text")
.attr("x", x.rangeBand() / 2)
.attr("y", function(d) { return y(d.bytes) + 3; })
.attr("dy", ".75em")
.text(function(d) { return d.bytes; });
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment