@uwdata/mosaic-plot 0.5.0
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.
- package/LICENSE +28 -0
- package/README.md +6 -0
- package/dist/mosaic-plot.js +42313 -0
- package/dist/mosaic-plot.min.js +69 -0
- package/package.json +38 -0
- package/src/index.js +30 -0
- package/src/interactors/Highlight.js +101 -0
- package/src/interactors/Interval1D.js +90 -0
- package/src/interactors/Interval2D.js +102 -0
- package/src/interactors/Nearest.js +66 -0
- package/src/interactors/PanZoom.js +121 -0
- package/src/interactors/Toggle.js +111 -0
- package/src/interactors/util/brush.js +45 -0
- package/src/interactors/util/close-to.js +9 -0
- package/src/interactors/util/get-field.js +4 -0
- package/src/interactors/util/invert.js +3 -0
- package/src/interactors/util/patchScreenCTM.js +13 -0
- package/src/interactors/util/sanitize-styles.js +9 -0
- package/src/interactors/util/to-kebab-case.js +9 -0
- package/src/legend.js +64 -0
- package/src/marks/ConnectedMark.js +66 -0
- package/src/marks/ContourMark.js +89 -0
- package/src/marks/DenseLineMark.js +146 -0
- package/src/marks/Density1DMark.js +104 -0
- package/src/marks/Density2DMark.js +69 -0
- package/src/marks/GeoMark.js +35 -0
- package/src/marks/Grid2DMark.js +191 -0
- package/src/marks/HexbinMark.js +88 -0
- package/src/marks/Mark.js +207 -0
- package/src/marks/RasterMark.js +121 -0
- package/src/marks/RasterTileMark.js +331 -0
- package/src/marks/RegressionMark.js +117 -0
- package/src/marks/util/bin-field.js +17 -0
- package/src/marks/util/density.js +226 -0
- package/src/marks/util/extent.js +56 -0
- package/src/marks/util/grid.js +57 -0
- package/src/marks/util/handle-param.js +14 -0
- package/src/marks/util/is-arrow-table.js +3 -0
- package/src/marks/util/is-color.js +18 -0
- package/src/marks/util/is-constant-option.js +41 -0
- package/src/marks/util/is-symbol.js +20 -0
- package/src/marks/util/raster.js +44 -0
- package/src/marks/util/stats.js +133 -0
- package/src/marks/util/to-data-array.js +70 -0
- package/src/plot-attributes.js +212 -0
- package/src/plot-renderer.js +161 -0
- package/src/plot.js +136 -0
- package/src/symbols.js +3 -0
- package/src/transforms/bin.js +81 -0
- package/src/transforms/index.js +3 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { scaleLinear } from 'd3';
|
|
2
|
+
import { Fixed, Transient } from '../../symbols.js';
|
|
3
|
+
|
|
4
|
+
export const xext = { x: ['min', 'max'] };
|
|
5
|
+
export const yext = { y: ['min', 'max'] };
|
|
6
|
+
export const xyext = { ...xext, ...yext };
|
|
7
|
+
|
|
8
|
+
export function plotExtent(mark, filter, channel, domainAttr, niceAttr) {
|
|
9
|
+
const { plot, stats } = mark;
|
|
10
|
+
const domain = plot.getAttribute(domainAttr);
|
|
11
|
+
const nice = plot.getAttribute(niceAttr);
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(domain) && !domain[Transient]) {
|
|
14
|
+
return domain;
|
|
15
|
+
} else {
|
|
16
|
+
const { field } = mark.channelField(channel);
|
|
17
|
+
const { column } = field;
|
|
18
|
+
const { min, max } = stats[column];
|
|
19
|
+
const dom = filteredExtent(filter, column) || (nice
|
|
20
|
+
? scaleLinear().domain([min, max]).nice().domain()
|
|
21
|
+
: [min, max]);
|
|
22
|
+
if (domain !== Fixed) dom[Transient] = true;
|
|
23
|
+
plot.setAttribute(domainAttr, dom, { silent: true });
|
|
24
|
+
return dom;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function extentX(mark, filter) {
|
|
29
|
+
return plotExtent(mark, filter, 'x', 'xDomain', 'xNice');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function extentY(mark, filter) {
|
|
33
|
+
return plotExtent(mark, filter, 'y', 'yDomain', 'yNice');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function filteredExtent(filter, column) {
|
|
37
|
+
if (!filter) return;
|
|
38
|
+
|
|
39
|
+
let lo;
|
|
40
|
+
let hi;
|
|
41
|
+
const visitor = (type, clause) => {
|
|
42
|
+
if (type === 'BETWEEN' && clause.field.column === column) {
|
|
43
|
+
const { range } = clause;
|
|
44
|
+
if (range && (lo == null || range[0] < lo)) lo = range[0];
|
|
45
|
+
if (range && (hi == null || range[1] > hi)) hi = range[1];
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
if (Array.isArray(filter)) {
|
|
50
|
+
filter.forEach(p => p.visit?.(visitor));
|
|
51
|
+
} else if (filter.visit) {
|
|
52
|
+
filter.visit(visitor);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return lo != null && hi != null && lo !== hi ? [lo, hi] : undefined;
|
|
56
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { isArrowTable } from './is-arrow-table.js';
|
|
2
|
+
|
|
3
|
+
export function grid1d(n, values) {
|
|
4
|
+
return valuesToGrid(new Float64Array(n), values);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function grid2d(m, n, values, groupby = []) {
|
|
8
|
+
return groupby.length
|
|
9
|
+
? Object.values(groupedValuesToGrids(m * n, values, groupby))
|
|
10
|
+
: [{ grid: valuesToGrid(new Float64Array(m * n), values) }];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function valuesToGrid(grid, values) {
|
|
14
|
+
if (isArrowTable(values)) {
|
|
15
|
+
// optimize access for Arrow tables
|
|
16
|
+
const numRows = values.numRows;
|
|
17
|
+
if (numRows === 0) return grid;
|
|
18
|
+
const index = values.getChild('index').toArray();
|
|
19
|
+
const value = values.getChild('value').toArray();
|
|
20
|
+
for (let row = 0; row < numRows; ++row) {
|
|
21
|
+
grid[index[row]] = value[row];
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
// fallback to iterable data
|
|
25
|
+
for (const row of values) {
|
|
26
|
+
grid[row.index] = row.value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return grid;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function groupedValuesToGrids(size, values, groupby) {
|
|
33
|
+
const grids = {};
|
|
34
|
+
const getGrid = key => {
|
|
35
|
+
const cell = grids[key] || (grids[key] = { key, grid: new Float64Array(size) });
|
|
36
|
+
return cell.grid;
|
|
37
|
+
};
|
|
38
|
+
if (isArrowTable(values)) {
|
|
39
|
+
// optimize access for Arrow tables
|
|
40
|
+
const numRows = values.numRows;
|
|
41
|
+
if (numRows === 0) return grids;
|
|
42
|
+
const index = values.getChild('index').toArray();
|
|
43
|
+
const value = values.getChild('value').toArray();
|
|
44
|
+
const groups = groupby.map(name => values.getChild(name));
|
|
45
|
+
for (let row = 0; row < numRows; ++row) {
|
|
46
|
+
const key = groups.map(vec => vec.get(row));
|
|
47
|
+
getGrid(key)[index[row]] = value[row];
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
// fallback to iterable data
|
|
51
|
+
for (const row of values) {
|
|
52
|
+
const key = groupby.map(col => row[col]);
|
|
53
|
+
getGrid(key)[row.index] = row.value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return grids;
|
|
57
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isParam } from '@uwdata/mosaic-core';
|
|
2
|
+
|
|
3
|
+
export function handleParam(client, key, param, update) {
|
|
4
|
+
if (isParam(param)) {
|
|
5
|
+
update = update || (() => client.requestUpdate());
|
|
6
|
+
param.addEventListener('value', value => {
|
|
7
|
+
client[key] = value;
|
|
8
|
+
return update();
|
|
9
|
+
});
|
|
10
|
+
client[key] = param.value;
|
|
11
|
+
} else {
|
|
12
|
+
client[key] = param;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { color } from 'd3';
|
|
2
|
+
|
|
3
|
+
// Mostly relies on d3-color, with a few extra color keywords. Currently this
|
|
4
|
+
// strictly requires that the value be a string; we might want to apply string
|
|
5
|
+
// coercion here, though note that d3-color instances would need to support
|
|
6
|
+
// valueOf to work correctly with InternMap.
|
|
7
|
+
// https://www.w3.org/TR/SVG11/painting.html#SpecifyingPaint
|
|
8
|
+
export function isColor(value) {
|
|
9
|
+
if (typeof value !== "string") return false;
|
|
10
|
+
value = value.toLowerCase().trim();
|
|
11
|
+
return (
|
|
12
|
+
value === "none" ||
|
|
13
|
+
value === "currentcolor" ||
|
|
14
|
+
(value.startsWith("url(") && value.endsWith(")")) || // <funciri>, e.g. pattern or gradient
|
|
15
|
+
(value.startsWith("var(") && value.endsWith(")")) || // CSS variable
|
|
16
|
+
color(value) !== null
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const constantOptions = new Set([
|
|
2
|
+
'order',
|
|
3
|
+
'sort',
|
|
4
|
+
'label',
|
|
5
|
+
'anchor',
|
|
6
|
+
'curve',
|
|
7
|
+
'tension',
|
|
8
|
+
'marker',
|
|
9
|
+
'markerStart',
|
|
10
|
+
'markerMid',
|
|
11
|
+
'markerEnd',
|
|
12
|
+
'textAnchor',
|
|
13
|
+
'lineAnchor',
|
|
14
|
+
'lineHeight',
|
|
15
|
+
'textOverflow',
|
|
16
|
+
'monospace',
|
|
17
|
+
'fontFamily',
|
|
18
|
+
'fontSize',
|
|
19
|
+
'fontStyle',
|
|
20
|
+
'fontVariant',
|
|
21
|
+
'fontWeight',
|
|
22
|
+
'frameAnchor',
|
|
23
|
+
'strokeLinejoin',
|
|
24
|
+
'strokeLinecap',
|
|
25
|
+
'strokeMiterlimit',
|
|
26
|
+
'strokeDasharray',
|
|
27
|
+
'strokeDashoffset',
|
|
28
|
+
'mixBlendMode',
|
|
29
|
+
'shapeRendering',
|
|
30
|
+
'imageRendering',
|
|
31
|
+
'preserveAspectRatio',
|
|
32
|
+
'interpolate',
|
|
33
|
+
'crossOrigin',
|
|
34
|
+
'paintOrder',
|
|
35
|
+
'pointerEvents',
|
|
36
|
+
'target'
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
export function isConstantOption(value) {
|
|
40
|
+
return constantOptions.has(value);
|
|
41
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const symbols = new Set([
|
|
2
|
+
'asterisk',
|
|
3
|
+
'circle',
|
|
4
|
+
'cross',
|
|
5
|
+
'diamond',
|
|
6
|
+
'diamond2',
|
|
7
|
+
'hexagon',
|
|
8
|
+
'plus',
|
|
9
|
+
'square',
|
|
10
|
+
'square2',
|
|
11
|
+
'star',
|
|
12
|
+
'times',
|
|
13
|
+
'triangle',
|
|
14
|
+
'triangle2',
|
|
15
|
+
'wye'
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export function isSymbol(value) {
|
|
19
|
+
return symbols.has(`${value}`.toLowerCase());
|
|
20
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { rgb } from 'd3';
|
|
2
|
+
|
|
3
|
+
export function raster(grid, data, w, h, scale, scheme) {
|
|
4
|
+
const n = (scheme.length >> 2) - 1;
|
|
5
|
+
for (let j = 0, k = 0; j < h; ++j) {
|
|
6
|
+
for (let i = 0, row = (h - j - 1) * w; i < w; ++i, k += 4) {
|
|
7
|
+
const c = (n * scale(grid[i + row])) << 2;
|
|
8
|
+
data[k + 0] = scheme[c + 0];
|
|
9
|
+
data[k + 1] = scheme[c + 1];
|
|
10
|
+
data[k + 2] = scheme[c + 2];
|
|
11
|
+
data[k + 3] = scheme[c + 3];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function palette(size, interp) {
|
|
17
|
+
const p = new Uint8ClampedArray(4 * size);
|
|
18
|
+
const n = size - 1;
|
|
19
|
+
for (let i = 0; i <= n; ++i) {
|
|
20
|
+
const v = interp(i / n);
|
|
21
|
+
const { r, g, b, opacity = 1 } = typeof v === 'string' ? rgb(v) : v;
|
|
22
|
+
const k = i << 2;
|
|
23
|
+
p[k + 0] = r;
|
|
24
|
+
p[k + 1] = g;
|
|
25
|
+
p[k + 2] = b;
|
|
26
|
+
p[k + 3] = (255 * opacity) | 0;
|
|
27
|
+
}
|
|
28
|
+
return p;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createCanvas(w, h) {
|
|
32
|
+
if (typeof document !== 'undefined') {
|
|
33
|
+
const c = document.createElement('canvas');
|
|
34
|
+
c.setAttribute('width', w);
|
|
35
|
+
c.setAttribute('height', h);
|
|
36
|
+
return c;
|
|
37
|
+
}
|
|
38
|
+
throw new Error('Can not create a canvas instance.');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function opacityMap(color = 'black') {
|
|
42
|
+
const { r, g, b } = rgb(color);
|
|
43
|
+
return opacity => ({ r, g, b, opacity });
|
|
44
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// https://github.com/jstat/jstat
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) 2013 jStat
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in
|
|
13
|
+
// all copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
|
|
23
|
+
export function ibetainv(p, a, b) {
|
|
24
|
+
var EPS = 1e-8;
|
|
25
|
+
var a1 = a - 1;
|
|
26
|
+
var b1 = b - 1;
|
|
27
|
+
var j = 0;
|
|
28
|
+
var lna, lnb, pp, t, u, err, x, al, h, w, afac;
|
|
29
|
+
if (p <= 0) return 0;
|
|
30
|
+
if (p >= 1) return 1;
|
|
31
|
+
if (a >= 1 && b >= 1) {
|
|
32
|
+
pp = p < 0.5 ? p : 1 - p;
|
|
33
|
+
t = Math.sqrt(-2 * Math.log(pp));
|
|
34
|
+
x = (2.30753 + t * 0.27061) / (1 + t * (0.99229 + t * 0.04481)) - t;
|
|
35
|
+
if (p < 0.5) x = -x;
|
|
36
|
+
al = (x * x - 3) / 6;
|
|
37
|
+
h = 2 / (1 / (2 * a - 1) + 1 / (2 * b - 1));
|
|
38
|
+
w = (x * Math.sqrt(al + h)) / h - (1 / (2 * b - 1) - 1 / (2 * a - 1)) * (al + 5 / 6 - 2 / (3 * h));
|
|
39
|
+
x = a / (a + b * Math.exp(2 * w));
|
|
40
|
+
} else {
|
|
41
|
+
lna = Math.log(a / (a + b));
|
|
42
|
+
lnb = Math.log(b / (a + b));
|
|
43
|
+
t = Math.exp(a * lna) / a;
|
|
44
|
+
u = Math.exp(b * lnb) / b;
|
|
45
|
+
w = t + u;
|
|
46
|
+
if (p < t / w) x = Math.pow(a * w * p, 1 / a);
|
|
47
|
+
else x = 1 - Math.pow(b * w * (1 - p), 1 / b);
|
|
48
|
+
}
|
|
49
|
+
afac = -gammaln(a) - gammaln(b) + gammaln(a + b);
|
|
50
|
+
for (; j < 10; j++) {
|
|
51
|
+
if (x === 0 || x === 1) return x;
|
|
52
|
+
err = ibeta(x, a, b) - p;
|
|
53
|
+
t = Math.exp(a1 * Math.log(x) + b1 * Math.log(1 - x) + afac);
|
|
54
|
+
u = err / t;
|
|
55
|
+
x -= t = u / (1 - 0.5 * Math.min(1, u * (a1 / x - b1 / (1 - x))));
|
|
56
|
+
if (x <= 0) x = 0.5 * (x + t);
|
|
57
|
+
if (x >= 1) x = 0.5 * (x + t + 1);
|
|
58
|
+
if (Math.abs(t) < EPS * x && j > 0) break;
|
|
59
|
+
}
|
|
60
|
+
return x;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function ibeta(x, a, b) {
|
|
64
|
+
// Factors in front of the continued fraction.
|
|
65
|
+
var bt =
|
|
66
|
+
x === 0 || x === 1 ? 0 : Math.exp(gammaln(a + b) - gammaln(a) - gammaln(b) + a * Math.log(x) + b * Math.log(1 - x));
|
|
67
|
+
if (x < 0 || x > 1) return false;
|
|
68
|
+
if (x < (a + 1) / (a + b + 2))
|
|
69
|
+
// Use continued fraction directly.
|
|
70
|
+
return (bt * betacf(x, a, b)) / a;
|
|
71
|
+
// else use continued fraction after making the symmetry transformation.
|
|
72
|
+
return 1 - (bt * betacf(1 - x, b, a)) / b;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function betacf(x, a, b) {
|
|
76
|
+
var fpmin = 1e-30;
|
|
77
|
+
var m = 1;
|
|
78
|
+
var qab = a + b;
|
|
79
|
+
var qap = a + 1;
|
|
80
|
+
var qam = a - 1;
|
|
81
|
+
var c = 1;
|
|
82
|
+
var d = 1 - (qab * x) / qap;
|
|
83
|
+
var m2, aa, del, h;
|
|
84
|
+
|
|
85
|
+
// These q's will be used in factors that occur in the coefficients
|
|
86
|
+
if (Math.abs(d) < fpmin) d = fpmin;
|
|
87
|
+
d = 1 / d;
|
|
88
|
+
h = d;
|
|
89
|
+
|
|
90
|
+
for (; m <= 100; m++) {
|
|
91
|
+
m2 = 2 * m;
|
|
92
|
+
aa = (m * (b - m) * x) / ((qam + m2) * (a + m2));
|
|
93
|
+
// One step (the even one) of the recurrence
|
|
94
|
+
d = 1 + aa * d;
|
|
95
|
+
if (Math.abs(d) < fpmin) d = fpmin;
|
|
96
|
+
c = 1 + aa / c;
|
|
97
|
+
if (Math.abs(c) < fpmin) c = fpmin;
|
|
98
|
+
d = 1 / d;
|
|
99
|
+
h *= d * c;
|
|
100
|
+
aa = (-(a + m) * (qab + m) * x) / ((a + m2) * (qap + m2));
|
|
101
|
+
// Next step of the recurrence (the odd one)
|
|
102
|
+
d = 1 + aa * d;
|
|
103
|
+
if (Math.abs(d) < fpmin) d = fpmin;
|
|
104
|
+
c = 1 + aa / c;
|
|
105
|
+
if (Math.abs(c) < fpmin) c = fpmin;
|
|
106
|
+
d = 1 / d;
|
|
107
|
+
del = d * c;
|
|
108
|
+
h *= del;
|
|
109
|
+
if (Math.abs(del - 1.0) < 3e-7) break;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return h;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function gammaln(x) {
|
|
116
|
+
var j = 0;
|
|
117
|
+
var cof = [
|
|
118
|
+
76.18009172947146, -86.5053203294167, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2,
|
|
119
|
+
-0.5395239384953e-5
|
|
120
|
+
];
|
|
121
|
+
var ser = 1.000000000190015;
|
|
122
|
+
var xx, y, tmp;
|
|
123
|
+
tmp = (y = xx = x) + 5.5;
|
|
124
|
+
tmp -= (xx + 0.5) * Math.log(tmp);
|
|
125
|
+
for (; j < 6; j++) ser += cof[j] / ++y;
|
|
126
|
+
return Math.log((2.506628274631 * ser) / xx) - tmp;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function qt(p, dof) {
|
|
130
|
+
var x = ibetainv(2 * Math.min(p, 1 - p), 0.5 * dof, 0.5);
|
|
131
|
+
x = Math.sqrt((dof * (1 - x)) / x);
|
|
132
|
+
return p > 0.5 ? x : -x;
|
|
133
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isArrowTable } from './is-arrow-table.js';
|
|
2
|
+
|
|
3
|
+
const INTEGER = 2;
|
|
4
|
+
const TIMESTAMP = 10;
|
|
5
|
+
|
|
6
|
+
export function toDataArray(data) {
|
|
7
|
+
return isArrowTable(data) ? arrowToObjects(data) : data;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Convert Apache Arrow tables to an array of vanilla JS objects.
|
|
12
|
+
* The internal conversions performed by the Arrow JS lib may not
|
|
13
|
+
* always produce values properly interpreted by Observable Plot.
|
|
14
|
+
* In addition, the Arrow JS lib uses Proxy objects to create
|
|
15
|
+
* row (tuple) objects, which can introduce some perf overhead.
|
|
16
|
+
* This method provides custom conversions of data that we can hand
|
|
17
|
+
* to Observable Plot. Internally, Plot will copy input data values
|
|
18
|
+
* into an array-based columnar organizations. If in the future Plot
|
|
19
|
+
* provides an efficient path to directly pass in columnar-data, we
|
|
20
|
+
* can revisit this method.
|
|
21
|
+
*/
|
|
22
|
+
export function arrowToObjects(data) {
|
|
23
|
+
const { batches, numRows: length } = data;
|
|
24
|
+
|
|
25
|
+
// return an empty array for empty tables
|
|
26
|
+
if (!length) return [];
|
|
27
|
+
|
|
28
|
+
// pre-allocate output objects
|
|
29
|
+
const objects = Array.from({ length }, () => ({}));
|
|
30
|
+
|
|
31
|
+
// for each row batch...
|
|
32
|
+
for (let k = 0, b = 0; b < batches.length; ++b) {
|
|
33
|
+
const batch = batches[b];
|
|
34
|
+
const { schema, numRows, numCols } = batch;
|
|
35
|
+
|
|
36
|
+
// for each column...
|
|
37
|
+
for (let j = 0; j < numCols; ++j) {
|
|
38
|
+
const child = batch.getChildAt(j);
|
|
39
|
+
const { name, type } = schema.fields[j];
|
|
40
|
+
const valueOf = convert(type);
|
|
41
|
+
|
|
42
|
+
// for each row in the current batch...
|
|
43
|
+
for (let o = k, i = 0; i < numRows; ++i, ++o) {
|
|
44
|
+
// extract/convert value from arrow, copy to output object
|
|
45
|
+
objects[o][name] = valueOf(child.get(i));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
k += numRows;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return objects;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function convert(type) {
|
|
56
|
+
const { typeId } = type;
|
|
57
|
+
|
|
58
|
+
// map timestamp numbers to date objects
|
|
59
|
+
if (typeId === TIMESTAMP) {
|
|
60
|
+
return v => v == null ? v : new Date(v);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// map bignum to number
|
|
64
|
+
if (typeId === INTEGER && type.bitWidth >= 64) {
|
|
65
|
+
return v => v == null ? v : Number(v);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// otherwise use Arrow JS defaults
|
|
69
|
+
return v => v;
|
|
70
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
export const attributeMap = new Map([
|
|
2
|
+
['style', 'style'],
|
|
3
|
+
['width', 'width'],
|
|
4
|
+
['height', 'height'],
|
|
5
|
+
['margin', 'margin'],
|
|
6
|
+
['marginLeft', 'marginLeft'],
|
|
7
|
+
['marginRight', 'marginRight'],
|
|
8
|
+
['marginTop', 'marginTop'],
|
|
9
|
+
['marginBottom', 'marginBottom'],
|
|
10
|
+
['align', 'align'],
|
|
11
|
+
['aspectRatio', 'aspectRatio'],
|
|
12
|
+
['axis', 'axis'],
|
|
13
|
+
['inset', 'inset'],
|
|
14
|
+
['grid', 'grid'],
|
|
15
|
+
['label', 'label'],
|
|
16
|
+
['padding', 'padding'],
|
|
17
|
+
['round', 'round'],
|
|
18
|
+
['xScale', 'x.type'],
|
|
19
|
+
['xDomain', 'x.domain'],
|
|
20
|
+
['xRange', 'x.range'],
|
|
21
|
+
['xNice', 'x.nice'],
|
|
22
|
+
['xInset', 'x.inset'],
|
|
23
|
+
['xInsetLeft', 'x.insetLeft'],
|
|
24
|
+
['xInsetRight', 'x.insetRight'],
|
|
25
|
+
['xClamp', 'x.clamp'],
|
|
26
|
+
['xRound', 'x.round'],
|
|
27
|
+
['xAlign', 'x.align'],
|
|
28
|
+
['xPadding', 'x.padding'],
|
|
29
|
+
['xPaddingInner', 'x.paddingInner'],
|
|
30
|
+
['xPaddingOuter', 'x.paddingOuter'],
|
|
31
|
+
['xAxis', 'x.axis'],
|
|
32
|
+
['xTicks', 'x.ticks'],
|
|
33
|
+
['xTickSize', 'x.tickSize'],
|
|
34
|
+
['xTickSpacing', 'x.tickSpacing'],
|
|
35
|
+
['xTickPadding', 'x.tickPadding'],
|
|
36
|
+
['xTickFormat', 'x.tickFormat'],
|
|
37
|
+
['xTickRotate', 'x.tickRotate'],
|
|
38
|
+
['xGrid', 'x.grid'],
|
|
39
|
+
['xLine', 'x.line'],
|
|
40
|
+
['xLabel', 'x.label'],
|
|
41
|
+
['xLabelAnchor', 'x.labelAnchor'],
|
|
42
|
+
['xLabelOffset', 'x.labelOffset'],
|
|
43
|
+
['xFontVariant', 'x.fontVariant'],
|
|
44
|
+
['xAriaLabel', 'x.ariaLabel'],
|
|
45
|
+
['xAriaDescription', 'x.ariaDescription'],
|
|
46
|
+
['xReverse', 'x.reverse'],
|
|
47
|
+
['xZero', 'x.zero'],
|
|
48
|
+
['yScale', 'y.type'],
|
|
49
|
+
['yDomain', 'y.domain'],
|
|
50
|
+
['yRange', 'y.range'],
|
|
51
|
+
['yNice', 'y.nice'],
|
|
52
|
+
['yInset', 'y.inset'],
|
|
53
|
+
['yInsetTop', 'y.insetTop'],
|
|
54
|
+
['yInsetBottom', 'y.insetBottom'],
|
|
55
|
+
['yClamp', 'y.clamp'],
|
|
56
|
+
['yRound', 'y.round'],
|
|
57
|
+
['yAlign', 'y.align'],
|
|
58
|
+
['yPadding', 'y.padding'],
|
|
59
|
+
['yPaddingInner', 'y.paddingInner'],
|
|
60
|
+
['yPaddingOuter', 'y.paddingOuter'],
|
|
61
|
+
['yAxis', 'y.axis'],
|
|
62
|
+
['yTicks', 'y.ticks'],
|
|
63
|
+
['yTickSize', 'y.tickSize'],
|
|
64
|
+
['yTickSpacing', 'y.tickSpacing'],
|
|
65
|
+
['yTickPadding', 'y.tickPadding'],
|
|
66
|
+
['yTickFormat', 'y.tickFormat'],
|
|
67
|
+
['yTickRotate', 'y.tickRotate'],
|
|
68
|
+
['yGrid', 'y.grid'],
|
|
69
|
+
['yLine', 'y.line'],
|
|
70
|
+
['yLabel', 'y.label'],
|
|
71
|
+
['yLabelAnchor', 'y.labelAnchor'],
|
|
72
|
+
['yLabelOffset', 'y.labelOffset'],
|
|
73
|
+
['yFontVariant', 'y.fontVariant'],
|
|
74
|
+
['yAriaLabel', 'y.ariaLabel'],
|
|
75
|
+
['yAriaDescription', 'y.ariaDescription'],
|
|
76
|
+
['yReverse', 'y.reverse'],
|
|
77
|
+
['yZero', 'y.zero'],
|
|
78
|
+
['facetMargin', 'facet.margin'],
|
|
79
|
+
['facetMarginTop', 'facet.marginTop'],
|
|
80
|
+
['facetMarginBottom', 'facet.marginBottom'],
|
|
81
|
+
['facetMarginLeft', 'facet.marginLeft'],
|
|
82
|
+
['facetMarginRight', 'facet.marginRight'],
|
|
83
|
+
['facetGrid', 'facet.grid'],
|
|
84
|
+
['facetLabel', 'facet.label'],
|
|
85
|
+
['fxDomain', 'fx.domain'],
|
|
86
|
+
['fxRange', 'fx.range'],
|
|
87
|
+
['fxNice', 'fx.nice'],
|
|
88
|
+
['fxInset', 'fx.inset'],
|
|
89
|
+
['fxInsetLeft', 'fx.insetLeft'],
|
|
90
|
+
['fxInsetRight', 'fx.insetRight'],
|
|
91
|
+
['fxRound', 'fx.round'],
|
|
92
|
+
['fxAlign', 'fx.align'],
|
|
93
|
+
['fxPadding', 'fx.padding'],
|
|
94
|
+
['fxPaddingInner', 'fx.paddingInner'],
|
|
95
|
+
['fxPaddingOuter', 'fx.paddingOuter'],
|
|
96
|
+
['fxAxis', 'fx.axis'],
|
|
97
|
+
['fxTicks', 'fx.ticks'],
|
|
98
|
+
['fxTickSize', 'fx.tickSize'],
|
|
99
|
+
['fxTickSpacing', 'fx.tickSpacing'],
|
|
100
|
+
['fxTickPadding', 'fx.tickPadding'],
|
|
101
|
+
['fxTickFormat', 'fx.tickFormat'],
|
|
102
|
+
['fxTickRotate', 'fx.tickRotate'],
|
|
103
|
+
['fxGrid', 'fx.grid'],
|
|
104
|
+
['fxLine', 'fx.line'],
|
|
105
|
+
['fxLabel', 'fx.label'],
|
|
106
|
+
['fxLabelAnchor', 'fx.labelAnchor'],
|
|
107
|
+
['fxLabelOffset', 'fx.labelOffset'],
|
|
108
|
+
['fxFontVariant', 'fx.fontVariant'],
|
|
109
|
+
['fxAriaLabel', 'fx.ariaLabel'],
|
|
110
|
+
['fxAriaDescription', 'fx.ariaDescription'],
|
|
111
|
+
['fxReverse', 'fx.reverse'],
|
|
112
|
+
['fyDomain', 'fy.domain'],
|
|
113
|
+
['fyRange', 'fy.range'],
|
|
114
|
+
['fyNice', 'fy.mice'],
|
|
115
|
+
['fyInset', 'fy,inset'],
|
|
116
|
+
['fyInsetTop', 'fy.insetTop'],
|
|
117
|
+
['fyInsetBottom', 'fy.insetBottom'],
|
|
118
|
+
['fyRound', 'fy.round'],
|
|
119
|
+
['fyAlign', 'fy.align'],
|
|
120
|
+
['fyPadding', 'fy.padding'],
|
|
121
|
+
['fyPaddingInner', 'fy.paddingInner'],
|
|
122
|
+
['fyPaddingOuter', 'fy.paddingOuter'],
|
|
123
|
+
['fyAxis', 'fy.axis'],
|
|
124
|
+
['fyTicks', 'fy.ticks'],
|
|
125
|
+
['fyTickSize', 'fy.tickSize'],
|
|
126
|
+
['fyTickSpacing', 'fy.tickSpacing'],
|
|
127
|
+
['fyTickPadding', 'fy.tickPadding'],
|
|
128
|
+
['fyTickFormat', 'fy.tickFormat'],
|
|
129
|
+
['fyTickRotate', 'fy.tickRotate'],
|
|
130
|
+
['fyGrid', 'fy.grid'],
|
|
131
|
+
['fyLine', 'fy.line'],
|
|
132
|
+
['fyLabel', 'fy.label'],
|
|
133
|
+
['fyLabelAnchor', 'fy.labelAnchor'],
|
|
134
|
+
['fyLabelOffset', 'fy.labelOffset'],
|
|
135
|
+
['fyFontVariant', 'fy.fontVariant'],
|
|
136
|
+
['fyAriaLabel', 'fy.ariaLabel'],
|
|
137
|
+
['fyAriaDescription', 'fy.ariaDescription'],
|
|
138
|
+
['fyReverse', 'fy.reverse'],
|
|
139
|
+
['colorScale', 'color.type'],
|
|
140
|
+
['colorDomain', 'color.domain'],
|
|
141
|
+
['colorRange', 'color.range'],
|
|
142
|
+
['colorClamp', 'color.clamp'],
|
|
143
|
+
['colorN', 'color.n'],
|
|
144
|
+
['colorNice', 'color.nice'],
|
|
145
|
+
['colorScheme', 'color.scheme'],
|
|
146
|
+
['colorInterpolate', 'color.interpolate'],
|
|
147
|
+
['colorPivot', 'color.pivot'],
|
|
148
|
+
['colorSymmetric', 'color.symmetric'],
|
|
149
|
+
['colorLabel', 'color.label'],
|
|
150
|
+
['colorReverse', 'color.reverse'],
|
|
151
|
+
['colorZero', 'color.zero'],
|
|
152
|
+
['colorTickFormat', 'color.tickFormat'],
|
|
153
|
+
['opacityScale', 'opacity.type'],
|
|
154
|
+
['opacityDomain', 'opacity.domain'],
|
|
155
|
+
['opacityRange', 'opacity.range'],
|
|
156
|
+
['opacityClamp', 'opacity.clamp'],
|
|
157
|
+
['opacityNice', 'opacity.nice'],
|
|
158
|
+
['opacityLabel', 'opacity.label'],
|
|
159
|
+
['opacityReverse', 'opacity.reverse'],
|
|
160
|
+
['opacityZero', 'opacity.zero'],
|
|
161
|
+
['opacityTickFormat', 'opacity.tickFormat'],
|
|
162
|
+
['rScale', 'r.type'],
|
|
163
|
+
['rDomain', 'r.domain'],
|
|
164
|
+
['rRange', 'r.range'],
|
|
165
|
+
['rClamp', 'r.clamp'],
|
|
166
|
+
['rNice', 'r.nice'],
|
|
167
|
+
['rZero', 'r.zero'],
|
|
168
|
+
['lengthScale', 'length.type'],
|
|
169
|
+
['lengthDomain', 'length.domain'],
|
|
170
|
+
['lengthRange', 'length.range'],
|
|
171
|
+
['lengthClamp', 'length.clamp'],
|
|
172
|
+
['lengthNice', 'length.nice'],
|
|
173
|
+
['lengthZero', 'length.zero'],
|
|
174
|
+
['projectionType', 'projection.type'],
|
|
175
|
+
['projectionParallels', 'projection.parallels'],
|
|
176
|
+
['projectionPrecision', 'projection.precision'],
|
|
177
|
+
['projectionRotate', 'projection.rotate'],
|
|
178
|
+
['projectionDomain', 'projection.domain'],
|
|
179
|
+
['projectionInset', 'projection.inset'],
|
|
180
|
+
['projectionInsetLeft', 'projection.insetLeft'],
|
|
181
|
+
['projectionInsetRight', 'projection.insetRight'],
|
|
182
|
+
['projectionInsetTop', 'projection.insetTop'],
|
|
183
|
+
['projectionInsetBottom', 'projection.insetBottom'],
|
|
184
|
+
['projectionClip', 'projection.clip']
|
|
185
|
+
]);
|
|
186
|
+
|
|
187
|
+
function setProperty(object, path, value) {
|
|
188
|
+
for (let i = 0; i < path.length; ++i) {
|
|
189
|
+
const key = path[i];
|
|
190
|
+
if (i === path.length - 1) {
|
|
191
|
+
object[key] = value;
|
|
192
|
+
} else {
|
|
193
|
+
object = (object[key] || (object[key] = {}));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function setAttributes(attributes, spec, symbols) {
|
|
199
|
+
// populate top-level and scale properties
|
|
200
|
+
for (const key in attributes) {
|
|
201
|
+
const specKey = attributeMap.get(key);
|
|
202
|
+
if (specKey == null) {
|
|
203
|
+
throw new Error(`Unrecognized plot attribute: ${key}`);
|
|
204
|
+
}
|
|
205
|
+
const value = attributes[key];
|
|
206
|
+
if (typeof value === 'symbol') {
|
|
207
|
+
symbols.push(key);
|
|
208
|
+
} else if (value !== undefined) {
|
|
209
|
+
setProperty(spec, specKey.split('.'), value);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|