A radial tree that refocuses on click
A 25-node radial tree: click a node to recentre it and fan its subtree over the circle.
Apache-2.0Runs on this page
Package: @awacloud/graphic-anim
A 25-node tree of three levels drawn radially in SVG. Click a node: the tree recentres on it and fans its subtree over the full circle, with a short animated transition. The line under the tree reports every click, every click on a node's detail badge (a link or an information), and every completed focus.
Uses @awacloud/graphic-anim (Apache-2.0). This example's code is Apache-2.0.
When to use it
A site map, an org chart, a topic or taxonomy browser: a tree too wide for an indented list, where the reader wants to jump from node to node and keep the neighbourhood in view.
Copy-paste steps
npm install @awacloud/fw @awacloud/graphic-anim- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/andnode_modules/@awacloud/graphic-anim/src/at/vendor/graphic-anim/(node serve.jsfrom the examples root does both; any static server works). - Copy
index.html,main.js,tree.json,look.jsonandstyle.cssnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link.
What it shows
createHypertree(svgRoot, { tree, radius, ... })builds the tree into an SVG you provide; itsviewBoxis0 0 800 800, twice the radius. The returned handle hasinstance(the tree, withfocus(id),focusSync(id)andfocusId) anddispose().tree.jsonis the data: nested nodes with anid, alabeland optionalchildren,action(a link) andinfo.ignoreWhileFocusing: truedrops a click that lands while a focus transition is running, instead of stacking a second one.labelDepth: 1labels only the focused node and its neighbours, so no two labels overlap whichever node has the focus.look.jsonis the page's own look, passed as options: a colour per branch (styled.palette), the label font, and the two badge icons (badgeGlyphs). The library ships neutral defaults.onNodeClick,onNodeDetailandonFocusare the callbacks. The library never navigates: the page decides what a detail click does.- Limits: the tree and the look are read with
fetchfrom the files next to the page, so open it through a server, not from the file system. The focus-cancelling andfocusSyncseams are not exercised from the page.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import { createHypertree } from '@awacloud/graphic-anim';
// The tree and the look are content-as-data, read next to the page (same origin, top-level await).
// Each URL is a literal, so a static build sees the two files the page reads.
const load = async (/** @type {URL} */ url) => (await fetch(url)).json();
const [tree, look] = await Promise.all([
load(new URL('./tree.json', import.meta.url)),
load(new URL('./look.json', import.meta.url)),
]);
/**
* Build the radial tree into the `<svg>` `mount` and report what happens to `readout`.
* @param {SVGElement} mount an SVG root whose viewBox is 0 0 800 800 (twice the radius)
* @param {HTMLElement} readout receives one line per click, detail click and completed focus
*/
export function mountHypertree(mount, readout) {
const say = (/** @type {string} */ text) => { readout.textContent = text; };
return createHypertree(mount, {
tree,
radius: 400,
// A click that lands while a focus transition is running is dropped, not stacked.
ignoreWhileFocusing: true,
// Label the focus and its neighbours only: no two labels overlap, whichever node is focused.
labelDepth: 1,
// One colour per branch, pill labels, dots shrinking with the distance from the focus.
styled: { palette: look.palette, fontFamily: look.fontFamily },
badgeGlyphs: look.badgeGlyphs,
onNodeClick: (/** @type {string} */ id) => say(`clicked: ${id}`),
// The library never navigates: the page decides what a link or an information badge does.
onNodeDetail: (/** @type {string} */ id, /** @type {string} */ kind, /** @type {unknown} */ value) =>
say(`detail: ${id} ${kind} — ${value}`),
onFocus: (/** @type {string} */ id, /** @type {{ previous: string|null, refocus: boolean }} */ info) => {
const how = info.refocus ? ' (already selected)' : info.previous ? ` (previous: ${info.previous})` : '';
say(`focused: ${id}${how || ' (initial focus)'}`);
},
});
}
/** Click the rendered node of an author id, the way a pointer would; false when there is none. */
export function clickNode(/** @type {Element} */ root, /** @type {string} */ id) {
const suffix = new RegExp(`-node-${id}-\\d+$`);
const node = [...root.querySelectorAll('[id]')].find((el) => suffix.test(el.getAttribute('id') ?? ''));
node?.dispatchEvent(new Event('click', { bubbles: true }));
return Boolean(node);
}
export const map = mountHypertree(
/** @type {SVGElement} */ (document.getElementById('mount')),
/** @type {HTMLElement} */ (document.getElementById('readout')),
);index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>graphic-anim · A radial tree that refocuses on click</title>
<link rel="stylesheet" href="../../_style/examples.css">
<link rel="stylesheet" href="./style.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/",
"@awacloud/graphic-anim": "/vendor/graphic-anim/main.js", "@awacloud/graphic-anim/": "/vendor/graphic-anim/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>A radial tree that refocuses on click</h1>
<p>Twenty-five nodes on three levels. Click a node: the tree recentres on it
and fans its subtree round the circle.</p>
<svg id="mount" viewBox="0 0 800 800" role="img" aria-label="A radial tree of three levels"></svg>
<p id="readout" class="readout" role="status">Click a node to focus it.</p>
</main>
</body>
</html>tree.json
{
"id": "root",
"label": "Root",
"children": [
{
"id": "branch-a",
"label": "Branch A",
"children": [
{ "id": "leaf-a1", "label": "Leaf A1" },
{
"id": "leaf-a2",
"label": "Leaf A2",
"info": "A leaf with information."
},
{ "id": "leaf-a3", "label": "Leaf A3" }
],
"action": "#branch-a",
"info": "The first branch, with three leaves."
},
{
"id": "branch-b",
"label": "Branch B",
"children": [
{ "id": "leaf-b1", "label": "Leaf B1" },
{ "id": "leaf-b2", "label": "Leaf B2" },
{ "id": "leaf-b3", "label": "Leaf B3" }
]
},
{
"id": "branch-c",
"label": "Branch C",
"children": [
{ "id": "leaf-c1", "label": "Leaf C1" },
{ "id": "leaf-c2", "label": "Leaf C2" },
{ "id": "leaf-c3", "label": "Leaf C3" }
],
"info": "A branch with information only."
},
{
"id": "branch-d",
"label": "Branch D",
"children": [
{ "id": "leaf-d1", "label": "Leaf D1" },
{ "id": "leaf-d2", "label": "Leaf D2" },
{ "id": "leaf-d3", "label": "Leaf D3" }
]
},
{
"id": "branch-e",
"label": "Branch E",
"children": [
{ "id": "leaf-e1", "label": "Leaf E1" },
{ "id": "leaf-e2", "label": "Leaf E2" },
{ "id": "leaf-e3", "label": "Leaf E3" }
],
"action": "#branch-e"
},
{
"id": "branch-f",
"label": "Branch F",
"children": [
{ "id": "leaf-f1", "label": "Leaf F1" },
{ "id": "leaf-f2", "label": "Leaf F2" },
{ "id": "leaf-f3", "label": "Leaf F3" }
]
}
]
}look.json
{
"palette": [
"#00838a",
"#e7501e",
"#fdc400",
"#5b6abf",
"#2e7d32",
"#ad1457"
],
"fontFamily": "'Open Sans', system-ui, sans-serif",
"badgeGlyphs": {
"action": {
"tag": "path",
"attrs": {
"d": "M16.469,8.924l-2.414,2.413c-0.156,0.156-0.408,0.156-0.564,0c-0.156-0.155-0.156-0.408,0-0.563l2.414-2.414c1.175-1.175,1.175-3.087,0-4.262c-0.57-0.569-1.326-0.883-2.132-0.883s-1.562,0.313-2.132,0.883L9.227,6.511c-1.175,1.175-1.175,3.087,0,4.263c0.288,0.288,0.624,0.511,0.997,0.662c0.204,0.083,0.303,0.315,0.22,0.52c-0.171,0.422-0.643,0.17-0.52,0.22c-0.473-0.191-0.898-0.474-1.262-0.838c-1.487-1.485-1.487-3.904,0-5.391l2.414-2.413c0.72-0.72,1.678-1.117,2.696-1.117s1.976,0.396,2.696,1.117C17.955,5.02,17.955,7.438,16.469,8.924 M10.076,7.825c-0.205-0.083-0.437,0.016-0.52,0.22c-0.083,0.205,0.016,0.437,0.22,0.52c0.374,0.151,0.709,0.374,0.997,0.662c1.176,1.176,1.176,3.088,0,4.263l-2.414,2.413c-0.569,0.569-1.326,0.883-2.131,0.883s-1.562-0.313-2.132-0.883c-1.175-1.175-1.175-3.087,0-4.262L6.51,9.227c0.156-0.155,0.156-0.408,0-0.564c-0.156-0.156-0.408-0.156-0.564,0l-2.414,2.414c-1.487,1.485-1.487,3.904,0,5.391c0.72,0.72,1.678,1.116,2.696,1.116s1.976-0.396,2.696-1.116l2.414-2.413c1.487-1.486,1.487-3.905,0-5.392C10.974,8.298,10.55,8.017,10.076,7.825"
}
},
"info": {
"tag": "path",
"attrs": {
"d": "M12.323,2.398c-0.741-0.312-1.523-0.472-2.319-0.472c-2.394,0-4.544,1.423-5.476,3.625C3.907,7.013,3.896,8.629,4.49,10.102c0.528,1.304,1.494,2.333,2.72,2.99L5.467,17.33c-0.113,0.273,0.018,0.59,0.292,0.703c0.068,0.027,0.137,0.041,0.206,0.041c0.211,0,0.412-0.127,0.498-0.334l1.74-4.23c0.583,0.186,1.18,0.309,1.795,0.309c2.394,0,4.544-1.424,5.478-3.629C16.755,7.173,15.342,3.68,12.323,2.398z M14.488,9.77c-0.769,1.807-2.529,2.975-4.49,2.975c-0.651,0-1.291-0.131-1.897-0.387c-0.002-0.004-0.002-0.004-0.002-0.004c-0.003,0-0.003,0-0.003,0s0,0,0,0c-1.195-0.508-2.121-1.452-2.607-2.656c-0.489-1.205-0.477-2.53,0.03-3.727c0.764-1.805,2.525-2.969,4.487-2.969c0.651,0,1.292,0.129,1.898,0.386C14.374,4.438,15.533,7.3,14.488,9.77z"
}
}
}
}style.css
/* SPDX-License-Identifier: Apache-2.0 */
#mount { display: block; width: min(100%, 35rem); height: auto; }
#mount text { user-select: none; }
#mount g[id^="node-"] { cursor: pointer; }
.readout { font-size: 0.9rem; }