作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下只有文本和颜色信息的数据集
var inputs = [{"text": "inleaf1", "col": "red"}, {"text": "inleaf2", "col": "blue"}, {"text": "inleaf3", "col": "green"}];
下面是用来绘制带有文本元素的矩形
var inputleaf = container.selectAll("rect")
.data(data)
.enter().append("rect")
.attr("class", "input-leaf")
.attr("width", elementwidth)
.attr("height", elementheight)
.attr("x", startX)
.attr("y", function (d, i) {
return startY + ((elementheight + verticalmargin) * i);
})
.attr("stroke-width", "2")
.attr("fill", "none")
.attr("stroke", function (d) {
return d.col;
})
.call(function (d) {
//--------------------add position details to the data set
});
var inputtext = container.selectAll("text")
.data(data)
.enter().append("text")
.attr("x", startX + elementwidth / 10)
.attr("y", function (d, i) {
return startY + (elementheight / 2) + ((elementheight + verticalmargin) * i);
})
.text(function (d) {
return d.text;
});
在迭代过程中,如何向数据集添加更多属性,例如职位详情。基本上我想更新数据集。
最佳答案
这是一种解决方案,也许不是最好的解决方案:在设置 attr
时更新数据。例如:
.attr("y", function (d, i) {
var y = i * 10;
inputs[i].y = y // or d.y = y
return y;
})
参见 this fiddle
关于javascript - 如何向 enter.append 中使用的 d3 数据集添加新元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246855/
我是一名优秀的程序员,十分优秀!