Created
June 9, 2012 20:49
-
-
Save max-mapper/2902557 to your computer and use it in GitHub Desktop.
histogram bucket sorting with crossfilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createDimensions(key) { | |
var asc = xf.dimension(function (d) { return +(d[key]) }) | |
var desc = xf.dimension(function (d) { return -1 * (+(d[key])) }) | |
var sorter = new BucketSort(+(desc.top(1)[0][key]), +(asc.top(1)[0][key]), 100) | |
var buckets = xf.dimension(function (d) { return sorter.getBucket(+d[key]) }) | |
var group = buckets.group() | |
return { | |
asc: asc, | |
desc: desc, | |
buckets: buckets, | |
group: group | |
} | |
} | |
function BucketSort(min, max, size) { | |
var me = this | |
var step = (max - min) / size | |
me.buckets = [] | |
_.times(size, function() { | |
me.buckets.push(min) | |
min += step | |
}) | |
} | |
BucketSort.prototype.getBucket = function(value) { | |
var idx = _.sortedIndex(this.buckets, value) | |
return this.buckets[idx] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note: this isnt tuned for performance, but any perf tips are welcome!