├── .gitignore ├── License.md ├── README.md ├── histogram.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eoinmurray/histogram/888147514ee12e5d70b30dd2cc24acebb0151cbc/.gitignore -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | 2 | Copyright (C) <2013> 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECT -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Histogram.js 2 | 3 | ```javascript 4 | npm i histogramjs 5 | var hist = require('histogramjs') 6 | ``` 7 | 8 | Frequency sort an array of data. 9 | Ripped from d3.js. this histogram function is fully self contained and can be dropped easily into project. 10 | 11 | It was created originally for use with webworkers. 12 | 13 | 14 | histogram({ 15 | data : Array, 16 | bins : Array 17 | }) 18 | 19 | 20 | #### Example. 21 | 22 | ```javascript 23 | var histogram = require('./histogram') 24 | 25 | var linspace = function linspace(a,b,n) { 26 | if(typeof n === "undefined") n = Math.max(Math.round(b-a)+1,1); 27 | if(n<2) { return n===1?[a]:[]; } 28 | var i,ret = Array(n); 29 | n--; 30 | for(i=n;i>=0;i--) { ret[i] = (i*b+(n-i)*a)/n; } 31 | return ret; 32 | } 33 | 34 | var x = linspace(0, 12, 100), 35 | y = [], 36 | num; 37 | 38 | for(var i = 0; i < 2000; i ++){ 39 | num = - Math.log(Math.random()) 40 | y.push(num) 41 | } 42 | 43 | var data = histogram({ 44 | data : y, 45 | bins : x 46 | }) 47 | 48 | for(var i = 0; i < data.length; i ++){ 49 | console.log('[' + data[i].x + ',' + data[i].y + '],') 50 | } 51 | 52 | ``` 53 | 54 | Run in node, it produces this distribution. 55 | 56 | 57 | ![](http://i.imgur.com/ZOAToPx.png) 58 | -------------------------------------------------------------------------------- /histogram.js: -------------------------------------------------------------------------------- 1 | (function(w, d){ 2 | 3 | 'use strict'; 4 | 5 | 6 | var histogram = function(opts) { 7 | var 8 | data = opts.data, 9 | bins_temp = opts.bins, 10 | i = bins_temp.length; 11 | 12 | var bisector = function(f) { 13 | return { 14 | left: function(a, x, lo, hi) { 15 | if (arguments.length < 3) lo = 0; 16 | if (arguments.length < 4) hi = a.length; 17 | while (lo < hi) { 18 | var mid = lo + hi >>> 1; 19 | if (f.call(a, a[mid], mid) < x) lo = mid + 1; 20 | else hi = mid; 21 | } 22 | return lo; 23 | }, 24 | right: function(a, x, lo, hi) { 25 | if (arguments.length < 3) lo = 0; 26 | if (arguments.length < 4) hi = a.length; 27 | while (lo < hi) { 28 | var mid = lo + hi >>> 1; 29 | if (x < f.call(a, a[mid], mid)) hi = mid; 30 | else lo = mid + 1; 31 | } 32 | return lo; 33 | } 34 | }; 35 | }; 36 | 37 | var hist_bisector = bisector(function(d) { return d; }); 38 | var bisectLeft = hist_bisector.left; 39 | var bisectRight = hist_bisector.right; 40 | var bisect = bisectRight; 41 | 42 | var minimum = function(array, f) { 43 | var i = -1, 44 | n = array.length, 45 | a, 46 | b; 47 | if (arguments.length === 1) { 48 | while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined; 49 | while (++i < n) if ((b = array[i]) != null && a > b) a = b; 50 | } else { 51 | while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined; 52 | while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b; 53 | } 54 | return a; 55 | }; 56 | 57 | var maximum = function(array, f) { 58 | var i = -1, 59 | n = array.length, 60 | a, 61 | b; 62 | if (arguments.length === 1) { 63 | while (++i < n && !((a = array[i]) != null && a <= a)) a = undefined; 64 | while (++i < n) if ((b = array[i]) != null && b > a) a = b; 65 | } else { 66 | while (++i < n && !((a = f.call(array, array[i], i)) != null && a <= a)) a = undefined; 67 | while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b; 68 | } 69 | return a; 70 | }; 71 | 72 | function value(x) { 73 | if (!arguments.length) return valuer; 74 | valuer = x; 75 | return histogram; 76 | }; 77 | 78 | function range(x) { 79 | if (!arguments.length) return ranger; 80 | ranger = hist_functor(x); 81 | return histogram; 82 | }; 83 | 84 | function hist_functor(v) { 85 | return typeof v === "function" ? v : function() { return v; }; 86 | }; 87 | 88 | function bins(x) { 89 | if (!arguments.length) return binner; 90 | binner = typeof x === "number" 91 | ? function(range) { return hist_layout_histogramBinFixed(range, x); } 92 | : hist_functor(x); 93 | return histogram; 94 | }; 95 | 96 | function frequency(x) { 97 | if (!arguments.length) return frequency; 98 | frequency = !!x; 99 | return histogram; 100 | }; 101 | 102 | function hist_layout_histogramBinSturges(range, values) { 103 | return hist_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1)); 104 | } 105 | 106 | function hist_layout_histogramBinFixed(range, n) { 107 | var x = -1, 108 | b = +range[0], 109 | m = (range[1] - b) / n, 110 | f = []; 111 | while (++x <= n) f[x] = m * x + b; 112 | return f; 113 | } 114 | 115 | function hist_layout_histogramRange(values) { 116 | return [minimum(values), maximum(values)]; 117 | } 118 | 119 | var frequency = true, 120 | valuer = Number, 121 | ranger = hist_layout_histogramRange, 122 | binner = hist_layout_histogramBinSturges; 123 | 124 | bins(bins_temp) 125 | 126 | var bins = [], 127 | values = data.map(valuer, this), 128 | range = ranger.call(this, values, i), 129 | thresholds = binner.call(this, range, values, i), 130 | bin, 131 | i = -1, 132 | n = values.length, 133 | m = thresholds.length - 1, 134 | k = frequency ? 1 : 1 / n, 135 | x; 136 | 137 | while (++i < m) { 138 | bin = bins[i] = []; 139 | bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]); 140 | bin.y = 0; 141 | } 142 | 143 | if (m > 0) { 144 | i = -1; while(++i < n) { 145 | x = values[i]; 146 | if (x >= range[0] && x <= range[1]) { 147 | bin = bins[bisect(thresholds, x, 1, m) - 1]; 148 | bin.y += k; 149 | bin.push(data[i]); 150 | } 151 | } 152 | } 153 | 154 | return bins; 155 | } 156 | 157 | 158 | // Exports and modularity 159 | if (typeof module !== 'undefined' && module.exports) { 160 | module.exports = histogram; 161 | } 162 | 163 | if (typeof ender === 'undefined') { 164 | this.histogram = histogram; 165 | } 166 | 167 | if (typeof define === "function" && define.amd) { 168 | define('histogram', [], function () { 169 | return histogram; 170 | }); 171 | } 172 | 173 | }).call(this); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "histogramjs", 3 | "version": "0.0.1", 4 | "description": "Simple histogram sorting", 5 | "main": "histogram.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "http://github.com/eoinmurray/histogram" 12 | }, 13 | "keywords": [ 14 | "histogram", 15 | "bin-sort", 16 | "sorting", 17 | "data", 18 | "d3" 19 | ], 20 | "author": "Eoin Murray", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/eoinmurray/histogram/issues" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var histogram = require('./histogram') 2 | 3 | var linspace = function linspace(a,b,n) { 4 | if(typeof n === "undefined") n = Math.max(Math.round(b-a)+1,1); 5 | if(n<2) { return n===1?[a]:[]; } 6 | var i,ret = Array(n); 7 | n--; 8 | for(i=n;i>=0;i--) { ret[i] = (i*b+(n-i)*a)/n; } 9 | return ret; 10 | } 11 | 12 | var 13 | x = linspace(0, 12, 100), 14 | y = [], 15 | num; 16 | 17 | for(var i = 0; i < 2000; i ++){ 18 | num = - Math.log(Math.random()) 19 | y.push(num) 20 | } 21 | 22 | var data = histogram({ 23 | data : y, 24 | i : x.length, 25 | bins : x 26 | }) 27 | 28 | for(var i = 0; i < data.length; i ++){ 29 | console.log('[' + data[i].x + ',' + data[i].y + '],') 30 | } 31 | --------------------------------------------------------------------------------