Public
Edited
Oct 24, 2023
4 forks
Importers
37 stars
Also listed in…
d3-color
d3-scale
Insert cell
Insert cell
interpolator = t => `hello ${t}`
Insert cell
hello = d3.scaleSequential(interpolator)
Insert cell
hello(0.5)
Insert cell
Insert cell
hello200 = d3.scaleSequential(interpolator).domain([200, 100])
Insert cell
hello200(170)
Insert cell
Insert cell
Insert cell
angryRainbow = d3.scaleSequential(t => d3.hsl(t * 360, 1, 0.5).toString())
Insert cell
angryRainbow(t0)
Insert cell
viewof t0 = ramp(angryRainbow)
Insert cell
Insert cell
d3.interpolateSinebow(t1)
Insert cell
viewof t1 = ramp(d3.scaleSequential(d3.interpolateSinebow))
Insert cell
Insert cell
Insert cell
{
const scale = d3.scaleSequential(d3.interpolateSinebow); // same as t1
const interpolator = scale.interpolator(); // read its interpolator
const mirror = t => interpolator(1 - t); // creates a mirror image of the interpolator
if (use_mirror) {
scale.interpolator(mirror); // updates the scale’s interpolator
}
return ramp(scale);
}
Insert cell
Insert cell
Insert cell
d3.scaleSequential(t => 10 - 2.5 * t).range()
Insert cell
d3.scaleSequential(d3.interpolateCividis).range()
Insert cell
Insert cell
{
const scale = d3.scaleSequential(d3.interpolateCool);
const before = scale(0.5);
const range = scale.range();
scale.range(range); // uses the same extrema, but resets the interpolator
const after = scale(0.5);
return { before, after, range };
}
Insert cell
d3.scaleSequential(t => t * t).rangeRound([0, 100])(1 / 3)
Insert cell
Insert cell
ramp(d3.scaleSequential(["blue", "green"]))
Insert cell
Insert cell
Insert cell
magnitude = d3.scaleSequentialLog(d3.interpolatePuBuGn).domain([1e-8, 1e8])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
qu = d3.scaleSequentialQuantile().domain([100, 1, 13])
Insert cell
[qu(1), qu(2), qu(13), qu(20), qu(99.99), qu(100)]
Insert cell
Insert cell
Insert cell
[qu(0), qu(10000)]
Insert cell
Insert cell
ramp(d3.scaleSequentialQuantile(d3.interpolateRdYlBu).domain([0, 0.2, 0.5, 0.9]))
Insert cell
Insert cell
seq = d3.scaleSequentialQuantile(d3.interpolateRdYlBu)
.domain(Float32Array.from({ length: 1000 }, d3.randomNormal(0.5, 0.15)))
Insert cell
ramp(seq)
Insert cell
seq.quantiles(4)
Insert cell
Insert cell
Insert cell
// Adapted from "@mbostock/color-ramp"
function ramp(color, numscale, n = 512) {
const canvas = DOM.canvas(n, 1);
const context = canvas.getContext("2d"),
w = width + 28;
canvas.style.margin = "0 -14px";
canvas.style.width = `${w}px`;
canvas.style.height = "40px";
canvas.style.imageRendering = "pixelated";

// companion numerical scale, to define the axis.
if (numscale === undefined) numscale = d3.scaleLinear();
if (color.domain) numscale.domain(color.domain());
numscale.range([0, n]);
const t = color.ticks ? color.ticks(n) : d3.range(n).map(i => i / (n - 1));

for (let i = 0; i < t.length; ++i) {
context.fillStyle = color(t[i]);
context.fillRect((i * n) / t.length, 0, 100, 1);
}

d3.select(canvas).on("mousemove click", function(event) {
const t = numscale.invert((d3.pointer(event)[0] / w) * n);
canvas.value = t;
canvas.dispatchEvent(new CustomEvent("input"));
});
canvas.value = 0;

return canvas;
}
Insert cell
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