import * as am5 from "@amcharts/amcharts5"; import am5themes_Animated from "@amcharts/amcharts5/themes/Animated"; import am5themes_ResponsiveMax from "@amcharts/amcharts5/themes/ResponsiveMax"; import * as am5plugins_json from "@amcharts/amcharts5/plugins/json"; // 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 ]); // Describe the chart as a JSON config // https://docs.amcharts.com/v5/concepts/serializing/ // refs are named parts, used as "#name"; a block can use the ones before it const config = { refs: [{ data: [ // the rows for the pie, used below as "#data" { country: "France", sales: 100000 }, { country: "Spain", sales: 160000 }, { country: "United Kingdom", sales: 80000 } ] }, { series: { // the series, made here and added to the chart below as "#series" type: "PieSeries", // the class to create, by its name settings: { // what PieSeries.new(root, { ... }) would take name: "Series", valueField: "sales", categoryField: "country", autoAppear: true, // the series animates in on load, as series.appear(1000) would appearDuration: 1000 }, properties: { // set on the series once it exists data: "#data", // the rows from the first ref, as series.data.setAll() would // the slices, for the keyboard: one Tab stop, the up and down arrow keys step through them slices: { properties: { template: { settings: { focusable: true, focusableGroup: "slices", hoverOnFocus: true, // a focused slice shows its tooltip ariaLabel: "{category}: {value}, {valuePercentTotal.formatNumber('0.0')}%" } } } } } } }], type: "PieChart", // the top level is the chart itself settings: { layout: "vertical", // root.verticalLayout: the pie above the legend // Accessibility: Tab reaches the chart, and a screen reader names it // https://docs.amcharts.com/v5/concepts/accessibility/ focusable: true, ariaLabel: "Pie chart of sales in France, Spain and the United Kingdom. Tab to the slices, then move between them with the up and down arrow keys", // Animate on load: what appear() does in code autoAppear: true, appearDuration: 1000, // for one second... appearDelay: 100 // ...starting after 100 ms }, properties: { series: ["#series"] // pushes the series from the refs into chart.series }, children: [{ // added to chart.children after the pie, so the legend is below it type: "Legend", settings: { centerX: { type: "Percent", value: 50 }, // am5.percent(50): the legend's middle... x: { type: "Percent", value: 50 }, // ...at the middle of the chart: centered layout: "horizontal" // all items in one row, not the default wrapping grid }, properties: { data: "#series.dataItems", // one legend item per slice, as legend.data.setAll() would // the legend, for the keyboard: one Tab stop, after the slices itemContainers: { properties: { template: { settings: { focusableGroup: "legend", tabindexOrder: 10 // focus runs from low to high (0 if not set) } } } } } }] }; // Build the chart from the config; parse() returns a promise of the chart // https://docs.amcharts.com/v5/concepts/serializing/ const parser = am5plugins_json.JsonParser.new(root); parser.parse(config, { parent: root.container // the chart goes into the root's container, as children.push() would });