简体   繁体   English

d3js svg通过id选择文本

[英]d3js svg text selection by id

I have implemented what I thought to be a relatively simple mouse-over behavior in d3, but I am clearly missing something, because I can't seem to figure out why it is not working. 我已经实现了d3中我认为是相对简单的鼠标悬停行为,但是我显然遗漏了一些东西,因为我似乎无法弄清为什么它不起作用。

The goal is to have a text blocks associated with an array of data points, and have the text block display when the user mouses over the data points. 目标是使文本块与数据点数组关联,并在用户将鼠标悬停在数据点上时显示文本块。 When the use clicks on the data point, the text block remains visible. 当用户单击数据点时,文本块保持可见。 I'm not using tooltips, as I need to display more than one text block at a time. 我没有使用工具提示,因为我一次需要显示多个文本块。

First, I am creating the text blocks in an svg using a wrapping function from Bostock himself ( http://bl.ocks.org/mbostock/7555321 ) 首先,我使用Bostock自己的包装函数在svg中创建文本块( http://bl.ocks.org/mbostock/7555321

function wrap(text, width, xLoc, yLoc) {
  text.each(function() {
    var text = d3.select(this),
        words = text.text().split(/\s+/).reverse(),
        word,
        line = [],
        lineNumber = 0,
        lineHeight = 1.0, // ems
        y = text.attr("y"),
        //dy = parseFloat(text.attr("dy")),
        dy = 0,
        tspan = text.text(null).append("tspan").attr("x", xLoc).attr("y", y).attr("dy", dy + "em");
    while (word = words.pop()) {
      line.push(word);
      tspan.text(line.join(" "));
      if (tspan.node().getComputedTextLength() > width) {
        line.pop();
        tspan.text(line.join(" "));
        line = [word];
        tspan = text.append("tspan").attr("x", xLoc).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
      }
    }
  });
}

The blocks themselves are created using this function: 块本身是使用以下功能创建的:

 svgContainer.selectAll("text")
    .data(data)
    .enter()
    .append("text")
        .attr("x", function(d) { return d.x_axis; })
        .attr("y", function(d) { return d.y_axis; })
        .attr("id", function(d) {return("textBlock" + d.documentNumber);})
        .attr("visibility", "visible")
        .text(function(d) { return d.description;});
        .call(wrap, 200, function(d) { return d.x_axis; }, function(d) { return d.y_axis; });

The mouseover function of the data points (plotted as svg circles) looks like this. 数据点的鼠标悬停功能(绘制为svg圆圈)如下所示。 Note that the following is configured simply to turn on the visibility of #textBlock5 regardless of which circle is moused over; 请注意,下面的配置仅用于打开#textBlock5的可见性,而不管将鼠标悬停在哪个圆圈上。 this is a debugging step just to remove the function from the select statement 这是一个调试步骤,仅用于从select语句中删除函数

    svgContainer.selectAll("circle")
        .data(data)
        .on("mouseover", function(d){
            return console.log("#textBlock"+d.documentNumber);
            d3.select("#textBlock5").attr("visibility", "visible");
            });

The ids are being constructed properly in the DOM (see below), but the mouseover function is not changing the visibility attribute...can anyone help me identify what I'm doing wrong here!? id在DOM中已正确构建(请参见下文),但是mouseover功能并未更改可见性属性...任何人都可以帮助我确定我在这里做错了什么吗?

<text x="927.1563942727792" y="673.2598605856803" id="textBlock5" visibility="visible">
<tspan x="927.1563942727792" y="673.2598605856803" dy="0em">text here</tspan>
//more tspans here...
</text>

OMG, birdspider. 天哪,飞鸟。 I cannot believe that I failed to see that...goes to show what value a good break would have done for me. 我不敢相信我没有看到……去证明一个好休息对我有什么价值。 Thanks for taking the time to give this a once-over and being so observant!! 感谢您抽出宝贵的时间对此进行一次仔细的观察!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM