forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.js
81 lines (79 loc) · 2 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
import {create} from "d3-selection";
import {identity} from "../channels.js";
import {Mark} from "../mark.js";
export class RuleX extends Mark {
constructor(
data,
{
x = identity,
stroke = "currentColor",
strokeWidth,
strokeOpacity
} = {}
) {
super(data, {x: {value: x, scale: "x"}});
this.stroke = stroke;
this.strokeWidth = strokeWidth;
this.strokeOpacity = strokeOpacity;
}
render(I, {x: {scale: x}}, {marginTop, height, marginBottom}) {
const {
stroke,
strokeWidth,
strokeOpacity,
channels: {
x: {value: X}
}
} = this;
return create("svg:g")
.attr("stroke", stroke)
.attr("stroke-width", strokeWidth)
.attr("stroke-opacity", strokeOpacity)
.call(g => g.selectAll("line")
.data(I)
.join("line")
.attr("x1", i => Math.round(x(X[i])) + 0.5) // TODO round
.attr("x2", i => Math.round(x(X[i])) + 0.5) // TODO round
.attr("y1", marginTop)
.attr("y2", height - marginBottom))
.node();
}
}
export class RuleY extends Mark {
constructor(
data,
{
y = identity,
stroke = "currentColor",
strokeWidth,
strokeOpacity
} = {}
) {
super(data, {y: {value: y, scale: "y"}});
this.stroke = stroke;
this.strokeWidth = strokeWidth;
this.strokeOpacity = strokeOpacity;
}
render(I, {y: {scale: y}}, {width, marginLeft, marginRight}) {
const {
stroke,
strokeWidth,
strokeOpacity,
channels: {
y: {value: Y}
}
} = this;
return create("svg:g")
.attr("stroke", stroke)
.attr("stroke-width", strokeWidth)
.attr("stroke-opacity", strokeOpacity)
.call(g => g.selectAll("line")
.data(I)
.join("line")
.attr("x1", marginLeft)
.attr("x2", width - marginRight)
.attr("y1", i => Math.round(y(Y[i])) + 0.5) // TODO round?
.attr("y2", i => Math.round(y(Y[i])) + 0.5)) // TODO round?
.node();
}
}