简体   繁体   English

为什么我的d3映射函数返回未定义的源属性?

[英]Why is my d3 map function returning an undefined source attribute?

I'm new with d3js and I'm having some issues trying to map a csv file to the links of the force layout. 我是d3js的新手,在尝试将csv文件映射到强制布局的链接时遇到一些问题。

this is my csv file: 这是我的csv文件:

node,edges,centrality_degree
1,"[(1, 2), (1, 3), (1, 4), (1, 5)]",1.0
2,"[(2, 1), (2, 3), (2, 4), (2, 5)]",1.0
3,"[(3, 1), (3, 2), (3, 4), (3, 5)]",1.0
4,"[(4, 1), (4, 2), (4, 3), (4, 5)]",1.0
5,"[(5, 1), (5, 2), (5, 3), (5, 4)]",1.0

I want to set the edges values to the links attribute of the d3.layout.force(), however when mapping the values, I'm getting an error: 我想将边缘值设置为d3.layout.force()的links属性,但是在映射这些值时,出现错误:

Uncaught TypeError: Cannot read property 'weight' of undefined Uncaught TypeError:无法读取未定义的属性“ weight”

I don't know also if is it correct to return an array with a bunch of objects, that's what I'm getting on the console. 我也不知道返回带有一堆对象的数组是否正确,这就是我在控制台上得到的。 5 arrays with 4 objects each, each object with its source and target attribute. 5个数组,每个数组有4个对象,每个对象都有其源和目标属性。 The attributes are all correct, except for the 5 array. 这些属性都是正确的,除了5数组。 I am getting an undefined value for the first source value of it. 我为它的第一个源值得到了一个未定义的值。 It was supposed to be 5. 应该是5。

Here is my code: 这是我的代码:

var force = d3.layout.force()
    .size([w, h]);

d3.csv("{{ g }}", function(d) {

    var nodes = d3.values(d.map( function(d){ return +d.node; } ));

    var links_list = d.map( function(d) {

        list_array = JSON.parse(d.edges.replace(/\(/g,"[").replace(/\)/g,"]"));

        var i, len = list_array.length;

        links = [];

        for (i=0; i<len; i++) {
            links.push({source: list_array[i][0] ,target: list_array[i][1] });
        }

        console.log(links);

        #console result:
        #[Object, Object, Object, Object]
        #[Object, Object, Object, Object]
        #[Object, Object, Object, Object]
        #[Object, Object, Object, Object]
        #expanding one array:
            #0: Object
            #1: Object
            #2: Object
            #3: Object
        #[Object, Object, Object, Object]
            #expanding the array[5][0] object: 
            #0: Object
                #source: undefined
                #target: 2
                #__proto__: Object

    });

    force
        .nodes(nodes)
        .links(links)
        .start();

...

});

What am I doing wrong and why I'm getting this error? 我在做什么错,为什么会出现此错误?

I appreciate your help. 我感谢您的帮助。

The values of the source and target attributes should be specified as indexes into the nodes array; 源和目标属性的值应指定为节点数组的索引; these will be replaced by references after the call to force start. 在强制启动之后,这些将被引用替换。

Note that array indices start from 0 and not from 1. But in your data from csv, you have links like (5, 1), (5, 2), (5, 3), (5, 4) and your nodes array has 5 elements only; 请注意,数组索引从0开始而不是从1开始。但是在csv中的数据中,您具有(5, 1), (5, 2), (5, 3), (5, 4)并且您的节点数组仅包含5个元素; index of last element would be 4. You should have reduced these numbers to 1 to represent the actual nodes. 最后一个元素的索引为4。您应该将这些数字减少为1以表示实际的节点。

For example: 例如:

var nodes = [{ name: "A", id: 1},{ name: "B", id: 2}];

Force layout expects the link array as shown below. 强制布局需要链接数组,如下所示。

var links = [{ source:0, target: 1 }];

Hope you got the problem. 希望你能解决这个问题。 You will have to do as shown below to get the actual indices. 您将必须执行以下操作以获得实际的索引。

 for (i=0; i<len; i++) {
    links.push({source: list_array[i][0]-1 ,target: list_array[i][1]-1 });
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM