Unlisted
Edited
Sep 28, 2023
Insert cell
Insert cell
M = import("https://esm.sh/gh/observablehq/plot/src/marks/text.js")
Insert cell
mono = M.monospaceWidth
Insert cell
normal = M.defaultWidth
Insert cell
words = "normal text user would hope to match with widths and MAYBE A FEW ALL-CAPS WORDS numbers too such as 1 2 23 123 1346 4444 9999 0.00 11111 aaaabbbb"
Insert cell
ratio("What will you do about this?", "mono")
Insert cell
ratio("What will you do about this?")
Insert cell
viewof actualWidth = {
const div = htl.html`<a>…`;
div.value = function (text, type = "default") {
div.innerHTML = `<span class="${type}">${text}</span>`;
const { width } = div.getBoundingClientRect();
return width;
};
return div;
}
Insert cell
ratio("0123456789")
Insert cell
ratio("abcdefghijklmnopqrstuvwxyz")
Insert cell
ratio("abcdefghijklmnopqrstuvwxyz".toUpperCase())
Insert cell
9.6 / 15.15 // prosposal for monospace: a character's base length would be 63.
Insert cell
ratio = (text, type) =>
(type === "mono" ? mono : normal)(text) / actualWidth(text, type)
Insert cell
<style>
.default { font-size: 11px; font-family: sans-serif; }
.mono { font-size: 11px; font-family: monospace;}
</style>
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