Unlisted
Edited
Dec 18
Importers
1 star
Insert cell
Insert cell
htmlToImage = import("https://cdn.skypack.dev/html-to-image@1.11.1?min")
Insert cell
Insert cell
image(chart)
Insert cell
image = async (node) => {
const n = d3.select(node);
const pb = n.style("padding-bottom");
const bg = n.style("background-color");
if (node.nodeName === "FIGURE") {
n.style("padding-bottom", ".4em");
n.style("background-color", "white");
}
const im = await htmlToImage.toCanvas(node, { pixelRatio: 2 });
im.style = `width: ${im.width >> 1}px`;
if (pb) n.style("padding-bottom", pb); // restores the original styles.
if (bg) n.style("background-color", bg);
return im;
}
Insert cell
import {plotca} from "@observablehq/plot-raster"
Insert cell
plotca1 = plotca
Insert cell
image(plotca1)
Insert cell
import {winds} from "@observablehq/plot-mapping"
Insert cell
winds1 = winds
Insert cell
image(winds1)
Insert cell
image(d3.select("h1").node())
Insert cell
import { chart as sankey } from "@d3/sankey/2"
Insert cell
sankeychart = sankey
Insert cell
image(sankeychart)
Insert cell
sankeychart2 = sankeychart.cloneNode(true)
Insert cell
image2(sankeychart2)
Insert cell
image2 = async (node) => {
const n = d3.select(node);
const pb = n.style("padding-bottom");
const bg = n.style("background-color");
if (node.nodeName === "FIGURE") {
n.style("padding-bottom", ".4em");
n.style("background-color", "white");
}
const colorUrlMap = fixColorUrl(n);
const im = await htmlToImage.toCanvas(node, { pixelRatio: 2 });
im.style = `width: ${im.width >> 1}px`;
if (pb) n.style("padding-bottom", pb); // restores the original styles.
if (bg) n.style("background-color", bg);
for (const [d, kind, v] of colorUrlMap) d.style[kind] = v;
return im;

// Fixes attributes and styles such as stroke="url(https://worker.url#id)"
function fixColorUrl(n) {
const re = new RegExp(
`^url[(]['"]?${document.location.href.replaceAll(
/[|\\{}()[\]^$+*?.]/g,
"\\$&"
)}(#.*?)['"]?[)]$`
);
const colorUrlMap = [];
for (const d of node.querySelectorAll("*")) {
const styles = getComputedStyle(d);
for (const kind of ["stroke", "fill"]) {
const st = styles[kind];
if (st && re.test(st)) {
colorUrlMap.push([d, kind, d.style[kind]]);
d.style[kind] = st.replace(re, 'url("$1")');
}
}
}
return colorUrlMap;
}
}
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