{
const width = 1400;
const height = 800;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 50;
const marginLeft = 80;
const spacing = 12;
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.style("overflow", "visible");
const x = d3.scaleBand()
.domain(Array.from(new Set(barley.map((d) => d.variety))))
.range([0, width - 400])
.padding(0.3);
const y = d3.scaleBand()
.domain(Array.from(new Set(barley.map((d) => d.site))))
.range([height - 100, 0])
.padding(0.3);
const g = svg
.append("g")
.attr("transform", `translate(${marginLeft}, ${marginBottom})`);
g.append("g")
.attr("transform", `translate(0,${height - 100})`)
.call(d3.axisBottom(x));
g.append("g")
.call(d3.axisLeft(y));
const barleyLayer = g.append("g").attr("class", "barley-layer");
const grouped = d3.group(barley, d => d.variety, d => d.site);
const updateFunctions = [];
for (const [variety, siteMap] of grouped) {
for (const [site, records] of siteMap) {
const record1931 = records.find(d => d.year === "1931") ;
const record1932 = records.find(d => d.year === "1932") ;
const cx = x(variety) + x.bandwidth() / 2;
const cy = y(site) + y.bandwidth() / 2;
const group = barleyLayer.append("g")
.attr("transform", `translate(${cx}, ${cy})`);
group.append("line")
.attr("x1", 0)
.attr("x2", 0)
.attr("y1", -spacing * 3.5)
.attr("y2", spacing * 4)
.attr("stroke", "black")
.attr("stroke-width", 1.5);
const updater = drawBarleyMark(group, record1931.yield, record1932.yield, 0, 0);
updateFunctions.push(updater);
}
}
const container = htl.html`<div></div>`;
const buttons = d3.select(container)
.selectAll("button")
.data(["1931", "1932"])
.join("button")
.text(d => ` ${d}`)
.style("margin-right", "8px")
.on("click", (event, year) => {
updateFunctions.forEach(fn => fn(year));
});
d3.select(container).append(() => svg.node());
return container;
}