Javascript D3.js.选择全部不工作
解决这个问题 在这一行代码中:Javascript D3.js.选择全部不工作,javascript,d3.js,Javascript,D3.js,解决这个问题 在这一行代码中: var node = nodesG.selectAll("circle.node").data(nodes, function (d) { console.log("hello"); return d.id; }); console.log永远不会运行。我不知道为什么 这是nodesG: var vis = d3.select(selection).append("svg") .attr("width",
var node = nodesG.selectAll("circle.node").data(nodes, function (d) {
console.log("hello");
return d.id;
});
console.log
永远不会运行。我不知道为什么
这是nodesG
:
var vis = d3.select(selection).append("svg")
.attr("width", width)
.attr("height", height);
nodesG = vis.append("g").attr("id", "nodes");
我想到了两件事。父项不存在,或者数据为空。 要么如此,要么这部分代码永远无法到达 试试这个:
var data = [1,2,3,4,5]
var body = d3.select("body")
var nothing = d3.select("#wut")
//shouldn't be called
var noparent = nothing.selectAll("div").data(data, function(d){
alert("no parent CALLED"); return d;
})
var nodata = body.selectAll("div").data([], function(d){
alert("no data CALLED");
return d;
})
var wrongdata = body.selectAll("div").data({banana:2, apple:3}, function(d){
alert("wrong data CALLED");
return d;
})
//should be called
var noenter = body.selectAll("div").data(data, function(d){
alert("no enter CALLED");
return d;
})
var nodes = body.selectAll("div").data(data, function(d){
alert("normal CALLED");
return d;
}).enter().append("div")
.text(function(d){ return d})
编辑:我添加了一个测试,该测试表明非数组不会导致连接。g元素是否出现在dom中?因此问题是数据没有通过数组传递。er否-数据不是空的,只是不是数组。确定。我还添加了该测试。