【D3.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
    });

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. 效果


  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值