d3.js - 为什么数据更新后我的输入选择不为空?
问题描述
我使用 d3 作为反应项目的一部分。在某些时候,我会显示数据(javascript 对象数组),如下所示。
当我更新二维数据数组时,我想更新更新对象的位置+大小并有条件地绘制边框。
但是,我注意到,d3 不仅更新与更新数据相关的 svg 对象,还总是更新我的数据数组的第一个元素并对所有其他元素执行输入操作。尽管对象没有改变。数组引用也应该相同。
这是为什么?
let bound = d3
.select(this.svgRef.current)
.selectAll(".square")
.data(this.props.data, d => d);
//enter
bound
.enter()
.append("rect")
.attr("class", "square")
.attr("id", d => d.id)
.attr("x", d => {console.log("enter", d); return d.x})
.attr("y", d => d.y)
.attr("width", d => d.width)
.attr("height", d => d.height)
.style("fill", d => {
return d.c;
})
.style("stroke", d => {
if (d.id == this.props.focus) {
return "#FF0000";
} else {
return null;
}
});
//updates
bound
.attr("x", d => { return d.x })
.attr("y", d => d.y)
.attr("width", d => d.width)
.attr("height", d => d.height)
.style("fill", d => {
return d.c;
})
.style("stroke", d => {
if (d.id == this.props.focus) {
return "#FF0000";
} else {
return null;
}
});
bound.exit().remove();
数据格式如下:
[{
"id":"1.png","c":"#FFFFFF",
"d":["No Data"],
"index":0,
"x":0,
"y":0,
"width":115,
"height":16
}, {
"id":"2.png",
"c":"#FFFFFF",
"d":["Example Data"],
"index":1,
"x":115,
"y":0,
"width":115,
"height":16
}, ...]
解决方案
我的问题的答案似乎是一个不正确的键功能。标识函数似乎不适用于对象数组。
推荐阅读
- python - 尝试在 python 中使用 Selenium 在贝宝上单击继续按钮
- javascript - 有人可以解释为什么我不能动态添加渐变样式吗?
- javascript - 材料表反应,在编辑或添加新行之前运行自定义函数
- php - WooCommerce 迷你购物车片段 - 刷新片段时如何加载脚本
- java - 按字母顺序对java中的字符串列表进行排序
- flutter - Flutter:如何在 TextField 的 onSubmitted() 上关闭键盘而不会失去焦点
- python - 如何从数值中找到日期字符串?
- mysql - 将多个表合并到一个新表中的一行
- swiftui - 如何在视图中的单个 VStack 中添加多个按钮(Xcode12,SwiftUI)
- kubernetes - Hashicorp Vault - Agent Injector - 有意义吗?