drawTernaryPlot = (someTernaryPlot, data) => {
const node = DOM.svg(width, height);
const svg = d3.select(node);
const radius = someTernaryPlot.radius();
const chart = svg
.append("g")
.attr("transform", `translate(${width / 2} ${height / 2 + yOffset})`)
.attr("font-family", "sans-serif")
.attr("id", "chart")
.on("mousemove", handleMouseMove);
const defs = chart.append("defs");
const clipPath = defs
.append("clipPath")
.attr("id", "trianglePath")
.append("path")
.attr("d", someTernaryPlot.triangle());
const axisLabelsGroups = chart
.append("g")
.attr("class", "axis-labels")
.append("g")
.attr("class", "labels")
.attr("font-size", 16);
axisLabelsGroups.call(axisLabels, someTernaryPlot.axisLabels());
const gridLinesPaths = someTernaryPlot
.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(someTernaryPlot.ticks())
.join("g")
.attr("class", "axis-ticks")
.call(ticks);
const ternaryData = data.map((d) => {
const [x, y] = someTernaryPlot(d);
return { x, y, ...d };
});
// data
const dots = chart
.append("g")
.attr("class", "data")
.attr("clip-path", "url(#trianglePath)")
.selectAll("circle")
.data(ternaryData)
.join("circle")
.attr("r", 4)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("fill", "#444")
.attr("stroke", "#ddd");
dots.append("title").text(
(d) => `${d.country}
Agriculture: ${d.agriculture}
Industry: ${d.industry}
Service: ${d.service}`
);
// initial triangle
const trianglePath = chart
.append("path")
.attr("d", someTernaryPlot.triangle())
.attr("fill", "transparent")
.attr("stroke", "black")
.attr("title", "Initial untransformed triangle");
// .attr("stroke-width", 2);
// const zoom = d3.zoom().scaleExtent([1, 100]).on("zoom", zoomed);
// const [tx, ty] = someTernaryPlot.translate();
// const k = someTernaryPlot.scale();
// // We need to sync d3-zoom with the tranform of the partial domains
// const initialTransform = d3.zoomIdentity
// .translate(tx * radius, ty * radius)
// .scale(k);
// chart.call(zoom).call(zoom.transform, initialTransform);
// function zoomed({ transform }) {
// const { x, y, k } = transform;
// const tx = x / radius,
// ty = y / radius;
// // apply transform
// someTernaryPlot.translate([tx, ty]);
// someTernaryPlot.scale(k);
// const zoomedDomains = someTernaryPlot.domainsFromVertices();
// someTernaryPlot.setDomains(zoomedDomains);
// // update data
// dots
// .attr("cx", (d) => someTernaryPlot(d)[0])
// .attr("cy", (d) => someTernaryPlot(d)[1]);
// // update gridlines and ticks
// const gridLinesPaths = someTernaryPlot
// .gridLines()
// .map((axisGrid) => axisGrid.map(d3.line()).join(" "));
// gridGroup.call(grid, gridLinesPaths);
// axisTicksGroups.data(someTernaryPlot.ticks()).call(ticks, (d) => d);
// }
function handleMouseMove(d) {
const xy = d3.pointer(d);
const inverse = someTernaryPlot.invert(xy);
node.dispatchEvent(new CustomEvent("input"), { bubbles: true });
node.value = inverse;
}
node.value = 0;
return svg.node();
}