Public
Edited
Aug 6, 2021
3 stars
Insert cell
Insert cell
Insert cell
labella = require("labella@1.1")
Insert cell
Insert cell
Insert cell
Insert cell
data = [
{ l: "Apples", x: 0 },
{ l: "Bananas", x: 100 },
{ l: "Grapefruit", x: 200 },
{ l: "Oranges", x: 300 },
{ l: "Pomegranates", x: 400 }
]
Insert cell
Insert cell
Insert cell
{
const r = new Renderer(400, 150, "canvas")
.textSize(textSize)
.textAlign("middle")
.textBaseline("top")
.stroke();

// Use an ascender and descender to measure text height.
const tHeight = r.textMetrics("pf").height;

// Get labella to calculate non-overlapping positions.
const nodes = data.map(
(d) => new labella.Node(d.x, r.textMetrics(d.l).width, d.l)
);
const force = new labella.Force({ maxPos: 400, nodeSpacing: 10 })
.nodes(nodes)
.compute();

// Draw each label at the newly calculated position.
force.nodes().forEach((n) => {
r.text(n.data, n.currentPos, n.layerIndex * tHeight);
});
return r.render();
}
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