import * as am5 from "@amcharts/amcharts5"; import * as am5percent from "@amcharts/amcharts5/percent"; 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 chart // https://docs.amcharts.com/v5/charts/percent-charts/sliced-chart/ const chart = root.container.children.push(am5percent.SlicedChart.new(root, { layout: root.verticalLayout // stacks the pyramid and the legend, so the legend goes below })); // Create series // https://docs.amcharts.com/v5/charts/percent-charts/sliced-chart/pyramid-series/ // each slice's area is its share of the total; the labels line up in a column beside the // pyramid, joined to their slices by ticks (alignLabels is on by default) const series = chart.series.push(am5percent.PyramidSeries.new(root, { orientation: "vertical", // the tip at the top, the base at the bottom valueField: "value", categoryField: "category" })); // Set data: slices go from the tip down, so the smallest value comes first // https://docs.amcharts.com/v5/charts/percent-charts/sliced-chart/#setting-data series.data.setAll([ { value: 3, category: "One" }, { value: 3, category: "Two" }, { value: 4, category: "Three" }, { value: 5, category: "Four" }, { value: 6, category: "Five" }, { value: 9, category: "Six" }, { value: 10, category: "Seven" } ]); // Create legend // https://docs.amcharts.com/v5/charts/percent-charts/legend-percent-series/ const legend = chart.children.push(am5.Legend.new(root, { centerX: am5.percent(50), // centered under the pyramid x: am5.percent(50), marginTop: 15, // some room between the pyramid and the legend... marginBottom: 15 // ...and below it })); legend.data.setAll(series.dataItems); // Accessibility: keyboard and screen reader support // https://docs.amcharts.com/v5/concepts/accessibility/ chart.setAll({ focusable: true, // Tab reaches the chart, and a screen reader names it ariaLabel: "Pyramid chart of seven values, One to Seven, from the smallest at the tip to the largest at the base. Tab to the levels, then move between them with the up and down arrow keys" }); // the slices: one Tab stop, the up and down arrow keys step through them series.slices.template.setAll({ focusable: true, focusableGroup: "slices", ariaLabel: "{category}: {value}, {valuePercentTotal.formatNumber('0.0')}%" }); // the legend: one Tab stop, after the slices legend.itemContainers.template.setAll({ focusableGroup: "legend", tabindexOrder: 10 // focus runs from low to high (0 if not set) }); // Make stuff animate on load // https://docs.amcharts.com/v5/concepts/animations/#forcing-appearance-animation series.appear(); chart.appear(1000, 100);