我正在制作一个高度相等的D3直方图,但即使在使用" viewBox"之后图表也没有响应。和" preserveaspectRatio"属性。这是我正在使用的代码
var w = 800;
var h = 80;
var barPadding = 1;
var hello = 10;
var dataset = [ hello, 10, 13, 19, 21, 25, 22, 18, 15, 13,
11, 12, 15, 20 ];
var dataset2 = [ "hey", "you", "know", "that", "you", "are", "an", 18, 15, 13,
11, 12, 15, 20 ];
//Create SVG element
var svg = d3.select("#chart")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("viewBox","0 0 800 80")
.attr("preserveAspectRatio","xMinYMin meet")
.append("g")
.attr("transform", "translate(1,1)");
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("y", 0)
.attr("width", 50)
.attr("height", 50)
.attr("rx", 5)
.attr("ry", 5)
.attr("fill", function(d) {
return "rgb(" + (d * 15) + ", " + (d * 5) + ", " + (d * 5) + ")";
})
.style("opacity", function(d){
return 0.05 * (d);
});
svg.selectAll("text")
.data(dataset2)
.enter()
.append("text")
.text(function(d) { return d; })
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return i * (w / dataset2.length) + (w / dataset2.length - barPadding) / 2;
})
.attr("y", 18)
.attr("font-family", "sans-serif")
.attr("font-size", "8px")
.attr("fill", "black");
上述代码的工作小提琴链接为here https://jsfiddle.net/hawkeye15/m22pyyjL/
答案 0 :(得分:2)
答案 1 :(得分:1)
试试这个:
.attr("width", "100%")
.attr("height", "100%")
而不是使用您的w
和h
变量。