vega-functions 5.12.1 → 5.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of vega-functions might be problematic. Click here for more details.

package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vega-functions",
3
- "version": "5.12.1",
3
+ "version": "5.13.1",
4
4
  "description": "Custom functions for the Vega expression language.",
5
5
  "keywords": [
6
6
  "vega",
@@ -15,26 +15,26 @@
15
15
  "repository": "vega/vega",
16
16
  "scripts": {
17
17
  "prebuild": "rimraf build",
18
- "build": "rollup -c",
18
+ "build": "rollup -c rollup.config.mjs",
19
19
  "pretest": "yarn build --config-test",
20
20
  "test": "tape 'test/**/*-test.js'",
21
21
  "prepublishOnly": "yarn test && yarn build"
22
22
  },
23
23
  "dependencies": {
24
- "d3-array": "^2.7.1",
25
- "d3-color": "^2.0.0",
26
- "d3-geo": "^2.0.1",
27
- "vega-dataflow": "^5.7.3",
28
- "vega-expression": "^5.0.0",
29
- "vega-scale": "^7.1.1",
30
- "vega-scenegraph": "^4.9.3",
31
- "vega-selections": "^5.3.1",
32
- "vega-statistics": "^1.7.9",
33
- "vega-time": "^2.0.4",
34
- "vega-util": "^1.16.0"
24
+ "d3-array": "^3.2.2",
25
+ "d3-color": "^3.1.0",
26
+ "d3-geo": "^3.1.0",
27
+ "vega-dataflow": "^5.7.5",
28
+ "vega-expression": "^5.0.1",
29
+ "vega-scale": "^7.3.0",
30
+ "vega-scenegraph": "^4.10.2",
31
+ "vega-selections": "^5.4.1",
32
+ "vega-statistics": "^1.8.1",
33
+ "vega-time": "^2.1.1",
34
+ "vega-util": "^1.17.1"
35
35
  },
36
36
  "devDependencies": {
37
- "vega-format": "^1.0.4"
37
+ "vega-format": "^1.1.0"
38
38
  },
39
- "gitHead": "774165e29850b66ec8b79ba52a7955f1ab936ea6"
39
+ "gitHead": "fb1092f6b931d450f9c210b67ae4752bd3dd461b"
40
40
  }
@@ -0,0 +1 @@
1
+ export { default } from '../../rollup.config.mjs';
package/src/codegen.js CHANGED
@@ -151,6 +151,12 @@ import {
151
151
  slice
152
152
  } from './functions/sequence';
153
153
 
