As a beginner in d3.js, I've met a problem in network visualization. I was trying to fix it in many ways, but nothing works well unfortunately. So I really need an advice, would be happy if someone can help me. I'm getting an error in d3.v3.js:5624:
Uncaught TypeError: Cannot read property 'weight' of undefined
My json is generating in the controller and looks like this:
{ "nodes" :
[{ "Name" : "One", "Weight" : 903 },
{ "Name" : "Two", "Weight" : 502 },
...
],
"links" :
[{ "Source" : "One", "Target" : "Two", "Volume" : 2 },
{ "Source" : "Two", "Target" : "Five", "Volume" : 1 },
...
]
}
So I'm calling
return Json(network, JsonRequestBehavior.AllowGet);
The class Network:
public class Network
{
public List<NetworkNodes> nodes {get; set;}
public List<NetworkLinks> links{ get; set;}
public Network(List<NetworkNodes> a, List<NetworkLinks> b)
{
nodes = a;
links = b;
}
}
And script by itself:
$(document).ready(function () {
var width = 1500,
height = 1500;
var force = d3.layout.force()
.charge(-100)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
$.getJSON('@Url.Action("BuildNetwork", "Query")', function (graph) {
// Compute the distinct nodes from the links.
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function (d) { return Math.sqrt(d.Value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.call(force.drag);
node.append("title")
.text(function (d) { return d.Name; });
force.on("tick", function () {
link.attr("x1", function (d) { return d.Source.x; })
.attr("y1", function (d) { return d.Source.y; })
.attr("x2", function (d) { return d.Target.x; })
.attr("y2", function (d) { return d.Target.y; });
node.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
});
});
});
I know, there is some stupid mistake I have made, but I'm too stupid to understand where :(