admin管理员组文章数量:1312888
I am using the code from to draw a multi-series line chart.
The lines have labels at the ends, but it gets a little mess up if the lines are close to each other.
Is it possible to add legends to line charts with d3? I have looked through the API, but I can't seem to find anything.
I am using the code from https://bl.ocks/mbostock/3884955 to draw a multi-series line chart.
The lines have labels at the ends, but it gets a little mess up if the lines are close to each other.
Is it possible to add legends to line charts with d3? I have looked through the API, but I can't seem to find anything.
Share Improve this question asked Aug 15, 2016 at 11:35 JamgreenJamgreen 11.1k32 gold badges122 silver badges231 bronze badges1 Answer
Reset to default 8There is no function for automatically creating legends in the default D3. D3 is a low level data visualization library with very high flexibility in terms of the final results. However, a legend is nothing but some shapes and texts so you sure can create it using D3. Here is something to get you started.
var legend_keys = ["Austin", "New York", "San Francisco"]
var lineLegend = svg.selectAll(".lineLegend").data(legend_keys)
.enter().append("g")
.attr("class","lineLegend")
.attr("transform", function (d,i) {
return "translate(" + width + "," + (i*20)+")";
});
lineLegend.append("text").text(function (d) {return d;})
.attr("transform", "translate(15,9)"); //align texts with boxes
lineLegend.append("rect")
.attr("fill", function (d, i) {return color_scale(d); })
.attr("width", 10).attr("height", 10);
本文标签: javascriptAdding legends to d3js line chartsStack Overflow
版权声明:本文标题:javascript - Adding legends to d3.js line charts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741909738a2404370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论