async function horizontalBarChart() {
const dataset = sampleData;
let dimensions = {
width: 685,
height: 450,
margin: {
top: 25,
right: 25,
bottom: 50,
left: 75
}
};
dimensions.boundedWidth =
dimensions.width - dimensions.margin.left - dimensions.margin.right;
dimensions.boundedHeight =
dimensions.height - dimensions.margin.top - dimensions.margin.bottom;
const wrapper = d3
.select("#wrapper-horizontalBarChart")
.append("svg")
.attr("width", dimensions.width)
.attr("height", dimensions.height);
const bounds = wrapper
.append("g")
.style(
"transform",
`translate(${dimensions.margin.left}px, ${dimensions.margin.top}px)`
);
const xAccessor = d => d.value;
const xScale = d3
.scaleLinear()
.domain([0, d3.max(dataset, xAccessor)])
.range([dimensions.margin.left, dimensions.boundedWidth])
.nice();
const xAxis = d3
.axisTop(xScale)
.ticks(width / 80)
.tickFormat(d3.format(".2s"));
const yAccessor = d => d.county;
const yScale = d3
.scaleBand()
.domain(d3.range(dataset.length))
.rangeRound([dimensions.margin.top, dimensions.boundedHeight])
.padding(0.1);
const yAxis = d3
.axisLeft(yScale)
.tickFormat(i => `${dataset[i].county} - ${dataset[i].gender}`)
.tickSizeOuter(0);
const colorAccessor = d => d.gender;
const colorScale = d3
.scaleOrdinal()
.domain(["Male", "Female"])
.range([allumaGreen, allumaSlate]);
const barHeight = 25;
bounds
.append("g")
.selectAll("rect")
.data(dataset)
.join("rect")
.attr("x", xScale(0))
.attr("y", (d, i) => yScale(i))
.attr("width", d => xScale(xAccessor(d) - xScale(0)))
.attr("height", yScale.bandwidth())
.style("fill", d => colorScale(colorAccessor(d)));
bounds
.append("g")
.style("transform", `translate(0,${dimensions.margin.top}px)`)
.call(xAxis);
bounds
.append("g")
.style("transform", `translate(${dimensions.margin.left}px, 0px)`)
.call(yAxis);
}