[英]Draw triangle at bottom or top of line
In below code I'm attempting to draw a triangle at bottom of line (to represent a arrow) : 在下面的代码中,我试图在线条的底部绘制一个三角形(代表一个箭头):
Code : 代码:
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//Draw the line
var line = svgContainer.append("line")
.attr("x1", 5)
.attr("y1", 5)
.attr("x2", 5)
.attr("y2", 50)
.attr("stroke-width", 2)
.attr("stroke", "black");
line.append("svg:path")
.attr("d", d3.svg.symbol.type("triangle-up"))
.style("fill", "black");
jsfiddle : http://jsfiddle.net/Qh9X5/3420/ jsfiddle: http : //jsfiddle.net/Qh9X5/3420/
But receive error : 但是收到错误:
Uncaught TypeError: undefined is not a function
How can the triangle be drawn at top or bottom of line ? 如何在线条的顶部或底部绘制三角形?
First of all, you don't need to make the arrowhead so manually. 首先,您不需要手动制作箭头。 SVG has a way to put a marker at the end and/or beginning of a path, and it rotates it for you to match the path's angle.
SVG提供了一种在路径的末尾放置标记的方法,它可以旋转标记以匹配路径的角度。 Check out any tutorial about SVG arrowheads, like this one .
查看有关SVG箭头的任何教程,例如本教程。
Still, if you want to do it the way you started: 不过,如果您要按照开始的方式进行操作:
d3.svg.symbol.type("triangle-up")
should become d3.svg.symbol().type("triangle-up")
d3.svg.symbol.type("triangle-up")
应该变成d3.svg.symbol().type("triangle-up")
and 和
Instead of line.append("svg:path")
, which puts a path inside a line element (hence invalid), do svgContainer.append("svg:path")
to make the path a sibling of the line. 代替
line.append("svg:path")
(将路径放置在line元素内(因此无效)),请执行svgContainer.append("svg:path")
以使路径成为该行的同级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.