【D3.js数据可视化系列教程】--(十四)有路径的文字

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/zhang__tianxu/article/details/11783095

1. 在svg 中插入一個text


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

  2. 添加文字路径


    
    
  1. // 先在 svg 中插入一個 path
  2. svg. append( 'path').attr({
  3. id: 'mypath',
  4. d: 'M50 100Q350 50 350 250Q250 50 50 250'
  5. }).style({
  6. fill: 'none',
  7. stroke: 'green',
  8. 'stroke-width': 10
  9. });
  10. svg. append( 'path').attr({
  11. id: 'mypath',
  12. d: 'M50 100Q350 50 350 250Q250 50 50 250'
  13. }).style({
  14. fill: 'none',
  15. stroke: 'green',
  16. 'stroke-width': 10
  17. });
  18. . append( 'textPath').attr({ //引用路径
  19. 'xlink:href': '#mypath'
  20. })

  3. 源码


    
    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>testD3-12-text.html </title>
  6. <script type="text/javascript" src="http://localhost:8080/spring/js/d3.v3.js"> </script>
  7. <style type="text/css">
  8. </style>
  9. </head>
  10. <body>
  11. <script type="text/javascript">
  12. // 在 body 中插入一個 svg
  13. var svg = d3.select( 'body' ).append( 'svg' );
  14. // 先在 svg 中插入一個 path
  15. svg.append( 'path' ).attr({
  16. id: 'mypath' ,
  17. d: 'M50 100Q350 50 350 250Q250 50 50 250'
  18. }).style({
  19. fill: 'none' ,
  20. stroke: 'green' ,
  21. 'stroke-width' : 10
  22. });
  23. // 在 svg 中插入一個 text
  24. svg.append( 'text' )
  25. .attr( 'x' , 10 )
  26. .attr( 'y' , 20 )
  27. .style( 'fill' , 'steelblue' )
  28. .style( 'font-size' , '24px' )
  29. .style( 'font-weight' , 'bold' )
  30. .append( 'textPath' ).attr({ //引用路径
  31. 'xlink:href' : '#mypath'
  32. })
  33. .text( '.text加入文字测试!并用xlink:href: #mypath加入了路径' );
  34. </script>
  35. </body>
  36. </html>

  4. 效果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值