Published
Edited
Mar 31, 2022
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
o = [0, 0]
Insert cell
basisA = [1, 0]
Insert cell
basisB = rotate([1, 0], angle)
Insert cell
lines = [
{ d: [basisA, multiply(basisB, d.b)], color: "#aaa" },
{ d: [multiply(basisA, d.a), multiply(basisB, d.c)], color: "#aaa" },
{ d: [o, basisA], color: "black" },
{ d: [o, multiply(basisB, d.c)], color: cColor },
{ d: [o, multiply(basisA, d.a)], color: aColor },
{ d: [o, multiply(basisB, d.b)], color: bColor }
]
Insert cell
scale = d3.scaleLinear([0, 1], [0, 100])
Insert cell
margin = 150
Insert cell
extent = {
const [a, b] = d3.extent(lines.flatMap(d => d.d.flatMap(d => d.map(scale))));
return [a - margin, b + margin];
}
Insert cell
viewBox = `${extent[0]} ${extent[0]}
${extent[1] - extent[0]} ${extent[1] - extent[0]}`
Insert cell
diagonal = Math.sqrt((extent[1] - extent[0]) ** 2 * 2)
Insert cell
aColor = "rgb(223, 0, 48)"
Insert cell
bColor = "rgb(100, 149, 237)"
Insert cell
cColor = "rgb(40, 174, 123)"
Insert cell
axis = basis =>
svg`<g>
<path
d="${line([multiply(basis, -diagonal), multiply(basis, diagonal)])}"
stroke="#ccc"
fill="none" />
${d3.range(...extent.map(d => Math.round(scale.invert(d)))).map(
d => svg`<text
text-anchor="middle"
fill="black"
font-weight="bold"
stroke="white"
stroke-width="0.7"
dy="0.31em"
transform="translate(${multiply(basis, d)
.map(scale)
.join(", ")})"
>
${d}
</text>`
)}
</g>`
Insert cell
labels = [
{ label: "1", color: "black", position: multiply(basisA, 1) },
{
label: "a",
color: aColor,
position: multiply(basisA, d.a)
},
{
label: d.type === "mult" ? "b" : "c ÷ a",
color: bColor,
position: multiply(basisB, d.b)
},
{
label: d.type === "mult" ? "a × b" : "c",
color: cColor,
position: multiply(basisB, d.c)
}
]
Insert cell
line = d3
.line()
.x(d => scale(d[0]))
.y(d => scale(d[1]))
Insert cell
// https://stackoverflow.com/a/2259502/120290
rotate = ([px, py], angle, [cx, cy] = [0, 0]) => {
const s = Math.sin(angle);
const c = Math.cos(angle);

// translate point back to origin
px -= cx;
py -= cy;

// rotate point
const xnew = px * c - py * s;
const ynew = px * s + py * c;

// translate point back
px = xnew + cx;
py = ynew + cy;
return [px, py];
}
Insert cell
multiply = ([px, py], coef) => [coef * px, coef * py]
Insert cell
Insert cell
import { radio } from "@jashkenas/inputs"
Insert cell
import { note } from "@tophtucker/scrapbook"
Insert cell
import { tweet } from "@mbostock/tweet"
Insert cell
d3 = require("d3@6")
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