[英]d3.js donut chart with legend outside donut
在以下代碼中,我想將甜甜圈圖例放在其右側的甜甜圈之外:
http://bl.ocks.org/juan-cb/1984c7f2b446fffeedde
我應該更改哪個行代碼來執行此操作?
var legend = svg.selectAll('.legend')
.data(color.domain())
.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', function(d, i) {
var height = legendRectSize + legendSpacing;
var offset = height * color.domain().length / 2;
var horz = -3 * legendRectSize;
var vert = i * height - offset;
return 'translate(' + horz + ',' + vert + ')';
});
legend.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', color)
.style('stroke', color);
legend.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function(d) { return d; });
與此類似:
http://www.jqueryscript.net/demo/jQuery-Plugin-To-Convert-Tabular-Data-Into-Donut-Charts-Chart-js/
編輯:解決方案只是更改水平和垂直坐標的問題。 不需要復雜的東西。
這兩個變量:
var horz = -3 * legendRectSize; // X-axis translation.
var vert = i * height - offset; // Y-axis translation.
您可以修改horz和vert公式進行翻譯。 像這樣:
var horz = 30 * legendRectSize; // Will be right side of the donut chart.
var vert = i * height - offset; // Still at the middle of the donut chart vertically.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.