@uwdata/vgplot 0.4.0 → 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.
Files changed (65) hide show
  1. package/README.md +4 -2
  2. package/dist/vgplot.js +5643 -5842
  3. package/dist/vgplot.min.js +14 -35
  4. package/package.json +8 -10
  5. package/src/api.js +292 -0
  6. package/src/connect.js +14 -0
  7. package/src/context.js +20 -0
  8. package/src/index.js +14 -303
  9. package/src/inputs.js +24 -0
  10. package/src/{directives → plot}/attributes.js +14 -5
  11. package/src/{directives → plot}/interactors.js +8 -6
  12. package/src/{directives → plot}/legends.js +14 -6
  13. package/src/{directives → plot}/marks.js +16 -13
  14. package/src/plot/named-plots.js +49 -0
  15. package/src/plot/plot.js +9 -0
  16. package/src/directives/plot.js +0 -39
  17. package/src/interactors/Highlight.js +0 -101
  18. package/src/interactors/Interval1D.js +0 -90
  19. package/src/interactors/Interval2D.js +0 -102
  20. package/src/interactors/Nearest.js +0 -66
  21. package/src/interactors/PanZoom.js +0 -121
  22. package/src/interactors/Toggle.js +0 -111
  23. package/src/interactors/util/brush.js +0 -45
  24. package/src/interactors/util/close-to.js +0 -9
  25. package/src/interactors/util/get-field.js +0 -4
  26. package/src/interactors/util/invert.js +0 -3
  27. package/src/interactors/util/patchScreenCTM.js +0 -13
  28. package/src/interactors/util/sanitize-styles.js +0 -9
  29. package/src/interactors/util/to-kebab-case.js +0 -9
  30. package/src/layout/index.js +0 -2
  31. package/src/legend.js +0 -64
  32. package/src/marks/ConnectedMark.js +0 -63
  33. package/src/marks/ContourMark.js +0 -89
  34. package/src/marks/DenseLineMark.js +0 -146
  35. package/src/marks/Density1DMark.js +0 -104
  36. package/src/marks/Density2DMark.js +0 -69
  37. package/src/marks/Grid2DMark.js +0 -191
  38. package/src/marks/HexbinMark.js +0 -88
  39. package/src/marks/Mark.js +0 -195
  40. package/src/marks/RasterMark.js +0 -122
  41. package/src/marks/RasterTileMark.js +0 -332
  42. package/src/marks/RegressionMark.js +0 -117
  43. package/src/marks/util/bin-field.js +0 -17
  44. package/src/marks/util/density.js +0 -226
  45. package/src/marks/util/extent.js +0 -56
  46. package/src/marks/util/grid.js +0 -57
  47. package/src/marks/util/handle-param.js +0 -14
  48. package/src/marks/util/is-arrow-table.js +0 -3
  49. package/src/marks/util/is-color.js +0 -18
  50. package/src/marks/util/is-constant-option.js +0 -40
  51. package/src/marks/util/is-symbol.js +0 -20
  52. package/src/marks/util/raster.js +0 -44
  53. package/src/marks/util/stats.js +0 -133
  54. package/src/marks/util/to-data-array.js +0 -58
  55. package/src/plot-attributes.js +0 -211
  56. package/src/plot-renderer.js +0 -161
  57. package/src/plot.js +0 -136
  58. package/src/spec/parse-data.js +0 -69
  59. package/src/spec/parse-spec.js +0 -422
  60. package/src/spec/to-module.js +0 -465
  61. package/src/spec/util.js +0 -43
  62. package/src/symbols.js +0 -3
  63. package/src/transforms/bin.js +0 -81
  64. package/src/transforms/index.js +0 -3
  65. /package/src/{directives → plot}/data.js +0 -0
