简体   繁体   English

如何使水平条形图的高度灵活,以便在添加新数据时调整大小?

[英]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.

相关问题 如何使条形图的高度成为数据? - How do I make my bar chart's height be the data? 调整大小后如何裁剪 konva 图像? - How do I crop a konva image when it has been resized? 首先填充另一个下拉框时,如何显示新的下拉框? - How do I make new drop down boxes appear when another one has been filled in first? 通过ajax加载内容后,如何在div的顶部制作水平滚动条? - How do I make a horizontal Scrollbar at the top of the div Work after the content has been loaded via ajax 脚本运行后如何在控制台中提供Tampermonkey添加的功能? - How do I make functions added by Tampermonkey be available in console after the script has been ran? 如何使用ChartJS在水平条形图上绘制垂直线? - How do I draw a vertical line on a horizontal bar chart with ChartJS? 如何检测何时将新元素添加到 jquery 的文档中? - How can I detect when a new element has been added to the document in jquery? ChartJS:如何使用水平准则制作条形图: - ChartJS: how to make a bar chart with a horizontal guideline: 使用javascript google条形图时如何设置正确的高度 - how do i set the correct height when using javascript google bar chart 如何检查特定模块是否已添加到 Worklet? - How do I check if a specific module has been added to a Worklet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM