import {
Component,
Input,
ChangeDetectionStrategy,
Output,
EventEmitter,
SimpleChanges,
OnChanges,
ChangeDetectorRef,
NgZone
} from '@angular/core';
import { formatLabel } from '../label.helper';
@Component({
selector: 'ngx-charts-legend',
template: `
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class LegendComponent implements OnChanges {
@Input() data;
@Input() title;
@Input() colors;
@Input() height;
@Input() width;
@Input() activeEntries;
@Output() labelClick: EventEmitter = new EventEmitter();
@Output() labelActivate: EventEmitter = new EventEmitter();
@Output() labelDeactivate: EventEmitter = new EventEmitter();
legendEntries: any[] = [];
constructor(private cd: ChangeDetectorRef, private zone: NgZone) { }
ngOnChanges(changes: SimpleChanges): void {
this.update();
}
update(): void {
this.zone.run(() => {
this.cd.markForCheck();
this.legendEntries = this.getLegendEntries();
});
}
getLegendEntries(): any[] {
let items = [];
for(const label of this.data) {
const formattedLabel = formatLabel(label);
let idx = items.findIndex((i) => {
return i.label === formattedLabel;
});
if (idx === -1) {
items.push({
label,
formattedLabel,
color: this.colors.getColor(label)
});
}
}
return items;
}
isActive(entry): boolean {
if(!this.activeEntries) return false;
let item = this.activeEntries.find(d => {
return entry.label === d.name;
});
return item !== undefined;
}
activate(item) {
this.zone.run(() => {
this.labelActivate.emit(item);
});
}
deactivate(item) {
this.zone.run(() => {
this.labelDeactivate.emit(item);
});
}
trackBy(index, item): string {
return item.label;
}
}