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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more