Skip to content

Instantly share code, notes, and snippets.

@radio412
Created March 27, 2015 21:04
Show Gist options
  • Select an option

  • Save radio412/6f0f1d0087c956742f49 to your computer and use it in GitHub Desktop.

Select an option

Save radio412/6f0f1d0087c956742f49 to your computer and use it in GitHub Desktop.
Applying a non-linear data pattern to a measure
establishCurve = function(curveArray,sumTotal,inverse){
if(inverse){
var maxVal = Math.max.apply(Math, curveArray);
var minVal = Math.min.apply(Math, curveArray);
curveArray = curveArray.map(function (x) {
return (maxVal-x)+minVal;
});
}
var curveTotal = curveArray.reduce(function(a, b) {
return a + b;
});
var data = [];
var sum = 0;
for(var i = 0; i< curveArray.length; i++){
var perc = curveArray[i]/curveTotal;
data.push(sumTotal*perc);
sum += sumTotal*perc;
}
return data;
}
//example curve
//monthly sample data applied to actual total population:
console.log(establishCurve([9,9,12,17,22,27,29,29,25,20,15,10], 50000000, false));
//The curve can be inverted with the last parameter:
console.log(establishCurve([9,9,12,17,22,27,29,29,25,20,15,10], 50000000, true));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment