[英]How do I make the height of a horizontal bar chart flexible in order to be resized when new data has been added in?
If I have a container, in this case my canvas, how I can set the height of a bar to be flexible?如果我有一个容器,在这种情况下是我的画布,我如何将栏的高度设置为灵活? In short, if a new data in dataArray will be added, the height of the bar has to change in order to fit the new data and remain inside my canvas.
简而言之,如果将添加 dataArray 中的新数据,则必须更改条的高度以适应新数据并保留在我的画布内。
I hope that make sense.我希望这是有道理的。
This is the code:这是代码:
var dataArray = [3, 20, 34, 50, 50, 50, 50, 60];
var width = 500;
var height = 500;
var widthScale = d3.scaleLinear()
.domain([0, d3.max(dataArray)])
.range([0, width - 100]);
var color = d3.scaleLinear()
.domain([d3.min(dataArray), d3.max(dataArray)])
.range(["red", "blue"]);
var x_axis = d3.axisBottom()
.scale(widthScale);
var canvas = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(10,0)")
var bars = canvas.selectAll("rect")
.data(dataArray)
.enter() //
.append("rect") //
.attr("width", function (d) { return widthScale(d); })
.attr("height", 50)
.attr("fill", function(d) { return color(d); })
.attr("y", function (d, i) { return (i * 51); });
canvas.append("g")
.attr("transform", "translate(0,480)")
.call(d3.axisBottom(widthScale));
Thank you so much for that.对此感激不尽。
Finally I found a solution.最后我找到了解决方案。
I had to resize the height of bars based on the dataset and the height of the canvas, minus a padding which I set as variable, in my case 1.我必须根据数据集和画布的高度调整条形的高度,减去我设置为变量的填充,在我的情况下为 1。
After that, I had to specify on the "y" to return i * the height / length of the dataset.之后,我必须在“y”上指定返回 i * 数据集的高度/长度。
Here's the code of only the bars:这是仅酒吧的代码:
var bars = canvas.selectAll("rect")
.data(dataArray)
.enter()
.append("rect")
.attr("width", function (d) { return widthScale(d); })
.attr("height", height / dataArray.length - barPadding)
.attr("fill", function(d) { return color(d); })
.attr("y", function (d, i) { return i * (height / dataArray.length) - 20); });
and this is the final version of the code:这是代码的最终版本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>D3</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
var dataArray = [3, 20, 34, 50, 60];
var width = 500;
var height = 500;
var widthScale = d3.scaleLinear()
.domain([0, d3.max(dataArray)])
.range([0, width - 100]);
var barPadding = 1;
var color = d3.scaleLinear()
.domain([d3.min(dataArray), d3.max(dataArray)])
.range(["red", "blue"]);
var x_axis = d3.axisBottom()
.scale(widthScale);
var canvas = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(10,0)")
var bars = canvas.selectAll("rect")
.data(dataArray)
.enter()
.append("rect")
.attr("width", function (d) { return widthScale(d); })
.attr("height", height / dataArray.length - barPadding)
.attr("fill", function(d) { return color(d); })
.attr("y", function (d, i) { return i * (height / dataArray.length) - 20); });
canvas.append("g")
.attr("transform", "translate(0,480)")
.call(d3.axisBottom(widthScale));
</script>
</body>
</html>
A solution using d3.scaleBand would be as follows.使用 d3.scaleBand 的解决方案如下。 The scaleBand is set up with a padding of 0.1, which means 10% of the height is allocated to padding between the bars and outside of the bars.
scaleBand 设置的填充为 0.1,这意味着 10% 的高度分配给条形之间和条形外部的填充。 scaleBand.bandwidth() is a handy tool to give you the height of the bars.
scaleBand.bandwidth() 是一个方便的工具,可以为您提供条形的高度。
var dataArray = [3, 20, 34, 50, 60]; var width = 500; var height = 500; var x_axis_margin = 20 var widthScale = d3.scaleLinear() .domain([0, d3.max(dataArray)]) .range([0, width - 100]); var y_scale = d3.scaleBand() .domain(dataArray.map((d,i) => i)) // create array of indices .range([0, (height - x_axis_margin)]) .padding(0.1) var barPadding = 1; var color = d3.scaleLinear() .domain([d3.min(dataArray), d3.max(dataArray)]) .range(["red", "blue"]); var x_axis = d3.axisBottom() .scale(widthScale); var canvas = d3.select("body") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(10,0)") var bars = canvas.selectAll("rect") .data(dataArray) .enter() .append("rect") .attr("width", function (d) { return widthScale(d); }) .attr("height", y_scale.bandwidth()) .attr("fill", function(d) { return color(d); }) .attr("y", function (d, i) { return y_scale(i); }); canvas.append("g") .attr("transform", "translate(0,"+ (height - x_axis_margin) +")") .call(d3.axisBottom(widthScale));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.