Public
Edited
Jan 4, 2023
1 fork
3 stars
Insert cell
Insert cell
legend_pic = {
let svg = d3
.create("svg")
.attr("width", 500)
.attr("height", 300)
.style("border", "solid 1px black");

svg
.append("g")
.selectAll("rect")
.data(d3.range(40, 480, 40))
.join("rect")
.attr("x", (i) => i)
.attr("y", (i) => 290 - i / 2)
.attr("width", 30)
.attr("height", (i) => i / 2)
.attr("fill", (i) => d3.interpolateViridis(i / 480));

svg
.append("g")
.attr("transform", "translate(20, 20)")
.append(() =>
Legend(d3.scaleSequential([0, 100], d3.interpolateViridis), {
title: "Legend"
})
);

return svg.node();
}
Insert cell
swatch_pic = {
let div = d3
.create("div")
.style("width", 500)
.style("height", 300)
.style("position", "static");

let svg = div
.append("svg")
.attr("width", 500)
.attr("height", 300)
.style("border", "solid 1px black");

svg
.selectAll("rect")
.data(d3.range(50, 500, 150))
.join("rect")
.attr("x", (i) => i)
.attr("y", (i) => 290 - (2 * i) / 3)
.attr("width", 100)
.attr("height", (i) => (2 * i) / 3)
.attr("fill", (i, j) => d3.schemeCategory10[j]);

div
.append("div")
.style("position", "absolute")
.style("top", "20px")
.style("left", "20px")
.append(() =>
Swatches(
d3.scaleOrdinal(
["blueberries", "oranges", "apples"],
d3.schemeCategory10
)
)
);

return div.node();
}
Insert cell
import { Legend, Swatches } from "@d3/color-legend"
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