从D3.js中的对象加载数据

时间:2016-12-09 02:33:16

标签: d3.js

我正在查看D3.js版本4的example,我需要从同一个脚本加载动态数据。 d3.csv()从数组或对象加载数据的模拟函数是什么?我不需要XMLHttpRequest,但我需要d3.csv()的相同行为。

小提琴在https://jsfiddle.net/i5ar/mbcu05z8/

1 个答案:

答案 0 :(得分:2)

以下是完整的小提琴:https://jsfiddle.net/mbcu05z8/27/

我将数组存储在变量中,就像你一样:

var myArr= [{"id":"AL","Under 5":310504,"5 to 13":552339,"14 to 17":259034,"total":1121877},{"id":"AK","Under 5":52083,"5 to 13":85640,"14 to 17":42153,"total":179876},{"id":"AZ","Under 5":515910,"5 to 13":828669,"14 to 17":362642,"total":1707221}];

然后我将d3.csv更改为名为draw()的自定义函数并调用它: - )

draw(myArr);
function draw(states) {
  var stateById = d3.map();
  states.forEach(function(d) {
    stateById.set(d.id, d);
  });

  setTimeout(()=>{  
    dispatch.call("load", window, stateById);
    dispatch.call("statechange", window, stateById.get("AL"));
  },20);
};