@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
73 lines • 2.09 kB
JavaScript
;
// Robert Penner's easeOutExpo
function easeOutExpo(t, b, c, d) {
return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
}
/**
* Counts from a number to the end incrementally.
*
* @export
* @param {any} countFrom
* @param {any} countTo
* @param {any} countDecimals
* @param {any} countDuration
* @param {any} callback
* @returns
*/
function count(countFrom, countTo, countDecimals, countDuration, callback) {
var startVal = Number(countFrom);
var endVal = Number(countTo);
var countDown = (startVal > endVal);
var decimals = Math.max(0, countDecimals);
var dec = Math.pow(10, decimals);
var duration = Number(countDuration) * 1000;
var startTime;
function runCount(timestamp) {
var frameVal;
var progress = timestamp - startTime;
if (countDown) {
frameVal = startVal - easeOutExpo(progress, 0, startVal - endVal, duration);
}
else {
frameVal = easeOutExpo(progress, startVal, endVal - startVal, duration);
}
if (countDown) {
frameVal = (frameVal < endVal) ? endVal : frameVal;
}
else {
frameVal = (frameVal > endVal) ? endVal : frameVal;
}
frameVal = Math.round(frameVal * dec) / dec;
var tick = progress < duration;
callback({
value: frameVal,
progress: progress,
timestamp: timestamp,
finished: !tick
});
if (tick) {
return requestAnimationFrame(function (val) { return runCount(val); });
}
}
return requestAnimationFrame(function (timestamp) {
startTime = timestamp;
return runCount(timestamp);
});
}
exports.count = count;
/**
* Determine decimals places
*
* @export
* @param {any} countTo
* @returns
*/
function decimalChecker(countTo) {
var endVal = Number(countTo);
if (endVal % 1 !== 0 && Math.abs(endVal) <= 10) {
return 2;
}
return 0;
}
exports.decimalChecker = decimalChecker;
//# sourceMappingURL=count.helper.js.map