时间:2021-05-26
近期在项目中要实现关系图,需要在线条上绘制文字。要实现这个功能,我们需要在SVG中连接的线条从标签line修改为path,这样才可能实现类似如下的效果:
1个简单的例子如下所示:
<svg viewBox="0 0 1000 300" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <path id="MyPath" d="M 100 200 C 200 100 300 0 400 100 C 500 200 600 300 700 200 C 800 100 900 100 900 100" fill="none" stroke="red"/> <text font-family="Verdana" font-size="42.5"> <textPath xlink:href="#MyPath" rel="external nofollow" > We go up, then we go down, then up again </textPath> </text> </svg>在这里我们需要实现1个path,然后设置其ID属性,之后我们创建textPath标签,并链接到上述的ID属性,这样就可以实现在路径上关联文字了。
而在D3中我们可以这样操作:
var link = svg.append("g").selectAll(".edgepath") .data(graph.links) .enter() .append("path") .style("stroke-width",0.5) .style("fill","none") .attr("marker-end",function(d){ return "url(#"+d.source+")"; }) .style("stroke","black") .attr("id", function(d,i){ return "edgepath"+i; }); var edges_text = svg.append("g").selectAll(".edgelabel") .data(graph.nodes) .enter() .append("text") .attr("class","edgelabel") .attr("id", function(d,i){ return "edgepath"+i; }) .attr("dx",80) .attr("dy",0); edges_text.append("textPath") .attr("xlink:href", function(d,i){ return "#edgepath"+i; }).text(function(d){ return d.id; })实际上这段代码就是上述例子的实现,这样就可以避免编写繁琐的SVG代码了。
总结
以上所述是小编给大家介绍的使用textPath实现线条上的文字,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
之前都是介绍在普通项目中使用mock.js,那么本次就来介绍一下在vue中使用mock.js实现前后端分离。安装:npminstallmockjs这里先写个小案
本文实例讲述了js实现鼠标悬停图片上时滚动文字说明的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:js实现鼠标悬停图片上时的滚动文字说明希望本
一、前言SVG元素用于在SVG图像中绘制文本。在svg中使用元素,可以实现绘制文字,文字旋转,多行文字,具有超级链接的文字等。二、基本文本要绘制文本,使用元素。
在工作中有时候会在word中使用到线条,但是很多朋友不知道如何在word中画线条,其实画线条工具在绘图工具栏中,可以直接选择的,今天小编就带领大家来学习下wor
本文实例讲述了JS实现DIV容器赋值的方法。分享给大家供大家参考,具体如下:给某个DIV容器赋值js函数,ajax中使用,支持ie和firefoxfunctio