@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
112 lines (99 loc) • 2.82 kB
text/typescript
import {
Component,
Input,
Output,
EventEmitter,
ChangeDetectionStrategy,
SimpleChanges,
OnChanges
} from '@angular/core';
import { trimLabel } from '../trim-label.helper';
import { formatLabel } from '../label.helper';
export class AdvancedLegendComponent implements OnChanges {
width: number;
data;
colors;
select: EventEmitter<any> = new EventEmitter();
activate: EventEmitter<any> = new EventEmitter();
deactivate: EventEmitter<any> = new EventEmitter();
legendItems: any[] = [];
totalLabel: string = 'total';
total: number;
roundedTotal: number;
ngOnChanges(changes: SimpleChanges): void {
this.update();
}
getTotal(): number {
return this.data
.map(d => d.value)
.reduce((sum, d) => { return sum + d; }, 0);
}
update(): void {
this.total = this.getTotal();
this.roundedTotal = this.total;
this.legendItems = this.getLegendItems();
}
getLegendItems(): any {
return this.data.map((d, index) => {
const label = formatLabel(d.name);
const value = d.value;
const percentage = value / this.total * 100;
const color = this.colors.getColor(label);
return {
value,
color,
label: trimLabel(label, 20),
originalLabel: d.name,
percentage
};
});
}
trackBy(item) {
return item.formattedLabel;
}
}