Published
Edited
Nov 5, 2018
3 stars
Insert cell
Insert cell
Insert cell
uncentered = html`<svg width="400px" height="400px">
<circle cx="200" cy="200" fill="#1f77b4" r="100"></circle>
<text fill="white" x="200" y="200" font-size="120">A</text>
</svg>`
Insert cell
Insert cell
centeredWithJs = html`<svg width="400px" height="400px">
<circle cx="200" cy="200" fill="#1f77b4" r="100"></circle>
<text fill="white" x="200" y="200" font-size="120">A</text>
</svg>`
Insert cell
Insert cell
// Function that takes in an SVG element containing a circle and a text element. Centers the text
// element inside the circle using the text element's bounding box.
function center(example) {
// Get current x and y values.
let letter = d3.select(example).select('text');
let x = Number(letter.attr('x'));
let y = Number(letter.attr('y'));
// Get bounding box and compute the center point.
let bb = letter.node().getBBox();
console.log('Bounding box:', bb);
let centerX = bb.width / 2;
let centerY = bb.height / 4;
console.log(`Center point: (${centerX}, ${centerY})`);
// Adjust x and y.
letter.attr('x', x - centerX);
letter.attr('y', y + centerY);
return [(x - centerX), (y + centerY)];
}
Insert cell
// Call center with the example above.
center(centeredWithJs)
Insert cell
Insert cell
centeredWithProps = html`<svg width="400px" height="400px">
<g>
<circle cx="200" cy="200" fill="#1f77b4" r="100"></circle>
<text fill="white" x="200" y="200" font-size="120" text-anchor="middle" alignment-baseline="central">A</text>
</g>
</svg>`
Insert cell
Insert cell
Insert cell
centeredManually = html`<svg width="400px" height="400px">
<circle cx="200" cy="200" fill="#1f77b4" r="100"></circle>
<text fill="white" x="155" y="238" font-size="120">A</text>
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
firaCircle = html`<svg width="400px" height="400px">
<circle cx="200" cy="200" fill="#1f77b4" r="100"></circle>
<text id="firaLetter" fill="white" x="200" y="200" font-size="120" font-family="'Fira Mono', monospace">A</text>
</svg>`
Insert cell
center(firaCircle)
Insert cell
Insert cell
Insert cell
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