javascript - 将标题附加到 D3 树布局会导致错误 (D3)
问题描述
我正在尝试向 D3 树布局中的一些 SVG 标签添加工具提示。这是使用过渡呈现标签的函数:
buildLabels() {
const labelSelection = d3.select('svg g.labels')
.selectAll('text.label')
.data(this.nodes);
labelSelection.exit()
.remove();
labelSelection.enter()
.append('text')
.style('fill', 'none')
.style('stroke', 'none')
.style('stroke-width', '0')
.attr('transform', (d: any) => // ...)
.style('fill-opacity', 0)
.transition()
.duration(450)
.ease(d3.easeCircleIn)
.attr('transform', (d: any) => {
// ...
})
.attr('class', 'label')
.style('stroke', '#393D3E')
.style('fill', '#393D3E')
.style('fill-opacity', 1)
.style('stroke-width', '.4')
.style('text-anchor', (d: any) => d.parent ? 'start' : 'end')
.text(d => d.name);
}
我试过添加
.append('title')
.text(d => d.name)
之后.text
,但我得到一个很长的控制台错误
core.js:4061 ERROR TypeError: labelSelection.enter(...).append(...).style(...).style(...).style(...).attr(...).style(...).transition(...).duration(...).ease(...).attr(...).attr(...).style(...).style(...).style(...).style(...).style(...).text(...).append is not a function
如果我将功能更改为:
labelSelection.enter()
.append('text')
.text(d => d.name)
.append('title')
.text(d => d.name);
我得到了我期待的 DOM,即
<text>
Node name
<title>Node name</title>
</text>
但是,没有一个节点看起来正确,也没有它们应该的位置。当然,过渡也都被删除了。
我的问题是,是否有另一种方法可以添加一个不笨重的标题,或者如何解决上述错误。谢谢!
解决方案
您正在尝试附加到转换:
labelSelection.enter()
.append('text') // returns a selection of newly entered text elements
.style(...) // returns that same selection
.attr(... ) // returns that same selection
// ...
.transition() // returns a transition
.duration(450) // returns that same transition
.ease(...) // returns that same transition
// ...
.text(d => d.name) // returns that same transition
.append(...) // error
过渡和选择共享许多方法(例如.style()
、.attr()
、甚至.text()
),因此它们看起来非常相似,但它们并不共享所有方法。
你可以做selection.append()
,但不行transition.append()
。这就是您收到错误消息的原因,append
它不是转换方法,它解释了您的错误消息:
labelSelection.enter(...).append(...).style(...).style(...).style(...).attr(...).style(...).transition(...).duration(...).ease(...).attr(...).attr(...).style(...).style(...).style(...).style(...).style(...).text(...).append is not a function
.text 在这种情况下返回一个转换(因为它被链接到一个转换,如上面第一个代码块所示),因此我们可以将其简化为“transition.append 不是函数”。
相反,您可以通过保留对相关选择的引用来分解您的方法链接:
var labelEnter = labelSelection.enter()
.append('text')
.style(...)
.attr(... )
// ...
labelEnter.transition()
.duration(450)
.ease(...)
// ...
labelEnter.append("title")
.text(...)
我认为使您的方法链不必要地长的替代方法是使用transition.selection()
,它返回转换对应的选择:
labelSelection.enter()
.append('text')
.style(...)
.attr(... )
// ...
.transition()
.duration(450)
.ease(...)
// ...
.text(d => d.name);
.selection() // return a selection instead of a transition
.append("title")
.text(...)
推荐阅读
- c# - Visual Studio C# 应用程序连接到 MySql 服务器
- java - 列中带有 ImageView 数组的 JavaFX TableView
- asp.net-core - 如何在 MVC 核心 2.0 中使用 DisplayTemplates
- python - python:如何拆分按第一列分组的excel文件
- xpath - xpath 查询到页面上的特定数字
- android - 如何将亚马逊波利文本中的简历添加到 android 中的语音中?
- jquery - jQuery函数不在页面加载时调用
- javascript - 如何在js中连接对象属性
- firebase - 如何处理 Firebase 登录作为首次访问
- java - Appium 收集任何屏幕上的所有文本