forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.js
138 lines (132 loc) · 4.5 KB
/
rule.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
import {create} from "../context.js";
import {identity, number} from "../options.js";
import {Mark} from "../plot.js";
import {isCollapsed} from "../scales.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyChannelStyles, offset} from "../style.js";
import {maybeIntervalX, maybeIntervalY} from "../transforms/interval.js";
const defaults = {
ariaLabel: "rule",
fill: null,
stroke: "currentColor"
};
export class RuleX extends Mark {
constructor(data, options = {}) {
const {x, y1, y2, inset = 0, insetTop = inset, insetBottom = inset} = options;
super(
data,
{
x: {value: x, scale: "x", optional: true},
y1: {value: y1, scale: "y", optional: true},
y2: {value: y2, scale: "y", optional: true}
},
options,
defaults
);
this.insetTop = number(insetTop);
this.insetBottom = number(insetBottom);
}
render(index, scales, channels, dimensions, context) {
const {x, y} = scales;
const {x: X, y1: Y1, y2: Y2} = channels;
const {width, height, marginTop, marginRight, marginLeft, marginBottom} = dimensions;
const {insetTop, insetBottom} = this;
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions)
.call(applyTransform, this, {x: X && x}, offset, 0)
.call((g) =>
g
.selectAll()
.data(index)
.enter()
.append("line")
.call(applyDirectStyles, this)
.attr("x1", X ? (i) => X[i] : (marginLeft + width - marginRight) / 2)
.attr("x2", X ? (i) => X[i] : (marginLeft + width - marginRight) / 2)
.attr("y1", Y1 && !isCollapsed(y) ? (i) => Y1[i] + insetTop : marginTop + insetTop)
.attr(
"y2",
Y2 && !isCollapsed(y)
? y.bandwidth
? (i) => Y2[i] + y.bandwidth() - insetBottom
: (i) => Y2[i] - insetBottom
: height - marginBottom - insetBottom
)
.call(applyChannelStyles, this, channels)
)
.node();
}
}
export class RuleY extends Mark {
constructor(data, options = {}) {
const {x1, x2, y, inset = 0, insetRight = inset, insetLeft = inset} = options;
super(
data,
{
y: {value: y, scale: "y", optional: true},
x1: {value: x1, scale: "x", optional: true},
x2: {value: x2, scale: "x", optional: true}
},
options,
defaults
);
this.insetRight = number(insetRight);
this.insetLeft = number(insetLeft);
}
render(index, scales, channels, dimensions, context) {
const {x, y} = scales;
const {y: Y, x1: X1, x2: X2} = channels;
const {width, height, marginTop, marginRight, marginLeft, marginBottom} = dimensions;
const {insetLeft, insetRight} = this;
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions, context)
.call(applyTransform, this, {y: Y && y}, 0, offset)
.call((g) =>
g
.selectAll()
.data(index)
.enter()
.append("line")
.call(applyDirectStyles, this)
.attr("x1", X1 && !isCollapsed(x) ? (i) => X1[i] + insetLeft : marginLeft + insetLeft)
.attr(
"x2",
X2 && !isCollapsed(x)
? x.bandwidth
? (i) => X2[i] + x.bandwidth() - insetRight
: (i) => X2[i] - insetRight
: width - marginRight - insetRight
)
.attr("y1", Y ? (i) => Y[i] : (marginTop + height - marginBottom) / 2)
.attr("y2", Y ? (i) => Y[i] : (marginTop + height - marginBottom) / 2)
.call(applyChannelStyles, this, channels)
)
.node();
}
}
/** @jsdoc ruleX */
export function ruleX(data, options) {
let {x = identity, y, y1, y2, ...rest} = maybeIntervalY(options);
[y1, y2] = maybeOptionalZero(y, y1, y2);
return new RuleX(data, {...rest, x, y1, y2});
}
/** @jsdoc ruleY */
export function ruleY(data, options) {
let {y = identity, x, x1, x2, ...rest} = maybeIntervalX(options);
[x1, x2] = maybeOptionalZero(x, x1, x2);
return new RuleY(data, {...rest, y, x1, x2});
}
// For marks specified either as [0, x] or [x1, x2], or nothing.
function maybeOptionalZero(x, x1, x2) {
if (x === undefined) {
if (x1 === undefined) {
if (x2 !== undefined) return [0, x2];
} else {
if (x2 === undefined) return [0, x1];
}
} else if (x1 === undefined) {
return x2 === undefined ? [0, x] : [x, x2];
} else if (x2 === undefined) {
return [x, x1];
}
return [x1, x2];
}