fix(transform): stattistics

This commit is contained in:
thinkinggis 2019-02-26 19:32:09 +08:00
parent e611930470
commit 374554037c
4 changed files with 83 additions and 11 deletions

View File

@ -10,7 +10,8 @@ module.exports = {
include: [ include: [
'src/**/*.js', 'src/**/*.js',
'test/**/*.js', 'test/**/*.js',
'node_modules/three/**/*.js' 'node_modules/three/**/*.js',
'node_modules/simple-statistics/src/*.js'
], ],
exclude: [ exclude: [
'node_modules/@babel/**/*.js' 'node_modules/@babel/**/*.js'

View File

@ -27,7 +27,10 @@ export function pointToHexbin(data, option) {
const columns = getColumn(hex, option.field); const columns = getColumn(hex, option.field);
hex[option.method] = statistics[option.method](columns); hex[option.method] = statistics[option.method](columns);
} }
const item = {};
item[option.method] = hex[option.method];
return { return {
...item,
coordinates: unProjectFlat([ hex.x, hex.y ]), coordinates: unProjectFlat([ hex.x, hex.y ]),
id: index + 1 id: index + 1
}; };

View File

@ -1,10 +1,76 @@
/* 'max', 'mean', 'median', 'min', 'mode', 'product', 'standardDeviation', function max(x) {
* 'sum', 'sumSimple', 'variance', 'count', 'distinct' if (x.length === 0) {
*/ throw new Error('max requires at least one data point');
export { default as min } from 'simple-statistics/src/min'; }
export { default as max } from 'simple-statistics/src/max';
export { default as mean } from 'simple-statistics/src/mean';
export { default as sum } from 'simple-statistics/src/sum';
export { default as median } from 'simple-statistics/src/median';
export { default as standardDeviation } from 'simple-statistics/src/standard_deviation';
let value = x[0];
for (let i = 1; i < x.length; i++) {
// On the first iteration of this loop, max is
// undefined and is thus made the maximum element in the array
if (x[i] > value) {
value = x[i];
}
}
return value;
}
function min(x) {
if (x.length === 0) {
throw new Error('min requires at least one data point');
}
let value = x[0];
for (let i = 1; i < x.length; i++) {
// On the first iteration of this loop, min is
// undefined and is thus made the minimum element in the array
if (x[i] < value) {
value = x[i];
}
}
return value;
}
function sum(x) {
// If the array is empty, we needn't bother computing its sum
if (x.length === 0) {
return 0;
}
// Initializing the sum as the first number in the array
let sum = x[0];
// Keeping track of the floating-point error correction
let correction = 0;
let transition;
for (let i = 1; i < x.length; i++) {
transition = sum + x[i];
// Here we need to update the correction in a different fashion
// if the new absolute value is greater than the absolute sum
if (Math.abs(sum) >= Math.abs(x[i])) {
correction += sum - transition + x[i];
} else {
correction += x[i] - transition + sum;
}
sum = transition;
}
// Returning the corrected sum
return sum + correction;
}
function mean(x) {
if (x.length === 0) {
throw new Error('mean requires at least one data point');
}
return sum(x) / x.length;
}
export {
sum,
max,
min,
mean
};

View File

@ -16,6 +16,8 @@ describe('hexagon Test', function() {
const data = { const data = {
dataArray dataArray
}; };
const hexgonGrid = pointToHexbin(data, { size: 100, field: 'count', method: 'sum' }); const hexgonGrid = pointToHexbin(data, { size: 100, field: 'v', method: 'sum' });
expect(hexgonGrid.dataArray.length).eql(567);
console.log(hexgonGrid);
}); });
}); });