简体   繁体   English

更新 d3 正文

[英]Update d3 body text

How do I best update a body text element from a SVG tree node mouseover event?如何最好地从 SVG 树节点鼠标悬停事件更新正文文本元素? When I try the following the text is updated, but the SVG is removed from the display.当我尝试以下操作时,文本已更新,但 SVG 已从显示中移除。 Here is a the code:这是一个代码:

var svg = d3.select('body')
  .append('text').text('The Entry Point and M Code: ')
  .attr('class', 'centralText')
  .attr('x', 10)
  .attr('y', 10)
  .attr('text-anchor', 'middle')

  .append('svg')

here is my event code:这是我的事件代码:

var nodeEnter = node.enter().append('g')
    .attr('class', node_class)
    .attr('transform', function(d) {
      return 'translate(' + source.x0 + ',' + source.y0 + ')'; })
    .style('cursor', function(d) {
      return (d.children || d._children) ? 'pointer' : '';})
    .on('click', click)
    .on("mouseover", function(d) {
         d3.select('body')
            .text('M Code: is this')

There are two separate issues that are coming up.有两个不同的问题即将出现。 First, your initialization of svg is appending it to a text element, which is a bad idea.首先,您对svg的初始化是将其附加到text元素,这是一个坏主意。 Second, to update the text you're replacing the body text and not the text element.其次,要更新文本,您要替换正文文本而不是文本元素。

Two changes need to be made.需要进行两个更改。 First, change your mouseover function to this:首先,将mouseover功能更改为:

 d3.select('body').select('text')
            .text('M Code: is this');

This would normally fix it, but the second problem is that the svg is appended to the text element so it will still be erased.这通常会修复它,但第二个问题是svg附加到text元素,因此它仍然会被删除。 To fix this, change your declaration to the following:要解决此问题,请将您的声明更改为以下内容:

d3.select('body')
  .append('text').text('The Entry Point and M Code: ')
  .attr('class', 'centralText')
  .attr('x', 10)
  .attr('y', 10)
  .attr('text-anchor', 'middle');

 var svg = d3.select('body').append('svg');

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

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