1. 在svg 中插入一個text
var svg = d3.select('body').append('svg');
svg.append('text')
.attr('x', 10)
.attr('y', 20)
.style('fill', 'steelblue')
.style('font-size', '24px')
.style('font-weight', 'bold')
.text('.text加入文字测试!');
2. 添加文字路径
svg.append('path').attr({
id: 'mypath',
d: 'M50 100Q350 50 350 250Q250 50 50 250'
}).style({
fill: 'none',
stroke: 'green',
'stroke-width': 10
});
svg.append('path').attr({
id: 'mypath',
d: 'M50 100Q350 50 350 250Q250 50 50 250'
}).style({
fill: 'none',
stroke: 'green',
'stroke-width': 10
});
.append('textPath').attr({
'xlink:href': '#mypath'
})
3. 源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>testD3-12-text.html</title>
<script type="text/javascript" src="http://localhost:8080/spring/js/d3.v3.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var svg = d3.select('body').append('svg');
svg.append('path').attr({
id: 'mypath',
d: 'M50 100Q350 50 350 250Q250 50 50 250'
}).style({
fill: 'none',
stroke: 'green',
'stroke-width': 10
});
svg.append('text')
.attr('x', 10)
.attr('y', 20)
.style('fill', 'steelblue')
.style('font-size', '24px')
.style('font-weight', 'bold')
.append('textPath').attr({
'xlink:href': '#mypath'
})
.text('.text加入文字测试!并用xlink:href: #mypath加入了路径');
</script>
</body>
</html>
4. 效果