I am currently working on creating a vertical bar chart using D3.js, similar to the one shown in this https://i.sstatic.net/pig0g.gif
(source: statcan.gc.ca)
However, I am facing an issue as I am unable to display two sets of data for comparison. Following a tutorial from , I have created two classes "chart" and "chart1" with separate data but only the first set is being displayed. What could be causing this problem? Below is the code snippet: http://jsfiddle.net/x8rax/
<meta charset="utf-8">
<style>
.chart rect {
fill: rgb(203, 232, 118);
}
.chart2 rect {
fill: rgb(50, 50, 50);
}
.chart text {
fill: white;
font: 10px sans-serif;
text-anchor: end;
}
</style>
<svg class="chart"></svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var data = [4, 8, 15, 16, 23, 42];
var width = 420,
barHeight = 80;
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")
.attr("transform", function(d, i) { return "translate(0," + i * barHeight +")"; });
bar.append("rect")
.attr("width", x)
.attr("height", barHeight - 60);
bar.append("text")
.attr("x", function(d) { return x(d) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d) { return d; });
// The actual data may not be relevant at this point.
</script>
<svg class = "chart2"></svg>
<script>
var data2 = [10, 10, 10, 10, 10, 10];
var width = 420,
barHeight = 80;
var x2 = d3.scale.linear()
.domain([0, d3.max(data2)])
.range([0, width]);
var chart2 = d3.select(".chart")
.attr("width", width)
.attr("height", barHeight * data.length);
var bar2 = chart.selectAll("g")
.data(data2)
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * barHeight +")"; });
bar2.append("rect")
.attr("width", x2)
.attr("height", barHeight - 60);
</script>