forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtick.js
119 lines (113 loc) · 3.2 KB
/
tick.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
import {create} from "d3";
import {filter} from "../defined.js";
import {Mark, identity, number} from "../mark.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyChannelStyles, offset} from "../style.js";
const defaults = {
fill: null,
stroke: "currentColor"
};
class AbstractTick extends Mark {
constructor(data, channels, options) {
super(data, channels, options, defaults);
}
render(I, scales, channels, dimensions) {
const {x: X, y: Y} = channels;
const {dx, dy} = this;
const index = filter(I, X, Y);
return create("svg:g")
.call(applyIndirectStyles, this)
.call(this._transform, scales, dx, dy)
.call(g => g.selectAll("line")
.data(index)
.join("line")
.call(applyDirectStyles, this)
.attr("x1", this._x1(scales, channels, dimensions))
.attr("x2", this._x2(scales, channels, dimensions))
.attr("y1", this._y1(scales, channels, dimensions))
.attr("y2", this._y2(scales, channels, dimensions))
.call(applyChannelStyles, channels))
.node();
}
}
export class TickX extends AbstractTick {
constructor(data, options = {}) {
const {
x,
y,
inset = 0,
insetTop = inset,
insetBottom = inset
} = options;
super(
data,
[
{name: "x", value: x, scale: "x"},
{name: "y", value: y, scale: "y", type: "band", optional: true}
],
options
);
this.insetTop = number(insetTop);
this.insetBottom = number(insetBottom);
}
_transform(selection, {x}, dx, dy) {
selection.call(applyTransform, x, null, offset + dx, dy);
}
_x1(scales, {x: X}) {
return i => X[i];
}
_x2(scales, {x: X}) {
return i => X[i];
}
_y1(scales, {y: Y}, {marginTop}) {
const {insetTop} = this;
return Y ? i => Y[i] + insetTop : marginTop + insetTop;
}
_y2({y}, {y: Y}, {height, marginBottom}) {
const {insetBottom} = this;
return Y ? i => Y[i] + y.bandwidth() - insetBottom : height - marginBottom - insetBottom;
}
}
export class TickY extends AbstractTick {
constructor(data, options = {}) {
const {
x,
y,
inset = 0,
insetRight = inset,
insetLeft = inset
} = options;
super(
data,
[
{name: "y", value: y, scale: "y"},
{name: "x", value: x, scale: "x", type: "band", optional: true}
],
options
);
this.insetRight = number(insetRight);
this.insetLeft = number(insetLeft);
}
_transform(selection, {y}, dx, dy) {
selection.call(applyTransform, null, y, dx, offset + dy);
}
_x1(scales, {x: X}, {marginLeft}) {
const {insetLeft} = this;
return X ? i => X[i] + insetLeft : marginLeft + insetLeft;
}
_x2({x}, {x: X}, {width, marginRight}) {
const {insetRight} = this;
return X ? i => X[i] + x.bandwidth() - insetRight : width - marginRight - insetRight;
}
_y1(scales, {y: Y}) {
return i => Y[i];
}
_y2(scales, {y: Y}) {
return i => Y[i];
}
}
export function tickX(data, {x = identity, ...options} = {}) {
return new TickX(data, {...options, x});
}
export function tickY(data, {y = identity, ...options} = {}) {
return new TickY(data, {...options, y});
}