AmCharts 4 处理空数据

AmCharts 4 handling null data

我正在使用动态加载数据的 AmCharts。有时,响应中可能会发送空数据。我需要处理 AmCharts4 中的空数据。 In this link I see the example for Amcharts3 handling null data

var chart = am4core.create("reason_for_failure", am4charts.PieChart);
        // 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;

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

        chart.data = [];

看起来他提供的示例代码 more/less 直接来自我们的一个饼图演示,例如Pie Chart With Legend, so I mixed and matched that with the solution in the mirror GitHub issue(底部演示link)。

同样,beforevalidated event 非常适合处理 chart.data 被分配一个空数组的情况,例如

chart.events.on("beforevalidated", function(event) {
  // check if there's data
  console.log(event.target.data.length);
  if (event.target.data.length == 0) {
    // handle null data here
  }
});

应该是beforevalidated事件,因为如果没有数据,beforedatavalidated不会触发。

演示:

https://codepen.io/team/amcharts/pen/88d11b5385a2669319c1a0fcdaa1e199/