与json D3js错误

时间:2013-10-02 20:24:13

标签: javascript jquery json d3.js

您好我正在尝试关注this example,我使用硬编码的json:

var json = {
        "nodes": 
        [
            {
                "name": "Gosha",
                "url": "www",
                "center": true,
                "group": 1
            },
            {
                "name": "Vlad",
                "url": "www",
                "center": false,
                "group": 1
            },
            {
                "name": "Carmel",
                "url": "www",
                "center": false,
                "group": 1
            }
        ],
        "links": 
        [
            {
                "source": "0",
                "target": "1",
                "weight": 0.5,
                "value": 1
            },
            {
                "source": "0",
                "target": "2",
                "weight": 0.6,
                "value": 1
            },
            {
                "source": "1",
                "target": "2",
                "weight": 0.8,
                "value": 1
            }
        ]
    }

这是我的js代码:

var svg = d3.select("#sig").append("svg")
        .attr("width", width)
        .attr("height", height);

    var force = d3.layout.force()
        .gravity(.05)
        .distance(100)
        .charge(-100)
        .size([width, height]);

    force
        .nodes(json.nodes)
        .links(json.links)
        .start(); //error on this line

我有这个错误: 未捕获的TypeError:无法调用未定义的方法'push'

我认为问题与json有关(当我从示例中放入json时它工作正常)。我试图验证我的json - 它是有效的。

有什么想法吗?

谢谢, 维拉德

1 个答案:

答案 0 :(得分:0)

           {
            "source": "0",
            "target": "1",
            "weight": 0.5,
            "value": 1
          }

 source and target should be number like this :

         {
            "source": 1,
            "target": 1,
            "weight": 0.5,
            "value": 1
          }