ピクセルであなたの凡例の幅を新しい変数を定義し、設定します。
var legendWidth = 150;
使用svg
要素の幅のため、この変数:
:この方法を属性
var svg = d3.select('#chart')
.append('svg')
.attr('width', width + legendWidth) // <== !!!
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + (width/2) +
',' + (height/2) + ')');
は伝説
transform
ために、あなたの関数を書き換え
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 vert = i * height - offset;
return 'translate(' + width/2 + ',' + vert + ')'; // <== move every legend item on half of width
});
チェックworking example here。
jsFiddleを指定しますか? –
https://plnkr.co/edit/EW0k581XFBIeelwonwl2?p=preview – Vishnu
https://jsfiddle.net/nx4aph3w/ JS Fiddle – Vishnu