novo-elements
Version:
Bullhorn's NOVO Element Repository for Angular 2
76 lines (70 loc) • 2.97 kB
text/typescript
// NG2
import { Component, ElementRef, Input, OnInit, OnChanges, SimpleChanges } from '@angular/core';
export class CardDonutChartElement implements OnInit, OnChanges {
value: number;
label;
color;
chartFillMax;
uid;
isChartDrawing;
constructor(private element: ElementRef) {
// Geometric number that represents 100% of the chart area
this.chartFillMax = 629;
// Unique ID for instance
this.uid = Math.round(Math.random() * 1000);
// Prevent Collision
this.isChartDrawing = false;
}
ngOnChanges(changes?: SimpleChanges) {
if (!this.isChartDrawing) {
this.drawChart();
}
}
ngOnInit() {
if (!this.isChartDrawing) {
this.drawChart();
}
this.color = this.color || '#662255';
}
drawChart() {
this.isChartDrawing = true;
setTimeout(() => {
let chartContainer = this.element.nativeElement.querySelector(`#chart-percent-${this.uid}`);
let fillElements = this.element.nativeElement.querySelectorAll('.fill');
for (let i = 0; i < fillElements.length; i++) {
fillElements[i].setAttribute('stroke', this.color);
}
// Set fill amount
this.element.nativeElement.querySelector(`#chart-fill-${this.uid}`).setAttribute('stroke-dashoffset', (this.chartFillMax * this.value).toString());
// Set Text Color
chartContainer.style.color = this.color;
// Set percentage for chart
chartContainer.setAttribute('data-percent', `${(Math.round(this.value * 100)).toString()}%`);
// Set Label
chartContainer.setAttribute('data-name', this.label);
this.isChartDrawing = false;
});
}
}