d3: Cannot read property 'weight' of undefined in d3.force implementation

北战南征 提交于 2019-12-25 02:55:13

问题


I understand that the question has been asked a million times. Yet i am unable to figure out what is the issue. I am trying to replicate the force directed graph as given in the link below [http://bl.ocks.org/paulovn/9686202][1] The jsfiddle link is http://jsfiddle.net/k8a3dyuL/ I am unable to understand why i am getting this error.

I created a sample json object for which it worked as expected.I am able to establish connection between the nodes.

"nodes": [
{
  "index": 0, 
  "links": [
    1,
    2,
    3
    ], 
  "score": 7.35923957824707, 
  "level": 3, 
  "title": "Blood Diamond", 
  "cover": "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/wRamTzV6udKaMXrspxqOuo02zk0.jpg", 
  "label": "Blood Diamond (2006)", 
  "director": "Edward Zwick", 
  "cast": "Leonardo DiCaprio, Djimon Hounsou, Jennifer Connelly, Arnold Vosloo, David Harewood, Jimi Mistry, Michael Sheen, Marius Weyers, Stephen Collins, Ato Essandoh", 
  "year": 2006, 
  "duration": "143 minutes", 
  "genre": "Action, Drama, Adventure, Thriller", 
  "id": 1768448
},
{
  "index": 1, 
  "links": [

  ], 
  "score": 7.812180042266846, 
  "level": 2, 
  "title": "Casablanca", 
  "cover": "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/sm1QVZu5RKe1vXVHZooo4SZyHMx.jpg", 
  "label": "Casablanca (1942)", 
  "director": "Michael Curtiz", 
  "cast": "Humphrey Bogart, Ingrid Bergman, Paul Henreid, Claude Rains, Conrad Veidt, Sydney Greenstreet, Peter Lorre, Madeleine Lebeau, Dooley Wilson, Joy Page", 
  "year": 1942, 
  "duration": "102 minutes", 
  "genre": "Drama, Romance, War, Crime", 
  "id": 1750024
},
{
  "index": 2, 
  "links": [
    0
  ], 
  "score": 7.167639255523682, 
  "level": 2, 
  "title": "Lonely Hearts", 
  "cover": "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/wxsyl5wjobtYhll3rf5nGuC4kBN.jpg", 
  "label": "Lonely Hearts (2006)", 
  "director": "Todd Robinson", 
  "cast": "John Travolta, James Gandolfini, Salma Hayek, Jared Leto, Laura Dern, Dan Byrd, Alice Krige, Dagmara Dominczyk, Bailee Madison", 
  "year": 2006, 
  "duration": "108 minutes", 
  "genre": "Drama, Romance, Thriller, Crime", 
  "id": 1758379
}, 
{
  "index": 3, 
  "links": [
    1
    ], 
  "score": 7.595, 
  "level": 3, 
  "title": "Some Movie", 
  "cover": "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/wRamTzV6udKaMXrspxqOuo02zk0.jpg", 
  "label": "Some Movie", 
  "director": "Edward Zwick", 
  "cast": "Leonardo DiCaprio, Djimon Hounsou, Jennifer Connelly, Arnold Vosloo, David Harewood, Jimi Mistry, Michael Sheen, Marius Weyers, Stephen Collins, Ato Essandoh", 
  "year": 2006, 
  "duration": "143 minutes", 
  "genre": "Action, Drama, Adventure, Thriller", 
  "id": 1768448
}
],
"links": [
{
  "source": 0, 
  "target": 2, 
  "weight": 0.9770450592041016
},
{
  "source": 0, 
  "target": 3, 
  "weight": 0.9770450592041016
},
{
  "source": 1, 
  "target": 1, 
  "weight": 0.9770450592041016
}
]
}

Not Working

{
"nodes": [{
    "index": 897,
    "links": [22,
    806,
    897],
    "score": "NA",
    "level": 1,
    "title": "Customer position",
    "cover": "comapny name",
    "label": "label",
    "director": "dir",
    "cast": "cast",
    "year": 2000,
    "duration": 2,
    "genre": "NA",
    "id": 897,
    "weight": 1
},
{
    "index": 737,
    "links": [737,
    869],
    "score": "NA",
    "level": 1,
    "title": "Customer position",
    "cover": "comapny name",
    "label": "label",
    "director": "dir",
    "cast": "cast",
    "year": 2000,
    "duration": 2,
    "genre": "NA",
    "id": 737,
    "weight": 1
},
{
    "index": 869,
    "links": [737,
    869],
    "score": "NA",
    "level": 1,
    "title": "Customer position",
    "cover": "comapny name",
    "label": "label",
    "director": "dir",
    "cast": "cast",
    "year": 2000,
    "duration": 2,
    "genre": "NA",
    "id": 869,
    "weight": 1
}],
"links": [
{
    "source": 737,
    "target": 869,
    "weight": 0.9770450592041016
},
{
    "source": 897,
    "target": 806,
    "weight": 0.9770450592041016
},
{
    "source": 897,
    "target": 22,
    "weight": 0.9770450592041016
}
]

}

How can I resolve this?

来源:https://stackoverflow.com/questions/28558334/d3-cannot-read-property-weight-of-undefined-in-d3-force-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!