UNPKG

1.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Jitter = exports.interpolate = exports.rangeOf = void 0;
4const scale_1 = require("@antv/scale");
5const util_1 = require("@antv/util");
6const helper_1 = require("./utils/helper");
7const order_1 = require("./utils/order");
8function rangeOf(value, scaleOptions, padding) {
9 if (value === null)
10 return [-0.5, 0.5];
11 const domain = (0, order_1.domainOf)(value, scaleOptions);
12 const scale = new scale_1.Band({ domain, range: [0, 1], padding });
13 const step = scale.getBandWidth();
14 return [-step / 2, step / 2];
15}
16exports.rangeOf = rangeOf;
17function interpolate(t, a, b) {
18 return a * (1 - t) + b * t;
19}
20exports.interpolate = interpolate;
21/**
22 * The jitter transform produce dx and dy channels for marks (especially for point)
23 * with ordinal x and y dimension, say to make them jitter in their own space.
24 */
25const Jitter = (options = {}) => {
26 const { padding = 0, paddingX = padding, paddingY = padding, random = Math.random, } = options;
27 return (I, mark) => {
28 const { encode, scale } = mark;
29 const { x: scaleX, y: scaleY } = scale;
30 const [X] = (0, helper_1.columnOf)(encode, 'x');
31 const [Y] = (0, helper_1.columnOf)(encode, 'y');
32 const rangeX = rangeOf(X, scaleX, paddingX);
33 const rangeY = rangeOf(Y, scaleY, paddingY);
34 const DY = I.map(() => interpolate(random(), ...rangeY));
35 const DX = I.map(() => interpolate(random(), ...rangeX));
36 return [
37 I,
38 (0, util_1.deepMix)({
39 scale: {
40 x: { padding: 0.5 },
41 y: { padding: 0.5 },
42 },
43 }, mark, {
44 encode: { dy: (0, helper_1.column)(DY), dx: (0, helper_1.column)(DX) },
45 }),
46 ];
47 };
48};
49exports.Jitter = Jitter;
50exports.Jitter.props = {};
51//# sourceMappingURL=jitter.js.map
\No newline at end of file