forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.js
171 lines (158 loc) · 4.79 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {pathRound as path} from "d3";
import {create} from "../context.js";
import {Mark} from "../mark.js";
import {maybeFrameAnchor, maybeNumberChannel, maybeTuple, keyword, identity} from "../options.js";
import {
applyChannelStyles,
applyDirectStyles,
applyFrameAnchor,
applyIndirectStyles,
applyTransform
} from "../style.js";
import {template} from "../template.js";
const defaults = {
ariaLabel: "vector",
fill: "none",
stroke: "currentColor",
strokeWidth: 1.5,
strokeLinejoin: "round",
strokeLinecap: "round"
};
const defaultRadius = 3.5;
// The size of the arrowhead is proportional to its length, but we still allow
// the relative size of the head to be controlled via the mark’s width option;
// doubling the default radius will produce an arrowhead that is twice as big.
// That said, we’ll probably want a arrow with a fixed head size, too.
const wingRatio = defaultRadius * 5;
const shapeArrow = {
draw(context, l, r) {
const wing = (l * r) / wingRatio;
context.moveTo(0, 0);
context.lineTo(0, -l);
context.moveTo(-wing, wing - l);
context.lineTo(0, -l);
context.lineTo(wing, wing - l);
}
};
const shapeSpike = {
draw(context, l, r) {
context.moveTo(-r, 0);
context.lineTo(0, -l);
context.lineTo(r, 0);
}
};
const shapes = new Map([
["arrow", shapeArrow],
["spike", shapeSpike]
]);
function isShapeObject(value) {
return value && typeof value.draw === "function";
}
function Shape(shape) {
if (isShapeObject(shape)) return shape;
const value = shapes.get(`${shape}`.toLowerCase());
if (value) return value;
throw new Error(`invalid shape: ${shape}`);
}
export class Vector extends Mark {
constructor(data, options = {}) {
const {x, y, r = defaultRadius, length, rotate, shape = shapeArrow, anchor = "middle", frameAnchor} = options;
const [vl, cl] = maybeNumberChannel(length, 12);
const [vr, cr] = maybeNumberChannel(rotate, 0);
super(
data,
{
x: {value: x, scale: "x", optional: true},
y: {value: y, scale: "y", optional: true},
length: {value: vl, scale: "length", optional: true},
rotate: {value: vr, optional: true}
},
options,
defaults
);
this.r = +r;
this.length = cl;
this.rotate = cr;
this.shape = Shape(shape);
this.anchor = keyword(anchor, "anchor", ["start", "middle", "end"]);
this.frameAnchor = maybeFrameAnchor(frameAnchor);
}
render(index, scales, channels, dimensions, context) {
const {x, y} = scales;
const {x: X, y: Y, length: L, rotate: A} = channels;
const {length, rotate, anchor, shape, r} = this;
const [cx, cy] = applyFrameAnchor(this, dimensions);
return create("svg:g", context)
.call(applyIndirectStyles, this, dimensions, context)
.call(applyTransform, this, {x: X && x, y: Y && y})
.call((g) =>
g
.selectAll()
.data(index)
.enter()
.append("path")
.call(applyDirectStyles, this)
.attr(
"transform",
template`translate(${X ? (i) => X[i] : cx},${Y ? (i) => Y[i] : cy})${
A ? (i) => ` rotate(${A[i]})` : rotate ? ` rotate(${rotate})` : ``
}${
anchor === "start"
? ``
: anchor === "end"
? L
? (i) => ` translate(0,${L[i]})`
: ` translate(0,${length})`
: L
? (i) => ` translate(0,${L[i] / 2})`
: ` translate(0,${length / 2})`
}`
)
.attr(
"d",
L
? (i) => {
const p = path();
shape.draw(p, L[i], r);
return p;
}
: (() => {
const p = path();
shape.draw(p, length, r);
return p;
})()
)
.call(applyChannelStyles, this, channels)
)
.node();
}
}
/** @jsdoc vector */
export function vector(data, options = {}) {
let {x, y, ...rest} = options;
if (options.frameAnchor === undefined) [x, y] = maybeTuple(x, y);
return new Vector(data, {...rest, x, y});
}
/** @jsdoc vectorX */
export function vectorX(data, options = {}) {
const {x = identity, ...rest} = options;
return new Vector(data, {...rest, x});
}
/** @jsdoc vectorY */
export function vectorY(data, options = {}) {
const {y = identity, ...rest} = options;
return new Vector(data, {...rest, y});
}
/** @jsdoc spike */
export function spike(data, options = {}) {
const {
shape = shapeSpike,
stroke = defaults.stroke,
strokeWidth = 1,
fill = stroke,
fillOpacity = 0.3,
anchor = "start",
...rest
} = options;
return vector(data, {...rest, shape, stroke, strokeWidth, fill, fillOpacity, anchor});
}