The concept here is to organize the bars into a DIV container and create separation between the group div elements with margin or padding.
const data = [4, 8, 15, 16, 23, 42];
let groupedData=[];
for(let i=0;i<data.length/2;i++){
groupedData[i]={
"first":data[i*2],
"second":data[i*2+1]
};
}
console.log(groupedData);
const xScale = d3.scaleLinear()
.domain([0, d3.max(data)])
.range([0, 420]);
barsEnter=d3.select(".chart")
.selectAll("div.bars")
.data(groupedData).enter().append('div').attr('class','bargroup');
barsEnter.append('div')
.style("width", function(d) { return xScale(d.first) + "px"; })
.text(function(d) { return d.first; });
barsEnter.append('div')
.style("width", function(d) { return xScale(d.second) + "px"; })
.text(function(d) { return d.second; });
The CSS has been updated as follows:
.chart .bargroup{
margin-bottom:10px;
}
.chart .bargroup div {
font: 10px sans-serif;
background-color: steelblue;
text-align: right;
padding: 3px;
margin: 1px;
color: white;
}
You may access the updated code through the link provided below:
http://codepen.io/anon/pen/YqJpzK