-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathplugin.ts
More file actions
580 lines (499 loc) · 18.1 KB
/
Copy pathplugin.ts
File metadata and controls
580 lines (499 loc) · 18.1 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import "./polyfills/index";
import charts from "../charts/charts";
import { initialiseStyles } from "../series/colorStyles";
import style from "../../../dist/css/perspective-viewer-d3fc.css";
import { HTMLPerspectiveViewerElement } from "@perspective-dev/viewer";
import type * as psp_types from "@perspective-dev/viewer";
import * as d3 from "d3";
import { Chart, Settings, Type } from "../types";
import * as d3fc_style_1 from "@d3fc/d3fc-chart/src/css.js";
import * as d3fc_style_2 from "@d3fc/d3fc-element/src/css.js";
const DEFAULT_PLUGIN_SETTINGS = {
initial: {
type: "number",
count: 1,
names: [],
},
selectMode: "select",
};
const D3FC_GLOBAL_STYLES = [d3fc_style_1.css, d3fc_style_2.css, style].map(
(x) => {
const sheet = new CSSStyleSheet();
sheet.replaceSync(x);
return sheet;
},
);
const EXCLUDED_SETTINGS = [
"crossValues",
"mainValues",
"realValues",
"splitValues",
"filter",
"data",
"size",
"colorStyles",
"textStyles",
"agg_paths",
"treemaps",
"axisMemo",
"columns_config",
];
async function register_element(plugin_name: string) {
const perspectiveViewerClass = customElements.get("perspective-viewer");
await perspectiveViewerClass.registerPlugin(plugin_name);
}
export function register(...plugin_names: string[]) {
const plugins = new Set(
plugin_names.length > 0
? plugin_names
: charts.map((chart) => chart.plugin.name),
);
charts.forEach((chart) => {
if (plugins.has(chart.plugin.name)) {
const name = chart.plugin.name
.toLowerCase()
.replace(/[ \t\r\n\/]*/g, "");
const plugin_name = `perspective-viewer-d3fc-${name}`;
customElements.define(
plugin_name,
class extends HTMLPerspectiveViewerD3fcPluginElement {
_chart = chart;
static _chart = chart;
},
);
customElements
.whenDefined("perspective-viewer")
.then(() => register_element(plugin_name));
}
});
}
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector;
}
class HTMLPerspectiveViewerD3fcPluginElement extends HTMLElement {
_chart: Chart;
static _chart: Chart;
_settings: Settings | null;
render_warning: boolean;
_initialized: boolean;
_container: HTMLElement;
_staged_view;
config;
constructor() {
super();
this._settings = null;
this.render_warning = true;
}
connectedCallback() {
if (!this._initialized) {
this.attachShadow({ mode: "open" });
for (const sheet of D3FC_GLOBAL_STYLES) {
this.shadowRoot.adoptedStyleSheets.push(sheet);
}
this.shadowRoot.innerHTML += `<div id="container" class="chart"></div>`;
this._container = this.shadowRoot.querySelector(".chart");
this._initialized = true;
}
}
get name() {
return this._chart.plugin.name;
}
get category() {
return this._chart.plugin.category;
}
get select_mode() {
return this._chart.plugin.selectMode || "select";
}
get group_rollups(): string[] {
return ["flat"];
}
get min_config_columns() {
return (
this._chart.plugin.initial?.count ||
DEFAULT_PLUGIN_SETTINGS.initial.count
);
}
get config_column_names() {
return (
this._chart.plugin.initial?.names ||
DEFAULT_PLUGIN_SETTINGS.initial.names
);
}
// get initial() {
// return chart.plugin.initial
// || DEFAULT_PLUGIN_SETTINGS.initial;
// }
static get max_cells() {
return this._chart.plugin.max_cells || 10_000;
}
static set max_cells(x) {
this._chart.plugin.max_cells = x;
}
static get max_columns() {
return this._chart.plugin.max_columns || 50;
}
static set max_columns(x) {
this._chart.plugin.max_columns = x;
}
get max_cells() {
return this._chart.plugin.max_cells || 10_000;
}
set max_cells(x) {
this._chart.plugin.max_cells = x;
}
get max_columns() {
return this._chart.plugin.max_columns || 50;
}
set max_columns(x) {
this._chart.plugin.max_columns = x;
}
can_render_column_styles(type: Type, group: string) {
return this._chart.can_render_column_styles?.call(this, type, group);
}
column_style_controls(type: Type, group: string) {
return this._chart.column_style_controls?.call(this, type, group);
}
async render() {
var canvas = document.createElement("canvas");
var container: HTMLElement =
this.shadowRoot.querySelector("#container");
canvas.width = container.offsetWidth;
canvas.height = container.offsetHeight;
const context = canvas.getContext("2d");
context.fillStyle =
window
.getComputedStyle(this)
.getPropertyValue("--plugin--background") || "white";
context.fillRect(0, 0, canvas.width, canvas.height);
const text_color = window
.getComputedStyle(this)
.getPropertyValue("color");
const svgs = Array.from(
this.shadowRoot.querySelectorAll<SVGElement>(
"svg:not(#dragHandles)",
),
);
for (const svg of svgs.reverse()) {
var img = document.createElement("img");
const defaultOffset = 0;
img.width = svg.parentElement
? svg.parentElement.offsetWidth
: defaultOffset;
img.height = svg.parentElement
? svg.parentElement.offsetHeight
: defaultOffset;
// Pretty sure this is a chrome bug - `drawImage()` call
// without this scales incorrectly.
const new_svg = svg.cloneNode(true) as SVGElement;
if (!new_svg.hasAttribute("viewBox")) {
new_svg.setAttribute(
"viewBox",
`0 0 ${img.width} ${img.height}`,
);
}
new_svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
for (const text of new_svg.querySelectorAll("text")) {
text.setAttribute("fill", text_color);
}
var xml = new XMLSerializer().serializeToString(new_svg);
xml = xml.replace(/[^\x00-\x7F]/g, "");
const done = new Promise((x, y) => {
img.onload = x;
img.onerror = y;
});
try {
img.src = `data:image/svg+xml;base64,${btoa(xml)}`;
await done;
} catch (e) {
const done = new Promise((x, y) => {
img.onload = x;
img.onerror = y;
});
img.src = `data:image/svg+xml;utf8,${xml}`;
await done;
}
context.drawImage(
img,
(svg.parentNode as HTMLElement).offsetLeft,
(svg.parentNode as HTMLElement).offsetTop,
img.width,
img.height,
);
}
const canvases = Array.from(this.shadowRoot.querySelectorAll("canvas"));
for (const canvas of canvases.reverse()) {
context.drawImage(
canvas,
(canvas.parentNode as HTMLElement).offsetLeft,
(canvas.parentNode as HTMLElement).offsetTop,
canvas.width / window.devicePixelRatio,
canvas.height / window.devicePixelRatio,
);
}
return await new Promise(
(x) => canvas.toBlob((blob) => x(blob)),
// @ts-ignore
"image/png", // uhhhh, what is going on here?
);
}
async draw(view, end_col, end_row) {
if (this.offsetParent === null) {
this._staged_view = [view, end_col, end_row];
return;
}
this._staged_view = undefined;
if (this._settings) {
this._settings.axisMemo = [
[Infinity, -Infinity],
[Infinity, -Infinity],
];
}
await this.update(view, end_col, end_row, true);
}
async update(view, end_col, end_row, clear = false) {
if (this.offsetParent === null) {
return;
}
const viewer = this.parentElement as HTMLPerspectiveViewerElement;
let jsonp, metadata;
const leaves_only = this._chart.plugin.name !== "Sunburst";
if (end_col && end_row) {
jsonp = view.to_columns_string({
end_row,
end_col,
leaves_only,
});
} else if (end_col) {
jsonp = view.to_columns_string({
end_col,
leaves_only,
});
} else if (end_row) {
jsonp = view.to_columns_string({
end_row,
leaves_only,
});
} else {
jsonp = view.to_columns_string({ leaves_only });
}
metadata = await Promise.all([
viewer.getViewConfig(),
viewer.getTable().then((table) => table.schema()),
view.expression_schema(false),
view.schema(false),
jsonp,
view.get_config(),
]);
let [
real_config,
table_schema,
expression_schema,
view_schema,
json_string,
config,
] = metadata;
let json2 = JSON.parse(json_string);
const keys = Object.keys(json2);
let json = {
row(ridx) {
const obj: { __ROW_PATH__?: any[] } = {};
for (const name of keys) {
obj[name] = json2[name][ridx];
}
return obj;
},
};
this.config = real_config;
const realValues = this.config.columns;
/**
* Retrieve a tree axis column from the table and
* expression schemas, returning a String type or
* `undefined`.
* @param {String} column a column name
*/
const get_pivot_column_type = function (column) {
let type = table_schema[column];
if (!type) {
type = expression_schema[column];
}
return type;
};
const { columns, group_by, split_by, filter } = config;
const first_col = json2[Object.keys(json2)[0]] || [];
const filtered =
group_by.length > 0
? first_col.reduce(
(acc, _, idx) => {
const col = json.row(idx);
if (
col.__ROW_PATH__ &&
col.__ROW_PATH__.length == group_by.length
) {
acc.agg_paths.push(acc.aggs.slice());
acc.rows.push(col);
} else {
const len = col.__ROW_PATH__.filter(
(x) => x !== undefined,
).length;
acc.aggs[len] = col;
acc.aggs = acc.aggs.slice(0, len + 1);
}
return acc;
},
{ rows: [], aggs: [], agg_paths: [] },
)
: {
rows: first_col.map((_, idx) => json.row(idx)),
};
const dataMap = (col, i) =>
!group_by.length ? { ...col, __ROW_PATH__: [i] } : col;
const mapped = filtered.rows.map(dataMap);
let settings = {
realValues,
crossValues: group_by.map((r) => ({
name: r,
type: get_pivot_column_type(r),
})),
mainValues: columns.map((a) => ({
name: a,
type: view_schema[a],
})),
splitValues: split_by.map((r) => ({
name: r,
type: get_pivot_column_type(r),
})),
filter,
data: mapped,
agg_paths: filtered.agg_paths,
...this.config.plugin_config,
};
const handler = {
set: (obj, prop, value) => {
if (!EXCLUDED_SETTINGS.includes(prop)) {
this._container &&
this._container.dispatchEvent(
new Event("perspective-plugin-update", {
bubbles: true,
composed: true,
}),
);
}
obj[prop] = value;
return true;
},
};
const axisMemo = [
[Infinity, -Infinity],
[Infinity, -Infinity],
];
this._settings = new Proxy(
{
axisMemo,
...this._settings,
...settings,
},
handler,
);
// If only a right-axis Y axis remains, reset the alt
// axis list to default.
if (
this._settings.splitMainValues &&
this._settings.splitMainValues.length >= columns.length
) {
this._settings.splitMainValues = [];
}
initialiseStyles(this._container, this._settings);
if (clear) {
this._container.innerHTML = "";
}
this._draw();
await new Promise((resolve) => requestAnimationFrame(resolve));
}
async clear() {
if (this._container) {
this._container.innerHTML = "";
}
}
_draw() {
if (this.offsetParent !== null) {
const containerDiv = d3.select(this._container);
const name = this._chart.plugin.name
.toLowerCase()
.replace(/[ \t\r\n\/]*/g, "");
const chartClass = `chart ${name}`;
this._settings.size = this._container.getBoundingClientRect();
if (this._settings.data.length > 0) {
this._chart(
containerDiv.attr("class", chartClass),
this._settings,
);
} else {
containerDiv.attr("class", `${chartClass} disabled`);
}
}
}
/**
* TODO we need to `clear()` here unnecessarily due to a bug in the tremap module which
* causes non-cleared redraws duplicate column labels when calculating column name
* resize/repositions - see `treemapLabel.js`.
*/
async resize(_view) {
if (this.offsetParent !== null) {
if (this._settings?.data !== undefined) {
this._draw();
} else {
const [view, end_col, end_row] = this._staged_view;
this._staged_view = undefined;
this.draw(view, end_col, end_row);
}
}
}
async restyle(view) {
let settings = this._settings;
if (settings) {
delete settings["colorStyles"];
delete settings["textStyles"];
if (this.isConnected) {
initialiseStyles(this._container, settings);
this.resize(view);
}
}
}
async delete() {
this._container.innerHTML = "";
}
getContainer() {
return this._container;
}
save() {
const settings = { ...this._settings };
EXCLUDED_SETTINGS.forEach((s) => {
delete settings[s];
});
return settings;
}
restore(settings: Settings, columns_config: psp_types.ColumnConfigValues) {
const new_settings: Partial<Settings> = {};
for (const name of EXCLUDED_SETTINGS) {
if (this._settings?.[name] !== undefined) {
new_settings[name] = this._settings?.[name];
}
}
this._settings = {
...new_settings,
...settings,
columns_config,
};
}
}