d3.js在调整大小事件中缩放圆环图

时间:2012-12-11 10:56:48

标签: javascript d3.js data-visualization

我用两个尺寸绘制了我的图表到窗口的宽度:

var width = (window.innerWidth < 1280) ? 400 : 600,
height = (window.innerWidth < 1280) ? 400 : 500,
radius = Math.min(width, height) / 2;

var arc = d3.svg.arc().innerRadius(radius).outerRadius(radius - 10),
arcFinal = d3.svg.arc().innerRadius(radius - 100).outerRadius(radius - 10),
arcHover = d3.svg.arc().innerRadius(radius - 100).outerRadius(radius);

var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("id", "pie")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");}

在resize事件中更新此功能的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

好的,我通过添加:

来修复
...
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", "0, 0, " + width + ", " + height)
.attr("preserveAspectRatio", "xMinYMin")
...

然后我更新了resize事件的宽度和高度