chart = {
const MIN_WIDTH_FOR_LABELS = 30;
const DEFAULT_OPACITY = '0.7';
const OPACITY_ON_OVER = '1';
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const rect = svg.append("g")
.selectAll("g")
.data(series)
.join("g")
;
rect.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", d => x(d[0]))
.attr("y", (d, i) => y(d.data.name))
.attr("width", d => x(d[1]) - x(d[0]))
.attr("height", y.bandwidth())
.attr("fill", d => color(d.key))
.attr("opacity", DEFAULT_OPACITY)
.on('mouseover', (d, i) => {
tooltip
.html(
`<div>${d3.format('.3s')(d.data[d.key].v)} ${d.data[d.key].c}</div>`
)
.style('visibility', 'visible');
d3.select(this).transition()
.attr("opacity", OPACITY_ON_OVER);
})
.on('mousemove', () => {
tooltip
.style('top', d3.event.pageY - 10 + 'px')
.style('left', d3.event.pageX + 10 + 'px');
})
.on('mouseout', () => {
tooltip.html(``).style('visibility', 'hidden');
d3.select(this).transition().attr("opacity", DEFAULT_OPACITY);
});
rect.selectAll("text")
.data(d => d)
.join("text")
.attr('font-family','sans-serif')
.attr('font-size', '0.9em')
.attr("x", d => x(d[0]) + 5)
.attr("y", (d, i) => y(d.data.name) + 20)
.attr("fill", 'black')
.append('tspan')
.attr("x", d => x(d[0]) + 5)
.attr("dx", 0)
.attr("dy", 0)
.attr('font-size', '0.8em')
.text(d => x(d[1]) - x(d[0]) > MIN_WIDTH_FOR_LABELS ? `${d3.format('.3s')(d.data[d.key].v)}` : '')
.append('tspan')
.attr('font-size', '0.8em')
.text(d => x(d[1]) - x(d[0]) > MIN_WIDTH_FOR_LABELS ? ` ${d.data[d.key].c}` : '')
;
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();
}