【发布时间】:2019-07-02 17:16:40
【问题描述】:
我有一些需要绘制的数据 (Nodes)。这些节点可以重叠,因此绘制它们的顺序很重要(应该显示在顶部的节点需要最后绘制)。
这些节点的位置以及因此的 z 轴可能会发生变化,这就是为什么我尝试使用包含当前 index 的 key 来模拟这种行为em> 节点存储在列表中。
case class Node(id: Int)
def addNodesToSVG = {
val sortedData: List[Node] = ???
val nodesSelection = d3.select("#nodes").selectAll(".node")
.data(sortedData.toJSArray, (n: Node) => {
n.id.toString +
// the position of those nodes may change over time
// that's why we need to include the position in the identifier
sortedData.indexOf(n)
}
nodesSelection.enter().append("g").attr("class", "node") // ...
nodesSelection
.attr("transform", transform) // ...
nodesSelection.exit().remove()
}
不幸的是,这似乎没有按预期工作。
理论上,如果我只有两个节点(n1 和 n2),我认为这就是我认为的工作方式,它们保存在 List(n1, n2) 中
node key
----- ---
n1 10 // node 1 at position 0
n2 21 // node 2 at position 1
现在,如果我将列表更改为 List(n2, n1) 并再次调用 addNodesToSVG,这就是我认为会发生的事情:
node key
----- ---
n2 20 // node 1 at position 0
n1 12 // node 2 at position 1
由于这些是未知的,我认为它将删除 (nodesSelection.exit().remove()) 旧节点并以正确的顺序绘制“新”节点。然而,这并没有发生。为什么?
编辑经过更多调试后,我发现我的 exit 选择的大小始终为 0。
【问题讨论】:
-
nodesSelection.append("g") // ...这是什么?闻起来像不当使用 -
是的,你是对的;将我的代码复制到 SO 时我犯了一个错误。抱歉,我编辑了我的问题。
标签: javascript scala d3.js scala.js