d3.js appending text when there is already text on the page -


i'm trying append text text on page. first time it, works. second time tried it, had trouble until found this: http://knowledgestockpile.blogspot.com/2012/01/understanding-selectall-data-enter.html

once added key function, worked entries first. how first show up?

var dataset=[{"x":0, "y":2},{"x":1, "y":2},{"x":2, "y":2},{"x":3, "y":2}];   vis.selectall("text")      .data(dataset)      .enter()      .append("text")      .text(function(d,i) {return d.x;})      .attr("text-anchor", "middle")      .attr("x", function(d, i) {return scalex(d.x);})      .attr("y", function(d) {return scaley(-5) ;})      .attr("font-family", "sans-serif")      .attr("font-size", "11px")      .attr("fill", "black");    vis.selectall("text")      .data(dataset,function(d){return d;})      .enter()      .append("text")      .text(function(d,i) {return d.x;})      .attr("text-anchor", "middle")      .attr("x", function(d, i) {return scalex(d.x);})      .attr("y", function(d) {return scaley(20) ;})      .attr("font-family", "sans-serif")      .attr("font-size", "11px")      .attr("fill", "black"); 

here fiddle: http://jsfiddle.net/tvinci/hbasc/2/

by selecting text , calling data(), you're telling d3 match new elements existing elements. when this, intention update existing elements instead of adding of them new ones.

if want add text twice, can assign different classes text in both selections such .enter() selection contain elements in both cases. code like

vis.selectall("text.one")    .data(dataset)    .enter()    .append("text")    .attr("class", "one")    ...  vis.selectall("text.two")    .data(dataset)    .enter()    .append("text")    .attr("class", "two")    ... 

if want update existing text, remove .enter() in second call chain. won't necessary pass in key function in case.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -