admin管理员组文章数量:1401149
I have created text nodes that has some value. So whenever data updates, only the value should update, the text nodes shouldn't be created again.
systemLevel
.enter()
.append('g')
.classed("system-level", true)
.attr("depth", function(d, i) {
return i;
})
.each(function(d, i) {
var columnHeader = graphHeaders.append("g")
.classed("system-column-header", true);
columnHeader.append('text')
.attr('font-size', '14')
.attr('font-weight', 'bold')
.attr('fill', "red")
.attr('x', 50 * i)
.attr('y', 50)
.text(function() {
return d.newUser;
});
columnHeader.append('text')
.attr('font-size', '14')
.attr('font-weight', 'bold')
.attr('fill', "blue")
.attr('x', 50* i)
.attr('y', 70)
.text(function() {
return d.value;
});
});
I have created an example on Js Bin. ,output
I am not sure, how to update just the text value. Any help is appreciated!
I have created text nodes that has some value. So whenever data updates, only the value should update, the text nodes shouldn't be created again.
systemLevel
.enter()
.append('g')
.classed("system-level", true)
.attr("depth", function(d, i) {
return i;
})
.each(function(d, i) {
var columnHeader = graphHeaders.append("g")
.classed("system-column-header", true);
columnHeader.append('text')
.attr('font-size', '14')
.attr('font-weight', 'bold')
.attr('fill', "red")
.attr('x', 50 * i)
.attr('y', 50)
.text(function() {
return d.newUser;
});
columnHeader.append('text')
.attr('font-size', '14')
.attr('font-weight', 'bold')
.attr('fill', "blue")
.attr('x', 50* i)
.attr('y', 70)
.text(function() {
return d.value;
});
});
I have created an example on Js Bin. https://jsbin./dixeqe/edit?js,output
I am not sure, how to update just the text value. Any help is appreciated!
Share Improve this question edited Oct 2, 2015 at 3:56 user2532865 asked Oct 2, 2015 at 0:56 user2532865user2532865 1191 silver badge10 bronze badges 2- So what exactly is the problem? The D3 update pattern is covered in numerous tutorials, e.g. bost.ocks/mike/circles – Lars Kotthoff Commented Oct 2, 2015 at 1:34
- I have referred many tutorials on update, but still not able to use it in this particular scenario. – user2532865 Commented Oct 2, 2015 at 4:05
1 Answer
Reset to default 6You're not using the usual D3 update pattern, described in many tutorials (e.g. here). You need to restructure the code to use this instead of unconditionally appending new elements:
var columnHeader = graphHeaders.selectAll("g").data(dataset);
columnHeader.enter().append("g").classed("system-column-header", true);
var texts = columnHeader.selectAll("text").data(function(d) { return [d.newUser, d.value]; });
texts.enter().append("text")
.attr('font-size', '14')
.attr('font-weight', 'bold')
.attr('fill', "red")
.attr('x', function(d, i, j) { return 50 * j; })
.attr('y', function(d, i) { return 50 + 20 * i; });
texts.text(function(d) { return d; });
Modified jsbin here.
本文标签: javascriptD3 how to update textStack Overflow
版权声明:本文标题:javascript - D3 how to update text? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744213954a2595548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论