I want to sum up the weight of all edges connected to a node and use it as the node score? The sum would be an array where than I would need to order them from the highest node with more edges to the lowest one. By the way I am using force layout in D3.
Thank you.
So far:
var sum=[]
edges.style("stroke", function(d, i) {
if ((d.source.name == node_name) || (d.target.name == node_name))
{ dataset.nodes.push(function(d){
//console.log(d.name)
return {sum:d.name}});
sum+= d.weight;
//console.log(d3.max(sum))
return "#000"}
else
{
return "#ccc"}
});
})
As you're loading your data with d3.json
, one approach would be to calculate the score directly after the data is loaded and not on click like in your fiddle. If your data is formatted like this example , then the following code stores the score of each node as score
attribute on the node. Maybe you have to adjust the attribute names nodes
, links
, source
, target
, score
and value
according to your dataset.
d3.json("assignments.json", function(error, graph) {
graph.nodes = graph.nodes.map(function(node, index){
node.score = graph.links
.filter(function(d){
return (d.source === index || d.target === index);
})
.map(function(d){
return d.value;
})
.reduce(function(prev, curr){
return prev + curr;
},0);
return node;
});
/**
* init force here
*/
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.