viewof contourTernaryPlot = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const chart = svg
.append("g")
.attr("transform", `translate(${width / 2} ${height / 2 + yOffset})`)
.attr("height", 300)
.attr("font-family", "sans-serif")
.attr("id", "chart");
chart.append("circle").attr("r", 4).attr("fill", "gold");
chart
.append("circle")
.attr("r", 4)
.attr("cx", 90)
.attr("cy", -3 * bandwidth)
.attr("fill", "coral");
chart
.append("circle")
.attr("r", 4)
.attr("cx", -3 * bandwidth)
.attr("cy", 120)
.attr("fill", "coral");
const defs = chart.append("defs");
const clipPath = defs
.append("clipPath")
.attr("id", "trianglePath")
.append("path")
.attr("d", densityContourTernaryPlot.triangle());
const trianglePath = chart
.append("path")
.attr("d", densityContourTernaryPlot.triangle())
.attr("fill", "none")
.attr("stroke-width", "none");
const axisLabelsGroup = chart
.append("g")
.attr("class", "axis-labels")
.call(axisLabels, densityContourTernaryPlot.axisLabels());
const gridLinesPaths = densityContourTernaryPlot
.gridLines()
.map((axisGrid) => axisGrid.map(d3.line()).join(" "));
const gridGroup = chart
.append("g")
.attr("class", "grid")
.call(grid, gridLinesPaths);
const axisTicksGroups = chart
.append("g")
.attr("class", "ternary-ticks")
.attr("font-size", 10)
.selectAll("g")
.data(densityContourTernaryPlot.ticks())
.join("g")
.attr("class", "axis-ticks");
axisTicksGroups.call(ticks);
const triangleBounds = chart
.append("path")
.attr("d", densityContourTernaryPlot.triangle())
.attr("stroke", "black")
.attr("fill", "none")
.attr("stroke-width", 2);
const dots = chart
.append("g")
.attr("stroke", "white")
.attr("class", "data")
.attr("clip-path", "url(#trianglePath)")
.selectAll("circle")
.data(ternaryData)
.join("circle")
.attr("r", 2)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y);
dots.append("title").text(
(d) => `${d.country}
Agriculture: ${d.agriculture}
Industry: ${d.industry}
Service: ${d.service}`
);
chart
.append("g")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("clip-path", "url(#trianglePath)")
.selectAll("path")
.data(contours)
.join("path")
.attr("stroke-width", (d, i) => (i % 5 ? 0.25 : 1))
.attr("d", d3.geoPath());
return svg.node();
}