Published unlisted
Edited
Dec 27, 2021
2 stars
Insert cell
Insert cell
Insert cell
Plot.plot({
[facet ? "facet" : "_disabled"]: {
data,
x: d => (d.x1 > d.x2) ? "left" : "right",
y: d => (d.y1 > d.y2) ? "down" : "up"
},
[facet ? "fy" : "_fy"]: { domain: ["up", "down"] },
marks: [
arrow(data, {
x1: "x1", x2: "x2", y1: "y1", y2: "y2", // mark coordinates
stroke: "value", // mark style
strokeWidth: 1.5, // global style
angle, arrowheadangle, arrowheadlen // plugin config
})
]})
Insert cell
Insert cell
Insert cell
Insert cell
mutable debug = ({})
Insert cell
function arrow() {
class Arrow extends Plot.Link {
constructor(_, opts) {
// initialize Plot.link normally
const R = super(...arguments);
// save the options, we'll use some of them
this.opts = opts; // 💡 shouldn't this be the general rule? we'd save this constructor step
}
render() {
const opts = this.opts;
// Let Plot.link create paths from data
const node = super.render(...arguments);
const lines = d3.select(node).attr("fill", "none").selectAll("path");

// get their properties and replace the paths' d:
lines.select(function() {
const [x1, y1, x2, y2] = this.getAttribute("d").split(/[ML,]/g).slice(1).map(Number);
this.setAttribute("d", getPath(x1, y1, x2, y2, opts));
});
return node;
}
}

return new Arrow(...arguments);
// TODO? write this as a relative tension rather than angle?
// (see https://observablehq.com/@mbostock/inequality-in-american-cities#arc)
function getPath(x1, y1, x2, y2, {angle = Math.PI/4, arrowheadlen = 10, arrowheadangle = 0.4}) {
const CLOCKWISE = +(+angle < 0);
const ANGLE = Math.abs(+angle);
const D = 1 / (2 * Math.tan(Math.max(1e-7, ANGLE / 2)));
// get the chord length between points
const dx = x2 - x1, dy = y2 - y1;
const h = Math.hypot(dy, dx);
// get the distance at which chord of height h subtends {angle} radians
const d = h * D;
// get the radius {r} of the circumscribed circle
const r = Math.hypot(d, h / 2);
// arrow head
const l = Math.min(arrowheadlen, h * 0.3), a = arrowheadangle;
const tangentAngle = Math.atan2(dy, dx)
+ (CLOCKWISE ? 1 : -1) * ANGLE / 2 * (1 - (l / r / Math.PI));
const x3 = x2 - l * Math.cos(tangentAngle + a), y3 = y2 - l * Math.sin(tangentAngle + a);
const x4 = x2 - l * Math.cos(tangentAngle - a), y4 = y2 - l * Math.sin(tangentAngle - a);
// build path string
return r < 1e5
? `M ${x1} ${y1} A ${r} ${r} 0 0 ${CLOCKWISE} ${x2} ${y2} L ${x3} ${y3} M ${x2} ${y2} L ${x4} ${y4}`
: `M ${x1} ${y1} L ${x2} ${y2} L ${x3} ${y3} M ${x2} ${y2} L ${x4} ${y4}`
}
}
Insert cell
Insert cell
data = Array.from({length: 30}, () => ({ x1: random(), x2: random() + random() + random(), y1: random(), y2: random(), value: Math.random() }))
Insert cell
random = d3.randomNormal()
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more