在饼图(Am4图表)中仅显示图例中的前5个值

时间:2019-04-07 13:33:02

标签: legend pie-chart amcharts legend-properties amcharts4

enter image description here

我有一个饼图,其中包含使用Am4图表制作的图例,但我只需要显示前5个图例值。我写了以下代码:

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;

我只需要立陶宛,捷克共和国,爱尔兰,德国和比利时出现在图例中,但目前它们都出现了。在图片中,我突出显示了不应显示的图例值。 我尝试使用legend.data数组,但它始终返回一个空数组。

我该如何解决这个问题?

0 个答案:

没有答案