-
Notifications
You must be signed in to change notification settings - Fork 4
/
line-chart.component.ts
97 lines (83 loc) · 3.09 KB
/
line-chart.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Component, ElementRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild } from '@angular/core';
import { BehaviorSubject, Subject } from 'rxjs';
import { LegendItemStyle, LineChartD3 } from '../../d3/line-chart.d3';
import { RenderOptions } from '../../d3/xy-chart.d3';
import { GUIDE_DOGE, t } from '../../i18n';
import { formatX, formatY } from '../../utils/formatters';
import { A11yPlaceholderDirective } from '../../directives/a11y-placeholder/a11y-placeholder.directive';
import { DAY } from '../../utils/timeUnits';
import { LineChartMeta } from '../../datasets/metas/line-chart.meta';
import { TimeSeriesDatum } from '../../datasets/queries/time-series.query';
import { TimeSeriesPoint } from '../../datasets/metas/types';
import { A11yHostComponent } from '../a11y-host/a11y-host.component';
import { SummarizationMeta } from '../../services/summarization/types';
export type LineChartDatum = TimeSeriesDatum<LegendItemStyle>;
@Component({
selector: 'app-line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.scss'],
})
export class LineChartComponent extends A11yHostComponent implements
RenderOptions<TimeSeriesPoint, LineChartDatum>, OnChanges, OnInit, OnDestroy {
@ViewChild(A11yPlaceholderDirective, { static: true }) a11yPlaceholder: A11yPlaceholderDirective;
@Input() endDate = new Date();
@Input() startDate = new Date(this.endDate.getTime() - 30 * DAY);
@Input() meta: LineChartMeta;
@Input() height = 500;
@Input() width = 800;
formatX = formatX;
formatY = formatY;
data$ = new BehaviorSubject<LineChartDatum[]>([]);
summarizationMetas$ = new BehaviorSubject<SummarizationMeta[]>([]);
activePoint$ = new BehaviorSubject<TimeSeriesPoint | null>(null);
lineChartD3: LineChartD3;
private destroy$ = new Subject();
constructor(
public elementRef: ElementRef<HTMLElement>,
) {
super(elementRef);
this.lineChartD3 = new LineChartD3(this);
}
get data() {
return this.data$.value;
}
get activePoint() {
return this.activePoint$.value;
}
set activePoint(activePoint) {
this.activePoint$.next(activePoint);
}
get VISUALIZATION() {
return t(GUIDE_DOGE.VISUALIZATION);
}
get legendItems() {
if (!this.activePoint) {
return [];
}
const xTime = this.activePoint.x.getTime();
return this.data
.map(datum => ({
label: datum.label,
activePoint: datum.points.find(point => point.x.getTime() === xTime),
}))
.filter((datum): datum is { label: string, activePoint: TimeSeriesPoint } => datum.activePoint !== undefined);
}
ngOnInit() {
this.lineChartD3.render();
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
this.lineChartD3.clear();
}
ngOnChanges(changes: SimpleChanges): void {
if (['startDate', 'endDate', 'meta'].some(key => key in changes)) {
const data = this.meta.queryData({
range: [this.startDate, this.endDate],
});
this.data$.next(data);
this.summarizationMetas$.next(this.meta.summarizationMetas ?? []);
this.activePoint$.next(null);
}
}
}