forked from perspective-dev/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxis.js
More file actions
224 lines (182 loc) · 6.85 KB
/
axis.js
File metadata and controls
224 lines (182 loc) · 6.85 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import {select} from "d3-selection";
import {line} from "d3-shape";
import {dataJoin as _dataJoin} from "d3fc";
const identity = d => d;
const axis = (orient, scale) => {
let tickArguments = [10];
let tickValues = null;
let decorate = () => {};
let tickFormat = null;
let tickSizeOuter = 6;
let tickSizeInner = 6;
let tickPadding = 3;
let centerAlignTicks = false;
let tickOffset = () => (centerAlignTicks && scale.step ? scale.step() / 2 : 0);
const svgDomainLine = line();
const dataJoin = _dataJoin("g", "tick").key(identity);
const domainPathDataJoin = _dataJoin("path", "domain");
// returns a function that creates a translation based on
// the bound data
const containerTranslate = (scale, trans) => {
let offset = 0;
if (scale.bandwidth) {
offset = scale.bandwidth() / 2;
if (scale.round()) {
offset = Math.round(offset);
}
}
return d => trans(scale(d) + offset, 0);
};
const translate = (x, y) => (isVertical() ? `translate(${y}, ${x})` : `translate(${x}, ${y})`);
const pathTranspose = arr => (isVertical() ? arr.map(d => [d[1], d[0]]) : arr);
const isVertical = () => orient === "left" || orient === "right";
const tryApply = (fn, args, defaultVal) => (scale[fn] ? scale[fn].apply(scale, args) : defaultVal);
const axis = selection => {
if (selection.selection) {
dataJoin.transition(selection);
domainPathDataJoin.transition(selection);
}
selection.each((data, index, group) => {
const element = group[index];
const container = select(element);
if (!element.__scale__) {
container
.attr("fill", "none")
.attr("font-size", 10)
.attr("font-family", "sans-serif")
.attr("text-anchor", orient === "right" ? "start" : orient === "left" ? "end" : "middle");
}
// Stash a snapshot of the new scale, and retrieve the old snapshot.
const scaleOld = element.__scale__ || scale;
element.__scale__ = scale.copy();
const ticksArray = tickValues == null ? tryApply("ticks", tickArguments, scale.domain()) : tickValues;
const tickFormatter = tickFormat == null ? tryApply("tickFormat", tickArguments, identity) : tickFormat;
const sign = orient === "bottom" || orient === "right" ? 1 : -1;
// add the domain line
const range = scale.range();
const domainPathData = pathTranspose([[range[0], sign * tickSizeOuter], [range[0], 0], [range[1], 0], [range[1], sign * tickSizeOuter]]);
const domainLine = domainPathDataJoin(container, [data]);
domainLine.attr("d", svgDomainLine(domainPathData)).attr("stroke", "#000");
const g = dataJoin(container, ticksArray);
// enter
g.enter()
.attr("transform", containerTranslate(scaleOld, translate))
.append("path")
.attr("stroke", "#000");
const labelOffset = sign * ((centerAlignTicks ? 0 : tickSizeInner) + tickPadding);
g.enter()
.append("text")
.attr("transform", translate(0, labelOffset))
.attr("fill", "#000");
// exit
g.exit().attr("transform", containerTranslate(scale, translate));
// update
g.select("path")
.attr("visibility", (d, i) => (i === ticksArray.length - 1 && centerAlignTicks ? "hidden" : ""))
.attr("d", d => {
const offset = sign * tickOffset(d);
return svgDomainLine(pathTranspose([[offset, 0], [offset, sign * tickSizeInner]]));
});
g.select("text")
.attr("transform", translate(0, labelOffset))
.attr("dy", () => {
let offset = "0em";
if (isVertical()) {
offset = "0.32em";
} else if (orient === "bottom") {
offset = "0.71em";
}
return offset;
})
.text(tickFormatter);
g.attr("transform", containerTranslate(scale, translate));
decorate(g, data, index);
});
};
axis.tickFormat = (...args) => {
if (!args.length) {
return tickFormat;
}
tickFormat = args[0];
return axis;
};
axis.tickSize = (...args) => {
if (!args.length) {
return tickSizeInner;
}
tickSizeInner = tickSizeOuter = Number(args[0]);
return axis;
};
axis.tickSizeInner = (...args) => {
if (!args.length) {
return tickSizeInner;
}
tickSizeInner = Number(args[0]);
return axis;
};
axis.tickSizeOuter = (...args) => {
if (!args.length) {
return tickSizeOuter;
}
tickSizeOuter = Number(args[0]);
return axis;
};
axis.tickPadding = (...args) => {
if (!args.length) {
return tickPadding;
}
tickPadding = args[0];
return axis;
};
axis.centerAlignTicks = (...args) => {
if (!args.length) {
return centerAlignTicks;
}
centerAlignTicks = args[0];
return axis;
};
axis.tickOffset = (...args) => {
if (!args.length) {
return tickOffset;
}
tickOffset = args[0];
return axis;
};
axis.decorate = (...args) => {
if (!args.length) {
return decorate;
}
decorate = args[0];
return axis;
};
axis.scale = (...args) => {
if (!args.length) {
return scale;
}
scale = args[0];
return axis;
};
axis.ticks = (...args) => {
tickArguments = [...args];
return axis;
};
axis.tickArguments = (...args) => {
if (!args.length) {
return tickArguments.slice();
}
tickArguments = args[0] == null ? [] : [...args[0]];
return axis;
};
axis.tickValues = (...args) => {
if (!args.length) {
return tickValues.slice();
}
tickValues = args[0] == null ? [] : [...args[0]];
return axis;
};
return axis;
};
export const axisTop = scale => axis("top", scale);
export const axisBottom = scale => axis("bottom", scale);
export const axisLeft = scale => axis("left", scale);
export const axisRight = scale => axis("right", scale);