Skip to content

Instantly share code, notes, and snippets.

@enjalot
Forked from erikhazzard/index.html
Created February 14, 2012 18:58
Show Gist options
  • Save enjalot/1829187 to your computer and use it in GitHub Desktop.
Save enjalot/1829187 to your computer and use it in GitHub Desktop.
d3 tspan append
<html>
<meta charset='utf-8' />
<head>
</head>
<body>
<svg>
<text id='text_node' y="20px" text-anchor="start" opacity="1">
<tspan>You</tspan>
<tspan dx='.5em'>shall</tspan>
<tspan dx='.5em' dy='.9em'>not</tspan>
<tspan dx='.4em' dy='.9em'>pass</tspan>
</text>
</svg>
<script src='http://mbostock.github.com/d3/d3.js'></script>
<script>
var node = document.getElementById('text_node');
var bbox = node.getBBox();
var computed_text_length = node.getComputedTextLength();
//Does not work
text_node = d3.select('#text_node')
text_node.append('tspan')
.text('Fly')
.attr('dx', '.2em')
text_node.append('tspan')
.text('you')
.attr('dx', '.2em')
.attr('dy', '.9em')
text_node.append('tspan')
.text('fools')
.attr('dx', '.2em')
.attr('dy', '.9em')
/*
d3.select('#text_node').append('tspan')
.text('Fly')
.attr('dx', '.2em')
.attr('dy', '.9em')
d3.select('#text_node').append('tspan')
.text('you')
.attr('dx', '.2em')
.attr('dy', '.9em')
*/
console.log('BBOX width: ', bbox.width);
console.log('Computed text length: ', computed_text_length);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment