• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

【D3.V3.js系列教程】--(十四)有路径的文字

【D3.V3.js系列教程】--(十四)有路径的文字

1、 在 svg 中插入一個 text

// 在 body 中插入一個 svg
var svg = d3.select('body').append('svg');
 
// 在 svg 中插入一個 text
svg.append('text')
.attr('x', 10)
.attr('y', 20)
.style('fill', 'steelblue')
.style('font-size', '24px')
.style('font-weight', 'bold')
.text('.text加入文字测试!');

2、添加文字路径

 // 先在 svg 中插入一個 path
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">
// 在 body 中插入一個 svg
	var svg = d3.select('body').append('svg');
 // 先在 svg 中插入一個 path
	svg.append('path').attr({
		id: 'mypath',
		d: 'M50 100Q350 50 350 250Q250 50 50 250'
	}).style({
		fill: 'none',
		stroke: 'green',
		'stroke-width': 10
	});
	// 在 svg 中插入一個 text
	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、效果


 

posted @ 2013-09-17 22:44  Class Xman  阅读(337)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3