我有一個父容器(div.barChartContainer),它的高度和寬度都是從視口計算出來的,例如:width:calc(100vh - 200px)。 SVG容器被附加到div.barChartContainer元素。製作SVG容器在D3 v4中父容器的寬度和高度都是100%(而不是像素)
我正在努力如何讓SVG元素的寬度和高度達到其父元素的100%,希望我能得到一些幫助。現在我有靜態像素量(它目前呈現正確,但沒有響應)。
謝謝!
var margin = {top: 20, right: 20, bottom: 30, left: 80},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// set the ranges
var y = d3.scaleBand()
.range([height, 0])
.padding(0.4);
var x = d3.scaleLinear()
.range([0, width]);
var svg = d3.select(".barChartContainer").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
這是有用嗎? https://stackoverflow.com/questions/9400615/whats-the-best-way-to-make-a-d3-js-visualisation-layout-responsive – RobinL
@RobinL - 是的,謝謝你! –