@@ -1,133 +0,0 @@
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
- }
@@ -1,58 +0,0 @@
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
- export function arrowToObjects(data) {
11
- const { batches, numRows: length } = data;
12
-
13
- // return an empty array for empty tables
14
- if (!length) return [];
15
-
16
- // pre-allocate output objects
17
- const objects = Array.from({ length }, () => ({}));
18
-
19
- // for each row batch...
20
- for (let k = 0, b = 0; b < batches.length; ++b) {
21
- const batch = batches[b];
22
- const { schema, numRows, numCols } = batch;
23
-
24
- // for each column...
25
- for (let j = 0; j < numCols; ++j) {
26
- const child = batch.getChildAt(j);
27
- const { name, type } = schema.fields[j];
28
- const valueOf = convert(type);
29
-
30
- // for each row in the current batch...
31
- for (let o = k, i = 0; i < numRows; ++i, ++o) {
32
- // extract/convert value from arrow, copy to output object
33
- objects[o][name] = valueOf(child.get(i));
34
- }
35
- }
36
-
37
- k += numRows;
38
- }
39
-
40
- return objects;
41
- }
42
-
43
- function convert(type) {
44
- const { typeId } = type;
45
-
46
- // map timestamp numbers to date objects
47
- if (typeId === TIMESTAMP) {
48
- return v => v == null ? v : new Date(v);
49
- }
50
-
51
- // map bignum to number
52
- if (typeId === INTEGER && type.bitWidth >= 64) {
53
- return v => v == null ? v : Number(v);
54
- }
55
-
56
- // otherwise use Arrow JS defaults
57
- return v => v;
58
- }
@@ -1,211 +0,0 @@
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
- ['colorNice', 'color.nice'],
144
- ['colorScheme', 'color.scheme'],
145
- ['colorInterpolate', 'color.interpolate'],
146
- ['colorPivot', 'color.pivot'],
147
- ['colorSymmetric', 'color.symmetric'],
148
- ['colorLabel', 'color.label'],
149
- ['colorReverse', 'color.reverse'],
150
- ['colorZero', 'color.zero'],
151
- ['colorTickFormat', 'color.tickFormat'],
152
- ['opacityScale', 'opacity.type'],
153
- ['opacityDomain', 'opacity.domain'],
154
- ['opacityRange', 'opacity.range'],
155
- ['opacityClamp', 'opacity.clamp'],
156
- ['opacityNice', 'opacity.nice'],
157
- ['opacityLabel', 'opacity.label'],
158
- ['opacityReverse', 'opacity.reverse'],
159
- ['opacityZero', 'opacity.zero'],
160
- ['opacityTickFormat', 'opacity.tickFormat'],
161
- ['rScale', 'r.type'],
162
- ['rDomain', 'r.domain'],
163
- ['rRange', 'r.range'],
164
- ['rClamp', 'r.clamp'],
165
- ['rNice', 'r.nice'],
166
- ['rZero', 'r.zero'],
167
- ['lengthScale', 'length.type'],
168
- ['lengthDomain', 'length.domain'],
169
- ['lengthRange', 'length.range'],
170
- ['lengthClamp', 'length.clamp'],
171
- ['lengthNice', 'length.nice'],
172
- ['lengthZero', 'length.zero'],
173
- ['projectionType', 'projection.type'],
174
- ['projectionParallels', 'projection.parallels'],
175
- ['projectionPrecision', 'projection.precision'],
176
- ['projectionRotate', 'projection.rotate'],
177
- ['projectionDomain', 'projection.domain'],
178
- ['projectionInset', 'projection.inset'],
179
- ['projectionInsetLeft', 'projection.insetLeft'],
180
- ['projectionInsetRight', 'projection.insetRight'],
181
- ['projectionInsetTop', 'projection.insetTop'],
182
- ['projectionInsetBottom', 'projection.insetBottom'],
183
- ['projectionClip', 'projection.clip']
184
- ]);
185
-
186
- function setProperty(object, path, value) {
187
- for (let i = 0; i < path.length; ++i) {
188
- const key = path[i];
189
- if (i === path.length - 1) {
190
- object[key] = value;
191
- } else {
192
- object = (object[key] || (object[key] = {}));
193
- }
194
- }
195
- }
196
-
197
- export function setAttributes(attributes, spec, symbols) {
198
- // populate top-level and scale properties
199
- for (const key in attributes) {
200
- const specKey = attributeMap.get(key);
201
- if (specKey == null) {
202
- throw new Error(`Unrecognized plot attribute: ${key}`);
203
- }
204
- const value = attributes[key];
205
- if (typeof value === 'symbol') {
206
- symbols.push(key);
207
- } else if (value !== undefined) {
208
- setProperty(spec, specKey.split('.'), value);
209
- }
210
- }
211
- }
@@ -1,161 +0,0 @@
1
- import * as Plot from '@observablehq/plot';
2
- import { setAttributes } from './plot-attributes.js';
3
- import { Fixed } from './symbols.js';
4
-
5
- const OPTIONS_ONLY_MARKS = new Set([
6
- 'frame',
7
- 'hexgrid',
8
- 'sphere',
9
- 'graticule'
10
- ]);
11
-
12
-
13
-
14
- // construct Plot output
15
- // see https://github.com/observablehq/plot
16
- export async function plotRenderer(plot) {
17
- const spec = { marks: [] };
18
- const symbols = [];
19
- const { attributes, marks } = plot;
20
-
21
- // populate top-level and scale properties
22
- setAttributes(attributes, spec, symbols);
23
-
24
- // populate marks
25
- const indices = [];
26
- for (const mark of marks) {
27
- for (const { type, data, options } of mark.plotSpecs()) {
28
- if (OPTIONS_ONLY_MARKS.has(type)) {
29
- spec.marks.push(Plot[type](options));
30
- } else {
31
- spec.marks.push(Plot[type](data, options));
32
- }
33
- indices.push(mark.index);
34
- }
35
- }
36
-
37
- // infer labels
38
- inferLabels(spec, plot);
39
-
40
- // render plot
41
- const svg = Plot.plot(spec);
42
-
43
- // annotate svg with mark indices
44
- annotatePlot(svg, indices);
45
-
46
- // set symbol-valued attributes, such as fixed domains
47
- setSymbolAttributes(plot, svg, attributes, symbols);
48
-
49
- // initialize interactors
50
- for (const interactor of plot.interactors) {
51
- await interactor.init(svg);
52
- }
53
-
54
- return svg;
55
- }
56
-
57
- function setSymbolAttributes(plot, svg, attributes, symbols) {
58
- symbols.forEach(key => {
59
- const value = attributes[key];
60
- if (value === Fixed) {
61
- if (!key.endsWith('Domain')) {
62
- throw new Error(`Unsupported fixed attribute: ${key}`);
63
- }
64
- const type = key.slice(0, -'Domain'.length);
65
- const scale = svg.scale(type);
66
- if (scale?.domain) {
67
- plot.setAttribute(key, attributes[`${type}Reverse`]
68
- ? scale.domain.slice().reverse()
69
- : scale.domain);
70
- }
71
- } else {
72
- throw new Error(`Unrecognized symbol: ${value}`);
73
- }
74
- });
75
- }
76
-
77
- function inferLabels(spec, plot) {
78
- const { marks } = plot;
79
- inferLabel('x', spec, marks, ['x', 'x1', 'x2']);
80
- inferLabel('y', spec, marks, ['y', 'y1', 'y2']);
81
- inferLabel('fx', spec, marks);
82
- inferLabel('fy', spec, marks);
83
- }
84
-
85
- function inferLabel(key, spec, marks, channels = [key]) {
86
- const scale = spec[key] || {};
87
- if (scale.axis === null || scale.label !== undefined) return; // nothing to do
88
-
89
- const fields = marks.map(mark => mark.channelField(channels)?.field);
90
- if (fields.every(x => x == null)) return; // no columns found
91
-
92
- // check for consistent columns / labels
93
- let candCol;
94
- let candLabel;
95
- let type;
96
- for (let i = 0; i < fields.length; ++i) {
97
- const { column, label } = fields[i] || {};
98
- if (column === undefined && label === undefined) {
99
- continue;
100
- } else if (candCol === undefined && candLabel === undefined) {
101
- candCol = column;
102
- candLabel = label;
103
- type = getType(marks[i].data, channels) || 'number';
104
- } else if (candLabel !== label) {
105
- candLabel = undefined;
106
- } else if (candCol !== column) {
107
- candCol = undefined;
108
- }
109
- }
110
- let candidate = candLabel || candCol;
111
- if (candidate === undefined) return;
112
-
113
- // adjust candidate label formatting
114
- if ((type === 'number' || type === 'date') && (key === 'x' || key === 'y')) {
115
- if (scale.percent) candidate = `${candidate} (%)`;
116
- const order = (key === 'x' ? 1 : -1) * (scale.reverse ? -1 : 1);
117
- if (key === 'x' || scale.labelAnchor === 'center') {
118
- candidate = (key === 'x') === order < 0 ? `← ${candidate}` : `${candidate} →`;
119
- } else {
120
- candidate = `${order < 0 ? '↑ ' : '↓ '}${candidate}`;
121
- }
122
- }
123
-
124
- // add label to spec
125
- spec[key] = { ...scale, label: candidate };
126
- }
127
-
128
- function annotatePlot(svg, indices) {
129
- const facets = svg.querySelectorAll('g[aria-label="facet"]');
130
- if (facets.length) {
131
- for (const facet of facets) {
132
- annotateMarks(facet, indices);
133
- }
134
- } else {
135
- annotateMarks(svg, indices);
136
- }
137
- }
138
-
139
- function annotateMarks(svg, indices) {
140
- let index = -1;
141
- for (const child of svg.children) {
142
- const aria = child.getAttribute('aria-label') || '';
143
- const skip = child.nodeName === 'style'
144
- || aria.includes('-axis')
145
- || aria.includes('-grid');
146
- if (!skip) {
147
- child.setAttribute('data-index', indices[++index]);
148
- }
149
- }
150
- }
151
-
152
- function getType(data, channels) {
153
- for (const row of data) {
154
- for (let j = 0; j < channels.length; ++j) {
155
- const v = row[channels[j]];
156
- if (v != null) {
157
- return v instanceof Date ? 'date' : typeof v;
158
- }
159
- }
160
- }
161
- }
package/src/plot.js DELETED
@@ -1,136 +0,0 @@
1
- import { distinct, synchronizer } from '@uwdata/mosaic-core';
2
- import { plotRenderer } from './plot-renderer.js';
3
-
4
- const DEFAULT_ATTRIBUTES = {
5
- width: 640,
6
- marginLeft: 40,
7
- marginRight: 20,
8
- marginTop: 20,
9
- marginBottom: 30
10
- };
11
-
12
- export class Plot {
13
- constructor(element) {
14
- this.attributes = { ...DEFAULT_ATTRIBUTES };
15
- this.listeners = null;
16
- this.interactors = [];
17
- this.legends = [];
18
- this.marks = [];
19
- this.markset = null;
20
- this.element = element || document.createElement('div');
21
- this.element.setAttribute('class', 'plot');
22
- this.element.style.display = 'flex';
23
- this.element.value = this;
24
- this.params = new Map;
25
- this.synch = synchronizer();
26
- }
27
-
28
- margins() {
29
- return {
30
- left: this.getAttribute('marginLeft'),
31
- top: this.getAttribute('marginTop'),
32
- bottom: this.getAttribute('marginBottom'),
33
- right: this.getAttribute('marginRight')
34
- };
35
- }
36
-
37
- innerWidth() {
38
- const { left, right } = this.margins();
39
- return this.getAttribute('width') - left - right;
40
- }
41
-
42
- innerHeight() {
43
- const { top, bottom } = this.margins();
44
- return this.getAttribute('height') - top - bottom;
45
- }
46
-
47
- pending(mark) {
48
- this.synch.pending(mark);
49
- }
50
-
51
- update(mark) {
52
- if (this.synch.ready(mark) && !this.pendingRender) {
53
- this.pendingRender = true;
54
- requestAnimationFrame(() => this.render());
55
- }
56
- return this.synch.promise;
57
- }
58
-
59
- async render() {
60
- this.pendingRender = false;
61
- const svg = await plotRenderer(this);
62
- const legends = this.legends.flatMap(({ legend, include }) => {
63
- const el = legend.init(svg);
64
- return include ? el : [];
65
- });
66
- this.element.replaceChildren(svg, ...legends);
67
- this.synch.resolve();
68
- }
69
-
70
- getAttribute(name) {
71
- return this.attributes[name];
72
- }
73
-
74
- setAttribute(name, value, options) {
75
- if (distinct(this.attributes[name], value)) {
76
- if (value === undefined) {
77
- delete this.attributes[name];
78
- } else {
79
- this.attributes[name] = value;
80
- }
81
- if (!options?.silent) {
82
- this.listeners?.get(name)?.forEach(cb => cb(name, value));
83
- }
84
- return true;
85
- }
86
- return false;
87
- }
88
-
89
- addAttributeListener(name, callback) {
90
- const map = this.listeners || (this.listeners = new Map);
91
- if (!map.has(name)) map.set(name, new Set);
92
- map.get(name).add(callback);
93
- return this;
94
- }
95
-
96
- removeAttributeListener(name, callback) {
97
- return this.listeners?.get(name)?.delete(callback);
98
- }
99
-
100
- addParams(mark, paramSet) {
101
- const { params } = this;
102
- for (const param of paramSet) {
103
- if (params.has(param)) {
104
- params.get(param).push(mark);
105
- } else {
106
- params.set(param, [mark]);
107
- param.addEventListener('value', () => {
108
- return Promise.allSettled(
109
- params.get(param).map(mark => mark.requestQuery())
110
- );
111
- });
112
- }
113
- }
114
- }
115
-
116
- addMark(mark) {
117
- mark.setPlot(this, this.marks.length);
118
- this.marks.push(mark);
119
- this.markset = null;
120
- return this;
121
- }
122
-
123
- get markSet() {
124
- return this.markset || (this.markset = new Set(this.marks));
125
- }
126
-
127
- addInteractor(sel) {
128
- this.interactors.push(sel);
129
- return this;
130
- }
131
-
132
- addLegend(legend, include = true) {
133
- legend.setPlot(this);
134
- this.legends.push({ legend, include });
135
- }
136
- }