Public
Edited
Oct 23, 2023
8 forks
31 stars
Insert cell
Insert cell
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
Insert cell
Insert cell
setDomain = {
color.domain("A list of ten words to explore this scale’s domain".split(/ /));
return color.domain();
}
Insert cell
Insert cell
d3.schemeCategory10
Insert cell
Insert cell
(setDomain, color.range())
Insert cell
Insert cell
werner = {
replay;
return d3.scaleOrdinal([`#383867`, `#584c77`, `#33431e`, `#a36629`, `#92462f`, `#b63e36`, `#b74a70`, `#946943`]);
}
Insert cell
Insert cell
run = {
const data = "While the original colors have aged, they have been preserved here as in the original in an effort to faithfully reproduce the guide.".split(
/ /
);
var i = 0;

while (i++ < data.length) {
const str = data.slice(0, i);
yield html`<p style="font-family:sans-serif;color:white;font-weight:bold;line-height:1.8em;">${str
.map(
word =>
`<span style="background:${werner(word)};padding:5px;">
${word}
</span>`
)
.join("")}`;
werner;
await Promises.delay(300);
}
}
Insert cell
Insert cell
Insert cell
(run, werner.domain())
Insert cell
Insert cell
taint = d3.scaleOrdinal(["earth", "wind", "fire"], ["green", "red", "blue"])
Insert cell
invert = d3.scaleOrdinal(taint.range(), taint.domain())
Insert cell
invert("green")
Insert cell
Insert cell
unknown = d3.scaleOrdinal([0, 1, 2], ["A", "B", "C"])
Insert cell
unknown(3)
Insert cell
no_unknown = d3.scaleOrdinal([0, 1, 2], ["A", "B", "C"])
.unknown("…")
Insert cell
no_unknown(3)
Insert cell
Insert cell
unknown.unknown()
Insert cell
no_unknown.unknown()
Insert cell
Insert cell
source = d3.scaleOrdinal()
.domain([0, 1])
.range(["red", "blue", "green"])
Insert cell
{
const a2 = source.copy()(2);
const a3 = source.copy()(3); // without the copy, a3 would go back to "red"
return { a2, a3 };
}
Insert cell
Insert cell
{
const scale = d3.scaleOrdinal();

scale.range(["first", "second"]);
const before = scale("A") + ", " + scale("B");

scale.range(["1st", "2nd"]);
const after = scale("A") + ", " + scale("B");

return { before, after };
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const symbols = d3.scaleOrdinal().range(d3.symbols),
color = d3.scaleOrdinal(d3.schemePaired),
height = 200,
symbol = d3.symbol().size(200),
data = d3.range(500).map(i => ({
x: width * Math.random(),
y: height * Math.random(),
s: Math.floor(9 * Math.random()),
c: Math.floor(20 * Math.random())
})),
paths = data.map(
d =>
`<path transform="translate(${d.x},${d.y})" d="${symbol.type(
symbols(d.s)
)()}" fill="${color(d.c)}" />`
);

return svg`<svg width=${width} height=${height}>${paths.join("")}</svg>`;
}
Insert cell
Insert cell
atoms = new Map([["C", 12], ["H", 1], ["O", 16]])
Insert cell
colors = new Map([["C", "black"], ["H", "white"], ["O", "blue"]])
Insert cell
m = d3.scaleOrdinal()
.domain(atoms.keys())
.range(colors.values())
Insert cell
m("O")
Insert cell
Insert cell
new Map(m.domain().map(d => [d, m(d)]))
Insert cell
Insert cell
d3 = require("d3-scale@3", "d3-scale-chromatic@2", "d3-shape@2", "d3-array@2")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more