From 374554037c764867d63f31bbbcb22ec522d14c51 Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Tue, 26 Feb 2019 19:32:09 +0800 Subject: [PATCH] fix(transform): stattistics --- .torch.compile.opts.js | 3 +- src/source/transform/hexagon.js | 3 + src/source/transform/statistics.js | 84 ++++++++++++++++++++++++--- test/unit/source/transfrom/hexagon.js | 4 +- 4 files changed, 83 insertions(+), 11 deletions(-) diff --git a/.torch.compile.opts.js b/.torch.compile.opts.js index b65d4cd02f..c2e828ddc9 100755 --- a/.torch.compile.opts.js +++ b/.torch.compile.opts.js @@ -10,7 +10,8 @@ module.exports = { include: [ 'src/**/*.js', 'test/**/*.js', - 'node_modules/three/**/*.js' + 'node_modules/three/**/*.js', + 'node_modules/simple-statistics/src/*.js' ], exclude: [ 'node_modules/@babel/**/*.js' diff --git a/src/source/transform/hexagon.js b/src/source/transform/hexagon.js index df796c7450..f0aa9692f1 100644 --- a/src/source/transform/hexagon.js +++ b/src/source/transform/hexagon.js @@ -27,7 +27,10 @@ export function pointToHexbin(data, option) { const columns = getColumn(hex, option.field); hex[option.method] = statistics[option.method](columns); } + const item = {}; + item[option.method] = hex[option.method]; return { + ...item, coordinates: unProjectFlat([ hex.x, hex.y ]), id: index + 1 }; diff --git a/src/source/transform/statistics.js b/src/source/transform/statistics.js index 2a7a317846..de599d2d41 100644 --- a/src/source/transform/statistics.js +++ b/src/source/transform/statistics.js @@ -1,10 +1,76 @@ -/* 支持 'max', 'mean', 'median', 'min', 'mode', 'product', 'standardDeviation', - * 'sum', 'sumSimple', 'variance', 'count', 'distinct' - */ -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'; +function max(x) { + if (x.length === 0) { + throw new Error('max 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, 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 +}; diff --git a/test/unit/source/transfrom/hexagon.js b/test/unit/source/transfrom/hexagon.js index b0877e7b80..aad4edb018 100644 --- a/test/unit/source/transfrom/hexagon.js +++ b/test/unit/source/transfrom/hexagon.js @@ -16,6 +16,8 @@ describe('hexagon Test', function() { const data = { 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); }); });