Sunburst chart with a legend including all nodes

This demo shows how we can include all nodes from all levels of a Sunburst chart into its legend.

Code

let legend = container.children.push(am5.Legend.new(root, {}));

function addLegendItems(items) {
  legend.data.pushAll(items);
  am5.array.each(items, function(item) {
    if (item.get("children")) {
      addLegendItems(item.get("children"));
    }
  });
}

addLegendItems(series.dataItems[0].get("children"));
var legend = container.children.push(am5.Legend.new(root, {}));

function addLegendItems(items) {
  legend.data.pushAll(items);
  am5.array.each(items, function(item) {
    if (item.get("children")) {
      addLegendItems(item.get("children"));
    }
  });
}

addLegendItems(series.dataItems[0].get("children"));

The above code iterates through the whole hierarchy and pushes all data items from all nodes into legend's data.

Demo

See the Pen
Sunburst Chart with a legend including all nodes
by amCharts team (@amcharts)
on CodePen.0

Posted in Uncategorized