forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.js
85 lines (81 loc) · 2.44 KB
/
link.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
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
import {path} from "d3";
import {create} from "../context.js";
import {Curve} from "../curve.js";
import {Mark} from "../plot.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
import {markers, applyMarkers} from "./marker.js";
const defaults = {
ariaLabel: "link",
fill: "none",
stroke: "currentColor",
strokeMiterlimit: 1
};
export class Link extends Mark {
constructor(data, options = {}) {
const {x1, y1, x2, y2, curve, tension} = options;
super(
data,
{
x1: {value: x1, scale: "x"},
y1: {value: y1, scale: "y"},
x2: {value: x2, scale: "x", optional: true},
y2: {value: y2, scale: "y", optional: true}
},
options,
defaults
);
this.curve = Curve(curve, tension);
markers(this, options);
}
render(index, scales, channels, dimensions, context) {
const {x1: X1, y1: Y1, x2: X2 = X1, y2: Y2 = Y1} = channels;
const {curve} = this;
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions, context)
.call(applyTransform, this, scales)
.call((g) =>
g
.selectAll()
.data(index)
.enter()
.append("path")
.call(applyDirectStyles, this)
.attr("d", (i) => {
const p = path();
const c = curve(p);
c.lineStart();
c.point(X1[i], Y1[i]);
c.point(X2[i], Y2[i]);
c.lineEnd();
return p;
})
.call(applyChannelStyles, this, channels)
.call(applyMarkers, this, channels)
)
.node();
}
}
/** @jsdoc link */
export function link(data, options = {}) {
let {x, x1, x2, y, y1, y2, ...remainingOptions} = options;
[x1, x2] = maybeSameValue(x, x1, x2);
[y1, y2] = maybeSameValue(y, y1, y2);
return new Link(data, {...remainingOptions, x1, x2, y1, y2});
}
// If x1 and x2 are specified, return them as {x1, x2}.
// If x and x1 and specified, or x and x2 are specified, return them as {x1, x2}.
// If only x, x1, or x2 are specified, return it as {x1}.
export function maybeSameValue(x, x1, x2) {
if (x === undefined) {
if (x1 === undefined) {
if (x2 !== undefined) return [x2];
} else {
if (x2 === undefined) return [x1];
}
} else if (x1 === undefined) {
return x2 === undefined ? [x] : [x, x2];
} else if (x2 === undefined) {
return [x, x1];
}
return [x1, x2];
}