spo*_*pod 10 html javascript d3.js
使用d3.js绘制带箭头的线条时遇到问题.我确实看到了一些教程并编写了这段代码,但我只看到没有箭头标记的行.有人可以看看它,并告诉我我在哪里失踪.提前致谢.
var w = 300;
var h = 300;
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//arrow
svg.append("svg:defs")
.append("svg:marker")
.attr("id", "triangle")
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto");
//line
svg.append("line")
.attr("x1", 100)
.attr("y1", 100)
.attr("x2", 200)
.attr("y2", 100)
.attr("stroke-width", 1)
.attr("stroke", "black")
.attr("marker-end", "url(#triangle)");
Run Code Online (Sandbox Code Playgroud)
小智 8
将您的标记创建更改为以下内容:
svg.append("svg:defs").append("svg:marker")
.attr("id", "triangle")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M 0 -5 10 10")
.style("stroke", "black");
Run Code Online (Sandbox Code Playgroud)
执行此操作后,您将看到正在绘制的线条.您可能希望使用fill而不是stroke如果您想要填充箭头,您可以使用以下代码获得:
svg.append("svg:defs").append("svg:marker")
.attr("id", "triangle")
.attr("refX", 6)
.attr("refY", 6)
.attr("markerWidth", 30)
.attr("markerHeight", 30)
.attr("orient", "auto")
.append("path")
.attr("d", "M 0 0 12 6 0 12 3 6")
.style("fill", "black");
Run Code Online (Sandbox Code Playgroud)