UNPKG

d2b

Version:

A d3-based reusable chart library.

23 lines (19 loc) 596 B
import functor from '../util/functor'; import number from '../util/number'; function mean(arr, value, weight) { var totalWeight = 0, contribution = 0; weight = functor(weight || 1); value = functor(value || function(d){return d;}); arr .filter(function(a){ return !isNaN(number(weight(a))) && !isNaN(number(value(a))); }) .forEach(function(item){ var w = weight(item), v = value(item); totalWeight += w; contribution += v * w; }); if(arr.length && totalWeight) return contribution / totalWeight; } mean.tendancy = 'mean'; export default mean;