function ratioBar (value, {
bind = null,
barFill = ['#0d75ff', '#b875eb'],
textFill = '#fff',
margin = {top: 10, right: 10, bottom: 10, left: 10},
width = 300,
height = 50,
} = {}) {
const data = [value, 100 - value]
const w = width + margin.left + margin.right
const h = height + margin.top + margin.bottom
bind.selectAll('svg').remove();
const svg = bind.append('svg')
.attr('width', w)
.attr('height', h)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
const xScale = d3.scaleLinear()
.domain([0, 100])
.range([margin.left, width])
const wrap = svg.append('g')
.attr('class', 'ratio-wrap')
const firstRect = wrap.append('rect')
.attr('height', height)
.attr('width', xScale(data[0]))
.attr('y', margin.top)
.style('fill', barFill[0])
const scndRect = wrap.append('rect')
.attr('height', height)
.attr('x', xScale(data[0]))
.attr('y', margin.top)
.attr('width', xScale(data[1]))
.style('fill',barFill[1])
const label = svg.append('text')
.attr('class', 'ratio-label')
.attr('y', height / 2 + margin.top)
.attr('dy', 5)
.style('text-anchor', 'middle')
.style('fill', textFill)
.attr('x', function () {
console.log('xScale(data[0])', xScale(data[0]))
if (xScale(data[0]) < 25 || xScale(data[0]) > (w - 35)) {
console.log('ratio returning width')
return width / 2
} else {
return xScale(data[0])
}
})
.text(data[0] + ' : ' + data[1])
return svg;
}