如何自动调整 d3.js 图的大小以包含轴
how to automatically resize d3.js graph to include axis
我在构建条形图时遇到了一些问题。我是第一次学习 d3.js,作为一个一直与 PHP/MySQL 一起工作的人,我不必学习 javascript。结果,我有点挣扎。
我的问题本质上更具概念性。如果,比方说在条形图中,Y 轴包含在一个 g 元素中,而条形图包含在另一个元素中,我如何确保我的轴根据显示的数据采用动态宽度?
我设法生成了一个条形图并且效果很好,但填充是一个固定数字(假设为 50 像素)。现在效果很好,因为我的数字从 0 到 50,所以一切都合适。如果我得到数万亿会怎样?轴的宽度会改变,但我的填充仍然是 50px,这意味着它会裁剪我的内容。
说到这里,"convention" 是什么?有什么技巧吗?
谢谢
您可能会在这里使用的一个技巧是我喜欢称之为 "double-render" 的技巧。您基本上首先绘制轴(在其余图之前)并获得最大刻度标签的宽度。您可以使用该值作为边距按常规绘制绘图。这个技巧对于字符串 "category" 标签特别有用,但也适用于数字。
这是一个注释示例。 运行多次查看它如何改装轴:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis--x path {
display: none;
}
</style>
<svg width="300" height="300"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//chancejs.com/chance.min.js"></script>
<script>
// set up some random data
// pick a random max value to render on the yaxis
var maxVal = chance.integer({
min: 1,
max: chance.pickone([1e1, 1e5, 1e10])
}),
// generate some fake data
data = [{
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}, {
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}, {
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}, {
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}];
// create svg and set up a y scale, the height value doesn't matter
var svg = d3.select("svg"),
y = d3.scaleLinear().rangeRound([100, 0]);
// set domain
y.domain([0, d3.max(data, function(d) {
return d.y;
})]);
// draw fake axis
var yAxis = svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
// determine max width of text label
var mW = 0;
yAxis.selectAll(".tick>text").each(function(d) {
var w = this.getBBox().width;
if (w > mW) mW = w;
});
// remove fake yaxis
yAxis.remove();
// draw plot normally
var margin = {
top: 20,
right: 20,
bottom: 30,
left: mW + 10 // max with + padding fudge
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// reset to actual height
y.range([height, 0]);
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1);
x.domain(data.map(function(d) {
return d.x;
}));
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.x);
})
.attr("y", function(d) {
return y(d.y);
})
.attr("width", x.bandwidth())
.attr("height", function(d) {
return height - y(d.y);
});
</script>
我在构建条形图时遇到了一些问题。我是第一次学习 d3.js,作为一个一直与 PHP/MySQL 一起工作的人,我不必学习 javascript。结果,我有点挣扎。
我的问题本质上更具概念性。如果,比方说在条形图中,Y 轴包含在一个 g 元素中,而条形图包含在另一个元素中,我如何确保我的轴根据显示的数据采用动态宽度?
我设法生成了一个条形图并且效果很好,但填充是一个固定数字(假设为 50 像素)。现在效果很好,因为我的数字从 0 到 50,所以一切都合适。如果我得到数万亿会怎样?轴的宽度会改变,但我的填充仍然是 50px,这意味着它会裁剪我的内容。
说到这里,"convention" 是什么?有什么技巧吗?
谢谢
您可能会在这里使用的一个技巧是我喜欢称之为 "double-render" 的技巧。您基本上首先绘制轴(在其余图之前)并获得最大刻度标签的宽度。您可以使用该值作为边距按常规绘制绘图。这个技巧对于字符串 "category" 标签特别有用,但也适用于数字。
这是一个注释示例。 运行多次查看它如何改装轴:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis--x path {
display: none;
}
</style>
<svg width="300" height="300"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//chancejs.com/chance.min.js"></script>
<script>
// set up some random data
// pick a random max value to render on the yaxis
var maxVal = chance.integer({
min: 1,
max: chance.pickone([1e1, 1e5, 1e10])
}),
// generate some fake data
data = [{
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}, {
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}, {
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}, {
x: chance.word(),
y: chance.floating({
min: 0,
max: maxVal
})
}];
// create svg and set up a y scale, the height value doesn't matter
var svg = d3.select("svg"),
y = d3.scaleLinear().rangeRound([100, 0]);
// set domain
y.domain([0, d3.max(data, function(d) {
return d.y;
})]);
// draw fake axis
var yAxis = svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
// determine max width of text label
var mW = 0;
yAxis.selectAll(".tick>text").each(function(d) {
var w = this.getBBox().width;
if (w > mW) mW = w;
});
// remove fake yaxis
yAxis.remove();
// draw plot normally
var margin = {
top: 20,
right: 20,
bottom: 30,
left: mW + 10 // max with + padding fudge
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// reset to actual height
y.range([height, 0]);
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1);
x.domain(data.map(function(d) {
return d.x;
}));
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y));
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.x);
})
.attr("y", function(d) {
return y(d.y);
})
.attr("width", x.bandwidth())
.attr("height", function(d) {
return height - y(d.y);
});
</script>