How to make a dashed line legend with d3.js

喜欢而已 提交于 2020-01-31 18:14:31

问题


I usually make a line legend by appending a rect giving it a very small height so that it looks like a line.

But now I need a dashed line legend. I am not able to do it by my old way. Can anyone show me a quick example of how to make a line legend with append('path') with d3.js?


回答1:


You can make it like this with a line DOM:

  var legend = svg.selectAll(".legend")
      .data(ageNames.slice().reverse())//data set for legends
    .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

  legend.append("line")//making a line for legend
      .attr("x1", width - 28)
      .attr("x2", width)
      .attr("y1", 10)
      .attr("y2", 10)
      .style("stroke-dasharray","5,5")//dashed array for line
      .style("stroke", color);

  legend.append("text")
      .attr("x", width - 44)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "end")
      .text(function(d) { return d; });

Working example here

Hope this work



来源:https://stackoverflow.com/questions/35516083/how-to-make-a-dashed-line-legend-with-d3-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!