0

我试图弄清楚是否有一种方法可以在 D3.js 中加载数据时调用函数。我的代码在下面,我不确定我是否走在正确的轨道上,看起来很简单,但我无法让它工作

d3.json("Country_data.json", mac.call(Country_data));


function mac(e) {

//I  would like for this function to perform some operations.
//The data in the file Country_data is passed to this function
}

如果有人对我如何实现这一点有任何想法,我将不胜感激,谢谢。

4

1 回答 1

1

您的代码正在传递调用的结果mac(),它应该传递一个引用,mac就像这样......

d3.json("Country_data.json", mac);

function mac(error, countryData) {
  if (error) {
    // deal with error
  } else {
    // perform some operations on countryData
  }
}

或将回调声明为与 d3.json 调用内联的匿名函数:

d3.json("Country_data.json", function (error, countryData) {
  if (error) {
    // deal with error
  } else {
    // perform some operations on countryData
  }
});
于 2013-07-24T02:55:00.003 回答