import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core'; import { calculateViewDimensions, ViewDimensions } from '../common/view-dimensions.helper'; import { ColorHelper } from '../common/color.helper'; import { BaseChartComponent } from '../common/base-chart.component'; @Component({ selector: 'ngx-charts-advanced-pie-chart', template: `
`, changeDetection: ChangeDetectionStrategy.OnPush }) export class AdvancedPieChartComponent extends BaseChartComponent { @Input() gradient: boolean; @Input() activeEntries: any[] = []; @Output() activate: EventEmitter = new EventEmitter(); @Output() deactivate: EventEmitter = new EventEmitter(); data: any; dims: ViewDimensions; domain: any[]; outerRadius: number; innerRadius: number; transform: string; colors: ColorHelper; legendWidth: number; margin = [20, 20, 20, 20]; update(): void { super.update(); this.zone.run(() => { this.dims = calculateViewDimensions({ width: this.width * 4 / 12.0, height: this.height, margins: this.margin }); this.domain = this.getDomain(); this.setColors(); let xOffset = this.dims.width / 2; let yOffset = this.margin[0] + this.dims.height / 2; this.legendWidth = this.width - this.dims.width - this.margin[1]; this.outerRadius = Math.min(this.dims.width, this.dims.height) / 2.5; this.innerRadius = this.outerRadius * 0.75; this.transform = `translate(${xOffset} , ${yOffset})`; }); } getDomain(): any[] { return this.results.map(d => d.name); } onClick(data) { this.select.emit(data); } setColors(): void { this.colors = new ColorHelper(this.scheme, 'ordinal', this.domain, this.customColors); } onActivate(event): void { if(this.activeEntries.indexOf(event) > -1) return; this.activeEntries = [ event, ...this.activeEntries ]; this.activate.emit({ value: event, entries: this.activeEntries }); } onDeactivate(event): void { const idx = this.activeEntries.indexOf(event); this.activeEntries.splice(idx, 1); this.activeEntries = [...this.activeEntries]; this.deactivate.emit({ value: event, entries: this.activeEntries }); } }