UNPKG

3.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.pixelsOf = exports.abstractOf = exports.selectionOf = exports.domainOf = exports.invert = exports.isOrdinalScale = void 0;
4const d3_array_1 = require("d3-array");
5function constrain(x, lo, hi) {
6 return Math.min(hi, Math.max(lo, x));
7}
8function isOrdinalScale(scale) {
9 return !!scale.getBandWidth;
10}
11exports.isOrdinalScale = isOrdinalScale;
12function invert(scale, x, start) {
13 if (!isOrdinalScale(scale))
14 return scale.invert(x);
15 const { adjustedRange } = scale;
16 const { domain } = scale.getOptions();
17 const offset = start ? -1 : 0;
18 const step = scale.getStep();
19 const range = start ? adjustedRange : adjustedRange.map((d) => d + step);
20 // R[i0 - 1] < x <= R[i0]
21 const i0 = (0, d3_array_1.bisectLeft)(range, x);
22 const i1 = constrain(i0 + offset, 0, domain.length - 1);
23 return domain[i1];
24}
25exports.invert = invert;
26function domainOf(scale, values, ratioX) {
27 if (!values)
28 return scale.getOptions().domain;
29 if (!isOrdinalScale(scale)) {
30 const sortedDomain = (0, d3_array_1.sort)(values);
31 if (!ratioX)
32 return sortedDomain;
33 const [d] = sortedDomain;
34 const { range } = scale.getOptions();
35 const [r0, r1] = range;
36 const v = r0 > r1 ? -1 : 1;
37 const d1 = scale.invert(scale.map(d) + v * ratioX);
38 return [d, d1];
39 }
40 const { domain } = scale.getOptions();
41 const v1 = values[0];
42 const start = domain.indexOf(v1);
43 if (ratioX) {
44 const end = start + Math.round(domain.length * ratioX);
45 return domain.slice(start, end);
46 }
47 const v2 = values[values.length - 1];
48 const end = domain.indexOf(v2);
49 return domain.slice(start, end + 1);
50}
51exports.domainOf = domainOf;
52function selectionOf(x, y, x1, y1, scale, coordinate) {
53 const { x: scaleX, y: scaleY } = scale;
54 const abstractDomain = (point, start) => {
55 const [x, y] = coordinate.invert(point);
56 return [invert(scaleX, x, start), invert(scaleY, y, start)];
57 };
58 const p0 = abstractDomain([x, y], true);
59 const p1 = abstractDomain([x1, y1], false);
60 const domainX = domainOf(scaleX, [p0[0], p1[0]]);
61 const domainY = domainOf(scaleY, [p0[1], p1[1]]);
62 return [domainX, domainY];
63}
64exports.selectionOf = selectionOf;
65function abstractOf(domain, scale) {
66 const [d0, d1] = domain;
67 const maybeStep = (scale) => (scale.getStep ? scale.getStep() : 0);
68 return [scale.map(d0), scale.map(d1) + maybeStep(scale)];
69}
70exports.abstractOf = abstractOf;
71function pixelsOf(selection, scale, coordinate) {
72 const { x: scaleX, y: scaleY } = scale;
73 const [X, Y] = selection;
74 const AX = abstractOf(X, scaleX);
75 const AY = abstractOf(Y, scaleY);
76 const p0 = [AX[0], AY[0]];
77 const p1 = [AX[1], AY[1]];
78 const [x, y] = coordinate.map(p0);
79 const [x1, y1] = coordinate.map(p1);
80 return [x, y, x1, y1];
81}
82exports.pixelsOf = pixelsOf;
83//# sourceMappingURL=scale.js.map
\No newline at end of file