chart = {
const width = 1194;
const height = 600;
const marginTop = 40;
const marginBottom = 50;
const marginRight = 32;
const marginLeft = 16;
const xAccessor = (d) => d.totalRead;
const yAccessor = (d) => d.publication;
const xObservations = d3.map(data, xAccessor);
const xDomain = [0, d3.max(xObservations)];
const yDomain = d3.groupSort(data, ([d]) => d.totalRead, yAccessor);
const xRange = [marginLeft, width - marginRight];
const yRange = [height - marginBottom, marginTop];
const xScaleType = d3.scaleLinear;
const yScaleType = d3.scaleBand;
const xScale = xScaleType(xDomain, xRange);
const yScale = yScaleType(yDomain, yRange).padding(0.1);
const xAxis = d3.axisTop(xScale).tickSizeOuter(0);
const svgEl = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("fill", "#0b0f0b")
.attr("viewBox", [0, 0, width, height]);
svgEl
.append("g")
.attr("transform", `translate(0,${marginTop})`)
.call(xAxis)
.call((g) => g.select(".domain").remove())
.call((g) =>
g
.selectAll(".tick:not(:first-child) line")
.clone()
.attr("y1", height - marginBottom - marginTop)
.attr("stroke-opacity", 0.3)
)
.call((g) =>
g
.append("text")
.attr("x", width - marginRight)
.attr("y", -marginTop + 15)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text("Total articles read →")
);
svgEl
.append("g")
.classed("bars", true)
.attr("stroke", "#ff6d00")
.selectAll()
.data(data)
.join("rect")
.attr("y", (d) => yScale(d.publication))
.attr("x", marginLeft)
.attr("width", (d) => xScale(d.totalRead) - marginLeft)
.attr("height", yScale.bandwidth());
svgEl
.append("g")
.classed("labels", true)
.attr("fill", "#d7d7db")
.selectAll()
.data(data)
.join("text")
.attr("x", marginLeft + 10)
.attr("y", (d) => yScale(d.publication) + 45)
.text((a) => `${a.publication} (${a.totalRead})`)
.attr("text-anchor", "start");
return svgEl.node();
}