我知道JSON无法像其他许多问题一样在Chrome中本地加载。我正在尝试重新创建:https://romsson.github.io/dragit/example/nations.html

我试图实现对json进行硬编码的解决方案。

工作代码:

d3.json("https://rawgit.com/romsson/dragit/master/data/nations.json", function(nations) {


....

我尝试在本地加载nations.json

var nations = [{"name":"Angola","region":"Sub-Saharan Africa","income":[[2006,12127071],[2007,12420476],[2008,12707546]],
"lifeExpectancy":[[2006,46.02],[2007,46.54],[2008,47.06]]},
{"name":"Benin","region":"Sub-Saharan Africa","income":[[2006,7862944],[2007,8078314],[2008,8294941]],
"lifeExpectancy":[[2006,60.6],[2007,61.03],[2008,61.47]]}]

d3.json(nations, function(nations) {

....


我得到错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

最佳答案

d3.json用于从url(不是页面中存在的对象)加载数据。因此,如果页面中已有对象,则将其传递给所需的函数。



function draw(nationsObj){
    // A bisector since many nation's data is sparsely-defined.
       var bisect = d3.bisector(function(d) { return d[0]; });

        .....contents
}

draw(nations);

08-25 10:01