forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.js
140 lines (135 loc) · 4.42 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
139
140
import {create} from "d3";
import {filter} from "../defined.js";
import {Mark, identity, maybeColor, title, number} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform, applyAttr} from "../style.js";
export class RuleX extends Mark {
constructor(
data,
{
x,
y1,
y2,
title,
stroke,
inset = 0,
insetTop = inset,
insetBottom = inset,
...options
} = {}
) {
const [vstroke, cstroke] = maybeColor(stroke, "currentColor");
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},
{name: "title", value: title, optional: true},
{name: "stroke", value: vstroke, scale: "color", optional: true}
],
options
);
Style(this, {stroke: cstroke, ...options});
this.insetTop = number(insetTop);
this.insetBottom = number(insetBottom);
}
render(
I,
{x, y},
{x: X, y1: Y1, y2: Y2, title: L, stroke: S},
{width, height, marginTop, marginRight, marginLeft, marginBottom}
) {
const index = filter(I, X, Y1, Y2, S);
return create("svg:g")
.call(applyIndirectStyles, this)
.call(applyTransform, X && x, null, 0.5, 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 ? i => Y1[i] + this.insetTop : marginTop + this.insetTop)
.attr("y2", Y2 ? (y.bandwidth ? i => Y2[i] + y.bandwidth() - this.insetBottom : i => Y2[i] - this.insetBottom) : height - marginBottom - this.insetBottom)
.call(applyAttr, "stroke", S && (i => S[i]))
.call(title(L)))
.node();
}
}
export class RuleY extends Mark {
constructor(
data,
{
x1,
x2,
y,
title,
stroke,
inset = 0,
insetRight = inset,
insetLeft = inset,
...options
} = {}
) {
const [vstroke, cstroke] = maybeColor(stroke, "currentColor");
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},
{name: "title", value: title, optional: true},
{name: "stroke", value: vstroke, scale: "color", optional: true}
],
options
);
Style(this, {stroke: cstroke, ...options});
this.insetRight = number(insetRight);
this.insetLeft = number(insetLeft);
}
render(
I,
{x, y},
{y: Y, x1: X1, x2: X2, title: L, stroke: S},
{width, height, marginTop, marginRight, marginLeft, marginBottom}
) {
const index = filter(I, Y, X1, X2);
return create("svg:g")
.call(applyIndirectStyles, this)
.call(applyTransform, null, Y && y, 0, 0.5)
.call(g => g.selectAll("line")
.data(index)
.join("line")
.call(applyDirectStyles, this)
.attr("x1", X1 ? i => X1[i] + this.insetLeft : marginLeft + this.insetLeft)
.attr("x2", X2 ? (x.bandwidth ? i => X2[i] + x.bandwidth() - this.insetRight : i => X2[i] - this.insetRight) : width - marginRight - this.insetRight)
.attr("y1", Y ? i => Y[i] : (marginTop + height - marginBottom) / 2)
.attr("y2", Y ? i => Y[i] : (marginTop + height - marginBottom) / 2)
.call(applyAttr, "stroke", S && (i => S[i]))
.call(title(L)))
.node();
}
}
export function ruleX(data, {x = identity, y, y1, y2, ...options} = {}) {
([y1, y2] = maybeOptionalZero(y, y1, y2));
return new RuleX(data, {...options, x, y1, y2});
}
export function ruleY(data, {y = identity, x, x1, x2, ...options} = {}) {
([x1, x2] = maybeOptionalZero(x, x1, x2));
return new RuleY(data, {...options, 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];
}