d3.js:如何在图表上为散点图添加标签

tmn*_*ael 7 javascript charts scatter d3.js

我正在尝试在此图表的散点上添加标签:http://bost.ocks.org/mike/d3/workshop/dot-chart.html

我认为稍微修改这段代码会起作用,但它没有:

svg.selectAll(".dot")
  .append("text")
  .text("fooLabelsOfScatterPoints");
Run Code Online (Sandbox Code Playgroud)

tmn*_*ael 11

迈克罗宾逊,你的榜样帮了忙.

对于那些想知道的人,这就是我所做的:

我删除了:

svg.selectAll(".dot")
  .data(data)
  .enter().append("circle")
  .attr("class", "dot")
  .attr("cx", function(d) { return x(d.x); })
  .attr("cy", function(d) { return y(d.y); })
  .attr("r", 12);
Run Code Online (Sandbox Code Playgroud)

并补充说:

var node = svg.selectAll("g")
                .data(data)
                .enter()
                .append("g");

node.append("circle")
  .attr("class", "dot")
  .attr("cx", function(d) { return x(d.x); })
  .attr("cy", function(d) { return y(d.y); })
  .attr("r", 12);

node.append("text")
  .attr("x", function(d) { return x(d.x); })
  .attr("y", function(d) { return y(d.y); })
  .text("fooLabelsOfScatterPoints");
Run Code Online (Sandbox Code Playgroud)

我将"text"标签添加到"g"标签上,而不是将"text"标签附加到"circle"标签上.