svg - How to append a text element with inline tspan children? -
starting dom contains like
<svg id="svg0" width="600" height="300" xmlns="http://www.w3.org/2000/svg" version="1.1"> </svg>
...i want programmatically modify element in d3.select("#svg0")
end with
<svg id="svg0" width="600" height="300" xmlns="http://www.w3.org/2000/svg" version="1.1"> <text x="20" y="20"> lorem ipsum <tspan style="alignment-baseline:text-before-edge">dolor</tspan> sit amet</text> </svg>
this far can get:
var $svg = d3.select("#svg0"); $svg.append("text").text("lorem ipsum ") .attr({x:"20", y:"20"});
it looks though rest should easy, i've spent last 2 hours trying "obvious" things finish without success.1
what 1 have finish task described above?
1i've tried far many things describe them all. suffice text
method, when used setter, wipes out whatever textcontent
text
object had before. means that, effectively, method can called once, precludes solutions relying on calling .text(...)
second time add " sit amet" fragment.)
normally think use html
function this, docs:
note: name suggests, selection.html supported on html elements. svg elements , other non-html elements not support innerhtml property, , incompatible selection.html. consider using xmlserializer convert dom subtree text. see innersvg polyfill, provides shim support innerhtml property on svg elements.
here's polyfill: http://jsfiddle.net/gngf5/
and if don't want that, can hack using multiple tspan
elements w/ transform
, seen here: http://jsfiddle.net/caucm/
var $svg = d3.select("#svg0"); var $text = $svg.append("text"); var $tspan1 = $text.append('tspan'); var $tspan2 = $text.append('tspan'); var $tspan3 = $text.append('tspan'); $text.attr('transform', 'translate(0, 18)'); $tspan1.text('lorem ipsum'); $tspan2.text('dolor').style('alignment-baseline', 'text-before-edge'); $tspan3.text('sit amet');
Comments
Post a Comment