BarChart experiment with D3 (SVG)

http://bost.ocks.org/mike/bar/2/

by Anton

HTML

<svg class="chart"></svg>

CSS

.chart rect {
    fill: steelblue;
}

.chart text {
  fill: white;
  font: 10px sans-serif;
  text-anchor: end;
}

JavaScript

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);

// bar - array of selected g-elements
var bar = chart.selectAll("g")
        .data(data)
    .enter().append("g")
        .attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });

// .append - to each entry in the bar[]
bar.append("rect")
        .attr("width", x)
        .attr("height", barHeight - 1)
    .append("title")
        .text(function(d) { return "Value " + d; });

bar.append("text")
    .attr("x", function(d) { return x(d) - 5; })
    .attr("y", barHeight / 2)
    .attr("dy", ".3em")
    .text(function(d) { return d; });