Public
Edited
Feb 20
3 forks
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
qfrBarycentric = barycentric()
.a(d => d.quartz)
.b(d => d.feldspar)
.c(d => d.lithic)
Insert cell
qfrTernaryPlot = ternaryPlot(qfrBarycentric)
.radius(radius)
.labels(["Quartz", "Feldspar", "Lithic fragments"])
Insert cell
drawTernaryPlot = (someTernaryPlot) => {
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("height", 300)
.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 axisLabelsGroup = chart
.append("g")
.attr("class", "axis-labels")
.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");

axisTicksGroups.call(ticks);

const trianglePath = chart
.append("path")
.attr("d", someTernaryPlot.triangle())
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2);

chart
.append("g")
.attr("class", "divisions")
.selectAll("path")
.data(ternaryDivisions.map((d) => d.coords))
.join("path")
.attr("d", closedLine)
.attr("fill", "none")
.attr("fill-opacity", "0.6")
.attr("stroke", "black")
.attr("stroke-width", 2)
.append("title")
.text((d) => d.texture);

chart
.append("g")
.selectAll("text")
.data(ternaryDivisions)
.join("text")
.attr(
"transform",
(d) => `translate(${someTernaryPlot(d.center)})rotate(${d.rotation})`
)
.attr("text-anchor", "middle")
.attr("stroke", "ghostwhite")
.attr("stroke-width", 4)
.attr("paint-order", "stroke")
.attr("alignment-baseline", "middle")
.attr("font-size", 13)
.text((d) => d.texture);

function handleMouseMove(d) {
const xy = d3.pointer(d);
const inverse = qfrTernaryPlot.invert(xy);

node.dispatchEvent(new CustomEvent("input"), { bubbles: true });
node.value = inverse;
}

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
format = d3.format(".2%")
Insert cell
Insert cell
height = 640
Insert cell
radius = Math.min(width, height) / 2
Insert cell
// to keep the triangle centered
yOffset = radius / 4
Insert cell
Insert cell
import { valuesTable, grid, axisLabels, ticks } from "@julesblm/d3-ternary"
Insert cell
ternaryPlot = d3Ternary.ternaryPlot
Insert cell
barycentric = d3Ternary.barycentric
Insert cell
d3Ternary = import("d3-ternary")
Insert cell
d3 = require('d3@v6')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more