-
Notifications
You must be signed in to change notification settings - Fork 56
/
component.ts
143 lines (126 loc) · 3.98 KB
/
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { service } from '@ember-decorators/service';
import Component from '@ember/component';
import { task, TaskInstance } from 'ember-concurrency';
import I18n from 'ember-i18n/services/i18n';
import KeenDataviz from 'keen-dataviz';
import { Moment } from 'moment';
import { layout } from 'ember-osf-web/decorators/component';
import Node from 'ember-osf-web/models/node';
import AnalyticsService from 'ember-osf-web/services/analytics';
import { ChartSpec } from 'analytics-page/components/analytics-charts/component';
import KeenService from 'analytics-page/services/keen';
import styles from './styles';
import template from './template';
enum OverlayReason {
Error,
Loading,
}
@layout(template, styles)
export default class ChartWrapper extends Component {
@service keen!: KeenService;
@service i18n!: I18n;
@service analytics!: AnalyticsService;
// Required arguments
chartSpec!: ChartSpec;
chartEnabled!: boolean;
nodeTaskInstance!: TaskInstance<Node>;
startDate!: Moment;
endDate!: Moment;
// Private properties
chart!: KeenDataviz; // set in didInsertElement
overlayShown: boolean = true;
keenError: boolean = false;
loading: boolean = false;
loadKeen = task(function *(this: ChartWrapper) {
this.showOverlay(OverlayReason.Loading);
const node = yield this.nodeTaskInstance;
try {
let data = yield this.keen.queryNode(
node,
this.startDate,
this.endDate,
this.chartSpec.keenQueryType,
this.chartSpec.keenQueryOptions,
);
if (this.chartSpec.processData) {
data = this.chartSpec.processData(data, this.i18n, node);
}
this.hideOverlay();
this.chart.data(data).render();
} catch (e) {
this.showOverlay(OverlayReason.Error);
throw e;
}
}).restartable();
didInsertElement(this: ChartWrapper) {
this.chart = new KeenDataviz()
.el(`#${this.elementId} .${styles.Chart}`)
.title(' '); // Prevent keen-dataviz from adding a default title
this.initSkeletonChart();
if (this.chartEnabled) {
this.get('loadKeen').perform();
}
}
didUpdateAttrs(this: ChartWrapper) {
if (this.chartEnabled) {
this.get('loadKeen').perform();
}
}
showOverlay(this: ChartWrapper, reason?: OverlayReason) {
this.set('keenError', (reason === OverlayReason.Error));
this.set('loading', (reason === OverlayReason.Loading));
this.set('overlayShown', true);
this.chart.chartOptions({
interaction: {
enabled: false,
},
});
}
hideOverlay(this: ChartWrapper) {
this.set('overlayShown', false);
this.clearSkeletonChart();
this.chart.chartOptions({
interaction: {
enabled: true,
},
});
}
initSkeletonChart(this: ChartWrapper) {
this.chartSpec.configureChart(this.chart, this.i18n);
this.chart.chartOptions({
data: {
labels: false,
},
pie: {
label: {
show: false,
},
},
axis: {
x: {
tick: {
format: () => '',
},
},
y: {
tick: {
format: () => '',
},
},
},
});
if (this.chartSpec.fakeData) {
this.chart.data(this.chartSpec.fakeData());
}
this.showOverlay();
this.chart.render();
}
clearSkeletonChart(this: ChartWrapper) {
this.chart.chartOptions({
data: {},
pie: {},
axis: {},
});
this.chartSpec.configureChart(this.chart, this.i18n);
}
}