3

enter image description here

I have a pie chart with legends made using Am4 charts but I need to display only the top 5 legend values. I wrote the following code:

var chart = am4core.create("chartdiv2", am4charts.PieChart);
        chart.hiddenState.properties.opacity = 0; // this creates initial fade-in

        // Add data
        chart.data = [{
            "country": "Lithuania",
            "litres": 501.9
        }, {
            "country": "Czech Republic",
            "litres": 301.9
        }, {
            "country": "Ireland",
            "litres": 201.1
        }, {
            "country": "Germany",
            "litres": 165.8
        }, {
            "country": "Belgium",
            "litres": 60
        }, {
            "country": "The Netherlands",
            "litres": 50
        }];

        // Set inner radius
        chart.innerRadius = am4core.percent(50);

        //Add label
        var label = chart.seriesContainer.createChild(am4core.Label);
        label.text = "200";
        label.horizontalCenter = "middle";
        label.verticalCenter = "middle";
//        label.fontSize = 50;

        // Add and configure Series
        var pieSeries = chart.series.push(new am4charts.PieSeries());
        pieSeries.dataFields.value = "litres";
        pieSeries.dataFields.category = "country";
        pieSeries.slices.template.stroke = am4core.color("#fff");
        pieSeries.slices.template.strokeWidth = 2;
        pieSeries.slices.template.strokeOpacity = 1;
        pieSeries.ticks.template.disabled = true;
        pieSeries.labels.template.disabled = true;

        // This creates initial animation
        pieSeries.hiddenState.properties.opacity = 1;
        pieSeries.hiddenState.properties.endAngle = -90;
        pieSeries.hiddenState.properties.startAngle = -90;

        pieSeries.legendSettings.labelText = '{category}';
        pieSeries.legendSettings.valueText = null;
        pieSeries.labels.template.text = "{category}: {value}";
        pieSeries.slices.template.tooltipText = "{category}: {value}";

        chart.legend = new am4charts.Legend();
        chart.legend.fontSize = 10;
        chart.legend.markers.template.width = 10;
        chart.legend.markers.template.height = 10;

I need only Lithuania, Czech Republic, Ireland, Germany and Belgium to show up in the legend but currently, they all show up. In the pic, I have highlighted the legend values that should not show up. I tried using the legend.data array but it always returns an empty array.

How do I go about solving this?

Thanatos
  • 282
  • 5
  • 18

0 Answers0