@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
116 lines (100 loc) • 2.6 kB
text/typescript
import {
Component, Input, Output, EventEmitter, ElementRef,
OnChanges, SimpleChanges, ChangeDetectionStrategy
} from '@angular/core';
import d3 from '../d3';
import { invertColor } from '../utils/color-utils';
export class TreeMapCellComponent implements OnChanges {
fill;
x;
y;
width;
height;
label;
value;
valueType;
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
});
}
}