forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.js
113 lines (109 loc) · 3.13 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
import {ascending} from "d3-array";
import {create} from "d3-selection";
import {filter} from "../defined.js";
import {Mark, identity, maybeColor} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js";
export class RuleX extends Mark {
constructor(
data,
{
x = identity,
y1,
y2,
z,
stroke,
transform,
...style
} = {}
) {
const [vstroke, cstroke = vstroke == null ? "currentColor" : undefined] = maybeColor(stroke);
super(
data,
[
{name: "x", value: x, scale: "x"},
{name: "y1", value: y1, scale: "y", optional: true},
{name: "y2", value: y2, scale: "y", optional: true},
{name: "z", value: z, optional: true},
{name: "stroke", value: vstroke, scale: "color", optional: true}
],
transform
);
Style(this, {stroke: cstroke, ...style});
}
render(
I,
{x, y, color},
{x: X, y1: Y1, y2: Y2, z: Z, stroke: S},
{marginTop, height, marginBottom}
) {
const index = filter(I, X, Y1, Y2, S);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
.call(g => g.selectAll("line")
.data(index)
.join("line")
.call(applyDirectStyles, this)
.attr("x1", i => Math.round(x(X[i])) + 0.5)
.attr("x2", i => Math.round(x(X[i])) + 0.5)
.attr("y1", Y1 ? i => y(Y1[i]) : marginTop)
.attr("y2", Y2 ? i => y(Y2[i]) : height - marginBottom)
.attr("stroke", S && (i => color(S[i]))))
.node();
}
}
export class RuleY extends Mark {
constructor(
data,
{
x1,
x2,
y = identity,
z,
stroke,
transform,
...style
} = {}
) {
const [vstroke, cstroke = vstroke == null ? "currentColor" : undefined] = maybeColor(stroke);
super(
data,
[
{name: "y", value: y, scale: "y"},
{name: "x1", value: x1, scale: "x", optional: true},
{name: "x2", value: x2, scale: "x", optional: true},
{name: "z", value: z, optional: true},
{name: "stroke", value: vstroke, scale: "color", optional: true}
],
transform
);
Style(this, {stroke: cstroke, ...style});
}
render(
I,
{x, y, color},
{y: Y, x1: X1, x2: X2, z: Z, stroke: S},
{width, marginLeft, marginRight}
) {
const index = filter(I, Y, X1, X2);
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
return create("svg:g")
.call(applyIndirectStyles, this)
.call(g => g.selectAll("line")
.data(index)
.join("line")
.call(applyDirectStyles, this)
.attr("x1", X1 ? i => x(X1[i]) : marginLeft)
.attr("x2", X2 ? i => x(X2[i]) : width - marginRight)
.attr("y1", i => Math.round(y(Y[i])) + 0.5)
.attr("y2", i => Math.round(y(Y[i])) + 0.5)
.attr("stroke", S && (i => color(S[i]))))
.node();
}
}
export function ruleX(data, options) {
return new RuleX(data, options);
}
export function ruleY(data, options) {
return new RuleY(data, options);
}