我根据以下数据生成了一系列圆圈和一个文本字段。请参阅下面的代码。
var data = [{"x":534.720996869109,"y":188.402300350323,"label":"ATP","size":5},
{"x":526.793135268385,"y":494.495864118909,"label":"PK","size":10},
{"x":539.854817710164,"y":332.435549874068,"label":"rpoA","size":10},
{"x":528.357841173126,"y":236.960433131191,"label":"rpoB","size":10}]
var width = 1000,height = 1000;
var x = d3.scale.linear()
.domain([0, 1000])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, 1000])
.range([0, height]);
var svgContainer = d3.select("body")
.append("svg:svg")
.attr("width", width)
.attr("height",height);
//text
var text = svgContainer.selectAll("text")
.data(data)
.enter()
.append("text");
var textbAttributes = text
.attr("x", 800)
.attr("y", 100)
.text(function(d) { return ''})
.attr("font-family", "sans-serif")
.attr("font-size", "20px")
.attr("fill","black");
//circles
var nodesGroup = svgContainer.append("g");
var nodes = nodesGroup.selectAll("circle")
.data(data)
.enter()
.append("svg:circle");
var nodeattr = nodes
.attr("cy", function(d,i){return y(d.y); })
.attr("cx", function(d,i){return x(d.x); })
.on("click", function(){
d3.select("text")
.text(function(d,i){return d.label;})
})
.attr("r", function(d,i){return d.size;});
我希望能够在单击圆圈时使用 svg 文本元素“文本”中的相应数据元素“标签”刷新文本字段。然而,上面的代码只返回第一个标签元素,而不管我点击的圆圈。感谢所有帮助。谢谢!