@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
115 lines (100 loc) • 2.42 kB
text/typescript
import {
Component,
Input,
Output,
SimpleChanges,
EventEmitter,
ElementRef,
OnChanges,
ChangeDetectionStrategy,
} from '@angular/core';
import { id } from '../utils/id';
import d3 from '../d3';
export class AreaComponent implements OnChanges {
data;
path;
startingPath;
fill;
opacity = 1;
startOpacity = 0.5;
endOpacity = 1;
activeLabel;
gradient: boolean = false;
stops: any[];
select = new EventEmitter();
element: HTMLElement;
gradientId: string;
gradientFill: string;
areaPath: string;
initialized: boolean = false;
gradientStops: any[];
hasGradient: boolean = false;
constructor(element: ElementRef) {
this.element = element.nativeElement;
}
ngOnChanges(changes: SimpleChanges): void {
if (!this.initialized) {
this.loadAnimation();
this.initialized = true;
} else {
this.update();
}
}
update(): void {
let pageUrl = window.location.href;
this.gradientId = 'grad' + id().toString();
this.gradientFill = `url(${pageUrl}#${this.gradientId})`;
if (this.gradient || this.stops) {
this.gradientStops = this.getGradient();
this.hasGradient = true;
} else {
this.hasGradient = false;
}
this.animateToCurrentForm();
}
loadAnimation(): void {
this.areaPath = this.startingPath;
setTimeout(this.update.bind(this), 100);
}
animateToCurrentForm(): void {
let node = d3.select(this.element).select('.area');
node.transition().duration(750)
.attr('d', this.path);
}
getGradient() {
if (this.stops) {
return this.stops;
}
return [
{
offset: 0,
color: this.fill,
opacity: this.startOpacity
},
{
offset: 100,
color: this.fill,
opacity: this.endOpacity
}];
}
}