Billboard.js图表大于容器 [英] Billboardjs chart bigger than it's container
问题描述
在生成广告牌图表而不提供尺寸时,有时生成的svg大于容器。有没有办法确保svg大小始终在范围内?
When generating a billboard chart and not providing a size, sometime the resulting svg is bigger than the container. Is there a way to make sure the svg size is always within bounds?
也就是说,没有在生成配置中提供大小。
That is, without providing size in the generation config.
var chart = bb.generate({
bindto: "#chart",
data: {
columns: [
["data1", 130, 200, 100, 170, 150, 250],
["data2", 130, 100, 140, 35, 110, 50]
],
types: {
data1: "line",
data2: "area-spline"
},
colors: {
data1: "red",
data2: "green"
}
}
});
setTimeout(() => {
const containerW = $("#chart").width();
const svgW = $("#chart svg").width();
console.log(`div width - ${containerW}`);
console.log(`SVG width - ${svgW}`);
});
#chart {
width: 501px;
height: 201px;
border: 1px solid green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/billboard.js/dist/billboard.min.css" />
<script src="https://cdn.jsdelivr.net/npm/billboard.js/dist/billboard.pkgd.min.js"></script>
<title>JS Bin</title>
</head>
<body>
<div id="chart"></div>
</body>
</html>
推荐答案
billboard.js从其父节点获取维度值。具体来说,偏移值-node.offsetWidth / offsetHeight。
billboard.js is taking the dimension value from its parent node. Specifically, the offset values - node.offsetWidth/offsetHeight.
- https://github.com/naver/billboard.js/blob/master/src/internals/size.js#L100
在您的示例中,父级< div>具有边距值。 jQuery的 .width()
返回不
From your example, the parent <div> has the margin value. The jQuery's .width()
returns without it.
因此,根据其过程,它设置了正确的尺寸。如果需要使用边距/填充值,则可以为其放置另一个父包装。
So, according its process, it's setting correct dimension. If you need to handle with margin/padding values, you might put another parent wrapper for it.
<div id="wrapper"> // <-- set margin/padding if you need
<div id="chart">
<svg> ...
这篇关于Billboard.js图表大于容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!