【发布时间】:2017-11-16 20:02:18
【问题描述】:
我有这样的数据:
var nodes = [
{
"id": "A",
"label": "Data A",
"group": "0",
"level": "0"
}, {
"id": "B",
"label": "Data B",
"group": "1",
"level": "1"
},
// etc //
]
var links = [
{
"source": "A",
"target": "B",
"strength": "0.5",
"value": "276"
}, {
"source": "A",
"target": "C",
"strength": "0.5",
"value": "866"
},
// etc //
]
我一直在尝试根据链接中的value 设置节点半径大小,这些链接将节点称为target。
因此,例如,节点 B 的大小应根据值 276(以节点 B 作为其目标的链接)。
这是我用的:
var nodeElements = g.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", function(node,link){
var radius = 12;
if (node.level == 0) radius = radius * 2; // Setting up the main node as bigger than others
node.weight = link.filter(function(link) {
if (link.target.index == node.index){
var linkValue = link.value;
if (linkValue > 500) ? (radius = 12) : (radius = 6);
}
});
return radius;
})
.attr("fill", getNodeColor)
.attr("stroke", "#fff")
.attr('stroke-width', 2)
.on('mouseover', selectNode)
但这似乎不起作用。说它无法识别link.filter,I've taken from here。我正在使用 d3.js v4。
试图寻找线索,但仍然没有任何提示。有什么想法吗?
【问题讨论】:
-
设置“r”属性的函数不正确。匿名函数的参数是“当前数据 (d)、当前索引 (i) 和当前组 (节点)”。见github.com/d3/d3-selection#selection_attr
标签: javascript d3.js d3-force-directed