forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.js
113 lines (108 loc) · 3.23 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
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
import {geoPath, pathRound as path} from "d3";
import {create} from "../context.js";
import {curveAuto, PathCurve} from "../curve.js";
import {Mark} from "../mark.js";
import {coerceNumbers} from "../options.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 = PathCurve(curve, tension);
markers(this, options);
}
project(channels, values, context) {
// For the auto curve, projection is handled at render.
if (this.curve !== curveAuto) {
super.project(channels, values, context);
}
}
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, dimensions, context)
.call(applyTransform, this, scales)
.call((g) =>
g
.selectAll()
.data(index)
.enter()
.append("path")
.call(applyDirectStyles, this)
.attr(
"d",
curve === curveAuto && context.projection
? sphereLink(context.projection, X1, Y1, X2, Y2)
: (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();
}
}
function sphereLink(projection, X1, Y1, X2, Y2) {
const path = geoPath(projection);
X1 = coerceNumbers(X1);
Y1 = coerceNumbers(Y1);
X2 = coerceNumbers(X2);
Y2 = coerceNumbers(Y2);
return (i) =>
path({
type: "LineString",
coordinates: [
[X1[i], Y1[i]],
[X2[i], Y2[i]]
]
});
}
/** @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];
}