forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.js
More file actions
75 lines (71 loc) · 2.42 KB
/
frame.js
File metadata and controls
75 lines (71 loc) · 2.42 KB
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
import {create} from "../context.js";
import {Mark} from "../mark.js";
import {maybeKeyword, number} from "../options.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform} from "../style.js";
const defaults = {
ariaLabel: "frame",
fill: "none",
stroke: "currentColor"
};
const lineDefaults = {
ariaLabel: "frame",
fill: null,
stroke: "currentColor",
strokeLinecap: "square"
};
export class Frame extends Mark {
constructor(options = {}) {
const {
anchor = null,
inset = 0,
insetTop = inset,
insetRight = inset,
insetBottom = inset,
insetLeft = inset,
rx,
ry
} = options;
super(undefined, undefined, options, anchor == null ? defaults : lineDefaults);
this.anchor = maybeKeyword(anchor, "anchor", ["top", "right", "bottom", "left"]);
this.insetTop = number(insetTop);
this.insetRight = number(insetRight);
this.insetBottom = number(insetBottom);
this.insetLeft = number(insetLeft);
this.rx = number(rx);
this.ry = number(ry);
}
render(index, scales, channels, dimensions, context) {
const {marginTop, marginRight, marginBottom, marginLeft, width, height} = dimensions;
const {anchor, insetTop, insetRight, insetBottom, insetLeft, rx, ry} = this;
const x1 = marginLeft + insetLeft;
const x2 = width - marginRight - insetRight;
const y1 = marginTop + insetTop;
const y2 = height - marginBottom - insetBottom;
return create(anchor ? "svg:line" : "svg:rect", context)
.call(applyIndirectStyles, this, dimensions, context)
.call(applyDirectStyles, this)
.call(applyTransform, this, {})
.call(
anchor === "left"
? (line) => line.attr("x1", x1).attr("x2", x1).attr("y1", y1).attr("y2", y2)
: anchor === "right"
? (line) => line.attr("x1", x2).attr("x2", x2).attr("y1", y1).attr("y2", y2)
: anchor === "top"
? (line) => line.attr("x1", x1).attr("x2", x2).attr("y1", y1).attr("y2", y1)
: anchor === "bottom"
? (line) => line.attr("x1", x1).attr("x2", x2).attr("y1", y2).attr("y2", y2)
: (rect) =>
rect
.attr("x", x1)
.attr("y", y1)
.attr("width", x2 - x1)
.attr("height", y2 - y1)
.attr("rx", rx)
.attr("ry", ry)
)
.node();
}
}
export function frame(options) {
return new Frame(options);
}