-
Notifications
You must be signed in to change notification settings - Fork 23
/
annotation-series.js
45 lines (36 loc) · 1 KB
/
annotation-series.js
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
// Wraps the most awesome d3-annotation component (https://d3-annotation.susielu.com/)
// so that it can be rendered as a series
export const seriesSvgAnnotation = () => {
// the underlying component that we are wrapping
const d3Annotation = d3.annotation();
let xScale = d3.scaleLinear();
let yScale = d3.scaleLinear();
const join = fc.dataJoin("g", "annotation");
const series = selection => {
selection.each((data, index, group) => {
const projectedData = data.map(d => ({
...d,
x: xScale(d.x),
y: yScale(d.y)
}));
d3Annotation.annotations(projectedData);
join(d3.select(group[index]), projectedData).call(d3Annotation);
});
};
series.xScale = (...args) => {
if (!args.length) {
return xScale;
}
xScale = args[0];
return series;
};
series.yScale = (...args) => {
if (!args.length) {
return yScale;
}
yScale = args[0];
return series;
};
fc.rebindAll(series, d3Annotation);
return series;
};