function BarChart(
data,
{
x = (d) => d,
y = (d, i) => i,
title,
marginTop = 30,
marginRight = 0,
marginBottom = 10,
marginLeft = 1,
width = 640,
height,
xType = d3.scaleLinear,
xDomain,
xRange = [marginLeft, width - marginRight],
xFormat,
xLabel,
yPadding = 0.1,
yDomain,
yRange,
color = "currentColor",
titleColor = "white",
titleAltColor = "currentColor",
avgDatum
} = {}
) {
const X = d3.map(data, x);
const Y = d3.map(data, y);
if (xDomain === undefined) xDomain = [0, d3.max(X)];
if (yDomain === undefined) yDomain = Y;
yDomain = new d3.InternSet(yDomain);
// Omit any data not present in the y-domain.
const I = d3.range(X.length).filter((i) => yDomain.has(Y[i]));
// Compute the default height.
if (height === undefined)
height =
Math.ceil((yDomain.size + yPadding) * 25) + marginTop + marginBottom;
if (yRange === undefined) yRange = [marginTop, height - marginBottom];
// Construct scales and axes.
const xScale = xType(xDomain, xRange);
const yScale = d3.scaleBand(yDomain, yRange).padding(yPadding);
const xAxis = d3.axisTop(xScale).ticks(width / 80, xFormat);
const yAxis = d3.axisLeft(yScale).tickSizeOuter(0);
// Compute titles.
if (title === undefined) {
const formatValue = xScale.tickFormat(100, xFormat);
title = (i) => `${formatterNumber(formatValue(X[i]))}`;
} else {
const O = d3.map(data, (d) => d);
const T = title;
title = (i) => T(O[i], i, data);
}
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; height: intrinsic;");
svg
.append("g")
.attr("class", "rects")
.attr("fill", color)
.selectAll("rect")
.data(I)
.join("rect")
.attr("x", xScale(0))
.attr("y", (i) => yScale(Y[i]))
.attr("width", (i) => xScale(X[i]) - xScale(0))
.attr("height", yScale.bandwidth())
.classed("myCountry", (i) => data[i].country_code === myCountry)
.on("mouseover", (el, i) => {
d3.select(el.target).classed("hover", true);
d3.select(".labels")
.selectAll("text")
.filter((idNode) => i === idNode)
.classed("visible", true);
d3.select(".y.axes")
.selectAll(".tick")
.filter((countryName) => countryName === data[i].country_name)
.classed("visible", true);
})
.on("mouseout", (el, i) => {
d3.select(el.target).classed("hover", false);
d3.select(".labels")
.selectAll("text")
.filter((idNode) => i === idNode)
.classed("visible", false);
d3.select(".y.axes").selectAll(".tick").classed("visible", false);
});
svg
.append("g")
.attr("class", "labels")
.attr("fill", titleColor)
.attr("text-anchor", "end")
.attr("font-size", 10)
.selectAll("text")
.data(I)
.join("text")
.attr("x", (i) => xScale(X[i] - 3))
.attr("y", (i) => yScale(Y[i]) + yScale.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", -4)
.text(title)
.classed("myCountry", (i) => data[i].country_code === myCountry)
.call((text) =>
text
.filter((i) => xScale(X[i]) - xScale(0) < 20) // short bars
.attr("dx", +4)
.attr("fill", titleAltColor)
.attr("text-anchor", "start")
);
svg
.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.attr("class", "y axes")
.call(yAxis)
.selectAll(".tick")
.filter((countryName, i) => {
const countryObj = data.filter((d) => d.country_name === countryName)[0];
return countryObj.country_code === myCountry;
})
.classed("myCountry", true);
svg
.append("g")
.attr("class", "avgLine")
.append("line")
.attr("x1", xScale(avgDatum))
.attr("x2", xScale(avgDatum))
.attr("y1", 22)
.attr("y2", height - marginBottom)
.attr("stroke", "black")
.attr("stroke-dasharray", "5, 5");
svg
.select(".avgLine")
.append("text")
.attr("x", xScale(avgDatum) + 3)
.attr("y", 20)
.text(locale[languageSelector].avgLine(formatterNumber(avgEu)))
.attr("text-anchor", "end");
svg
.append("rect")
.attr("x", xScale(30) - 10)
.attr("y", 30)
.attr("width", 20)
.attr("height", 15)
.attr("fill", "white");
svg
.append("g")
.attr("transform", `translate(0,${marginTop})`)
.attr("class", "x axes")
.call(xAxis)
.call((g) => g.select(".domain").remove())
.call((g) =>
g
.selectAll(".tick line")
.clone()
.attr("y2", height - marginTop - marginBottom)
.attr("stroke-opacity", 0.1)
)
.call((g) =>
g
.append("text")
.attr("x", width - marginRight)
.attr("y", -22)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(xLabel)
);
return svg.node();
}