Published unlisted
Edited
Jul 3, 2020
Importers
Insert cell
Insert cell
Insert cell
points = [[100, 100], [400, 100], [700, 400], [100, 400]]
Insert cell
Insert cell
X = curve(points.map(d => d[0]))
Insert cell
Y = curve(points.map(d => d[1]))
Insert cell
{
const context = DOM.context2d(800, 500);

for (let tmax = -5; tmax < 30; tmax += .1) {
context.clearRect(0, 0, 800, 500);

// long spiral
context.beginPath();
for (let t = -5; t < tmax; t += .01) {
const alpha = 1 - t / 30;
context.lineTo(
alpha * X(t) + (1 - alpha) * 250,
alpha * Y(t) + (1 - alpha) * 250
);
}
context.lineWidth = .5;
context.stroke();

// main domain
context.beginPath();
for (let t = 0; t < 1; t += .001) {
const alpha = 1;
context.lineTo(
alpha * X(t) + (1 - alpha) * 250,
alpha * Y(t) + (1 - alpha) * 250
);
}

context.lineWidth = 2;
context.stroke();

// data points
context.fillStyle = "red";
for (const p of points) {
context.beginPath();
context.arc(...p, 3, 0, 7);
context.fill();
}

yield context.canvas;
}

return context.canvas;
}
Insert cell
Insert cell
Insert cell
culori = require("culori@0.11.2")
Insert cell
culoriClosed = array => {
const f = culori.interpolateSplineMonotone(d => d, "closed", 1)(array),
n = array.length - 1,
k = 1 + 1 / n;
return t => f(k * (t - Math.floor(t)));
}
Insert cell
Insert cell
Insert cell
function monotone(values, type = "default") {
let n = values.length - 1;
values = values.slice();
switch (type) {
case "default":
values.push(2 * values[n] - values[n - 1]);
values.unshift(2 * values[0] - values[1]);
return t => monotone(clamp(t, 0, 1));
case "closed":
values.unshift(values[n]);
values.push(values[1]);
values.push(values[2]);
n += 2;
const k = 1 - 1 / n;
return t => monotone(k * frac(t));
case "open":
throw new Error('open monotone spline not implemented yet');
}

function monotone(t) {
const i = Math.min(n - 1, Math.floor(t * n)),
y_im1 = values[i],
y_i = values[i + 1],
y_ip1 = values[i + 2],
y_ip2 = values[i + 3],
d = t * n - i,
s_im1 = n * (y_i - y_im1),
s_i = n * (y_ip1 - y_i),
s_ip1 = n * (y_ip2 - y_ip1),
yp_i =
(sign(s_im1) + sign(s_i)) *
min(abs(s_im1), abs(s_i), 0.25 * n * abs(y_ip1 - y_im1)),
yp_ip1 =
(sign(s_i) + sign(s_ip1)) *
min(abs(s_i), abs(s_ip1), 0.25 * n * abs(y_ip2 - y_i));

return (
(((yp_i + yp_ip1 - 2 * s_i) * d + (3 * s_i - 2 * yp_i - yp_ip1)) * d +
yp_i) *
(d / n) +
y_i
);
}

function frac(t) {
return t - Math.floor(t);
}

function clamp(t, min, max) {
return Math.min(max, Math.max(min, t));
}
}
Insert cell
function monotoneClosed(values) {
return monotone(values, "closed");
}
Insert cell
import { abs, max, min, sign } from "@fil/math"
Insert cell
function cubic(values, type = "default") {
let n = values.length - 1;
values = values.slice();
switch (type) {
case "default":
values.push(2 * values[n] - values[n - 1]);
values.unshift(2 * values[0] - values[1]);
return t => cubic(clamp(t, 0, 1));
case "closed":
values.unshift(values[n]);
values.push(values[1]);
values.push(values[2]);
n += 2;
const k = 1 - 1 / n;
return t => cubic(k * frac(t));
case "open":
throw new Error('open monotone spline not implemented yet');
}

function cubic(t) {
const i = Math.min(n - 1, Math.floor(t * n)),
v0 = values[i],
v1 = values[i + 1],
v2 = values[i + 2],
v3 = values[i + 3],
d = t * n - i,
s20 = v2 - v0,
s31 = v3 - v1,
s21 = (v2 - v1) * 2;
return (
(d / 2) *
(((s20 + s31 - 2 * s21) * d + (3 * s21 - 2 * s20 - s31)) * d + s20) +
v1
);
}

function frac(t) {
return t - Math.floor(t);
}

function clamp(t, min, max) {
return Math.min(max, Math.max(min, t));
}
}
Insert cell
function cubicClosed(values) {
return cubic(values, "closed");
}
Insert cell
data = [3, 2.8, 2.5, 1, 0.95, 0.8, 0.5, 0.1, 0.05]
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell
d3 = require("d3-interpolate@1")
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