{
const margin = { top: 20, right: 10, bottom: 20, left: 100 };
const visWidth = widthPop - margin.left - margin.right;
const visHeight = heightPop - margin.top - margin.bottom;
const InvertedCountryScale = d3.scaleBand()
.domain(populations.map(d => d.country))
.range([0, visWidth])
.paddingInner(0.2)
.paddingOuter(0.02);
const svg = d3.create("svg")
.attr("width", visWidth + margin.left + margin.right)
.attr("height", visHeight + margin.top + margin.bottom);
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const r = 6;
const barCenter = d =>
InvertedCountryScale(d.country) + (InvertedCountryScale.bandwidth()) / 2;
g.append("g")
.selectAll("line")
.data(populations)
.join("line")
.attr("x1", d => barCenter(d))
.attr("x2", d => barCenter(d))
.attr("y1", InvertedPopulationScale(0))
.attr("y2", d => InvertedPopulationScale(d.population))
.attr("stroke", "steelblue")
.attr("stroke-width", 4)
.attr("stroke-linecap", "round");
g.append("g")
.selectAll("circle")
.data(populations)
.join("circle")
.attr("cx", d => barCenter(d))
.attr("cy", d => InvertedPopulationScale(d.population))
.attr("r", r)
.attr("fill", "steelblue");
g.append("g")
.call(d3.axisLeft(InvertedPopulationScale).ticks(8, "~s"))
.call(g => g.select(".domain").remove());
g.append("g")
.attr("transform", `translate(0, ${InvertedPopulationScale(0)})`)
.call(d3.axisBottom(InvertedCountryScale))
.call(g => g.select(".domain").remove())
.selectAll("text")
.attr("dy", "0.8em")
.attr("text-anchor", "middle");
svg.select("text")
.attr("x", -(margin.top + visHeight / 2))
.attr("y", -margin.left + 16);
return svg.node();
}