Published
Edited
Mar 6, 2020
7 forks
Importers
62 stars
Insert cell
Insert cell
function ramp(color, n = 512) {
const canvas = DOM.canvas(n, 1);
const context = canvas.getContext("2d");
canvas.style.margin = "0 -14px";
canvas.style.width = "calc(100% + 28px)";
canvas.style.height = "40px";
canvas.style.imageRendering = "-moz-crisp-edges";
canvas.style.imageRendering = "pixelated";
for (let i = 0; i < n; ++i) {
context.fillStyle = color(i / (n - 1));
context.fillRect(i, 0, 1, 1);
}
return canvas;
}
Insert cell
ramp(t => `hsl(${t * 360},100%,50%)`) // The dreaded angry rainbow!
Insert cell
ramp(d3.interpolateRainbow) // A better cylical color scheme.
Insert cell
ramp(d3.interpolateViridis) // A nice sequential color scheme.
Insert cell
ramp(d3.interpolateViridis, 12) // With a discrete number of steps.
Insert cell
ramp(d3.interpolateRgb("red", "blue")) // Also works nicely with D3 interpolators.
Insert cell
ramp(d3.interpolateLab("red", "blue"))
Insert cell
ramp(d3.interpolateHclLong("red", "blue"))
Insert cell
d3 = require("d3-scale-chromatic@1", "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