UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular

70 lines 2.02 kB
// 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 */ export 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); }); } /** * Determine decimals places * * @export * @param {any} countTo * @returns */ export function decimalChecker(countTo) { var endVal = Number(countTo); if (endVal % 1 !== 0 && Math.abs(endVal) <= 10) { return 2; } return 0; } //# sourceMappingURL=count.helper.js.map