svg = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const margin = { left: 80, right: 20, top: 75, bottom: 50 },
iwidth = width - margin.left - margin.right,
iheight = height - margin.top - margin.bottom;
const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
const x = d3
.scaleBand()
.domain([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
.padding(0.3)
.range([0, iwidth]);
const y = d3
.scaleLinear()
.domain([
0,
d3.max(groupedData, (gr) => d3.median(gr[1], (g) => g["count"]))
])
.range([iheight, 0])
.nice();
const color2 = d3
.scaleSequentialSqrt(d3.interpolateGreens)
.domain(y.domain());
g.append("text")
.attr("class", "title")
.attr("text-anchor", "middle")
.attr("x", iwidth / 2)
.attr("y", -(height / 25))
.style("font-size", width / 50)
.style("font-family", '"Open Sans", sans-serif')
.text(
"Coscinodiscus morphotype Median Cell Count by Month in all Sample Sites"
);
g.append("g")
.attr("class", "x--axis")
.style("font-size", width / 100)
.call(d3.axisBottom(x).tickFormat(monthFmt))
.attr("transform", `translate(0, ${iheight})`)
.call((axis) =>
axis
.append("text")
.text("Month")
.style("fill", "black")
.style("font-size", width / 75)
.attr("transform", `translate(${iwidth / 2}, 40)`)
.style("text-anchor", "middle")
);
g.append("g")
.attr("class", "y--axis")
.style("font-size", width / 100)
.call(d3.axisLeft(y));
g.append("text")
.attr("class", "y label")
.attr("text-anchor", "middle")
.attr("x", -(iheight / 2))
.attr("y", -width / 25)
.style("font-size", "14px")
.attr("transform", "rotate(-90)")
.style("font-family", '"Open Sans", sans-serif')
.text("Average Cell Count per Liter");
g.selectAll(".bar")
.data(groupedData)
.join("rect")
.attr("class", "bar")
.attr("x", (gr) => x(gr[0]))
.attr("width", x.bandwidth())
.attr("y", (gr) => y(d3.median(gr[1], (g) => g["count"])))
.attr("height", (gr) => iheight - y(d3.median(gr[1], (g) => g["count"])))
.attr("fill", (gr) => color2(d3.median(gr[1], (g) => g["count"])));
g.selectAll(".value")
.data(groupedData)
.join("text")
.attr("class", "value")
.attr("text-anchor", "middle")
.attr("x", (gr) => x(gr[0]) + x.bandwidth() / 2)
.attr("y", (gr) => y(d3.median(gr[1], (g) => g["count"])) - height / 200)
.style("fill", "black")
.style("font-size", "8pt")
.style("font-family", '"Open Sans", sans-serif')
.text((gr) => d3.format(".2f")(d3.median(gr[1], (g) => g["count"])));
return svg.node();
}