amcharts4 Force Directed Tree 不动态更新

amcharts4 Force Directed Tree not dynamically updating

我正在尝试在 amcharts4 中制作力导向树的构造动画。这棵树画得很好,但只有在我按顺序添加所有节点之后,而不是像我想要的那样 1 乘 1。不确定我是否做错了什么,或者 amcharts4 FDT 是否不允许这种类型的图表更新。

这是my codepen

  // https://www.amcharts.com/docs/v4/chart-types/force-directed/
am4core.useTheme(am4themes_animated);

// Create chart
var amchart = am4core.create("chartdiv", am4plugins_forceDirected.ForceDirectedTree);
// Create series
var series = amchart.series.push(new am4plugins_forceDirected.ForceDirectedSeries())
// Set data

// Set up data fields
series.dataFields.value = "value";
// series.dataFields.name = "name";
series.dataFields.children = "children";
series.dataFields.id = "name";
series.dataFields.linkWith = "link";
series.fontSize = 9;
series.minRadius = 40;
series.maxRadius = 70;
series.nodes.template.circle.fillOpacity = 0.9;
series.nodes.template.circle.filters.push(new am4core.DropShadowFilter());

function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
            break;
        }
    }
}

var array = [ {'name': 'A', 'text': 'A', 'username': 'A', 'value': 0.5333333333333333, 'link': ['B', 'C']}, {'name': 'B', 'text': 'B', 'username': 'B', 'value': 0.0, 'link': []}, {'name': 'C', 'text': 'C', 'username': 'C', 'value': 0.0}];
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
  console.log(array[i], true);
  amchart.addData(array[i]);
  amchart.invalidateData();
  sleep(2000);
}

AmCharts 的操作都是异步的,因此您的 sleep 方法并不是最好的方法,因为重绘很可能会不断中断,直到它会导致最终图表输出。

更好的方法是使用图表的 datavalidated 事件并使用它来确定何时添加下一个节点。如果您想要延迟,请将其包装在 setTimeout 中:

amchart.events.on('datavalidated', gradualFill);  
function gradualFill() {
  if (array.length) {
    setTimeout(function() {
      amchart.addData(array.pop()); //or array.shift(), or however you want to order the nodes' visibility
    }, 1000)
  }
  else {
    amchart.events.off('datavalidated', gradualFill); //remove this event handler when you're done
  }
}

下面的演示:

// https://www.amcharts.com/docs/v4/chart-types/force-directed/
am4core.useTheme(am4themes_animated);

// Create chart
var amchart = am4core.create("chartdiv", am4plugins_forceDirected.ForceDirectedTree);
// Create series
var series = amchart.series.push(new am4plugins_forceDirected.ForceDirectedSeries())
// Set data

// Set up data fields
series.dataFields.value = "value";
// series.dataFields.name = "name";
series.dataFields.children = "children";
series.dataFields.id = "name";
series.dataFields.linkWith = "link";
series.fontSize = 9;
series.minRadius = 40;
series.maxRadius = 70;
series.nodes.template.circle.fillOpacity = 0.9;
series.nodes.template.circle.filters.push(new am4core.DropShadowFilter());

var array = [ {'name': 'A', 'text': 'A', 'username': 'A', 'value': 0.5333333333333333, 'link': ['B', 'C']}, {'name': 'B', 'text': 'B', 'username': 'B', 'value': 0.0, 'link': []}, {'name': 'C', 'text': 'C', 'username': 'C', 'value': 0.0}];

amchart.events.on('datavalidated', gradualFill);  
function gradualFill() {
  if (array.length) {
    setTimeout(function() {
      amchart.addData(array.pop()); //or shift, or however you want to order the nodes' visibility
    }, 1000)
  }
  else {
    amchart.events.off('datavalidated', gradualFill); //remove this event handler when you're done
  }
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

#chartdiv {
  width: 100%;
  height: 400px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js"></script>
<script src="//www.amcharts.com/lib/4/plugins/forceDirected.js"></script>
<div id="chartdiv" style="height:200px;"></div>