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