import { Component, Input, Output, EventEmitter, ElementRef, OnChanges, SimpleChanges, ChangeDetectionStrategy } from '@angular/core'; import d3 from '../d3'; import { invertColor } from '../utils/color-utils'; @Component({ selector: 'g[ngx-charts-tree-map-cell]', template: ` {{label}} `, changeDetection: ChangeDetectionStrategy.OnPush }) export class TreeMapCellComponent implements OnChanges { @Input() fill; @Input() x; @Input() y; @Input() width; @Input() height; @Input() label; @Input() value; @Input() valueType; @Output() select = new EventEmitter(); element: HTMLElement; transform: string; formattedValue: string; // todo check string or number ? initialized: boolean = false; constructor(element: ElementRef) { this.element = element.nativeElement; } ngOnChanges(changes: SimpleChanges): void { this.update(); } update(): void { if (this.initialized) { this.animateToCurrentForm(); } else { this.loadAnimation(); this.initialized = true; } } loadAnimation(): void { let node = d3.select(this.element).select('.cell'); node .attr('opacity', 0) .attr('x', this.x) .attr('y', this.y); this.animateToCurrentForm(); } getTextColor(): string { return invertColor(this.fill); } animateToCurrentForm(): void { let node = d3.select(this.element).select('.cell'); node.transition().duration(750) .attr('opacity', 1) .attr('x', this.x) .attr('y', this.y) .attr('width', this.width) .attr('height', this.height); } onClick(): void { this.select.emit({ name: this.label, value: this.value }); } }