Sampling
by IPWright83
JavaScript
const numbers = [0, 350, 500, 900];
const days = [new Date(2018,0,1), new Date(2018,0,2), new Date(2018,0,3), new Date(2018,0,4), new Date(2018,0,5)];
const months = [new Date(2018,0,1), new Date(2018,1,1), new Date(2018,2,1), new Date(2018,3,1)];
const years = [new Date(2016, 0, 1), new Date(2017, 0, 1), new Date(2018, 0, 1)];
const createNumericalSpread = (data) => {
const extent = d3.extent(data);
const min = extent[0];
const max = extent[1];
const bucketSize = findNumericalSampleSize(data);
const values = [];
let last = min;
while(last <= max) {
values.push(last);
last += bucketSize;
}
return values;
}
d3.scaleLinearBand = function(linear) {
linear = linear || d3.scaleLinear();
let _data = [];
let _domain = [];
let _resolution = null;
let _step = null;
let _bandwidth = null;
let _paddingInner = 0;
let _paddingOuter = 0;
const findMinimumDiff = (data) => {
let minDiff = Number.MAX_VALUE;
// Handle the case where there is no data
if (data.length === 0) return 0;
// Find the minimum difference which will
// determine the size of the samples
for(let i = 1; i < data.length; i++) {
const diff = data[i] - data[i-1];
if (diff < minDiff) {
minDiff = diff;
}
}
return minDiff;
};
const calculateNumericalResolution = (data) => {
const minDiff = findMinimumDiff(data);
// Start from the largest sample and find which
// one best accomdates the minDiff we calculated
let bucket = Math.pow(10, 308);
while (bucket > minDiff) {
bucket /= 10;
}
return Math.round(bucket);
};
const calculateDateResolution = (data) => {
const minDiff = findMinimumDiff(data);
if (minDiff >= 31536000000) return "YEARS";
if (minDiff >= 2332800000 && minDiff <=...