Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created March 22, 2012 06:54
Show Gist options
  • Select an option

  • Save enjalot/2156737 to your computer and use it in GitHub Desktop.

Select an option

Save enjalot/2156737 to your computer and use it in GitHub Desktop.
samples for tributary
var w = 600
, h = 467
var data1 = [
5,
20,
55,
60,
89
];
function bars(data)
{
var max = d3.max(data)
//nice breakdown of d3 scales
//http://www.jeromecukier.net/blog/2011/08/11/d3-scales-and-color/
x = d3.scale.linear()
.domain([0, max])
.range([0, w])
y = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeBands([0, h], .2)
var vis = d3.select("svg")
//a good written tutorial of d3 selections coming from protovis
//http://www.jeromecukier.net/blog/2011/08/09/d3-adding-stuff-and-oh-understanding-selections/
var bars = vis.selectAll("rect.bar")
.data(data)
//update
bars
.attr("fill", "#0a0")
.attr("stroke", "#050")
//enter
bars.enter()
.append("svg:rect")
.attr("class", "bar")
.attr("fill", "#800")
.attr("stroke", "#800")
//exit
bars.exit()
.remove()
//apply to everything (enter and update)
bars
.attr("stroke-width", 4)
.attr("width", x)
.attr("height", y.rangeBand())
.attr("transform", function(d,i) {
return "translate(" + [0, y(i)] + ")"
})
}
bars(data1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment