{
const svg = d3.create("svg")
.attr("width", width)
.attr("height", y.range()[1])
.attr("font-family", "sans-serif")
.attr("font-size", "12")
.attr("text-anchor", "end");
const datanew = data.map((d) => {
return {Year: d.Year, Indicator: d[state]}
})
const x = d3.scaleLinear()
.domain([0, d3.max(datanew, d => d.Indicator)])
.range([0, width])
const bar = svg.selectAll("g")
.data(datanew)
.join("g")
.attr("transform", d => `translate(0,${y(d.Year)})`);
bar.append("rect")
.attr("fill", "steelblue")
.attr("width", d => x(d.Indicator))
.attr("height", y.bandwidth() - 1)
.on('mouseover', function (e, d, i) {
tooltip
.html(
`<div>${d.Indicator}</div>`)
.style('visibility', 'visible');
d3.select(this).attr('fill', '#ECD444');
})
.on('mousemove', function (e) {
tooltip
.style('top', e.pageY + 10 + 'px')
.style('left', e.pageX + 10 + 'px');
})
.on('mouseout', function () {
tooltip.html(``).style('visibility', 'hidden');
d3.select(this).transition().attr('fill', 'steelblue');
});
bar.append("text")
.attr("fill", "yellow")
.attr("x", d => x(d.Indicator) - 3)
.attr("y", y.bandwidth() / 2)
.attr("dy", "0.35em")
.text(d => d.Indicator);
return svg.node();
}