import * as am5 from "@amcharts/amcharts5"; import * as am5hierarchy from "@amcharts/amcharts5/hierarchy"; import am5themes_Animated from "@amcharts/amcharts5/themes/Animated"; import am5themes_ResponsiveMax from "@amcharts/amcharts5/themes/ResponsiveMax"; // Create root element // https://docs.amcharts.com/v5/getting-started/#root-element const root = am5.Root.new("chartdiv"); // Set themes // https://docs.amcharts.com/v5/concepts/themes/ root.setThemes([ am5themes_Animated.new(root), am5themes_ResponsiveMax.new(root) // fits the chart to small screens ]); // Create series // https://docs.amcharts.com/v5/charts/hierarchy/force-directed/ // a force simulation places the circles: linked ones pull together, the rest push apart; // a click on a circle opens or closes its children const series = root.container.children.push(am5hierarchy.ForceDirected.new(root, { minRadius: 25, // the node with the smallest total gets a 25 px radius... maxRadius: 55, // ...the biggest (the top node) 55 px, the rest in between nodePadding: 10, // 10 px of room around each circle, so they don't touch valueField: "value", categoryField: "name", childDataField: "children" })); // Set data // https://docs.amcharts.com/v5/charts/hierarchy/#setting-data // only the leaves have a value: each parent's total is the sum of its children series.data.setAll([{ name: "Music", children: [ { name: "Rock", children: [ { name: "Classic Rock", value: 64 }, { name: "Punk", value: 28 }, { name: "Metal", children: [ { name: "Heavy Metal", value: 35 }, { name: "Thrash", value: 17 } ] } ] }, { name: "Electronic", children: [ { name: "House", value: 42 }, { name: "Techno", value: 31 }, { name: "Ambient", value: 19 } ] }, { name: "Jazz", children: [ { name: "Swing", value: 24 }, { name: "Bebop", value: 33 }, { name: "Fusion", children: [ { name: "Jazz Rock", value: 14 }, { name: "Latin Jazz", value: 21 } ] } ] } ] }]); // Select the root node // https://docs.amcharts.com/v5/charts/hierarchy/#pre-selected-branch series.set("selectedDataItem", series.dataItems[0]); // Accessibility: keyboard and screen reader support // https://docs.amcharts.com/v5/concepts/accessibility/ series.setAll({ focusable: true, // Tab reaches the chart, and a screen reader names it ariaLabel: "Force-directed tree of music: three genres, Rock, Electronic and Jazz, and their styles, each circle sized by its total. Tab to the circles, move between them with the up and down arrow keys, and press Enter to open or close a circle's children" }); // the genres and styles: one Tab stop, the up and down arrow keys step through them series.nodes.template.setAll({ focusable: true, focusableGroup: "nodes", hoverOnFocus: true, // the focused circle shows its tooltip ariaLabel: "{category}: {sum}" }); // Make stuff animate on load // https://docs.amcharts.com/v5/concepts/animations/#forcing-appearance-animation series.appear(1000, 100);