forked from perspective-dev/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossAxis.js
More file actions
283 lines (243 loc) · 9.11 KB
/
crossAxis.js
File metadata and controls
283 lines (243 loc) · 9.11 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
import * as d3 from "d3";
import * as fc from "d3fc";
import minBandwidth from "./minBandwidth";
import withoutTicks from "./withoutTicks";
import {axisOrdinalBottom, axisOrdinalLeft} from "../d3fc/axis/axisOrdinal";
import {multiAxisBottom, multiAxisLeft} from "../d3fc/axis/multi-axis";
const AXIS_TYPES = {
none: "none",
ordinal: "ordinal",
time: "time",
linear: "linear"
};
export const scale = (settings, settingName = "crossValues") => {
switch (axisType(settings, settingName)) {
case AXIS_TYPES.none:
return withoutTicks(defaultScaleBand());
case AXIS_TYPES.time:
return d3.scaleTime();
case AXIS_TYPES.linear:
return d3.scaleLinear();
default:
return defaultScaleBand();
}
};
const defaultScaleBand = () => minBandwidth(d3.scaleBand());
const flattenArray = array => {
if (Array.isArray(array)) {
return [].concat(...array.map(flattenArray));
} else {
return [array];
}
};
export const domain = settings => {
let valueName = "crossValue";
let settingName = "crossValues";
const extentTime = fc.extentTime().accessors([d => new Date(d[valueName])]);
const _domain = function(data) {
const flattenedData = flattenArray(data);
switch (axisType(settings, settingName)) {
case AXIS_TYPES.time:
return extentTime(flattenedData);
default:
return [...new Set(flattenedData.map(d => d[valueName]))];
}
};
switch (axisType(settings)) {
case AXIS_TYPES.time:
fc.rebindAll(_domain, extentTime);
break;
}
_domain.valueName = (...args) => {
if (!args.length) {
return valueName;
}
valueName = args[0];
return _domain;
};
_domain.settingName = (...args) => {
if (!args.length) {
return settingName;
}
settingName = args[0];
return _domain;
};
return _domain;
};
export const labelFunction = (settings, valueName = "__ROW_PATH__", settingName = "crossValues") => {
switch (axisType(settings, settingName)) {
case AXIS_TYPES.none:
return d => d[valueName][0];
case AXIS_TYPES.time:
return d => new Date(d[valueName][0]);
case AXIS_TYPES.linear:
return d => d[valueName][0];
default:
return d => d[valueName].join("|");
}
};
export const label = (settings, settingName = "crossValues") => settings[settingName].map(v => v.name).join(", ");
const axisType = (settings, settingName = "crossValues") => {
if (settings[settingName].length === 0) {
return AXIS_TYPES.none;
} else if (settings[settingName].length === 1) {
if (settings[settingName][0].type === "datetime") {
return AXIS_TYPES.time;
}
}
return AXIS_TYPES.ordinal;
};
export const axisFactory = settings => {
let orient = "horizontal";
let settingName = "crossValues";
let domain = null;
const factory = () => {
switch (axisType(settings, settingName)) {
case AXIS_TYPES.ordinal:
const multiLevel = settings[settingName].length > 1 && settings[settingName].every(v => v.type !== "datetime");
// Calculate the label groups and corresponding group sizes
const levelGroups = axisGroups(domain);
const groupTickLayout = levelGroups.map(getGroupTickLayout);
const tickSizeInner = multiLevel ? groupTickLayout.map(l => l.size) : groupTickLayout[0].size;
const tickSizeOuter = groupTickLayout.reduce((s, v) => s + v.size, 0);
const createAxis = scale => {
const axis = pickAxis(multiLevel)(scale);
axis.tickLineAlign("right").tickPadding(8);
if (multiLevel) {
axis.groups(levelGroups)
.tickSizeInner(tickSizeInner)
.tickSizeOuter(tickSizeOuter);
}
return axis;
};
const decorate = (s, data, index) => {
const rotated = groupTickLayout[index].rotate;
hideOverlappingLabels(s, rotated);
if (orient === "horizontal") applyLabelRotation(s, rotated);
};
return {
bottom: createAxis,
left: createAxis,
size: `${tickSizeOuter + 10}px`,
decorate
};
}
// Default axis
return {
bottom: fc.axisBottom,
left: fc.axisLeft,
decorate: () => {}
};
};
const pickAxis = multiLevel => {
if (multiLevel) {
return orient === "horizontal" ? multiAxisBottom : multiAxisLeft;
}
return orient === "horizontal" ? axisOrdinalBottom : axisOrdinalLeft;
};
const axisGroups = domain => {
const groups = [];
domain.forEach(tick => {
const split = tick.split("|");
split.forEach((s, i) => {
while (groups.length <= i) groups.push([]);
const group = groups[i];
if (group.length > 0 && group[group.length - 1].text === s) {
group[group.length - 1].domain.push(tick);
} else {
group.push({text: s, domain: [tick]});
}
});
});
return groups.reverse();
};
const getGroupTickLayout = group => {
const width = settings.size.width;
const maxLength = Math.max(...group.map(g => g.text.length));
if (orient === "horizontal") {
// x-axis may rotate labels and expand the available height
if (group.length * (maxLength * 6 + 10) > width - 100) {
return {
size: maxLength * 3 + 20,
rotate: true
};
}
return {
size: 25,
rotate: false
};
} else {
// y-axis size always based on label size
return {
size: maxLength * 5 + 10,
rotate: false
};
}
};
const hideOverlappingLabels = (s, rotated) => {
const getTransformCoords = transform =>
transform
.substring(transform.indexOf("(") + 1, transform.indexOf(")"))
.split(",")
.map(c => parseInt(c));
const rectanglesOverlap = (r1, r2) => r1.x <= r2.x + r2.width && r2.x <= r1.x + r1.width && r1.y <= r2.y + r2.height && r2.y <= r1.y + r1.height;
const rotatedLabelsOverlap = (r1, r2) => r1.x + 14 > r2.x;
const previousRectangles = [];
s.each((d, i, nodes) => {
const tick = d3.select(nodes[i]);
const text = tick.select("text");
const transformCoords = getTransformCoords(tick.attr("transform"));
let rect = {};
let overlap = false;
if (rotated) {
rect = {x: transformCoords[0], y: transformCoords[1]};
overlap = previousRectangles.some(r => rotatedLabelsOverlap(r, rect));
} else {
const textRect = text.node().getBBox();
rect = {x: textRect.x + transformCoords[0], y: textRect.y + transformCoords[1], width: textRect.width, height: textRect.height};
overlap = previousRectangles.some(r => rectanglesOverlap(r, rect));
}
text.attr("visibility", overlap ? "hidden" : "");
if (!overlap) {
previousRectangles.push(rect);
}
});
};
const applyLabelRotation = (s, rotate) => {
s.each((d, i, nodes) => {
const tick = d3.select(nodes[i]);
const text = tick.select("text");
text.attr("transform", rotate ? "rotate(-45 5 5)" : "translate(0, 8)").style("text-anchor", rotate ? "end" : "");
});
};
factory.orient = (...args) => {
if (!args.length) {
return orient;
}
orient = args[0];
return factory;
};
factory.settingName = (...args) => {
if (!args.length) {
return settingName;
}
settingName = args[0];
return factory;
};
factory.domain = (...args) => {
if (!args.length) {
return domain;
}
domain = args[0];
return factory;
};
return factory;
};