forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.js
69 lines (66 loc) · 2.36 KB
/
vector.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
import {create} from "d3";
import {radians} from "../math.js";
import {maybeNumberChannel, maybeTuple, keyword} from "../options.js";
import {Mark} from "../plot.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform, offset} from "../style.js";
const defaults = {
fill: null,
stroke: "currentColor",
strokeWidth: 1.5,
strokeLinecap: "round"
};
export class Vector extends Mark {
constructor(data, options = {}) {
const {x, y, length, rotate, anchor = "middle"} = options;
const [vl, cl] = maybeNumberChannel(length, 12);
const [vr, cr] = maybeNumberChannel(rotate, 0);
super(
data,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "length", value: vl, scale: "length", optional: true},
{name: "rotate", value: vr, optional: true}
],
options,
defaults
);
this.length = cl;
this.rotate = cr;
this.anchor = keyword(anchor, "anchor", ["start", "middle", "end"]);
}
render(
index,
{x, y},
channels,
{width, height, marginTop, marginRight, marginBottom, marginLeft}
) {
const {x: X, y: Y, length: L, rotate: R} = channels;
const {dx, dy, length, rotate, anchor} = this;
const fl = L ? i => L[i] : () => length;
const fr = R ? i => R[i] : () => rotate;
const fx = X ? i => X[i] : () => (marginLeft + width - marginRight) / 2;
const fy = Y ? i => Y[i] : () => (marginTop + height - marginBottom) / 2;
const k = anchor === "start" ? 0 : anchor === "end" ? 1 : 0.5;
return create("svg:g")
.attr("fill", "none")
.call(applyIndirectStyles, this)
.call(applyTransform, x, y, offset + dx, offset + dy)
.call(g => g.selectAll()
.data(index)
.join("path")
.call(applyDirectStyles, this)
.attr("d", i => {
const l = fl(i), a = fr(i) * radians;
const x = Math.sin(a) * l, y = -Math.cos(a) * l;
const d = (x + y) / 5, e = (x - y) / 5;
return `M${fx(i) - x * k},${fy(i) - y * k}l${x},${y}m${-e},${-d}l${e},${d}l${-d},${e}`;
})
.call(applyChannelStyles, this, channels))
.node();
}
}
export function vector(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Vector(data, {...options, x, y});
}