@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
107 lines (89 loc) • 2.24 kB
text/typescript
import {
Component,
Input,
Output,
EventEmitter,
ElementRef,
OnChanges,
SimpleChanges,
ChangeDetectionStrategy
} from '@angular/core';
import d3 from '../d3';
export class PieGridSeriesComponent implements OnChanges {
colors;
data;
innerRadius = 70;
outerRadius = 80;
select = new EventEmitter();
element: HTMLElement;
layout: any;
arcs: any;
constructor(element: ElementRef) {
this.element = element.nativeElement;
}
ngOnChanges(changes: SimpleChanges): void {
this.update();
}
update(): void {
this.layout = d3.pie()
.value((d) => d.data.value).sort(null);
this.arcs = this.getArcs();
}
getArcs(): any[] {
return this.layout(this.data).map((arc, index) => {
let label = arc.data.data.name;
let other = arc.data.data.other;
if (index === 0) {
arc.startAngle = 0;
}
let color = this.colors(label);
return {
data: arc.data.data,
class: 'arc ' + 'arc' + index,
fill: color,
startAngle: other ? 0 : arc.startAngle,
endAngle: arc.endAngle,
animate: !other,
pointerEvents: !other
};
});
}
onClick(data): void {
this.select.emit({
name: this.data[0].data.name,
value: this.data[0].data.value
});
}
trackBy(index, item): string {
return item.data.name;
}
label(arc): string {
return arc.data.name;
}
color(arc): any {
return this.colors(this.label(arc));
}
}