{
const width = 400;
const height = 250;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 40;
const marginLeft = 60;
const color = d3.scaleOrdinal(d3.schemeTableau10)
.domain(data.map(d => d.name));
const x = d3.scaleLinear()
.domain([ 0, d3.max(data, d => d.value)])
.range([marginLeft, width - marginRight]);
const y = d3.scaleBand()
.domain(data.map(d => d.name))
.range([marginTop, height - marginBottom])
.padding(0.1);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(0))
.attr("y", d => y(d.name))
.attr("height", y.bandwidth)
.attr("width", d => x(d.value) - x(0))
.style("fill", d => color(d.name));
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x).ticks(5))
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y));
return svg.node();
}