javascript - D3js 与 json 错误

标签 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

我有这个错误: 未捕获类型错误:无法调用未定义的“push”方法

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

有什么想法吗?

谢谢, 弗拉德

最佳答案

           {
            "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
          }

关于javascript - D3js 与 json 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19146180/

相关文章:

json - 更改 VS 重新加载文件行为

json - Play Map[Int,_] 的 JSON 格式化程序

javascript - 如果接下来 10 秒内没有隐藏则隐藏 div

javascript - Bootstrap 中发生事件后如何显示警报?

javascript - 缩小 JavaScript 会弄乱字符

jquery - 让 jQuery 在页面上将 CSS 类添加到 iframe 内的 div 中?

jquery - Fancybox 2 垂直滚动问题

css - 带有 JSON 的旋转 slider

javascript - 存储用户之前是否按下过按钮

javascript - 如何在 HTML 中创建一个信息框来显示与悬停的 SVG 路径相关的信息?