Spaces:
Running
Running
File size: 23,577 Bytes
967f228 638568b 94bddc8 638568b 7ade112 638568b 967f228 638568b 94bddc8 bec7df7 631e2c6 967f228 7ade112 bec7df7 638568b 94bddc8 638568b 94bddc8 967f228 7ade112 638568b 0784d2a 638568b 0784d2a 638568b 0784d2a 638568b 0784d2a 638568b 0784d2a db3a530 0784d2a db3a530 0784d2a 94bddc8 638568b 94bddc8 638568b 94bddc8 638568b 967f228 638568b 94bddc8 638568b bec7df7 631e2c6 7ade112 967f228 638568b 967f228 638568b 967f228 638568b 967f228 638568b 967f228 638568b 967f228 638568b 967f228 638568b bec7df7 638568b bec7df7 638568b bec7df7 638568b bec7df7 638568b 967f228 638568b 967f228 112d72a 3e7d5ea 112d72a b254d51 7ade112 b254d51 dd437e2 b254d51 638568b 94bddc8 b254d51 94bddc8 967f228 638568b 0784d2a 638568b b254d51 dd437e2 b254d51 967f228 638568b 0784d2a 967f228 94bddc8 638568b 967f228 b254d51 0784d2a 638568b 94bddc8 b254d51 967f228 638568b 0784d2a 638568b 3e7d5ea 967f228 94bddc8 967f228 638568b 0784d2a b254d51 638568b 3e7d5ea 967f228 638568b 967f228 638568b 3e7d5ea 967f228 0784d2a 3e7d5ea 967f228 112d72a 3e7d5ea 967f228 638568b b254d51 0784d2a 638568b 3e7d5ea b254d51 3e7d5ea 112d72a dd437e2 b254d51 3e7d5ea b254d51 3e7d5ea b254d51 dd437e2 b254d51 638568b dc74802 638568b dc74802 638568b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | // The linked views of Pick (#2422), drawn by Mosaic vgplot over DuckDB-WASM in the browser: the domain by form map and
// the bars by domain and by form. One crossfilter selection holds the filters: a click on a bar or a map cell
// and the filter panel set its clauses; the charts, the counts of the filter options, the tables behind the charts and
// the list of selected sources all read it. app.js draws the rest of the page and loads this module after the first
// screen has painted. Every chart draws one layer only (#1083), the one the clause kind selects, in its color and its
// measure: material sets in the main color, in sets or units; corpora in gray, in rows. The two are never stacked or
// added. The same DuckDB-WASM holds meta/preview.parquet once a row's "Preview rows" first opens.
// Standard tool: linked charts over one crossfilter selection by Mosaic vgplot (wasmConnector, DuckDB-WASM).
import * as vg from 'https://cdn.jsdelivr.net/npm/@uwdata/vgplot@0.34.3/+esm';
import { widest as measured } from './charts.js';
// #2422: the page's colors in its theme (style.css), read when a chart is drawn, so a new theme draws them again
// (app.js): each layer's color (data.js KINDS: --ours for material sets, --other for corpora), the empty part of a map
// cell (--map-empty), the low end of a layer's color scale on the map (--map-low-ours, --map-low-other: its color at 20%
// over the empty cell's, so a cell with a small count still shows apart from an empty one) and the ink of the labels
const css = (name) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
const colors = () => ({ kind: { material_set: css('--ours'), corpus: css('--other') }, empty: css('--map-empty'),
low: { material_set: css('--map-low-ours'), corpus: css('--map-low-other') }, ink: css('--ink-2') });
export const sel = vg.Selection.crossfilter();
// The page's own columns of every source (app.js pageRow, from data.js), joined to meta/catalog.parquet of the commit
// the page reads. A domain key is a top-level code, _multi or _none (dkeys, a list); codes are the domain codes with
// their leaves; hcol is the map's column (a form, _other or _none); kind is the layer, material_set or corpus;
// collection is that of a material set ('' for a corpus); n_units counts the units of a material set and n_rows the
// rows of a corpus (0 for the other kind); #1524: exts are its file types and ext_rows the rows of each, in the same
// order; #2006: stages are the ids of the training stages one of its units passes (the commit's stage figures); #2422: pii is its
// personal-data mark (none, marked or unknown), sizeb the size class of its deduplicated size. The view dom holds one
// row per source and domain key (dkey): DuckDB does not take UNNEST next to an aggregate
// in one SELECT, so the domain charts and their tables group dom by dkey.
const SRC = `CREATE OR REPLACE TABLE src AS SELECT p.source, c.title,
CAST(p.dkeys AS VARCHAR[]) AS dkeys, CAST(p.codes AS VARCHAR[]) AS codes, CAST(p.formkey AS VARCHAR) AS formkey,
CAST(p.hcol AS VARCHAR) AS hcol, CAST(p.kind AS VARCHAR) AS kind, CAST(p.collection AS VARCHAR) AS collection,
CAST(p.status AS VARCHAR) AS status,
CAST(p.n_src AS DOUBLE) AS n_src, CAST(p.n_units AS DOUBLE) AS n_units, CAST(p.n_rows AS DOUBLE) AS n_rows,
CAST(p.sizeb AS VARCHAR) AS sizeb, CAST(p.tier AS VARCHAR) AS tier,
CAST(p.train AS VARCHAR) AS train, CAST(p.redist AS VARCHAR) AS redist, CAST(p.pii AS VARCHAR) AS pii,
CAST(p.qtext AS VARCHAR) AS qtext,
CAST(p.exts AS VARCHAR[]) AS exts, CAST(p.ext_rows AS DOUBLE[]) AS ext_rows, CAST(p.stages AS VARCHAR[]) AS stages
FROM page p LEFT JOIN catalog c USING (source)`;
const DOM = 'CREATE OR REPLACE VIEW dom AS SELECT *, UNNEST(dkeys) AS dkey FROM src';
// the page's DuckDB-WASM, once open
let duck = null;
/** Opens DuckDB-WASM and loads the tables: catalog (the catalog file the page read), page (rows), src and dom. */
export async function open(catalogFile, rows) {
const connector = vg.wasmConnector();
vg.coordinator().databaseConnector(connector);
const db = await connector.getDuckDB();
duck = db;
const name = `catalog.${catalogFile.format}`;
await db.registerFileBuffer(name, catalogFile.bytes);
await db.registerFileText('page.json', JSON.stringify(rows));
const load = catalogFile.format === 'json' ? vg.loadJSON : vg.loadParquet;
await vg.coordinator().exec([
load('catalog', name, { replace: true }),
vg.loadJSON('page', 'page.json', { replace: true }),
SRC,
DOM,
]);
}
// ---- the first rows of each source: meta/preview.parquet of the page's commit (the index's subset preview), loaded
// whole into the table preview the first time a row's "Preview rows" opens, and read from there for every source
let previewLoad = null;
/** The rows of source in meta/preview.parquet (read: a function that answers that file at the page's commit as a Blob,
* data.js wholeFile), in the file's order, without the column source. */
export async function previewRows(read, source) {
previewLoad ??= (async () => {
await duck.registerFileBuffer('preview.parquet', new Uint8Array(await (await read()).arrayBuffer()));
await vg.coordinator().exec([vg.loadParquet('preview', 'preview.parquet', { replace: true })]);
})().catch((e) => { previewLoad = null; throw e; });
await previewLoad;
const rows = await vg.coordinator().query(`SELECT * EXCLUDE (source) FROM preview WHERE source = ${lit(source)}`);
return rows.toArray().map((r) => ({ ...r }));
}
// ---- the metric of the charts, in the layer shown: material sets (sets) or their units, or the rows of corpora; its
// sum, and the same sum as text for the labels at the bar ends
const AGG = { sets: () => vg.sum('n_src'), units: () => vg.sum('n_units'), rows: () => vg.sum('n_rows') };
const LABEL = {
sets: () => vg.sql`format('{:,}', CAST(sum(n_src) AS BIGINT))`,
units: () => vg.sql`format('{:,}', CAST(sum(n_units) AS BIGINT))`,
rows: () => vg.sql`format('{:,}', CAST(sum(n_rows) AS BIGINT))`,
};
// the columns of the tables behind the charts, over the layer shown: its sources (n), the units of material sets and
// the rows of corpora (0 in the other layer)
const SUMS = 'count(*) AS n, sum(n_units) AS units, sum(n_rows) AS rows';
const whereOf = (filter) => (filter && filter.length ? `WHERE ${filter.map((p) => `(${p})`).join(' AND ')}` : '');
/** A client of the selection that runs one SQL query (sql(where)) and hands its rows to onRows. */
class Rows extends vg.MosaicClient {
constructor(sql, onRows, onError) {
super(sel);
this.sql = sql;
this.onRows = onRows;
this.onError = onError;
}
query(filter) { return this.sql(whereOf(filter)); }
queryResult(data) { this.onRows(data.toArray().map((r) => ({ ...r }))); return this; }
queryError(e) { this.onError?.(e); return this; }
}
const connect = (client) => { vg.coordinator().connect(client); return client; };
// ---- the filters: one clause source per filter, its predicate over src
const KEYS = ['domain', 'leaf', 'form', 'collection', 'kind', 'status', 'train', 'redist', 'tier', 'size', 'q', 'ext', 'stage', 'pii'];
const SOURCES = Object.fromEntries(KEYS.map((k) => [k, { filter: k, reset() {} }]));
// a clause that keeps every row, made the active clause last: the next round of queries then gives every client its
// own predicate (a client of the active clause would be skipped). A new clause object each time, so the selection emits.
const SETTLED = { filter: 'settled', reset() {} };
const settle = () => sel.update({ source: SETTLED, value: null, predicate: vg.sql`TRUE`, meta: { type: 'point' } });
const lit = vg.literal;
const PREDICATE = {
domain: (v) => vg.sql`list_has_any(dkeys, [${lit(v)}])`,
leaf: (v) => vg.sql`list_has_any(codes, [${lit(v)}])`,
form: (v) => vg.sql`formkey = ${lit(v)}`,
collection: (v) => vg.sql`collection = ${lit(v)}`,
kind: (v) => vg.sql`kind = ${lit(v)}`,
status: (v) => vg.sql`status = ${lit(v)}`,
train: (v) => (v === 'yes' ? vg.sql`train = 'yes'` : vg.sql`train <> 'no'`),
redist: (v) => (v === 'yes' ? vg.sql`redist = 'yes'` : vg.sql`redist <> 'no'`),
tier: (v) => vg.sql`tier = ${lit(v)}`,
size: (v) => vg.sql`sizeb = ${lit(v)}`,
q: (v) => vg.sql`contains(qtext, ${lit(v.toLowerCase())})`,
// #1524: the file types selected, joined by commas: a source with rows of any of them
ext: (v) => vg.or(...v.split(',').map((e) => vg.sql`list_contains(exts, ${lit(e)})`)),
// #2006: a source one of whose units passes the stage's condition (the commit's stage figures)
stage: (v) => vg.sql`list_contains(stages, ${lit(v)})`,
// #2422: the personal-data mark
pii: (v) => vg.sql`pii = ${lit(v)}`,
};
// what each filter's option counts are over: one value per row, or a list unnested; #2422: the filters the panel shows
// (app.js SELECTS), the file types apart (startFacets)
const FACET = {
stage: 'UNNEST(stages)',
domain: 'UNNEST(dkeys)',
form: 'formkey',
size: 'sizeb',
train: "UNNEST(CASE train WHEN 'yes' THEN ['yes', 'cond'] WHEN 'cond' THEN ['cond'] ELSE []::VARCHAR[] END)",
pii: 'pii',
};
// clients that a filter's clause leaves out: the facet of that filter, and the chart that sets it (its marks and its
// table); a click on the map leaves the map out of the domain and form clauses it sets; #2422: the layer leaves out
// the counts of the stages, each of which takes its own layer (app.js setFilter)
const facets = {};
const views = {};
const ALSO = { kind: ['stage', 'across'] };
const own = (key) => {
const out = new Set();
for (const k of [key, ...(ALSO[key] || [])]) if (facets[k]) out.add(facets[k]);
const chart = key === 'domain' ? views.dom : key === 'form' ? views.form : null;
for (const c of chart?.clients || []) out.add(c);
return out;
};
const origins = {};
function setClause(k, v, origin) {
origins[k] = origin;
const clients = own(k);
if (origin === 'map') for (const c of views.heat?.clients || []) clients.add(c);
sel.update({ source: SOURCES[k], clients, value: v || null, predicate: v ? PREDICATE[k](v) : null, meta: { type: 'point' } });
}
/** Sets the clauses of keys from F (the page's filter values; '' for off). origin 'map' when a map cell set them. */
export function apply(F, keys, origin = '') {
for (const k of keys) if (SOURCES[k]) setClause(k, F[k], origin);
settle();
syncToggles(F);
}
/** The filters whose clauses filter a chart's table ('dom', 'form' or 'heat'): every one on but its chart's own. */
export function filtersOf(view) {
const table = views[view]?.table;
return KEYS.filter((k) => sel.clauses.some((c) => c.source === SOURCES[k] && !c.clients?.has(table)));
}
/** The counts of the filter options under the current selection: onCounts(key, Map value -> sources, total); #1524:
* for the file types (ext), Map type -> its rows in the sources selected (each source's ext_rows), total the sources. */
export function startFacets(onCounts, onError) {
const counted = (k) => (rows) => {
const m = new Map(rows.map((r) => [r.k, Number(r.n)]));
onCounts(k, m, m.get('*') || 0);
};
for (const [k, expr] of Object.entries(FACET)) {
facets[k] = new Rows((where) => `SELECT CAST(k AS VARCHAR) AS k, count(*)::INTEGER AS n FROM (SELECT ${expr} AS k FROM src ${where}) GROUP BY k
UNION ALL SELECT '*', count(*)::INTEGER FROM src ${where}`, counted(k), onError);
}
facets.ext = new Rows((where) => `SELECT CAST(k AS VARCHAR) AS k, sum(n)::DOUBLE AS n FROM (SELECT UNNEST(exts) AS k, UNNEST(ext_rows) AS n FROM src ${where}) GROUP BY k
UNION ALL SELECT '*', count(*)::DOUBLE FROM src ${where}`, counted('ext'), onError);
}
export const connectFacets = () => Object.values(facets).forEach(connect);
/** #2422, second round: the ids of the sources the filters keep in either layer (every clause but the layer's), in no
* order: the field draws those of the layer not shown at half light. Called before the clauses are first set (apply),
* so the layer's clause leaves this client out, and connected with the facets (connectFacets). */
export function startAcross(onIds, onError) {
facets.across = new Rows((where) => `SELECT source FROM src ${where}`, (rows) => onIds(new Set(rows.map((r) => r.source))), onError);
}
/** The ids of the selected sources, in no order. */
export function startSelection(onIds, onError) {
connect(new Rows((where) => `SELECT source FROM src ${where}`, (rows) => onIds(new Set(rows.map((r) => r.source))), onError));
}
// ---- the charts. Each view: {el, pick, plot, table, clients}; clients are its marks and its table client. The ink of
// their text is the theme's (colors).
const STYLE = { fontFamily: 'inherit', fontSize: '15px', overflow: 'visible' };
// the font of the tick labels of Plot's color legend (the d3 axis it draws with sets it)
const LEGEND_FONT = '12.5px sans-serif'; // as style.css sets it on the legend (the axis itself says 10px)
/** The widest of texts in px (charts.js widest), drawn in font: by default the charts' font, STYLE.fontSize and the
* page's font family. */
const widest = (texts, font = `${STYLE.fontSize} ${getComputedStyle(document.body).fontFamily}`) =>
measured(texts, font);
// the room between a y axis label and the frame (Plot's tickPadding with tickSize 0, and a margin)
const AXIS_GAP = 10;
// the room left of the frame when the labels are drawn above the bars: half the width of the x axis's first tick
// label ("0"), which is centred on the frame's left edge and would otherwise be cut there
const EDGE_GAP = 8;
// the band of one bar when its label is drawn above it: the step, and the share of it between two bars
const STEP_ABOVE = 48;
const PAD_ABOVE = 0.4;
// The bars of a key, in the color of the layer shown (kind), the total at the end. metricWord and keyWord: the words
// the tooltip names its lines by (the scales' labels; the axes show none). The labels are measured: when the widest
// fits in 40 % of the width they are the y axis, its margin set from that width; otherwise each is a text mark on its
// own line above its bar, at the left edge, the y axis hidden, so the bars take the full width and no label is cut.
function bars({ field, keys, name, kind, metricWord, keyWord, metric, fmt, width, onPick, onTable, onError }) {
const c = colors();
const pick = vg.Selection.intersect();
// the bars by domain read dom, one row per source and domain key
const rows = field === 'dkey' ? 'dom' : 'src';
const from = () => vg.from(rows, { filterBy: sel });
const label = (k) => name(k);
const room = Math.ceil(widest(keys.map(label))) + AXIS_GAP;
const beside = room <= width * 0.4;
const thick = STEP_ABOVE * (1 - PAD_ABOVE);
const keyLabels = beside
? [vg.axisY({ tickFormat: label, tickSize: 0, label: null }), vg.marginLeft(room), vg.height(30 + 30 * keys.length)]
: [vg.yAxis(null), vg.yPadding(PAD_ABOVE), vg.marginLeft(EDGE_GAP), vg.height(32 + Math.round(STEP_ABOVE * (keys.length + PAD_ABOVE))),
vg.text(keys.map((k) => ({ k, t: label(k) })), { y: 'k', text: 't', frameAnchor: 'left', textAnchor: 'start',
lineAnchor: 'bottom', dy: -Math.round(thick / 2 + 3), fill: c.ink })];
const el = vg.plot(
vg.gridX({ ticks: 3 }),
vg.barX(from(), { x: AGG[metric](), y: field, fill: c.kind[kind], inset: 0.5,
tip: { format: { x: fmt, y: label } } }),
vg.toggleY({ as: pick }),
vg.text(from(), { x: AGG[metric](), y: field, text: LABEL[metric](), textAnchor: 'start', dx: 6, fill: c.ink }),
...keyLabels,
vg.axisX({ anchor: 'top', ticks: 3, tickFormat: fmt, label: null }),
vg.yDomain(keys),
vg.xLabel(metricWord), vg.yLabel(keyWord),
vg.width(width),
vg.marginRight(90), vg.marginTop(24), vg.marginBottom(8),
vg.style({ ...STYLE, color: c.ink }),
);
const table = new Rows((where) => `SELECT ${field} AS k, ${SUMS}
FROM ${rows} ${where} GROUP BY k ORDER BY k`, onTable, onError);
onRelease(el, (v) => onPick(v ? String(v[0]) : ''));
return { el, pick, plot: el.value, table };
}
// The map: a light cell where the selection has sources, filled in the color scale of the layer shown (kind) by its
// count (a symlog scale, its legend below, drawn from the same scale). The light cell takes the clicks; rowWord, colWord
// and metricWord name the tooltip's lines (the scales' labels; the axes show none). The row labels' margin is their
// measured width; #1216: the column labels are level, wrapped to the width of a column (Plot's lineWidth, in ems), and
// the bottom margin is the room their lines take.
const LINE_PX = 16;
/** #2422: Plot breaks a label only at spaces and hyphens (lineBreaks of
* https://github.com/observablehq/plot/blob/v0.6.17/src/marks/text.js), so a label of a script written without them
* (Chinese) would run into its neighbours' at any width: it is broken at the word boundaries Intl.Segmenter finds (a
* word wider than the room at its characters), each line within px; a label Plot can break, or that fits, is left as it
* is. */
function breakLines(text, px) {
if (/[\s-]/.test(text) || widest([text]) <= px) return text;
const words = [...new Intl.Segmenter(document.documentElement.lang, { granularity: 'word' }).segment(text)]
.flatMap(({ segment }) => (widest([segment]) <= px ? [segment] : [...segment]));
const lines = [''];
for (const w of words) {
if (lines.at(-1) && widest([lines.at(-1) + w]) > px) lines.push(w);
else lines[lines.length - 1] += w;
}
return lines.join('\n');
}
function heat({ rows, cols, rowName, colName, kind, rowWord, colWord, metric, fmt, width, metricWord, onPick, onTable, onError }) {
const c = colors();
const pick = vg.Selection.intersect();
const y = 'dkey';
const left = Math.ceil(widest(rows.map(rowName))) + AXIS_GAP;
const right = 20;
const w = Math.max(width, left + 26 * cols.length + right);
const colPx = (w - left - right) / Math.max(1, cols.length);
const lineWidth = Math.max(2, (colPx - 4) / parseFloat(STYLE.fontSize));
const colLabel = (k) => breakLines(colName(k), colPx - 4);
const lines = Math.max(1, ...cols.map((k) => {
const text = colLabel(k);
return text.includes('\n') ? text.split('\n').length : Math.ceil(widest([text]) / Math.max(1, colPx - 4));
}));
const el = vg.plot(
vg.cell(vg.from('dom', { filterBy: sel }), { x: 'hcol', y, fill: c.empty, inset: 1, rx: 3 }),
vg.toggle({ as: pick, channels: ['x', 'y'] }),
vg.cell(vg.from('dom', { filterBy: sel }), { x: 'hcol', y, fill: AGG[metric](), inset: 1, rx: 3,
pointerEvents: 'none', tip: { format: { x: colName, y: rowName, fill: fmt } } }),
vg.axisX({ tickFormat: colLabel, tickRotate: 0, tickSize: 0, lineWidth, label: null }),
vg.axisY({ tickFormat: rowName, tickSize: 0, label: null }),
vg.xDomain(cols), vg.yDomain(rows), vg.xPadding(0),
vg.xLabel(colWord), vg.yLabel(rowWord), vg.colorLabel(metricWord),
vg.colorScale('symlog'), vg.colorRange([c.low[kind], c.kind[kind]]),
vg.colorLegend({ label: metricWord, tickFormat: fmt, color: c.ink, width: LEGEND_WIDTH, marginLeft: LEGEND_MARGIN, marginRight: LEGEND_MARGIN }),
vg.width(w), vg.height(20 + LINE_PX * lines + 32 * rows.length),
vg.marginLeft(left), vg.marginBottom(12 + LINE_PX * lines), vg.marginTop(8), vg.marginRight(right),
vg.style({ ...STYLE, color: c.ink }),
);
legendTicks(el.value, fmt);
const table = new Rows((where) => `SELECT dkey AS k, formkey AS f, ${SUMS}
FROM dom ${where} GROUP BY k, f ORDER BY k, f`, onTable, onError);
onRelease(el, (v) => onPick(v ? { col: String(v[0]), row: String(v[1]) } : null));
return { el, pick, plot: el.value, table };
}
// The ticks of the map's color legend: the symlog scale crowds Plot's default ticks at its high end, so they are
// given: 0, the powers of ten up to the largest cell value, and that value when it is not a power of ten and its label
// does not touch the last one's. Mosaic builds the legend at every render from its options; ticks is read then, from
// the cells as last drawn (the filled cell mark's fill column).
const LEGEND_WIDTH = 300; // of the ramp legend (Plot's default is 240), for the larger tick labels
// the legend's room at each end, so the labels of its end ticks (0 and the largest value) are not cut at the edge
const LEGEND_MARGIN = 10;
function legendTicks(plot, fmt) {
const legend = plot.legends[0].legend;
const mark = plot.marks.find((m) => m.channelField('fill'));
Object.defineProperty(legend.options, 'ticks', { enumerable: true, get() {
const values = mark.data?.columns?.fill;
if (!values?.length) return undefined;
const max = Math.max(...Array.from(values, Number));
if (!(max > 0)) return [0];
const ticks = [0];
for (let p = 1; p <= max; p *= 10) ticks.push(p);
const last = ticks[ticks.length - 1];
const at = (v) => ((LEGEND_WIDTH - 2 * LEGEND_MARGIN) * Math.log1p(v)) / Math.log1p(max);
const half = (v) => widest([fmt(v)], LEGEND_FONT) / 2;
if (max !== last && at(max) - at(last) >= half(max) + half(last) + 4) ticks.push(max);
return ticks;
} });
}
// The toggle of a chart sets its value on pointerdown; the pick is read when the pointer is released (the last value
// picked, or null). A release with nothing picked before or after (a click on the empty part of a chart) picks nothing;
// a touch that scrolls the page instead is cancelled, and the toggle goes back to the filters.
// The tips of a chart are Observable Plot's pointer interaction, whose pointerdown listener on the same svg is added
// before the toggle's and calls stopImmediatePropagation on a mouse pointerdown (to pin the tip): the toggle would never
// see a mouse click. The listener here runs first, in the capture phase, and makes that call a no-op for this event.
let filters = {};
function onRelease(el, pick) {
const value = () => el.value.interactors[0]?.value;
let before = null;
el.addEventListener('pointerdown', (evt) => {
before = value();
evt.stopImmediatePropagation = () => {};
}, { capture: true });
el.addEventListener('pointerup', () => {
const v = value();
if (!before?.length && !v?.length) return;
pick(Array.isArray(v) && v.length ? v[v.length - 1] : null);
});
el.addEventListener('pointercancel', () => syncToggles(filters));
}
const disconnect = (view) => {
if (!view) return;
for (const c of view.clients) vg.coordinator().disconnect(c);
};
/** (Re)draws the three charts; opts: {dom, form, heat}, each the options of bars or heat. -> their elements. After a
* redraw the domain and form clauses are set again, so the new marks skip the clauses of their own chart. */
export function draw(opts, F) {
for (const k of ['dom', 'form', 'heat']) {
disconnect(views[k]);
const v = k === 'heat' ? heat(opts[k]) : bars(opts[k]);
v.clients = new Set([...v.plot.marks, v.table]);
views[k] = v;
connect(v.table);
}
for (const k of ['domain', 'form']) setClause(k, F[k], origins[k] || '');
settle();
syncToggles(F);
return { dom: views.dom.el, form: views.form.el, heat: views.heat.el };
}
// a toggle keeps its own list of picked values; it follows the filters, so the next click on a bar or a cell starts
// from them
function syncToggles(F) {
filters = F;
const set = (view, value) => {
const toggle = view?.plot.interactors[0];
if (toggle) toggle.value = value;
};
set(views.dom, F.domain ? [[F.domain]] : null);
set(views.form, F.form ? [[F.form]] : null);
set(views.heat, F.domain && F.form ? [[F.form, F.domain]] : null);
}
|