Skip to content

Instantly share code, notes, and snippets.

@walterm
Created July 10, 2014 05:11
Show Gist options
  • Save walterm/78b6346341114dcc99c9 to your computer and use it in GitHub Desktop.
Save walterm/78b6346341114dcc99c9 to your computer and use it in GitHub Desktop.
SVG simple barchart
$(function(){
var data = [4, 8, 15, 16, 23, 42];
var width = 420,
barHeight = 20;
var x = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, width]);
var chart = d3.select(".chart")
.attr("width", width)
.attr("height", barHeight * data.length);
var bar = chart.selectAll("g")
.data(data)
.enter().append("g") //creating a group for the rectangle
.attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });
bar.append("rect")
.attr("width", x)
.attr("height", barHeight - 1);
bar.append("text")
.attr("x", function(d) { return x(d) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d) { return d; });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment