@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,161 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
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
|
+
}
|
package/src/symbols.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { asColumn } from '@uwdata/mosaic-sql';
|
|
2
|
+
import { Transform } from '../symbols.js';
|
|
3
|
+
|
|
4
|
+
const EXTENT = [
|
|
5
|
+
'rectY-x', 'rectX-y', 'rect-x', 'rect-y'
|
|
6
|
+
];
|
|
7
|
+
|
|
8
|
+
function hasExtent(channel, type) {
|
|
9
|
+
return EXTENT.includes(`${type}-${channel}`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function bin(field, options = { steps: 25 }) {
|
|
13
|
+
const fn = (mark, channel) => {
|
|
14
|
+
return hasExtent(channel, mark.type)
|
|
15
|
+
? {
|
|
16
|
+
[`${channel}1`]: binField(mark, field, options),
|
|
17
|
+
[`${channel}2`]: binField(mark, field, { ...options, offset: 1 })
|
|
18
|
+
}
|
|
19
|
+
: {
|
|
20
|
+
[channel]: binField(mark, field, options)
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
fn[Transform] = true;
|
|
24
|
+
return fn;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function binField(mark, column, options) {
|
|
28
|
+
return {
|
|
29
|
+
column,
|
|
30
|
+
label: column,
|
|
31
|
+
get stats() { return ['min', 'max']; },
|
|
32
|
+
get columns() { return [column]; },
|
|
33
|
+
get basis() { return column; },
|
|
34
|
+
toString() {
|
|
35
|
+
const { min, max } = mark.stats[column];
|
|
36
|
+
const b = bins(min, max, options);
|
|
37
|
+
const col = asColumn(column);
|
|
38
|
+
const base = b.min === 0 ? col : `(${col} - ${b.min})`;
|
|
39
|
+
const alpha = `${(b.max - b.min) / b.steps}::DOUBLE`;
|
|
40
|
+
const off = options.offset ? `${options.offset} + ` : '';
|
|
41
|
+
return `${b.min} + ${alpha} * (${off}FLOOR(${base} / ${alpha})::INTEGER)`;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function bins(min, max, options) {
|
|
47
|
+
let { steps = 25, minstep = 0, nice = true } = options;
|
|
48
|
+
|
|
49
|
+
if (nice !== false) {
|
|
50
|
+
// use span to determine step size
|
|
51
|
+
const span = max - min;
|
|
52
|
+
const maxb = steps;
|
|
53
|
+
const logb = Math.LN10;
|
|
54
|
+
const level = Math.ceil(Math.log(maxb) / logb);
|
|
55
|
+
let step = Math.max(
|
|
56
|
+
minstep,
|
|
57
|
+
Math.pow(10, Math.round(Math.log(span) / logb) - level)
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// increase step size if too many bins
|
|
61
|
+
while (Math.ceil(span / step) > maxb) { step *= 10; }
|
|
62
|
+
|
|
63
|
+
// decrease step size if allowed
|
|
64
|
+
const div = [5, 2];
|
|
65
|
+
let v;
|
|
66
|
+
for (let i = 0, n = div.length; i < n; ++i) {
|
|
67
|
+
v = step / div[i];
|
|
68
|
+
if (v >= minstep && span / v <= maxb) step = v;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
v = Math.log(step);
|
|
72
|
+
const precision = v >= 0 ? 0 : ~~(-v / logb) + 1;
|
|
73
|
+
const eps = Math.pow(10, -precision - 1);
|
|
74
|
+
v = Math.floor(min / step + eps) * step;
|
|
75
|
+
min = min < v ? v - step : v;
|
|
76
|
+
max = Math.ceil(max / step) * step;
|
|
77
|
+
steps = Math.round((max - min) / step);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return { min, max, steps };
|
|
81
|
+
}
|