javascript - 在 javascript 中为直方图合并数组

标签 javascript arrays d3.js histogram binning

我在 Javascript 中有以下数组,我需要将其放入 20 个桶中。数据值介于 0 和 1 之间,因此 bin 大小将为 0.05。我觉得应该有一个函数接受两个参数,一个数组和一个 bin 大小,但我找不到一个。我知道 D3.js 有一些功能可以帮助构建这样的数组,但我不知道哪个功能可能有帮助。

var arr = [
  "0.362743", "0.357969", "0.356322", "0.355757", "0.358511",
  "0.357218", "0.356696", "0.354579", "0.828295", "0.391186",
  "0.378577", "0.39372", "0.396416", "0.395641", "0.37573",
  "0.379666", "0.377443", "0.391842", "0.402021", "0.377516",
  "0.38936", "0.38936", "0.400883", "0.393171", "0.374419",
  "0.400821", "0.380502", "0.396098", "0.388256", "0.398968",
  "0.392525", "0.401858", "0.387297", "0.376471", "0.378183",
  "0.379787", "0.382024", "0.387928", "0.395367", "0.391972",
  "0.381295", "0.391183", "0.383598", "0.386424", "0.384338",
  "0.401834", "0.406253", "0.392854", "0.399266", "0.400804",
  "0.391146", "0.395441", "0.396265", "0.397894", "0.384822",
  "0.385181", "0.395443", "0.400981", "0.401716", "0.406633",
  "0.406887", "0.40694", "0.391219", "0.387946", "0.398858",
  "0.402233", "0.388583", "0.389772", "0.397084", "0.711566",
  "0.954557", "0.524007", "0.672288", "0.668441", "0.421726",
  "0.549536", "0.932952", "0.397851", "0.395536", "0.354818",
  "0.374355", "0.375257", "0.362613", "0.391271", "0.379219",
  "0.363316", "0.866006", "0.862254", "0.864403", "0.861346",
  "0.845225", "0.784467", "0.801275", "0.638579", "0.847282",
  "0.847402", "0.847747", "0.790411", "0.835979", "0.838546"
]

最佳答案

随着 D3.js v6 的发布 d3.layout.histogram 已经 superseded通过 d3.bin()现在属于 d3-array模块。

要对数组进行分箱,您需要创建一个直方图生成器:

var histGenerator = d3.bin()
  .domain([0,1])    // Set the domain to cover the entire intervall [0,1]
  .thresholds(19);  // number of thresholds; this will create 19+1 bins

configure your thresholds 还有一些可用的选项从而你的垃圾箱,但这个发电机会完全按照你的要求去做。通过使用您的值数组调用生成器,您可以将计算的 bin 作为数组检索:

var bins = histGenerator(arr);

看看这个工作示例:

var arr = [
  "0.362743", "0.357969", "0.356322", "0.355757", "0.358511",
  "0.357218", "0.356696", "0.354579", "0.828295", "0.391186",
  "0.378577", "0.39372", "0.396416", "0.395641", "0.37573",
  "0.379666", "0.377443", "0.391842", "0.402021", "0.377516",
  "0.38936", "0.38936", "0.400883", "0.393171", "0.374419",
  "0.400821", "0.380502", "0.396098", "0.388256", "0.398968",
  "0.392525", "0.401858", "0.387297", "0.376471", "0.378183",
  "0.379787", "0.382024", "0.387928", "0.395367", "0.391972",
  "0.381295", "0.391183", "0.383598", "0.386424", "0.384338",
  "0.401834", "0.406253", "0.392854", "0.399266", "0.400804",
  "0.391146", "0.395441", "0.396265", "0.397894", "0.384822",
  "0.385181", "0.395443", "0.400981", "0.401716", "0.406633",
  "0.406887", "0.40694", "0.391219", "0.387946", "0.398858",
  "0.402233", "0.388583", "0.389772", "0.397084", "0.711566",
  "0.954557", "0.524007", "0.672288", "0.668441", "0.421726",
  "0.549536", "0.932952", "0.397851", "0.395536", "0.354818",
  "0.374355", "0.375257", "0.362613", "0.391271", "0.379219",
  "0.363316", "0.866006", "0.862254", "0.864403", "0.861346",
  "0.845225", "0.784467", "0.801275", "0.638579", "0.847282",
  "0.847402", "0.847747", "0.790411", "0.835979", "0.838546"
];

var histGenerator = d3.bin()
  .domain([0,1])    // Set the domain to cover the entire intervall [0;]
  .thresholds(19);  // number of thresholds; this will create 19+1 bins

var bins = histGenerator(arr);
console.log(bins);
<script src="http://d3js.org/d3.v6.js"></script>

关于javascript - 在 javascript 中为直方图合并数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445495/

相关文章:

javascript - ionic 日期时间 : Button for multiple ion-datetime created dynamically

javascript - 如何将局部变量值从函数传递到 addEventListener 函数?

javascript - 在 x 轴上显示开始日期和结束日期之间的所有日期

javascript - 如何转换一个js数组以使其成为一天

c - C 数组中第二最少出现的元素

javascript - 如何在 SVG 中的单词之间设置字体大小?

javascript - 用D3绘制简单的时间线

javascript - Webpack 编号的 block 未根据配置正确加载

javascript - 使用 RegEx 构造函数属性的短名称代替冗长名称的充分理由是什么?

使用数组输入和数组输出为 PostgreSQL 创建简单的 C 函数