154
+ import {
155
+ intersectLasso,
156
+ lassoAppend,
157
+ lassoPath
158
+ } from './functions/lasso';
159
+
154
160
  import {
155
161
  bandspace,
156
162
  bandwidth,
@@ -219,7 +225,7 @@ export const functionContext = {
219
225
  isTuple,
220
226
  isValid(_) { return _ != null && _ === _; },
221
227
  toBoolean,
222
- toDate,
228
+ toDate(_) { return toDate(_); }, // suppress extra arguments
223
229
  toNumber,
224
230
  toString,
225
231
  indexof,
@@ -267,7 +273,7 @@ export const functionContext = {
267
273
  warn,
268
274
  info,
269
275
  debug,
270
- extent,
276
+ extent(_) { return extent(_); }, // suppress extra arguments
271
277
  inScope,
272
278
  intersect,
273
279
  clampRange,
@@ -288,7 +294,10 @@ export const functionContext = {
288
294
  zoomPow,
289
295
  zoomSymlog,
290
296
  encode,
291
- modify
297
+ modify,
298
+ lassoAppend,
299
+ lassoPath,
300
+ intersectLasso
292
301
  };
293
302
 
294
303
  const eventFunctions = ['view', 'item', 'group', 'xy', 'x', 'y'], // event functions
@@ -0,0 +1,108 @@
1
+ import intersect from './intersect';
2
+ import {Bounds} from 'vega-scenegraph';
3
+ import {array} from 'vega-util';
4
+
5
+ /**
6
+ * Appends a new point to the lasso
7
+ *
8
+ * @param {*} lasso the lasso in pixel space
9
+ * @param {*} x the x coordinate in pixel space
10
+ * @param {*} y the y coordinate in pixel space
11
+ * @param {*} minDist the minimum distance, in pixels, that thenew point needs to be apart from the last point
12
+ * @returns a new array containing the lasso with the new point
13
+ */
14
+ export function lassoAppend(lasso, x, y, minDist = 5) {
15
+ lasso = array(lasso);
16
+ const last = lasso[lasso.length - 1];
17
+
18
+ // Add point to lasso if its the first point or distance to last point exceed minDist
19
+ return (last === undefined || Math.sqrt(((last[0] - x) ** 2) + ((last[1] - y) ** 2)) > minDist)
20
+ ? [...lasso, [x, y]]
21
+ : lasso;
22
+ }
23
+
24
+
25
+ /**
26
+ * Generates a svg path command which draws a lasso
27
+ *
28
+ * @param {*} lasso the lasso in pixel space in the form [[x,y], [x,y], ...]
29
+ * @returns the svg path command that draws the lasso
30
+ */
31
+ export function lassoPath(lasso) {
32
+ return array(lasso).reduce((svg, [x, y], i) => {
33
+ return svg += i == 0
34
+ ? `M ${x},${y} `
35
+ : i === lasso.length - 1
36
+ ? ' Z'
37
+ : `L ${x},${y} `;
38
+ }, '');
39
+ }
40
+
41
+
42
+
43
+ /**
44
+ * Inverts the lasso from pixel space to an array of vega scenegraph tuples
45
+ *
46
+ * @param {*} data the dataset
47
+ * @param {*} pixelLasso the lasso in pixel space, [[x,y], [x,y], ...]
48
+ * @param {*} unit the unit where the lasso is defined
49
+ *
50
+ * @returns an array of vega scenegraph tuples
51
+ */
52
+ export function intersectLasso(markname, pixelLasso, unit) {
53
+ const { x, y, mark } = unit;
54
+
55
+ const bb = new Bounds().set(
56
+ Number.MAX_SAFE_INTEGER,
57
+ Number.MAX_SAFE_INTEGER,
58
+ Number.MIN_SAFE_INTEGER,
59
+ Number.MIN_SAFE_INTEGER
60
+ );
61
+
62
+ // Get bounding box around lasso
63
+ for (const [px, py] of pixelLasso) {
64
+ if (px < bb.x1) bb.x1 = px;
65
+ if (px > bb.x2) bb.x2 = px;
66
+ if (py < bb.y1) bb.y1 = py;
67
+ if (py > bb.y2) bb.y2 = py;
68
+ }
69
+
70
+ // Translate bb against unit coordinates
71
+ bb.translate(x, y);
72
+
73
+ const intersection = intersect([[bb.x1, bb.y1], [bb.x2, bb.y2]],
74
+ markname,
75
+ mark);
76
+
77
+ // Check every point against the lasso
78
+ return intersection.filter(tuple => pointInPolygon(tuple.x, tuple.y, pixelLasso));
79
+ }
80
+
81
+
82
+
83
+ /**
84
+ * Performs a test if a point is inside a polygon based on the idea from
85
+ * https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
86
+ *
87
+ * This method will not need the same start/end point since it wraps around the edges of the array
88
+ *
89
+ * @param {*} test a point to test against
90
+ * @param {*} polygon a polygon in the form [[x,y], [x,y], ...]
91
+ * @returns true if the point lies inside the polygon, false otherwise
92
+ */
93
+ function pointInPolygon(testx, testy, polygon) {
94
+ let intersections = 0;
95
+
96
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
97
+ const [prevX, prevY] = polygon[j];
98
+ const [x, y] = polygon[i];
99
+
100
+ // count intersections
101
+ if (((y > testy) != (prevY > testy)) && (testx < (prevX - x) * (testy - y) / (prevY - y) + x)) {
102
+ intersections++;
103
+ }
104
+ }
105
+
106
+ // point is in polygon if intersection count is odd
107
+ return intersections & 1;
108
+ }
package/src/scales.js CHANGED
@@ -2,12 +2,21 @@ import {ScalePrefix} from './constants';
2
2
  import {scaleVisitor} from './visitors';
3
3
  import {Literal} from 'vega-expression';
4
4
  import {isFunction, isString, stringValue} from 'vega-util';
5
+ import {isRegisteredScale} from 'vega-scale';
5
6
 
6
- export function getScale(name, ctx) {
7
- let s;
8
- return isFunction(name) ? name
9
- : isString(name) ? (s = ctx.scales[name]) && s.value
10
- : undefined;
7
+ export function getScale(nameOrFunction, ctx) {
8
+
9
+ if (isFunction(nameOrFunction)) {
10
+ return nameOrFunction;
11
+ }
12
+
13
+ if (isString(nameOrFunction)) {
14
+ const maybeScale = ctx.scales[nameOrFunction];
15
+ return (maybeScale && isRegisteredScale(maybeScale.value)) ? maybeScale.value : undefined;
16
+
17
+ }
18
+
19
+ return undefined;
11
20
  }
12
21
 
13
22
  export function internalScaleFunctions(codegen, fnctx, visitors) {
package/rollup.config.js DELETED
@@ -1 +0,0 @@
1
- export { default } from '../../rollup.config';