@peter.prib/renderengine 0.7.0 → 0.9.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/REFERENCE.md +85 -0
- package/dist/renderengine-workspace.umd.js +960 -147
- package/dist/renderengine.umd.js +960 -147
- package/package.json +2 -2
- package/src/chartCatalog.js +10 -0
- package/src/charting/ChartChooser.js +243 -0
- package/src/charting/PopupMenu.js +152 -150
- package/src/charting/charts/TableChart.js +98 -0
- package/src/charting/table/TableView.js +263 -0
- package/src/charting/table/TableViewChart.js +126 -0
- package/src/index.js +79 -1
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Chart } from '../Chart.js';
|
|
2
|
+
import { ChartBox, ChartLabels } from '../ChartBox.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* TableChart — rows of values as a table: a header row of column names, then one row per record,
|
|
6
|
+
* numeric columns right-aligned. Drawn with the renderer's primitives like every chart, so it works
|
|
7
|
+
* under SVG and canvas and needs no DOM table. Values are shown exactly (not an axis's "1.20k"):
|
|
8
|
+
* a table is where a reader goes for the actual number.
|
|
9
|
+
*/
|
|
10
|
+
export class TableChart extends Chart {
|
|
11
|
+
static chartType = 'table';
|
|
12
|
+
|
|
13
|
+
/** Layout constants for the rows. */
|
|
14
|
+
static ROW_H = 26;
|
|
15
|
+
static PAD_X = 8;
|
|
16
|
+
static HEADER_FILL = '#eef1f6';
|
|
17
|
+
static STRIPE_FILL = '#f8f9fb';
|
|
18
|
+
static RULE = { color: '#d9dde4', width: 1 };
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The columns to show: `options.columns` (names, or `{ key, label?, align? }`), else every key of
|
|
22
|
+
* the first row. A column is right-aligned when every value it holds is a number (or null).
|
|
23
|
+
*/
|
|
24
|
+
static columns(data, given) {
|
|
25
|
+
const specs = (given ?? Object.keys(data[0] ?? {})).map(c => (typeof c === 'string' ? { key: c } : { ...c }));
|
|
26
|
+
return specs.map(c => ({
|
|
27
|
+
key: c.key,
|
|
28
|
+
label: c.label ?? c.key,
|
|
29
|
+
align: c.align ?? (data.length > 0 && data.every(r => r[c.key] == null || typeof r[c.key] === 'number') ? 'end' : 'start'),
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The table has no axes, so none of a chart's axis margins: an edge all round, and room for a title. */
|
|
34
|
+
static margins(title) {
|
|
35
|
+
return { left: 8, right: 8, top: title ? 50 : 8, bottom: 8 };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The height a table of `rowCount` rows needs — header, rows (at most `maxRows`), the "more rows"
|
|
40
|
+
* line, a title and the edges — so a page can size its container to fit before drawing.
|
|
41
|
+
*/
|
|
42
|
+
static heightFor(rowCount, { title, maxRows = Infinity } = {}) {
|
|
43
|
+
const shown = Math.min(rowCount, maxRows);
|
|
44
|
+
const more = rowCount > shown ? 1 : 0;
|
|
45
|
+
const { top, bottom } = TableChart.margins(title);
|
|
46
|
+
return top + (1 + shown + more) * TableChart.ROW_H + bottom;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A cell's text: a number exactly (to at most 4 decimals), null/undefined as `nullText`. */
|
|
50
|
+
static cellText(value, nullText = '—') {
|
|
51
|
+
if (value == null) return nullText;
|
|
52
|
+
if (typeof value === 'number') return Number.isInteger(value) ? String(value) : String(Number(value.toFixed(4)));
|
|
53
|
+
if (value instanceof Date) return value.toISOString().slice(0, 10);
|
|
54
|
+
return String(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_render() {
|
|
58
|
+
const data = this.data ?? [];
|
|
59
|
+
const box = ChartBox.fromRenderer(this.renderer, TableChart.margins(this.options.title));
|
|
60
|
+
const { x = box.x, y = box.y, width = box.width, title, maxRows = Infinity, nullText = '—' } = this.options;
|
|
61
|
+
const { ROW_H, PAD_X } = TableChart;
|
|
62
|
+
const columns = TableChart.columns(data, this.options.columns);
|
|
63
|
+
const colW = columns.length > 0 ? width / columns.length : width;
|
|
64
|
+
|
|
65
|
+
new ChartLabels({ title }).draw(this.renderer, { x, y, width, height: box.height });
|
|
66
|
+
|
|
67
|
+
const shown = data.slice(0, maxRows);
|
|
68
|
+
let top = y;
|
|
69
|
+
this.renderer.rect(x, top, width, ROW_H, TableChart.HEADER_FILL, null);
|
|
70
|
+
columns.forEach((c, i) => this._cell(c.label, c, x + i * colW, top, colW, { fontWeight: 'bold', fill: '#333' }));
|
|
71
|
+
top += ROW_H;
|
|
72
|
+
this._rule(x, top, width);
|
|
73
|
+
|
|
74
|
+
shown.forEach((row, r) => {
|
|
75
|
+
if (r % 2 === 1) this.renderer.rect(x, top, width, ROW_H, TableChart.STRIPE_FILL, null);
|
|
76
|
+
columns.forEach((c, i) => this._cell(TableChart.cellText(row[c.key], nullText), c, x + i * colW, top, colW, { fill: '#222' }));
|
|
77
|
+
top += ROW_H;
|
|
78
|
+
this._rule(x, top, width);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (data.length > shown.length) {
|
|
82
|
+
this.renderer.text(`${data.length - shown.length} more row(s) not shown`, x + PAD_X, top + ROW_H / 2, {
|
|
83
|
+
textAnchor: 'start', verticalAlign: 'middle', fontSize: '11px', fill: '#888',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_cell(text, column, left, top, colW, attrs) {
|
|
89
|
+
const end = column.align === 'end';
|
|
90
|
+
return this.renderer.text(text, end ? left + colW - TableChart.PAD_X : left + TableChart.PAD_X, top + TableChart.ROW_H / 2, {
|
|
91
|
+
textAnchor: end ? 'end' : 'start', verticalAlign: 'middle', fontSize: '12px', ...attrs,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
_rule(x, y, width) {
|
|
96
|
+
this.renderer.path(`M${x},${y} L${x + width},${y}`, 'none', TableChart.RULE);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { ChartChooser } from '../ChartChooser.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TableView — the state of an interactive table over result rows, kept apart from drawing so all of
|
|
5
|
+
* it can be tested without a DOM: the sort, the filters, the hidden columns and the page, and the
|
|
6
|
+
* menus a table offers on a cell, a row, a column and the table itself (as PopupMenu item specs).
|
|
7
|
+
*
|
|
8
|
+
* Filtering is by choosing values from the table, not typing: a cell's menu keeps or excludes rows
|
|
9
|
+
* with its value (and, for a number, rows above or below it); a column's menu filters to one of its
|
|
10
|
+
* values. The table's menu offers "Show as", the chart types ChartChooser says fit what is showing.
|
|
11
|
+
*
|
|
12
|
+
* Every change calls `onChange(view)`; the host redraws. A host adds its own items with
|
|
13
|
+
* `menus: { cell(ctx), row(ctx), column(ctx), table(ctx) }`, each returning PopupMenu items.
|
|
14
|
+
*/
|
|
15
|
+
const FILTER_OPS = {
|
|
16
|
+
'=': (v, x) => v === x,
|
|
17
|
+
'!=': (v, x) => v !== x,
|
|
18
|
+
'>': (v, x) => v != null && v > x,
|
|
19
|
+
'<': (v, x) => v != null && v < x,
|
|
20
|
+
'is null': (v) => v == null,
|
|
21
|
+
'is not null': (v) => v != null,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Order of two values for a sort: numbers numerically, everything else as text; nulls last. */
|
|
25
|
+
function compareValues(a, b) {
|
|
26
|
+
if (a == null) return b == null ? 0 : 1;
|
|
27
|
+
if (b == null) return -1;
|
|
28
|
+
if (typeof a === 'number' && typeof b === 'number') return a - b;
|
|
29
|
+
const x = String(a), y = String(b);
|
|
30
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A value as copied text: null as empty, an object as JSON. */
|
|
34
|
+
function plain(value) {
|
|
35
|
+
if (value == null) return '';
|
|
36
|
+
if (value instanceof Date) return value.toISOString();
|
|
37
|
+
return typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class TableView {
|
|
41
|
+
/** The most distinct values a column's "Filter to" menu lists. */
|
|
42
|
+
static MAX_FILTER_VALUES = 20;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {Array<object>} rows
|
|
46
|
+
* @param {object} [options]
|
|
47
|
+
* @param {Array<{name, kind?, measure?}>} [options.columns] hints, as ChartChooser takes them
|
|
48
|
+
* @param {boolean} [options.complete] false when the rows are only part of the answer (a top N)
|
|
49
|
+
* @param {number} [options.pageSize] rows per page (50; Infinity for all)
|
|
50
|
+
* @param {function} [options.onChange] called with the view after every change
|
|
51
|
+
* @param {object} [options.menus] the host's own items: { cell, row, column, table } → items
|
|
52
|
+
* @param {function} [options.copy] writes text to the clipboard (navigator.clipboard by default)
|
|
53
|
+
* @param {ChartChooser} [options.chooser]
|
|
54
|
+
*/
|
|
55
|
+
constructor(rows, { columns = null, complete = true, pageSize = 50, onChange = null, menus = {}, copy = null, chooser = null } = {}) {
|
|
56
|
+
this.rows = rows;
|
|
57
|
+
this.columns = ChartChooser.describe(rows, columns);
|
|
58
|
+
this.complete = complete;
|
|
59
|
+
this.pageSize = pageSize;
|
|
60
|
+
this.onChange = onChange;
|
|
61
|
+
this.menus = menus;
|
|
62
|
+
this.copy = copy ?? TableView.clipboard;
|
|
63
|
+
this.chooser = chooser ?? new ChartChooser();
|
|
64
|
+
this.sort = null; // { column, direction: 'asc' | 'desc' }
|
|
65
|
+
this.filters = []; // { column, op, value }
|
|
66
|
+
this.hidden = new Set();
|
|
67
|
+
this.page = 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static clipboard(text) {
|
|
71
|
+
return globalThis.navigator?.clipboard?.writeText(text);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ── What is showing ───────────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
visibleColumns() { return this.columns.filter(c => !this.hidden.has(c.name)); }
|
|
77
|
+
|
|
78
|
+
filtered() {
|
|
79
|
+
return this.filters.length === 0 ? this.rows
|
|
80
|
+
: this.rows.filter(r => this.filters.every(f => FILTER_OPS[f.op](r[f.column], f.value)));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The filtered rows in sort order (a copy; equal values keep their order). */
|
|
84
|
+
ordered() {
|
|
85
|
+
const rows = this.filtered();
|
|
86
|
+
if (this.sort === null) return rows;
|
|
87
|
+
const { column, direction } = this.sort;
|
|
88
|
+
const sign = direction === 'desc' ? -1 : 1;
|
|
89
|
+
// Nulls stay last whichever way: a reader looks for values, not for blanks.
|
|
90
|
+
return [...rows].sort((a, b) => (a[column] == null || b[column] == null ? compareValues(a[column], b[column]) : sign * compareValues(a[column], b[column])));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pageCount() {
|
|
94
|
+
return Number.isFinite(this.pageSize) ? Math.max(1, Math.ceil(this.filtered().length / this.pageSize)) : 1;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The rows on the current page, each with its position in the ordered rows (`index`, from 0). */
|
|
98
|
+
pageRows() {
|
|
99
|
+
const rows = this.ordered();
|
|
100
|
+
const start = Number.isFinite(this.pageSize) ? this.page * this.pageSize : 0;
|
|
101
|
+
const end = Number.isFinite(this.pageSize) ? start + this.pageSize : rows.length;
|
|
102
|
+
return rows.slice(start, end).map((row, i) => ({ row, index: start + i }));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** "Region = West · Revenue > 100", or '' with no filters. */
|
|
106
|
+
describeFilters() {
|
|
107
|
+
return this.filters.map(f => (f.op.startsWith('is') ? `${f.column} ${f.op}` : `${f.column} ${f.op} ${plain(f.value)}`)).join(' · ');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ── Changes ───────────────────────────────────────────────────────────────
|
|
111
|
+
|
|
112
|
+
changed() {
|
|
113
|
+
this.page = Math.min(this.page, this.pageCount() - 1);
|
|
114
|
+
this.onChange?.(this);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A header click: unsorted → ascending → descending → unsorted. */
|
|
118
|
+
toggleSort(column) {
|
|
119
|
+
const current = this.sort?.column === column ? this.sort.direction : null;
|
|
120
|
+
this.sortBy(column, current === null ? 'asc' : current === 'asc' ? 'desc' : null);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
sortBy(column, direction) {
|
|
124
|
+
this.sort = direction === null ? null : { column, direction };
|
|
125
|
+
this.page = 0;
|
|
126
|
+
this.changed();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
addFilter(column, op, value = null) {
|
|
130
|
+
if (!Object.hasOwn(FILTER_OPS, op)) throw new Error(`Unknown filter "${op}"`);
|
|
131
|
+
this.filters = [...this.filters.filter(f => !(f.column === column && f.op === op && f.value === value)), { column, op, value }];
|
|
132
|
+
this.page = 0;
|
|
133
|
+
this.changed();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
removeFilters(column) {
|
|
137
|
+
this.filters = this.filters.filter(f => f.column !== column);
|
|
138
|
+
this.changed();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
clearFilters() {
|
|
142
|
+
this.filters = [];
|
|
143
|
+
this.changed();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Hides a column; the last one showing cannot be hidden. */
|
|
147
|
+
hide(column) {
|
|
148
|
+
if (this.visibleColumns().length <= 1) return;
|
|
149
|
+
this.hidden.add(column);
|
|
150
|
+
this.changed();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
showAll() {
|
|
154
|
+
this.hidden.clear();
|
|
155
|
+
this.changed();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
goToPage(page) {
|
|
159
|
+
this.page = Math.max(0, Math.min(page, this.pageCount() - 1));
|
|
160
|
+
this.changed();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ── Text out ──────────────────────────────────────────────────────────────
|
|
164
|
+
|
|
165
|
+
/** Rows as tab-separated text, header first, the visible columns only. */
|
|
166
|
+
asTsv(rows = this.ordered()) {
|
|
167
|
+
const columns = this.visibleColumns().map(c => c.name);
|
|
168
|
+
const clean = (v) => plain(v).replace(/[\t\r\n]+/g, ' ');
|
|
169
|
+
return [columns.map(clean).join('\t'), ...rows.map(r => columns.map(c => clean(r[c])).join('\t'))].join('\n');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ── Menus (PopupMenu items) ──────────────────────────────────────────────
|
|
173
|
+
|
|
174
|
+
cellMenu(row, column) {
|
|
175
|
+
const value = row[column];
|
|
176
|
+
const kind = this.columns.find(c => c.name === column)?.kind;
|
|
177
|
+
const shown = value == null ? 'empty' : plain(value);
|
|
178
|
+
const items = value == null
|
|
179
|
+
? [{ label: `Only rows where ${column} is empty`, action: () => this.addFilter(column, 'is null') },
|
|
180
|
+
{ label: `Exclude rows where ${column} is empty`, action: () => this.addFilter(column, 'is not null') }]
|
|
181
|
+
: [{ label: `Only rows where ${column} = ${shown}`, action: () => this.addFilter(column, '=', value) },
|
|
182
|
+
{ label: `Exclude rows where ${column} = ${shown}`, action: () => this.addFilter(column, '!=', value) },
|
|
183
|
+
...(kind === 'numeric' ? [
|
|
184
|
+
{ label: `Only rows where ${column} > ${shown}`, action: () => this.addFilter(column, '>', value) },
|
|
185
|
+
{ label: `Only rows where ${column} < ${shown}`, action: () => this.addFilter(column, '<', value) },
|
|
186
|
+
] : [])];
|
|
187
|
+
return [
|
|
188
|
+
...items,
|
|
189
|
+
{ separator: true },
|
|
190
|
+
{ label: 'Copy value', action: () => this.copy(plain(value)) },
|
|
191
|
+
...this.extra('cell', { row, column, value }),
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
rowMenu(row) {
|
|
196
|
+
return [
|
|
197
|
+
{ label: 'Copy row', action: () => this.copy(this.asTsv([row])) },
|
|
198
|
+
{ label: 'Copy row as JSON', action: () => this.copy(JSON.stringify(row)) },
|
|
199
|
+
...this.extra('row', { row }),
|
|
200
|
+
];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
columnMenu(column) {
|
|
204
|
+
const sorted = this.sort?.column === column ? this.sort.direction : null;
|
|
205
|
+
const values = [...new Set(this.filtered().map(r => r[column]))].sort(compareValues);
|
|
206
|
+
const listed = values.slice(0, TableView.MAX_FILTER_VALUES);
|
|
207
|
+
return [
|
|
208
|
+
{ label: 'Sort ascending', checked: sorted === 'asc', action: () => this.sortBy(column, 'asc') },
|
|
209
|
+
{ label: 'Sort descending', checked: sorted === 'desc', action: () => this.sortBy(column, 'desc') },
|
|
210
|
+
...(sorted ? [{ label: 'Clear sort', action: () => this.sortBy(column, null) }] : []),
|
|
211
|
+
{ separator: true },
|
|
212
|
+
{
|
|
213
|
+
label: 'Filter to',
|
|
214
|
+
items: [
|
|
215
|
+
...listed.map(v => (v == null
|
|
216
|
+
? { label: '(empty)', action: () => this.addFilter(column, 'is null') }
|
|
217
|
+
: { label: plain(v), action: () => this.addFilter(column, '=', v) })),
|
|
218
|
+
...(values.length > listed.length ? [{ label: `${values.length - listed.length} more — filter from a cell`, disabled: true }] : []),
|
|
219
|
+
],
|
|
220
|
+
},
|
|
221
|
+
...(this.filters.some(f => f.column === column) ? [{ label: `Clear filters on ${column}`, action: () => this.removeFilters(column) }] : []),
|
|
222
|
+
{ separator: true },
|
|
223
|
+
{ label: 'Hide column', disabled: this.visibleColumns().length <= 1, action: () => this.hide(column) },
|
|
224
|
+
{ label: 'Copy column', action: () => this.copy([column, ...this.ordered().map(r => plain(r[column]))].join('\n')) },
|
|
225
|
+
...this.extra('column', { column }),
|
|
226
|
+
];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** The table's menu; `onShowAs(choice)` draws a chosen chart (the host's — RenderEngine.resultView). */
|
|
230
|
+
tableMenu(onShowAs = null) {
|
|
231
|
+
const choices = onShowAs ? this.chartChoices() : [];
|
|
232
|
+
return [
|
|
233
|
+
...(onShowAs ? [{
|
|
234
|
+
label: 'Show as',
|
|
235
|
+
items: choices.length > 0
|
|
236
|
+
? choices.map(c => ({ label: `${c.label} — ${c.reason}`, action: () => onShowAs(c) }))
|
|
237
|
+
: [{ label: 'No chart fits these columns', disabled: true }],
|
|
238
|
+
}, { separator: true }] : []),
|
|
239
|
+
{ label: 'Clear all filters', disabled: this.filters.length === 0, action: () => this.clearFilters() },
|
|
240
|
+
{ label: 'Show hidden columns', disabled: this.hidden.size === 0, action: () => this.showAll() },
|
|
241
|
+
{ label: 'Copy table', action: () => this.copy(this.asTsv()) },
|
|
242
|
+
...this.extra('table', {}),
|
|
243
|
+
];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* The chart types that fit what the table is showing — its filtered, sorted rows and visible
|
|
248
|
+
* columns — as ChartChooser gives them, without the table. Filtered rows are not the whole
|
|
249
|
+
* answer, so they are never offered as shares of a whole.
|
|
250
|
+
*/
|
|
251
|
+
chartChoices() {
|
|
252
|
+
const columns = this.visibleColumns();
|
|
253
|
+
const names = columns.map(c => c.name);
|
|
254
|
+
const rows = this.ordered().map(r => Object.fromEntries(names.map(n => [n, r[n]])));
|
|
255
|
+
return this.chooser.choose(rows, columns, { complete: this.complete && this.filters.length === 0 })
|
|
256
|
+
.filter(c => c.key !== 'table');
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
extra(kind, context) {
|
|
260
|
+
const items = this.menus?.[kind]?.({ view: this, ...context }) ?? [];
|
|
261
|
+
return items.length > 0 ? [{ separator: true }, ...items] : [];
|
|
262
|
+
}
|
|
263
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { TableChart } from '../charts/TableChart.js';
|
|
2
|
+
import { ChartBox, ChartLabels } from '../ChartBox.js';
|
|
3
|
+
import { PopupMenu } from '../PopupMenu.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* TableViewChart — the interactive table: a TableChart drawn from a TableView's state, whose parts
|
|
7
|
+
* answer clicks. A header sorts (and its ▾ opens the column's menu); a cell opens the cell's menu; a
|
|
8
|
+
* row number opens the row's menu; the corner ⋮ opens the table's menu (with "Show as" when the host
|
|
9
|
+
* can draw a chart); the footer's ‹ › page. Each change goes through the view, whose `onChange`
|
|
10
|
+
* redraws.
|
|
11
|
+
*
|
|
12
|
+
* Interaction needs a renderer whose shapes are DOM elements (SVG: `supportsSceneGraph`); anywhere
|
|
13
|
+
* else it draws the same table without the clicks.
|
|
14
|
+
*/
|
|
15
|
+
export class TableViewChart extends TableChart {
|
|
16
|
+
static GUTTER_W = 44;
|
|
17
|
+
static MENU_GLYPH = '▾';
|
|
18
|
+
static TABLE_GLYPH = '⋮';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param renderer
|
|
22
|
+
* @param {TableView} view
|
|
23
|
+
* @param {object} [options] title, nullText; onShowAs(choice) — draws a chart for the table menu's
|
|
24
|
+
* "Show as"; showMenu(x, y, items) — opens a menu (PopupMenu.show by default)
|
|
25
|
+
*/
|
|
26
|
+
constructor(renderer, view, options = {}) {
|
|
27
|
+
super(renderer, view.rows, options);
|
|
28
|
+
this.view = view;
|
|
29
|
+
this.showMenu = options.showMenu ?? ((x, y, items) => PopupMenu.show(x, y, items));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The height this draws: title, header, the page's rows and the footer. */
|
|
33
|
+
static heightFor(view, { title } = {}) {
|
|
34
|
+
const { top, bottom } = TableChart.margins(title);
|
|
35
|
+
return top + (2 + view.pageRows().length) * TableChart.ROW_H + bottom;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_render() {
|
|
39
|
+
const { view } = this;
|
|
40
|
+
const { title, nullText = '—' } = this.options;
|
|
41
|
+
const { ROW_H, PAD_X } = TableChart;
|
|
42
|
+
const box = ChartBox.fromRenderer(this.renderer, TableChart.margins(title));
|
|
43
|
+
const interactive = this.renderer.supportsSceneGraph === true;
|
|
44
|
+
const columns = view.visibleColumns().map(c => ({ key: c.name, label: c.name, align: c.kind === 'numeric' ? 'end' : 'start' }));
|
|
45
|
+
const gutter = TableViewChart.GUTTER_W;
|
|
46
|
+
const colW = columns.length > 0 ? (box.width - gutter) / columns.length : box.width - gutter;
|
|
47
|
+
const x0 = box.x + gutter;
|
|
48
|
+
|
|
49
|
+
new ChartLabels({ title }).draw(this.renderer, { x: box.x, y: box.y, width: box.width, height: box.height });
|
|
50
|
+
|
|
51
|
+
// Header: the corner (table menu), then a header per column (sort; ▾ column menu).
|
|
52
|
+
let top = box.y;
|
|
53
|
+
this.renderer.rect(box.x, top, box.width, ROW_H, TableChart.HEADER_FILL, null);
|
|
54
|
+
const corner = this.renderer.rect(box.x, top, gutter, ROW_H, TableChart.HEADER_FILL, null);
|
|
55
|
+
const cornerGlyph = this._text(TableViewChart.TABLE_GLYPH, box.x + gutter / 2, top, { textAnchor: 'middle', fontWeight: 'bold', fill: '#555' });
|
|
56
|
+
this._on(interactive, [corner, cornerGlyph], (e) => this.showMenu(e.clientX, e.clientY, view.tableMenu(this.options.onShowAs ?? null)), 'Table menu');
|
|
57
|
+
|
|
58
|
+
columns.forEach((c, i) => {
|
|
59
|
+
const left = x0 + i * colW;
|
|
60
|
+
const sorted = view.sort?.column === c.key ? (view.sort.direction === 'asc' ? ' ▲' : ' ▼') : '';
|
|
61
|
+
// In words: an arrow-like mark here read as a second sort arrow.
|
|
62
|
+
const filtered = view.filters.some(f => f.column === c.key) ? ' (filtered)' : '';
|
|
63
|
+
const head = this.renderer.rect(left, top, colW, ROW_H, TableChart.HEADER_FILL, null);
|
|
64
|
+
// The label leaves room at the right for the ▾.
|
|
65
|
+
const label = this._cell(`${c.label}${sorted}${filtered}`, c, left, top, colW - 16, { fontWeight: 'bold', fill: '#333' });
|
|
66
|
+
this._on(interactive, [head, label], () => view.toggleSort(c.key), 'Sort');
|
|
67
|
+
const glyph = this._text(TableViewChart.MENU_GLYPH, left + colW - 8, top, { textAnchor: 'middle', fill: '#777' });
|
|
68
|
+
this._on(interactive, [glyph], (e) => this.showMenu(e.clientX, e.clientY, view.columnMenu(c.key)), `${c.label} menu`);
|
|
69
|
+
});
|
|
70
|
+
top += ROW_H;
|
|
71
|
+
this._rule(box.x, top, box.width);
|
|
72
|
+
|
|
73
|
+
// Rows: a number in the gutter (row menu), a cell per column (cell menu).
|
|
74
|
+
view.pageRows().forEach(({ row, index }, r) => {
|
|
75
|
+
if (r % 2 === 1) this.renderer.rect(box.x, top, box.width, ROW_H, TableChart.STRIPE_FILL, null);
|
|
76
|
+
const number = this._text(String(index + 1), box.x + gutter - PAD_X, top, { textAnchor: 'end', fill: '#999', fontSize: '11px' });
|
|
77
|
+
this._on(interactive, [number], (e) => this.showMenu(e.clientX, e.clientY, view.rowMenu(row)), 'Row menu');
|
|
78
|
+
columns.forEach((c, i) => {
|
|
79
|
+
const left = x0 + i * colW;
|
|
80
|
+
const hit = this.renderer.rect(left, top, colW, ROW_H, 'transparent', null);
|
|
81
|
+
const text = this._cell(TableChart.cellText(row[c.key], nullText), c, left, top, colW, { fill: row[c.key] == null ? '#999' : '#222' });
|
|
82
|
+
this._on(interactive, [hit, text], (e) => this.showMenu(e.clientX, e.clientY, view.cellMenu(row, c.key)), null);
|
|
83
|
+
});
|
|
84
|
+
top += ROW_H;
|
|
85
|
+
this._rule(box.x, top, box.width);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this._footer(box, top, interactive);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** "1–50 of 120 rows (filtered from 300: Region = West)", and ‹ › when there is more than a page. */
|
|
92
|
+
_footer(box, top, interactive) {
|
|
93
|
+
const { view } = this;
|
|
94
|
+
const shown = view.pageRows();
|
|
95
|
+
const total = view.filtered().length;
|
|
96
|
+
const range = shown.length === 0 ? 'No rows' : `${shown[0].index + 1}–${shown.at(-1).index + 1} of ${total} row${total === 1 ? '' : 's'}`;
|
|
97
|
+
const filters = view.filters.length > 0 ? ` (filtered from ${view.rows.length}: ${view.describeFilters()})` : '';
|
|
98
|
+
this._text(`${range}${filters}`, box.x + TableChart.PAD_X, top, { textAnchor: 'start', fill: '#666', fontSize: '11px' });
|
|
99
|
+
if (view.pageCount() > 1) {
|
|
100
|
+
const right = box.x + box.width - TableChart.PAD_X;
|
|
101
|
+
const previous = this._text('‹ previous', right - 70, top, { textAnchor: 'end', fill: view.page > 0 ? '#2b5fd9' : '#bbb', fontSize: '11px' });
|
|
102
|
+
const next = this._text('next ›', right, top, { textAnchor: 'end', fill: view.page < view.pageCount() - 1 ? '#2b5fd9' : '#bbb', fontSize: '11px' });
|
|
103
|
+
this._on(interactive, [previous], () => view.goToPage(view.page - 1), 'Previous page');
|
|
104
|
+
this._on(interactive, [next], () => view.goToPage(view.page + 1), 'Next page');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_text(text, x, top, attrs) {
|
|
109
|
+
return this.renderer.text(text, x, top + TableChart.ROW_H / 2, { verticalAlign: 'middle', fontSize: '12px', ...attrs });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Wires a click to each element that can take one (SVG); a pointer cursor and a tooltip say so. */
|
|
113
|
+
_on(interactive, elements, handler, title) {
|
|
114
|
+
if (!interactive) return;
|
|
115
|
+
for (const el of elements) {
|
|
116
|
+
if (typeof el?.addEventListener !== 'function') continue;
|
|
117
|
+
el.addEventListener('click', (event) => { event.stopPropagation?.(); handler(event); });
|
|
118
|
+
if (el.style) el.style.cursor = 'pointer';
|
|
119
|
+
if (title && typeof el.setAttribute === 'function' && el.ownerDocument) {
|
|
120
|
+
const t = el.ownerDocument.createElementNS('http://www.w3.org/2000/svg', 'title');
|
|
121
|
+
t.textContent = title;
|
|
122
|
+
el.appendChild(t);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
package/src/index.js
CHANGED
|
@@ -33,6 +33,10 @@ import { LagPlotChart } from './charting/charts/LagPlotChart.js';
|
|
|
33
33
|
import { TreemapChart } from './charting/charts/TreemapChart.js';
|
|
34
34
|
import { SunburstChart } from './charting/charts/SunburstChart.js';
|
|
35
35
|
import { OutlineChart } from './charting/charts/OutlineChart.js';
|
|
36
|
+
import { TableChart } from './charting/charts/TableChart.js';
|
|
37
|
+
import { ChartChooser } from './charting/ChartChooser.js';
|
|
38
|
+
import { TableView } from './charting/table/TableView.js';
|
|
39
|
+
import { TableViewChart } from './charting/table/TableViewChart.js';
|
|
36
40
|
import { RadarChart } from './charting/charts/RadarChart.js';
|
|
37
41
|
import { FunnelChart } from './charting/charts/FunnelChart.js';
|
|
38
42
|
import { WaterfallChart } from './charting/charts/WaterfallChart.js';
|
|
@@ -215,7 +219,7 @@ import {
|
|
|
215
219
|
// separate browser-only entry: import 'renderengine/src/workspace.js' directly.
|
|
216
220
|
|
|
217
221
|
export {
|
|
218
|
-
Mat4, OrbitCamera, CHART_CATALOG, getChartCatalogEntry, chartRegistry,
|
|
222
|
+
Mat4, OrbitCamera, CHART_CATALOG, getChartCatalogEntry, chartRegistry, ChartChooser, TableChart, TableView, TableViewChart,
|
|
219
223
|
DIAGRAM_CATALOG, getDiagramCatalogEntry, diagramRegistry,
|
|
220
224
|
TREE_MODE_CATALOG, treeModeRegistry, getTreeModeCatalogEntry,
|
|
221
225
|
AXIS_KIND, ENCODING_CHANNEL, COORD_SYSTEM, COLOR_MODE, CAMERA_MODE,
|
|
@@ -1417,6 +1421,80 @@ export class RenderEngine {
|
|
|
1417
1421
|
this._renderChart(new FunnelChart(this.renderer, data, options));
|
|
1418
1422
|
}
|
|
1419
1423
|
|
|
1424
|
+
/**
|
|
1425
|
+
* Renders rows as a table — a header of column names, a row per record, numeric columns
|
|
1426
|
+
* right-aligned, values shown exactly. Drawn with the renderer's primitives, so it works under
|
|
1427
|
+
* SVG and canvas. ChartChooser offers it for any result, last.
|
|
1428
|
+
* @param {Array<object>} data one object per row
|
|
1429
|
+
* @param {object} [options]
|
|
1430
|
+
* @param {Array<string|{key:string, label?:string, align?:'start'|'end'}>} [options.columns] every key of the first row when absent
|
|
1431
|
+
* @param {number} [options.maxRows] rows beyond it are counted, not drawn
|
|
1432
|
+
* @param {string} [options.nullText] what a null or missing value shows ('—')
|
|
1433
|
+
* @param {string} [options.title]
|
|
1434
|
+
*/
|
|
1435
|
+
tableChart(data, options = {}) {
|
|
1436
|
+
if (!this.renderer) return;
|
|
1437
|
+
this._chart3D = false;
|
|
1438
|
+
this._chartState = { type: 'table', data, options };
|
|
1439
|
+
this._renderChart(new TableChart(this.renderer, data, options));
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
/**
|
|
1443
|
+
* Shows result rows as an interactive table — sort, filter, hide columns, page, copy, through
|
|
1444
|
+
* menus on its cells, rows, columns and the table itself — whose table menu can show what it
|
|
1445
|
+
* holds as any chart type ChartChooser says fits ("Show as"); a chart drawn that way has a
|
|
1446
|
+
* "◂ Table" link back to the table as it was left. One output for a result, table or chart.
|
|
1447
|
+
*
|
|
1448
|
+
* The container's height follows what is drawn (the table's rows, or `chartHeight` for a chart),
|
|
1449
|
+
* so the host gives the container a width only. Interaction needs the SVG renderer; on canvas
|
|
1450
|
+
* the table is drawn without it.
|
|
1451
|
+
* @param {Array<object>} rows
|
|
1452
|
+
* @param {object} [options]
|
|
1453
|
+
* @param {Array<{name, kind?, measure?}>} [options.columns] hints (ChartChooser's)
|
|
1454
|
+
* @param {boolean} [options.complete] false when the rows are only part of the answer
|
|
1455
|
+
* @param {number} [options.pageSize] rows per page (50)
|
|
1456
|
+
* @param {object} [options.menus] the host's own items: { cell, row, column, table } → items
|
|
1457
|
+
* @param {string} [options.title]
|
|
1458
|
+
* @param {number} [options.chartHeight] a chart's height (360)
|
|
1459
|
+
* @param {boolean} [options.fitHeight] size the container to what is drawn (true)
|
|
1460
|
+
* @returns {TableView|null} the table's state, for the host to read or change
|
|
1461
|
+
*/
|
|
1462
|
+
resultView(rows, options = {}) {
|
|
1463
|
+
if (!this.renderer) return null;
|
|
1464
|
+
const view = new TableView(rows, { ...options, onChange: () => this._drawResultTable(view, options) });
|
|
1465
|
+
this._drawResultTable(view, options);
|
|
1466
|
+
return view;
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
_drawResultTable(view, options) {
|
|
1470
|
+
this._fitResultHeight(TableViewChart.heightFor(view, options), options);
|
|
1471
|
+
this._chart3D = false;
|
|
1472
|
+
this._chartState = { type: 'table', data: view.rows, options };
|
|
1473
|
+
this._renderChart(new TableViewChart(this.renderer, view, {
|
|
1474
|
+
...options,
|
|
1475
|
+
onShowAs: (choice) => this._drawResultChart(view, choice, options),
|
|
1476
|
+
}));
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
_drawResultChart(view, choice, options) {
|
|
1480
|
+
this._fitResultHeight(options.chartHeight ?? 360, options);
|
|
1481
|
+
this.renderChartType(choice.key, choice.data, { title: options.title ?? choice.reason });
|
|
1482
|
+
// The way back, drawn over the chart: the same table, as it was left.
|
|
1483
|
+
if (!this.renderer.supportsSceneGraph) return;
|
|
1484
|
+
const back = this.renderer.text('◂ Table', 8, 14, { fill: '#2b5fd9', fontSize: '12px', verticalAlign: 'middle' });
|
|
1485
|
+
back?.addEventListener?.('click', () => this._drawResultTable(view, options));
|
|
1486
|
+
if (back?.style) back.style.cursor = 'pointer';
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
_fitResultHeight(height, options) {
|
|
1490
|
+
if (options.fitHeight === false || !this.container?.style) return;
|
|
1491
|
+
// min-height as well: in a flex layout (a notebook, a workbench pane) a height alone is
|
|
1492
|
+
// shrunk to fit, and the table drawn into the shrunk box lost its last rows.
|
|
1493
|
+
this.container.style.height = `${height}px`;
|
|
1494
|
+
this.container.style.minHeight = `${height}px`;
|
|
1495
|
+
this.fitToContainer();
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1420
1498
|
/**
|
|
1421
1499
|
* Renders a waterfall chart — running-total bars, each floating from the
|
|
1422
1500
|
* previous cumulative value to the new one (a "delta" bar), except
|