function dualBarcode (data, {
parentLabelKey = 'p_label',
parentValueKey = 'parent_value',
domain = [0, data[parentValueKey]],
height = 40,
width = 600,
line_height = height *0.8,
stroke_width= 1,
stroke = '#0077B7',
stroke_parent = '#ccc',
margin = {top: 15, right: 30, bottom: 0, left: 5},
} = {}) {
const w = width - margin.left - margin.right
const h = height - margin.top - margin.bottom
const xScale = d3.scaleLinear()
.domain(domain)
.range([0, w])
const svg = DOM.svg(width, height);
const sel = d3.select(svg)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
sel.selectAll('line')
.data(d3.range(data.parent_value))
.join('line')
.attr('x1', d => xScale(d))
.attr('x2', d => xScale(d))
.attr('y1', 0)
.attr('y2', line_height)
.attr('stroke-width', stroke_width)
.attr('stroke', d => {
if ((d + 1) <= data.child_value) {
return stroke
} else {
return stroke_parent
}
})
sel.append('text')
.attr('x', d => xScale(data.child_value -1))
.attr('y', -2)
.attr('font-size', '11px')
.attr('text-anchor', 'end')
.attr('fill', stroke)
.text(data.child_value)
sel.append('text')
.attr('x', d => xScale(data.parent_value -1))
.attr('y', -2)
.attr('font-size', '11px')
.attr('text-anchor', 'end')
.attr('fill', '#888')
.text(data.parent_value)
return svg;
}