|
| 1 | +import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; |
| 2 | +import { isArray } from 'lodash-es'; |
| 3 | + |
| 4 | +@Component({ |
| 5 | + selector: 'app-progress-chart', |
| 6 | + templateUrl: './progress-chart.component.html', |
| 7 | + styleUrls: ['./progress-chart.component.scss'], |
| 8 | +}) |
| 9 | +export class ProgressChartComponent implements OnInit { |
| 10 | + @Input() chartData = { |
| 11 | + datasets: [], |
| 12 | + color: '', |
| 13 | + disablePercentage: false, |
| 14 | + clickable: false, |
| 15 | + }; |
| 16 | + @Output() widgetOutput: EventEmitter<any> = new EventEmitter(); |
| 17 | + |
| 18 | + loading = true; |
| 19 | + color: string; |
| 20 | + disablePercentage: boolean; |
| 21 | + |
| 22 | + ngOnInit(): void { |
| 23 | + if (!isArray(this.chartData?.datasets)) { |
| 24 | + this.chartData.datasets = [this.chartData.datasets]; |
| 25 | + } |
| 26 | + |
| 27 | + this.chartData.datasets.forEach((data) => { |
| 28 | + if (data.hasOwnProperty('progress')) { |
| 29 | + data.progress = +data.progress; |
| 30 | + } else { |
| 31 | + let val = +data.value || 0; |
| 32 | + let max = +data.max || 100; |
| 33 | + data.progress = Math.round((val / max) * 10000) / 100; |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + this.color = this.chartData.color || '#488aff'; |
| 38 | + this.disablePercentage = this.chartData.disablePercentage || false; |
| 39 | + this.loading = false; |
| 40 | + } |
| 41 | + |
| 42 | + whichWidth(progress) { |
| 43 | + return progress > 0 ? Math.max(progress, 15) + '%' : 0; |
| 44 | + } |
| 45 | + |
| 46 | + itemClicked = (item) => { |
| 47 | + if (this.chartData.clickable) { |
| 48 | + this.widgetOutput.emit({ |
| 49 | + action: 'clicked', |
| 50 | + data: item, |
| 51 | + widget: 'progress-chart', |
| 52 | + }); |
| 53 | + } |
| 54 | + }; |
| 55 | +} |
0 commit comments