forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.js
127 lines (121 loc) · 4.24 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
import {create} from "d3";
import {filter} from "../defined.js";
import {Mark, identity, number} from "../mark.js";
import {isCollapsed} from "../scales.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyChannelStyles, offset} from "../style.js";
import {maybeIntervalX, maybeIntervalY} from "../transforms/interval.js";
const defaults = {
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,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y1", value: y1, scale: "y", optional: true},
{name: "y2", value: y2, scale: "y", optional: true}
],
options,
defaults
);
this.insetTop = number(insetTop);
this.insetBottom = number(insetBottom);
}
render(I, {x, y}, channels, dimensions) {
const {x: X, y1: Y1, y2: Y2} = channels;
const {width, height, marginTop, marginRight, marginLeft, marginBottom} = dimensions;
const {insetTop, insetBottom} = this;
const index = filter(I, X, Y1, Y2);
return create("svg:g")
.call(applyIndirectStyles, this)
.call(applyTransform, X && x, null, offset, 0)
.call(g => g.selectAll("line")
.data(index)
.join("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, channels))
.node();
}
}
export class RuleY extends Mark {
constructor(data, options = {}) {
const {
x1,
x2,
y,
inset = 0,
insetRight = inset,
insetLeft = inset
} = options;
super(
data,
[
{name: "y", value: y, scale: "y", optional: true},
{name: "x1", value: x1, scale: "x", optional: true},
{name: "x2", value: x2, scale: "x", optional: true}
],
options,
defaults
);
this.insetRight = number(insetRight);
this.insetLeft = number(insetLeft);
}
render(I, {x, y}, channels, dimensions) {
const {y: Y, x1: X1, x2: X2} = channels;
const {width, height, marginTop, marginRight, marginLeft, marginBottom} = dimensions;
const {insetLeft, insetRight, dx, dy} = this;
const index = filter(I, Y, X1, X2);
return create("svg:g")
.call(applyIndirectStyles, this)
.call(applyTransform, null, Y && y, dx, offset + dy)
.call(g => g.selectAll("line")
.data(index)
.join("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, channels))
.node();
}
}
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});
}
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];
}