2012-10-29 40 views
0

我目前正试图自定义一个例子,如... http://bl.ocks.org/1377729使用D3.json函数来获取json数据。使用D3.json函数导入json数据

此刻,我一直在努力做到这一点:

 d3.json("http:", function(json) { 

    var nodes = []; 
    var labelAnchors = []; 

在这里,我试图导入的功能,但它没有做任何事情

 for(var i = 0; i < 30; i++) { 


      var node = json; 
      nodes.push(node); 
      labelAnchors.push({ 
       node : node 
      }); 
      labelAnchors.push({ 
       node : node 
      }); 
     }; 

我也试图把函数进入.text函数

 anchorNode.append("svg:text") 
      .text(function(d, i) {return i % 2 == 0 ? "" : d.node.json }) 
      .style("fill", "#555") 
      .style("font-family", "Arial") 
      .style("font-size", 12); 

有谁能告诉我哪里出错了请!

+0

您的json网址未显示完成。 https://github.com/mbostock/d3/wiki/Requests#wiki-d3_json – nautat

+0

嗨@nautat我故意错过了那部分了..是否还有其他什么是错的? – user1701622

+0

你能分享一下你的json数据结构吗?从您引用的示例中,可以看到节点和labelAnchors是对象的数组(长度为30)。它们的节点对象如下所示:{label:“node0”}并形成节点数组。形成labelAnchors数组的对象如下所示:{node:{label:“node0”}}。 – nautat

回答

0

首先通过逐步穿过你的json数组来构建节点数组和labelAnchors数组。

for (var i = 0; i < json.length; i++) { 
    var node = json[i]; 
    nodes.push(node); 
    labelAnchors.push({ 
     node : node 
    }); 
    labelAnchors.push({ 
     node : node 
    }); 
    }; 

的例子依赖于拥有了一个名为label主要对象,而在你的情况下密钥被称为Name。因此,您需要将示例代码调整为如下形式:

anchorNode.append("svg:text") 
    .text(function(d, i) { 
     return i % 2 == 0 ? "" : d.node.Name; 
    }) 
    .style("fill", "#555") 
    .style("font-family", "Arial") 
    .style("font-size", 12);