@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
99 lines (79 loc) • 2.2 kB
text/typescript
import {
Component, Input, Output, EventEmitter,
ChangeDetectorRef, NgZone, OnDestroy, ElementRef
} from '@angular/core';
import { count, decimalChecker } from './count.helper';
/**
* Count up component
*
* Loosely inspired by:
* - https://github.com/izupet/angular2-counto
* - https://inorganik.github.io/countUp.js/
*
* @export
* @class CountUpDirective
*/
export class CountUpDirective implements OnDestroy {
countDuration: number = 1;
countPrefix: string = '';
countSuffix: string = '';
set countDecimals(val: number) {
this._countDecimals = val;
}
get countDecimals(): number {
if(this._countDecimals) return this._countDecimals;
return decimalChecker(this.countTo);
}
set countTo(val) {
this._countTo = parseFloat(val);
this.start();
}
get countTo(): any {
return this._countTo;
}
set countFrom(val) {
this._countFrom = parseFloat(val);
this.start();
}
get countFrom(): any {
return this._countFrom;
}
countChange = new EventEmitter();
countFinish = new EventEmitter();
nativeElement: any;
private value: any = '';
private animationReq: any;
private _countDecimals: number = 0;
private _countTo: number = 0;
private _countFrom: number = 0;
constructor(private cd: ChangeDetectorRef, private zone: NgZone, element: ElementRef) {
this.nativeElement = element.nativeElement;
}
ngOnDestroy(): void {
cancelAnimationFrame(this.animationReq);
}
start(): void {
cancelAnimationFrame(this.animationReq);
const callback = ({ value, progress, finished }) => {
this.zone.run(() => {
this.value = `${this.countPrefix}${value.toLocaleString()}${this.countSuffix}`;
this.cd.markForCheck();
if(!finished) this.countChange.emit({ value, progress });
if(finished) this.countFinish.emit({ value, progress });
});
};
this.animationReq = count(
this.countFrom,
this.countTo,
this.countDecimals,
this.countDuration,
callback);
}
}