@antv/g6 5.0.0-beta.5 → 5.0.0-beta.7
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/dist/g6.min.js +2 -2
- package/esm/index.d.ts +15 -7
- package/esm/index.js +1 -1
- package/esm/item/combo.d.ts +1 -1
- package/esm/item/combo.js +16 -9
- package/esm/item/combo.js.map +1 -1
- package/esm/item/edge.d.ts +22 -2
- package/esm/item/edge.js +93 -42
- package/esm/item/edge.js.map +1 -1
- package/esm/item/item.js +9 -5
- package/esm/item/item.js.map +1 -1
- package/esm/item/node.d.ts +1 -1
- package/esm/item/node.js +22 -15
- package/esm/item/node.js.map +1 -1
- package/esm/runtime/controller/data.d.ts +7 -10
- package/esm/runtime/controller/data.js +123 -281
- package/esm/runtime/controller/data.js.map +1 -1
- package/esm/runtime/controller/item.d.ts +1 -0
- package/esm/runtime/controller/item.js +21 -9
- package/esm/runtime/controller/item.js.map +1 -1
- package/esm/runtime/graph.d.ts +85 -7
- package/esm/runtime/graph.js +396 -61
- package/esm/runtime/graph.js.map +1 -1
- package/esm/stdlib/behavior/brush-select.js +1 -2
- package/esm/stdlib/behavior/brush-select.js.map +1 -1
- package/esm/stdlib/behavior/create-edge.d.ts +57 -0
- package/esm/stdlib/behavior/create-edge.js +207 -0
- package/esm/stdlib/behavior/create-edge.js.map +1 -0
- package/esm/stdlib/behavior/drag-canvas.d.ts +2 -1
- package/esm/stdlib/behavior/drag-canvas.js +26 -18
- package/esm/stdlib/behavior/drag-canvas.js.map +1 -1
- package/esm/stdlib/behavior/drag-combo.d.ts +2 -2
- package/esm/stdlib/behavior/drag-combo.js +35 -24
- package/esm/stdlib/behavior/drag-combo.js.map +1 -1
- package/esm/stdlib/behavior/drag-node.d.ts +3 -3
- package/esm/stdlib/behavior/drag-node.js +70 -57
- package/esm/stdlib/behavior/drag-node.js.map +1 -1
- package/esm/stdlib/behavior/index.d.ts +2 -0
- package/esm/stdlib/behavior/index.js +2 -0
- package/esm/stdlib/behavior/index.js.map +1 -1
- package/esm/stdlib/behavior/lasso-select.d.ts +1 -1
- package/esm/stdlib/behavior/lasso-select.js +1 -2
- package/esm/stdlib/behavior/lasso-select.js.map +1 -1
- package/esm/stdlib/behavior/scroll-canvas.d.ts +67 -0
- package/esm/stdlib/behavior/scroll-canvas.js +266 -0
- package/esm/stdlib/behavior/scroll-canvas.js.map +1 -0
- package/esm/stdlib/behavior/zoom-canvas.d.ts +2 -1
- package/esm/stdlib/behavior/zoom-canvas.js +30 -22
- package/esm/stdlib/behavior/zoom-canvas.js.map +1 -1
- package/esm/stdlib/data/mapNodeSize.d.ts +2 -2
- package/esm/stdlib/data/mapNodeSize.js +2 -2
- package/esm/stdlib/data/mapNodeSize.js.map +1 -1
- package/esm/stdlib/data/transformV4Data.d.ts +2 -2
- package/esm/stdlib/data/transformV4Data.js +2 -2
- package/esm/stdlib/data/transformV4Data.js.map +1 -1
- package/esm/stdlib/data/validateData.d.ts +2 -2
- package/esm/stdlib/data/validateData.js +9 -9
- package/esm/stdlib/data/validateData.js.map +1 -1
- package/esm/stdlib/index.d.ts +10 -6
- package/esm/stdlib/index.js +5 -1
- package/esm/stdlib/index.js.map +1 -1
- package/esm/stdlib/item/edge/base.js +4 -9
- package/esm/stdlib/item/edge/base.js.map +1 -1
- package/esm/stdlib/plugin/hull/hullComponent.d.ts +1 -1
- package/esm/stdlib/plugin/hull/hullComponent.js +3 -6
- package/esm/stdlib/plugin/hull/hullComponent.js.map +1 -1
- package/esm/stdlib/plugin/minimap/index.d.ts +5 -1
- package/esm/stdlib/plugin/minimap/index.js +116 -9
- package/esm/stdlib/plugin/minimap/index.js.map +1 -1
- package/esm/stdlib/plugin/snapline/index.d.ts +5 -1
- package/esm/stdlib/plugin/snapline/index.js +23 -12
- package/esm/stdlib/plugin/snapline/index.js.map +1 -1
- package/esm/types/graph.d.ts +34 -8
- package/esm/types/hook.d.ts +16 -2
- package/esm/types/spec.d.ts +6 -3
- package/esm/types/view.d.ts +5 -0
- package/esm/util/event.d.ts +3 -1
- package/esm/util/event.js +8 -1
- package/esm/util/event.js.map +1 -1
- package/esm/util/item.d.ts +16 -1
- package/esm/util/item.js +31 -8
- package/esm/util/item.js.map +1 -1
- package/esm/util/layout.d.ts +11 -0
- package/esm/util/layout.js +45 -0
- package/esm/util/layout.js.map +1 -1
- package/esm/util/point.js +5 -1
- package/esm/util/point.js.map +1 -1
- package/esm/util/shape.d.ts +1 -1
- package/esm/util/shape.js +3 -7
- package/esm/util/shape.js.map +1 -1
- package/esm/util/warn.d.ts +14 -0
- package/esm/util/warn.js +4 -0
- package/esm/util/warn.js.map +1 -0
- package/lib/index.d.ts +15 -7
- package/lib/index.js +1 -1
- package/lib/item/combo.d.ts +1 -1
- package/lib/item/combo.js +16 -9
- package/lib/item/combo.js.map +1 -1
- package/lib/item/edge.d.ts +22 -2
- package/lib/item/edge.js +92 -41
- package/lib/item/edge.js.map +1 -1
- package/lib/item/item.js +9 -5
- package/lib/item/item.js.map +1 -1
- package/lib/item/node.d.ts +1 -1
- package/lib/item/node.js +22 -15
- package/lib/item/node.js.map +1 -1
- package/lib/runtime/controller/data.d.ts +7 -10
- package/lib/runtime/controller/data.js +123 -281
- package/lib/runtime/controller/data.js.map +1 -1
- package/lib/runtime/controller/item.d.ts +1 -0
- package/lib/runtime/controller/item.js +21 -9
- package/lib/runtime/controller/item.js.map +1 -1
- package/lib/runtime/graph.d.ts +85 -7
- package/lib/runtime/graph.js +396 -61
- package/lib/runtime/graph.js.map +1 -1
- package/lib/stdlib/behavior/brush-select.js +1 -2
- package/lib/stdlib/behavior/brush-select.js.map +1 -1
- package/lib/stdlib/behavior/create-edge.d.ts +57 -0
- package/lib/stdlib/behavior/create-edge.js +211 -0
- package/lib/stdlib/behavior/create-edge.js.map +1 -0
- package/lib/stdlib/behavior/drag-canvas.d.ts +2 -1
- package/lib/stdlib/behavior/drag-canvas.js +26 -18
- package/lib/stdlib/behavior/drag-canvas.js.map +1 -1
- package/lib/stdlib/behavior/drag-combo.d.ts +2 -2
- package/lib/stdlib/behavior/drag-combo.js +35 -24
- package/lib/stdlib/behavior/drag-combo.js.map +1 -1
- package/lib/stdlib/behavior/drag-node.d.ts +3 -3
- package/lib/stdlib/behavior/drag-node.js +70 -57
- package/lib/stdlib/behavior/drag-node.js.map +1 -1
- package/lib/stdlib/behavior/index.d.ts +2 -0
- package/lib/stdlib/behavior/index.js +2 -0
- package/lib/stdlib/behavior/index.js.map +1 -1
- package/lib/stdlib/behavior/lasso-select.d.ts +1 -1
- package/lib/stdlib/behavior/lasso-select.js +1 -2
- package/lib/stdlib/behavior/lasso-select.js.map +1 -1
- package/lib/stdlib/behavior/scroll-canvas.d.ts +67 -0
- package/lib/stdlib/behavior/scroll-canvas.js +270 -0
- package/lib/stdlib/behavior/scroll-canvas.js.map +1 -0
- package/lib/stdlib/behavior/zoom-canvas.d.ts +2 -1
- package/lib/stdlib/behavior/zoom-canvas.js +30 -22
- package/lib/stdlib/behavior/zoom-canvas.js.map +1 -1
- package/lib/stdlib/data/mapNodeSize.d.ts +2 -2
- package/lib/stdlib/data/mapNodeSize.js +2 -2
- package/lib/stdlib/data/mapNodeSize.js.map +1 -1
- package/lib/stdlib/data/transformV4Data.d.ts +2 -2
- package/lib/stdlib/data/transformV4Data.js +2 -2
- package/lib/stdlib/data/transformV4Data.js.map +1 -1
- package/lib/stdlib/data/validateData.d.ts +2 -2
- package/lib/stdlib/data/validateData.js +9 -9
- package/lib/stdlib/data/validateData.js.map +1 -1
- package/lib/stdlib/index.d.ts +10 -6
- package/lib/stdlib/index.js +5 -1
- package/lib/stdlib/index.js.map +1 -1
- package/lib/stdlib/item/edge/base.js +4 -9
- package/lib/stdlib/item/edge/base.js.map +1 -1
- package/lib/stdlib/plugin/hull/hullComponent.d.ts +1 -1
- package/lib/stdlib/plugin/hull/hullComponent.js +3 -6
- package/lib/stdlib/plugin/hull/hullComponent.js.map +1 -1
- package/lib/stdlib/plugin/minimap/index.d.ts +5 -1
- package/lib/stdlib/plugin/minimap/index.js +116 -9
- package/lib/stdlib/plugin/minimap/index.js.map +1 -1
- package/lib/stdlib/plugin/snapline/index.d.ts +5 -1
- package/lib/stdlib/plugin/snapline/index.js +23 -12
- package/lib/stdlib/plugin/snapline/index.js.map +1 -1
- package/lib/types/graph.d.ts +34 -8
- package/lib/types/hook.d.ts +16 -2
- package/lib/types/spec.d.ts +6 -3
- package/lib/types/view.d.ts +5 -0
- package/lib/util/event.d.ts +3 -1
- package/lib/util/event.js +8 -1
- package/lib/util/event.js.map +1 -1
- package/lib/util/item.d.ts +16 -1
- package/lib/util/item.js +33 -9
- package/lib/util/item.js.map +1 -1
- package/lib/util/layout.d.ts +11 -0
- package/lib/util/layout.js +47 -1
- package/lib/util/layout.js.map +1 -1
- package/lib/util/point.js +5 -1
- package/lib/util/point.js.map +1 -1
- package/lib/util/shape.d.ts +1 -1
- package/lib/util/shape.js +3 -7
- package/lib/util/shape.js.map +1 -1
- package/lib/util/warn.d.ts +14 -0
- package/lib/util/warn.js +8 -0
- package/lib/util/warn.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/item/combo.ts +14 -9
- package/src/item/edge.ts +121 -55
- package/src/item/item.ts +13 -9
- package/src/item/node.ts +20 -15
- package/src/runtime/controller/data.ts +139 -339
- package/src/runtime/controller/item.ts +30 -12
- package/src/runtime/graph.ts +502 -67
- package/src/stdlib/behavior/brush-select.ts +4 -2
- package/src/stdlib/behavior/create-edge.ts +296 -0
- package/src/stdlib/behavior/drag-canvas.ts +30 -19
- package/src/stdlib/behavior/drag-combo.ts +2 -2
- package/src/stdlib/behavior/drag-node.ts +8 -8
- package/src/stdlib/behavior/index.ts +2 -0
- package/src/stdlib/behavior/lasso-select.ts +5 -3
- package/src/stdlib/behavior/scroll-canvas.ts +348 -0
- package/src/stdlib/behavior/zoom-canvas.ts +34 -23
- package/src/stdlib/data/mapNodeSize.ts +2 -2
- package/src/stdlib/data/transformV4Data.ts +2 -2
- package/src/stdlib/data/validateData.ts +9 -9
- package/src/stdlib/index.ts +6 -0
- package/src/stdlib/item/edge/base.ts +4 -8
- package/src/stdlib/plugin/hull/hullComponent.ts +8 -5
- package/src/stdlib/plugin/minimap/index.ts +120 -10
- package/src/stdlib/plugin/snapline/index.ts +36 -16
- package/src/types/graph.ts +37 -8
- package/src/types/hook.ts +16 -2
- package/src/types/spec.ts +9 -3
- package/src/types/view.ts +8 -3
- package/src/util/event.ts +14 -1
- package/src/util/item.ts +65 -28
- package/src/util/layout.ts +45 -0
- package/src/util/point.ts +4 -2
- package/src/util/shape.ts +6 -7
- package/src/util/warn.ts +20 -0
package/src/runtime/graph.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import EventEmitter from '@antv/event-emitter';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
AABB,
|
|
4
|
+
Canvas,
|
|
5
|
+
DataURLType,
|
|
6
|
+
DisplayObject,
|
|
7
|
+
PointLike,
|
|
8
|
+
Rect,
|
|
9
|
+
Cursor,
|
|
10
|
+
} from '@antv/g';
|
|
3
11
|
import { GraphChange, ID } from '@antv/graphlib';
|
|
4
12
|
import {
|
|
5
13
|
clone,
|
|
@@ -13,6 +21,7 @@ import {
|
|
|
13
21
|
isString,
|
|
14
22
|
map,
|
|
15
23
|
} from '@antv/util';
|
|
24
|
+
import { createDom } from '@antv/dom-util';
|
|
16
25
|
import { History } from '../stdlib/plugin/history';
|
|
17
26
|
import { Command } from '../stdlib/plugin/history/command';
|
|
18
27
|
import type {
|
|
@@ -47,7 +56,9 @@ import type {
|
|
|
47
56
|
import { FitViewRules, GraphTransformOptions } from '../types/view';
|
|
48
57
|
import { changeRenderer, createCanvas } from '../util/canvas';
|
|
49
58
|
import { formatPadding } from '../util/shape';
|
|
59
|
+
import { getLayoutBounds } from '../util/layout';
|
|
50
60
|
import { Plugin as PluginBase } from '../types/plugin';
|
|
61
|
+
import { ComboMapper, EdgeMapper, NodeMapper } from '../types/spec';
|
|
51
62
|
import {
|
|
52
63
|
DataController,
|
|
53
64
|
ExtensionController,
|
|
@@ -349,7 +360,11 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
349
360
|
main: Canvas;
|
|
350
361
|
transient: Canvas;
|
|
351
362
|
};
|
|
352
|
-
}>({ name: '
|
|
363
|
+
}>({ name: 'themechange' }),
|
|
364
|
+
mapperchange: new Hook<{
|
|
365
|
+
type: ITEM_TYPE;
|
|
366
|
+
mapper: NodeMapper | EdgeMapper | ComboMapper;
|
|
367
|
+
}>({ name: 'mapperchange' }),
|
|
353
368
|
treecollapseexpand: new Hook<{
|
|
354
369
|
ids: ID[];
|
|
355
370
|
animate: boolean;
|
|
@@ -362,12 +377,14 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
362
377
|
|
|
363
378
|
private formatSpecification(spec: Specification<B, T>) {
|
|
364
379
|
return {
|
|
380
|
+
...this.specification,
|
|
365
381
|
...spec,
|
|
366
382
|
optimize: {
|
|
367
383
|
tileBehavior: 2000,
|
|
368
384
|
tileBehaviorSize: 1000,
|
|
369
385
|
tileFirstRender: 10000,
|
|
370
386
|
tileFirstRenderSize: 1000,
|
|
387
|
+
...this.specification?.optimize,
|
|
371
388
|
...spec.optimize,
|
|
372
389
|
},
|
|
373
390
|
};
|
|
@@ -404,6 +421,32 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
404
421
|
});
|
|
405
422
|
}
|
|
406
423
|
|
|
424
|
+
/**
|
|
425
|
+
* Update the item display mapper for a specific item type.
|
|
426
|
+
* @param {ITEM_TYPE} type - The type of item (node, edge, or combo).
|
|
427
|
+
* @param {NodeMapper | EdgeMapper | ComboMapper} mapper - The mapper to be updated.
|
|
428
|
+
* */
|
|
429
|
+
public updateMapper(
|
|
430
|
+
type: ITEM_TYPE,
|
|
431
|
+
mapper: NodeMapper | EdgeMapper | ComboMapper,
|
|
432
|
+
) {
|
|
433
|
+
switch (type) {
|
|
434
|
+
case 'node':
|
|
435
|
+
this.specification.node = mapper as NodeMapper;
|
|
436
|
+
break;
|
|
437
|
+
case 'edge':
|
|
438
|
+
this.specification.edge = mapper as EdgeMapper;
|
|
439
|
+
break;
|
|
440
|
+
case 'combo':
|
|
441
|
+
this.specification.combo = mapper as ComboMapper;
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
this.hooks.mapperchange.emit({
|
|
445
|
+
type,
|
|
446
|
+
mapper,
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
407
450
|
/**
|
|
408
451
|
* Get the copy of specs(configurations).
|
|
409
452
|
* @returns graph specs
|
|
@@ -439,15 +482,28 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
439
482
|
const { autoFit } = this.specification;
|
|
440
483
|
if (autoFit) {
|
|
441
484
|
if (autoFit === 'view') {
|
|
442
|
-
await this.fitView();
|
|
485
|
+
await this.fitView({ rules: { boundsType: 'layout' } });
|
|
443
486
|
} else if (autoFit === 'center') {
|
|
444
|
-
await this.fitCenter();
|
|
487
|
+
await this.fitCenter('layout');
|
|
445
488
|
} else {
|
|
446
489
|
const { type, effectTiming, ...others } = autoFit;
|
|
447
490
|
if (type === 'view') {
|
|
448
|
-
|
|
491
|
+
const { padding, rules } = others as {
|
|
492
|
+
padding: Padding;
|
|
493
|
+
rules: FitViewRules;
|
|
494
|
+
};
|
|
495
|
+
await this.fitView(
|
|
496
|
+
{
|
|
497
|
+
padding,
|
|
498
|
+
rules: {
|
|
499
|
+
...rules,
|
|
500
|
+
boundsType: 'layout',
|
|
501
|
+
},
|
|
502
|
+
},
|
|
503
|
+
effectTiming,
|
|
504
|
+
);
|
|
449
505
|
} else if (type === 'center') {
|
|
450
|
-
await this.fitCenter(effectTiming);
|
|
506
|
+
await this.fitCenter('layout', effectTiming);
|
|
451
507
|
} else if (type === 'position') {
|
|
452
508
|
// TODO: align
|
|
453
509
|
await this.translateTo((others as any).position, effectTiming);
|
|
@@ -673,8 +729,8 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
673
729
|
*/
|
|
674
730
|
public async fitView(
|
|
675
731
|
options?: {
|
|
676
|
-
padding
|
|
677
|
-
rules
|
|
732
|
+
padding?: Padding;
|
|
733
|
+
rules?: FitViewRules;
|
|
678
734
|
},
|
|
679
735
|
effectTiming?: CameraAnimationOptions,
|
|
680
736
|
) {
|
|
@@ -682,13 +738,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
682
738
|
const [top, right, bottom, left] = padding
|
|
683
739
|
? formatPadding(padding)
|
|
684
740
|
: [0, 0, 0, 0];
|
|
685
|
-
const {
|
|
741
|
+
const {
|
|
742
|
+
direction = 'both',
|
|
743
|
+
ratioRule = 'min',
|
|
744
|
+
boundsType = 'render',
|
|
745
|
+
} = rules || {};
|
|
686
746
|
|
|
687
|
-
// Get the bounds of the whole graph.
|
|
688
747
|
const {
|
|
689
748
|
center: [graphCenterX, graphCenterY],
|
|
690
749
|
halfExtents,
|
|
691
|
-
} =
|
|
750
|
+
} =
|
|
751
|
+
boundsType === 'render'
|
|
752
|
+
? // Get the bounds of the whole graph content.
|
|
753
|
+
this.canvas.document.documentElement.getBounds()
|
|
754
|
+
: // Get the bounds of the nodes positions while the graph content is not ready.
|
|
755
|
+
getLayoutBounds(this);
|
|
692
756
|
const origin = this.canvas.canvas2Viewport({
|
|
693
757
|
x: graphCenterX,
|
|
694
758
|
y: graphCenterY,
|
|
@@ -739,11 +803,18 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
739
803
|
* Fit the graph center to the view center.
|
|
740
804
|
* @param effectTiming animation configurations
|
|
741
805
|
*/
|
|
742
|
-
public async fitCenter(
|
|
743
|
-
|
|
806
|
+
public async fitCenter(
|
|
807
|
+
boundsType: 'render' | 'layout' = 'render',
|
|
808
|
+
effectTiming?: CameraAnimationOptions,
|
|
809
|
+
) {
|
|
744
810
|
const {
|
|
745
811
|
center: [graphCenterX, graphCenterY],
|
|
746
|
-
} =
|
|
812
|
+
} =
|
|
813
|
+
boundsType === 'render'
|
|
814
|
+
? // Get the bounds of the whole graph content.
|
|
815
|
+
this.canvas.document.documentElement.getBounds()
|
|
816
|
+
: // Get the bounds of the nodes positions while the graph content is not ready.
|
|
817
|
+
getLayoutBounds(this);
|
|
747
818
|
await this.translateTo(
|
|
748
819
|
this.canvas.canvas2Viewport({ x: graphCenterX, y: graphCenterY }),
|
|
749
820
|
effectTiming,
|
|
@@ -1043,22 +1114,25 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1043
1114
|
|
|
1044
1115
|
const { graphCore } = this.dataController;
|
|
1045
1116
|
const { specification } = this.themeController;
|
|
1117
|
+
// debugger;
|
|
1046
1118
|
graphCore.once('changed', (event) => {
|
|
1047
1119
|
if (!event.changes.length) return;
|
|
1048
1120
|
const changes = event.changes;
|
|
1049
|
-
|
|
1050
|
-
type: itemType,
|
|
1051
|
-
changes: graphCore.reduceChanges(event.changes),
|
|
1052
|
-
graphCore,
|
|
1053
|
-
theme: specification,
|
|
1054
|
-
});
|
|
1055
|
-
this.emit('afteritemchange', {
|
|
1121
|
+
const timingParameters = {
|
|
1056
1122
|
type: itemType,
|
|
1057
1123
|
action: 'add',
|
|
1058
1124
|
models,
|
|
1059
1125
|
apiName: 'addData',
|
|
1060
1126
|
changes,
|
|
1127
|
+
};
|
|
1128
|
+
this.emit('beforeitemchange', timingParameters);
|
|
1129
|
+
this.hooks.itemchange.emit({
|
|
1130
|
+
type: itemType,
|
|
1131
|
+
changes: graphCore.reduceChanges(event.changes),
|
|
1132
|
+
graphCore,
|
|
1133
|
+
theme: specification,
|
|
1061
1134
|
});
|
|
1135
|
+
this.emit('afteritemchange', timingParameters);
|
|
1062
1136
|
});
|
|
1063
1137
|
|
|
1064
1138
|
const modelArr = isArray(models) ? models : [models];
|
|
@@ -1083,27 +1157,39 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1083
1157
|
public removeData(itemType: ITEM_TYPE, ids: ID | ID[]) {
|
|
1084
1158
|
const idArr = isArray(ids) ? ids : [ids];
|
|
1085
1159
|
const data = { nodes: [], edges: [], combos: [] };
|
|
1086
|
-
const {
|
|
1160
|
+
const { graphCore } = this.dataController;
|
|
1087
1161
|
const { specification } = this.themeController;
|
|
1088
|
-
const getItem =
|
|
1089
|
-
|
|
1090
|
-
data[`${itemType}s`] = idArr
|
|
1162
|
+
const getItem = itemType === 'edge' ? graphCore.getEdge : graphCore.getNode;
|
|
1163
|
+
const hasItem = itemType === 'edge' ? graphCore.hasEdge : graphCore.hasNode;
|
|
1164
|
+
data[`${itemType}s`] = idArr
|
|
1165
|
+
.map((id) => {
|
|
1166
|
+
if (!hasItem.bind(graphCore)(id)) {
|
|
1167
|
+
console.warn(
|
|
1168
|
+
`The ${itemType} data with id ${id} does not exist. It will be ignored`,
|
|
1169
|
+
);
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
return getItem.bind(graphCore)(id);
|
|
1173
|
+
})
|
|
1174
|
+
.filter(Boolean);
|
|
1091
1175
|
graphCore.once('changed', (event) => {
|
|
1092
1176
|
if (!event.changes.length) return;
|
|
1093
1177
|
const changes = event.changes;
|
|
1094
|
-
|
|
1095
|
-
type: itemType,
|
|
1096
|
-
changes: event.changes,
|
|
1097
|
-
graphCore,
|
|
1098
|
-
theme: specification,
|
|
1099
|
-
});
|
|
1100
|
-
this.emit('afteritemchange', {
|
|
1178
|
+
const timingParameters = {
|
|
1101
1179
|
type: itemType,
|
|
1102
1180
|
action: 'remove',
|
|
1103
1181
|
ids: idArr,
|
|
1104
1182
|
apiName: 'removeData',
|
|
1105
1183
|
changes,
|
|
1184
|
+
};
|
|
1185
|
+
this.emit('beforeitemchange', timingParameters);
|
|
1186
|
+
this.hooks.itemchange.emit({
|
|
1187
|
+
type: itemType,
|
|
1188
|
+
changes: event.changes,
|
|
1189
|
+
graphCore,
|
|
1190
|
+
theme: specification,
|
|
1106
1191
|
});
|
|
1192
|
+
this.emit('afteritemchange', timingParameters);
|
|
1107
1193
|
});
|
|
1108
1194
|
this.hooks.datachange.emit({
|
|
1109
1195
|
data,
|
|
@@ -1196,19 +1282,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1196
1282
|
const { specification } = this.themeController;
|
|
1197
1283
|
graphCore.once('changed', (event) => {
|
|
1198
1284
|
const changes = this.extendChanges(clone(event.changes));
|
|
1199
|
-
|
|
1200
|
-
type: itemType,
|
|
1201
|
-
changes: event.changes,
|
|
1202
|
-
graphCore,
|
|
1203
|
-
theme: specification,
|
|
1204
|
-
});
|
|
1205
|
-
this.emit('afteritemchange', {
|
|
1285
|
+
const timingParameters = {
|
|
1206
1286
|
type: itemType,
|
|
1207
1287
|
action: 'update',
|
|
1208
1288
|
models,
|
|
1209
1289
|
apiName: 'updateData',
|
|
1210
1290
|
changes,
|
|
1291
|
+
};
|
|
1292
|
+
this.emit('beforeitemchange', timingParameters);
|
|
1293
|
+
this.hooks.itemchange.emit({
|
|
1294
|
+
type: itemType,
|
|
1295
|
+
changes: event.changes,
|
|
1296
|
+
graphCore,
|
|
1297
|
+
theme: specification,
|
|
1211
1298
|
});
|
|
1299
|
+
this.emit('afteritemchange', timingParameters);
|
|
1212
1300
|
});
|
|
1213
1301
|
|
|
1214
1302
|
this.hooks.datachange.emit({
|
|
@@ -1317,6 +1405,15 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1317
1405
|
const changes = event.changes.filter(
|
|
1318
1406
|
(change) => !isEqual(change.newValue, change.oldValue),
|
|
1319
1407
|
);
|
|
1408
|
+
const timingParameters = {
|
|
1409
|
+
type,
|
|
1410
|
+
action: 'updatePosition',
|
|
1411
|
+
upsertAncestors,
|
|
1412
|
+
models,
|
|
1413
|
+
apiName: 'updatePosition',
|
|
1414
|
+
changes,
|
|
1415
|
+
};
|
|
1416
|
+
this.emit('beforeitemchange', timingParameters);
|
|
1320
1417
|
this.hooks.itemchange.emit({
|
|
1321
1418
|
type,
|
|
1322
1419
|
changes: event.changes,
|
|
@@ -1327,14 +1424,7 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1327
1424
|
animate: !disableAnimate,
|
|
1328
1425
|
callback,
|
|
1329
1426
|
});
|
|
1330
|
-
this.emit('afteritemchange',
|
|
1331
|
-
type,
|
|
1332
|
-
action: 'updatePosition',
|
|
1333
|
-
upsertAncestors,
|
|
1334
|
-
models,
|
|
1335
|
-
apiName: 'updatePosition',
|
|
1336
|
-
changes,
|
|
1337
|
-
});
|
|
1427
|
+
this.emit('afteritemchange', timingParameters);
|
|
1338
1428
|
});
|
|
1339
1429
|
|
|
1340
1430
|
this.hooks.datachange.emit({
|
|
@@ -1609,19 +1699,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1609
1699
|
graphCore.once('changed', (event) => {
|
|
1610
1700
|
if (!event.changes.length) return;
|
|
1611
1701
|
const changes = event.changes;
|
|
1612
|
-
|
|
1613
|
-
type: 'combo',
|
|
1614
|
-
changes: graphCore.reduceChanges(event.changes),
|
|
1615
|
-
graphCore,
|
|
1616
|
-
theme: specification,
|
|
1617
|
-
});
|
|
1618
|
-
this.emit('afteritemchange', {
|
|
1702
|
+
const timingParameters = {
|
|
1619
1703
|
type: 'combo',
|
|
1620
1704
|
action: 'add',
|
|
1621
1705
|
models: [model],
|
|
1622
1706
|
apiName: 'addCombo',
|
|
1623
1707
|
changes,
|
|
1708
|
+
};
|
|
1709
|
+
this.emit('beforeitemchange', timingParameters);
|
|
1710
|
+
this.hooks.itemchange.emit({
|
|
1711
|
+
type: 'combo',
|
|
1712
|
+
changes: graphCore.reduceChanges(event.changes),
|
|
1713
|
+
graphCore,
|
|
1714
|
+
theme: specification,
|
|
1624
1715
|
});
|
|
1716
|
+
this.emit('afteritemchange', timingParameters);
|
|
1625
1717
|
});
|
|
1626
1718
|
|
|
1627
1719
|
const data = {
|
|
@@ -1707,16 +1799,7 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1707
1799
|
graphCore.once('changed', (event) => {
|
|
1708
1800
|
if (!event.changes.length) return;
|
|
1709
1801
|
const changes = this.extendChanges(clone(event.changes));
|
|
1710
|
-
|
|
1711
|
-
type: 'combo',
|
|
1712
|
-
changes: event.changes,
|
|
1713
|
-
graphCore,
|
|
1714
|
-
theme: specification,
|
|
1715
|
-
upsertAncestors,
|
|
1716
|
-
action: 'updatePosition',
|
|
1717
|
-
callback,
|
|
1718
|
-
});
|
|
1719
|
-
this.emit('afteritemchange', {
|
|
1802
|
+
const timingParameters = {
|
|
1720
1803
|
type: 'combo',
|
|
1721
1804
|
ids: idArr,
|
|
1722
1805
|
dx,
|
|
@@ -1725,7 +1808,18 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1725
1808
|
upsertAncestors,
|
|
1726
1809
|
apiName: 'moveCombo',
|
|
1727
1810
|
changes,
|
|
1811
|
+
};
|
|
1812
|
+
this.emit('beforeitemchange', timingParameters);
|
|
1813
|
+
this.hooks.itemchange.emit({
|
|
1814
|
+
type: 'combo',
|
|
1815
|
+
changes: event.changes,
|
|
1816
|
+
graphCore,
|
|
1817
|
+
theme: specification,
|
|
1818
|
+
upsertAncestors,
|
|
1819
|
+
action: 'updatePosition',
|
|
1820
|
+
callback,
|
|
1728
1821
|
});
|
|
1822
|
+
this.emit('afteritemchange', timingParameters);
|
|
1729
1823
|
});
|
|
1730
1824
|
|
|
1731
1825
|
this.hooks.datachange.emit({
|
|
@@ -1824,6 +1918,15 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
1824
1918
|
return this.interactionController.getMode();
|
|
1825
1919
|
}
|
|
1826
1920
|
|
|
1921
|
+
/**
|
|
1922
|
+
* Set the cursor. But the cursor in item's style has higher priority.
|
|
1923
|
+
* @param cursor
|
|
1924
|
+
*/
|
|
1925
|
+
public setCursor(cursor: Cursor) {
|
|
1926
|
+
this.canvas.setCursor(cursor);
|
|
1927
|
+
this.transientCanvas.setCursor(cursor);
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1827
1930
|
/**
|
|
1828
1931
|
* Add behavior(s) to mode(s).
|
|
1829
1932
|
* @param behaviors behavior names or configs
|
|
@@ -2041,9 +2144,19 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
2041
2144
|
type: ITEM_TYPE | SHAPE_TYPE,
|
|
2042
2145
|
id: ID,
|
|
2043
2146
|
config: {
|
|
2044
|
-
action
|
|
2045
|
-
|
|
2046
|
-
|
|
2147
|
+
action?: 'remove' | 'add' | 'update' | undefined;
|
|
2148
|
+
/** Data to be merged into the transient item. */
|
|
2149
|
+
data?: Record<string, any>;
|
|
2150
|
+
/** Style to be merged into the transient shape. */
|
|
2151
|
+
style?: ShapeStyle;
|
|
2152
|
+
/** For type: 'edge' */
|
|
2153
|
+
drawSource?: boolean;
|
|
2154
|
+
/** For type: 'edge' */
|
|
2155
|
+
drawTarget?: boolean;
|
|
2156
|
+
/** Only shape with id in shapeIds will be cloned while type is ITEM_TYPE. If shapeIds is not assigned, the whole item will be cloned. */
|
|
2157
|
+
shapeIds?: string[];
|
|
2158
|
+
/** Whether show the shapes in shapeIds. True by default. */
|
|
2159
|
+
visible?: boolean;
|
|
2047
2160
|
upsertAncestors?: boolean;
|
|
2048
2161
|
},
|
|
2049
2162
|
canvas?: Canvas,
|
|
@@ -2058,6 +2171,328 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
|
|
|
2058
2171
|
return this.itemController.getTransient(String(id));
|
|
2059
2172
|
}
|
|
2060
2173
|
|
|
2174
|
+
// ===== download operations =====
|
|
2175
|
+
/**
|
|
2176
|
+
* Asynchronously generates a Data URL representation of the canvas content, including
|
|
2177
|
+
* background, main content, and transient canvas.
|
|
2178
|
+
* @param The type of the Data URL (e.g., 'image/png', 'image/jpeg').
|
|
2179
|
+
* @returns A Promise that resolves to the Data URL string.
|
|
2180
|
+
*/
|
|
2181
|
+
public async toDataURL(type?: DataURLType): Promise<string> {
|
|
2182
|
+
const backgroundCanvas = this.backgroundCanvas;
|
|
2183
|
+
const canvas = this.canvas;
|
|
2184
|
+
const transientCanvas = this.transientCanvas;
|
|
2185
|
+
const rendererType = this.rendererType;
|
|
2186
|
+
|
|
2187
|
+
const pixelRatio =
|
|
2188
|
+
typeof window !== 'undefined' ? window.devicePixelRatio : 1;
|
|
2189
|
+
const width = this.getSize()[0];
|
|
2190
|
+
const height = this.getSize()[1];
|
|
2191
|
+
|
|
2192
|
+
const vContainerDOM: HTMLDivElement = createDom(
|
|
2193
|
+
'<div id="virtual-image"></div>',
|
|
2194
|
+
);
|
|
2195
|
+
const vCanvas = createCanvas(
|
|
2196
|
+
rendererType,
|
|
2197
|
+
vContainerDOM,
|
|
2198
|
+
width,
|
|
2199
|
+
height,
|
|
2200
|
+
pixelRatio,
|
|
2201
|
+
);
|
|
2202
|
+
const vCanvasContextService = vCanvas.getContextService();
|
|
2203
|
+
let bgRect;
|
|
2204
|
+
if (rendererType !== 'svg') {
|
|
2205
|
+
bgRect = new Rect({
|
|
2206
|
+
style: {
|
|
2207
|
+
x: 0,
|
|
2208
|
+
y: 0,
|
|
2209
|
+
z: -1,
|
|
2210
|
+
width: vCanvasContextService.getDomElement().width,
|
|
2211
|
+
height: vCanvasContextService.getDomElement().height,
|
|
2212
|
+
//@ts-ignore
|
|
2213
|
+
fill: backgroundCanvas.getContextService().getDomElement().style[
|
|
2214
|
+
'background-color'
|
|
2215
|
+
],
|
|
2216
|
+
},
|
|
2217
|
+
});
|
|
2218
|
+
vCanvas.appendChild(bgRect);
|
|
2219
|
+
}
|
|
2220
|
+
const backgroundClonedGroup = backgroundCanvas.getRoot().cloneNode(true);
|
|
2221
|
+
const clonedGroup = canvas.getRoot().cloneNode(true);
|
|
2222
|
+
const transientClonedGroup = transientCanvas.getRoot().cloneNode(true);
|
|
2223
|
+
vCanvas.appendChild(backgroundClonedGroup);
|
|
2224
|
+
vCanvas.appendChild(clonedGroup);
|
|
2225
|
+
vCanvas.appendChild(transientClonedGroup);
|
|
2226
|
+
vCanvas.render();
|
|
2227
|
+
|
|
2228
|
+
if (!type) type = 'image/png';
|
|
2229
|
+
let dataURL = '';
|
|
2230
|
+
await vCanvasContextService.toDataURL({ type }).then((url) => {
|
|
2231
|
+
dataURL = url;
|
|
2232
|
+
});
|
|
2233
|
+
return dataURL;
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
/**
|
|
2237
|
+
* Asynchronously generates a Data URL representation of the full canvas content,
|
|
2238
|
+
* including background, main content, and transient canvas, with optional padding.
|
|
2239
|
+
* @param type The type of the Data URL (e.g., 'image/png', 'image/jpeg').
|
|
2240
|
+
* @param imageConfig Configuration options for the image (optional).
|
|
2241
|
+
* @returns A Promise that resolves to the Data URL string.
|
|
2242
|
+
*/
|
|
2243
|
+
public async toFullDataURL(
|
|
2244
|
+
type?: DataURLType,
|
|
2245
|
+
imageConfig?: { padding?: number | number[] },
|
|
2246
|
+
) {
|
|
2247
|
+
const backgroundCanvas = this.backgroundCanvas;
|
|
2248
|
+
const canvas = this.canvas;
|
|
2249
|
+
const transientCanvas = this.transientCanvas;
|
|
2250
|
+
const backgroundRoot = canvas.getRoot();
|
|
2251
|
+
const root = canvas.getRoot();
|
|
2252
|
+
const transientRoot = transientCanvas.getRoot();
|
|
2253
|
+
const rendererType = this.rendererType;
|
|
2254
|
+
|
|
2255
|
+
const backgroundBBox = backgroundRoot.getBBox();
|
|
2256
|
+
const BBox = root.getBBox();
|
|
2257
|
+
const transientBBox = transientRoot.getBBox();
|
|
2258
|
+
|
|
2259
|
+
let padding = imageConfig ? imageConfig.padding : undefined;
|
|
2260
|
+
if (!padding) {
|
|
2261
|
+
padding = [0, 0, 0, 0];
|
|
2262
|
+
} else if (isNumber(padding)) {
|
|
2263
|
+
padding = [padding, padding, padding, padding];
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
const left =
|
|
2267
|
+
(transientBBox.left
|
|
2268
|
+
? backgroundBBox.left
|
|
2269
|
+
? Math.min(backgroundBBox.left, BBox.left, transientBBox.left)
|
|
2270
|
+
: Math.min(BBox.left, transientBBox.left)
|
|
2271
|
+
: BBox.left) - padding[3];
|
|
2272
|
+
const right =
|
|
2273
|
+
(transientBBox.right
|
|
2274
|
+
? backgroundBBox.right
|
|
2275
|
+
? Math.max(backgroundBBox.right, BBox.right, transientBBox.right)
|
|
2276
|
+
: Math.max(BBox.right, transientBBox.right)
|
|
2277
|
+
: BBox.right) + padding[1];
|
|
2278
|
+
const top =
|
|
2279
|
+
(transientBBox.top
|
|
2280
|
+
? backgroundBBox.top
|
|
2281
|
+
? Math.min(backgroundBBox.top, BBox.top, transientBBox.top)
|
|
2282
|
+
: Math.min(BBox.top, transientBBox.top)
|
|
2283
|
+
: BBox.top) - padding[0];
|
|
2284
|
+
const bottom =
|
|
2285
|
+
(transientBBox.bottom
|
|
2286
|
+
? backgroundBBox.bottom
|
|
2287
|
+
? Math.max(backgroundBBox.bottom, BBox.bottom, transientBBox.bottom)
|
|
2288
|
+
: Math.max(BBox.bottom, transientBBox.bottom)
|
|
2289
|
+
: BBox.bottom) + padding[2];
|
|
2290
|
+
|
|
2291
|
+
const graphCenterX = (left + right) / 2;
|
|
2292
|
+
const graphCenterY = (top + bottom) / 2;
|
|
2293
|
+
const halfX = (right - left) / 2;
|
|
2294
|
+
const halfY = (bottom - top) / 2;
|
|
2295
|
+
|
|
2296
|
+
const pixelRatio =
|
|
2297
|
+
typeof window !== 'undefined' ? window.devicePixelRatio : 1;
|
|
2298
|
+
const vWidth = halfX * 2;
|
|
2299
|
+
const vHeight = halfY * 2;
|
|
2300
|
+
const vContainerDOM: HTMLDivElement = createDom(
|
|
2301
|
+
'<div id="virtual-image"></div>',
|
|
2302
|
+
);
|
|
2303
|
+
const vCanvas = createCanvas(
|
|
2304
|
+
rendererType,
|
|
2305
|
+
vContainerDOM,
|
|
2306
|
+
vWidth,
|
|
2307
|
+
vHeight,
|
|
2308
|
+
pixelRatio,
|
|
2309
|
+
);
|
|
2310
|
+
const vCanvasContextService = vCanvas.getContextService();
|
|
2311
|
+
if (rendererType !== 'svg') {
|
|
2312
|
+
const bgRect = new Rect({
|
|
2313
|
+
style: {
|
|
2314
|
+
x: 0,
|
|
2315
|
+
y: 0,
|
|
2316
|
+
z: -1,
|
|
2317
|
+
width: vCanvasContextService.getDomElement().width,
|
|
2318
|
+
height: vCanvasContextService.getDomElement().height,
|
|
2319
|
+
//@ts-ignore
|
|
2320
|
+
fill: backgroundCanvas.getContextService().getDomElement().style[
|
|
2321
|
+
'background-color'
|
|
2322
|
+
],
|
|
2323
|
+
},
|
|
2324
|
+
});
|
|
2325
|
+
vCanvas.appendChild(bgRect);
|
|
2326
|
+
}
|
|
2327
|
+
const backgroundClonedGroup = backgroundRoot.cloneNode(true);
|
|
2328
|
+
const clonedGroup = root.cloneNode(true);
|
|
2329
|
+
const transientClonedGroup = transientRoot.cloneNode(true);
|
|
2330
|
+
const transPosition: [number, number] = [
|
|
2331
|
+
-graphCenterX + halfX,
|
|
2332
|
+
-graphCenterY + halfY,
|
|
2333
|
+
];
|
|
2334
|
+
backgroundClonedGroup.setPosition(transPosition);
|
|
2335
|
+
clonedGroup.setPosition(transPosition);
|
|
2336
|
+
transientClonedGroup.setPosition(transPosition);
|
|
2337
|
+
vCanvas.appendChild(backgroundClonedGroup);
|
|
2338
|
+
vCanvas.appendChild(clonedGroup);
|
|
2339
|
+
vCanvas.appendChild(transientClonedGroup);
|
|
2340
|
+
vCanvas.render();
|
|
2341
|
+
|
|
2342
|
+
if (!type) type = 'image/png';
|
|
2343
|
+
let dataURL = '';
|
|
2344
|
+
await vCanvasContextService.toDataURL({ type }).then((url) => {
|
|
2345
|
+
dataURL = url;
|
|
2346
|
+
});
|
|
2347
|
+
return dataURL;
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
/**
|
|
2351
|
+
* Converts a Data URL to an image file and handles the download of the image.
|
|
2352
|
+
* @param dataURL The Data URL of the image to be converted and downloaded.
|
|
2353
|
+
* @param renderer The renderer type ('svg' or other) used for the canvas.
|
|
2354
|
+
* @param link The HTML link element used for image download.
|
|
2355
|
+
* @param fileName The desired name for the downloaded image file.
|
|
2356
|
+
*/
|
|
2357
|
+
private dataURLToImage(dataURL: string, renderer: string, link, fileName) {
|
|
2358
|
+
if (!dataURL || dataURL === 'data:') {
|
|
2359
|
+
console.error(
|
|
2360
|
+
'Download image failed. The graph is too large or there is invalid attribute values in graph items',
|
|
2361
|
+
);
|
|
2362
|
+
return;
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2365
|
+
if (typeof window !== 'undefined') {
|
|
2366
|
+
if (window.Blob && window.URL && renderer !== 'svg') {
|
|
2367
|
+
const arr = dataURL.split(',');
|
|
2368
|
+
let mime = '';
|
|
2369
|
+
if (arr && arr.length > 0) {
|
|
2370
|
+
const match = arr[0].match(/:(.*?);/);
|
|
2371
|
+
// eslint-disable-next-line prefer-destructuring
|
|
2372
|
+
if (match && match.length >= 2) mime = match[1];
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
const bstr = atob(arr[1]);
|
|
2376
|
+
let n = bstr.length;
|
|
2377
|
+
const u8arr = new Uint8Array(n);
|
|
2378
|
+
|
|
2379
|
+
while (n--) {
|
|
2380
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
2381
|
+
}
|
|
2382
|
+
|
|
2383
|
+
const blobObj = new Blob([u8arr], { type: mime });
|
|
2384
|
+
|
|
2385
|
+
if ((window.navigator as any).msSaveBlob) {
|
|
2386
|
+
(window.navigator as any).msSaveBlob(blobObj, fileName);
|
|
2387
|
+
} else {
|
|
2388
|
+
link.addEventListener('click', () => {
|
|
2389
|
+
link.download = fileName;
|
|
2390
|
+
link.href = window.URL.createObjectURL(blobObj);
|
|
2391
|
+
});
|
|
2392
|
+
}
|
|
2393
|
+
} else {
|
|
2394
|
+
link.addEventListener('click', () => {
|
|
2395
|
+
link.download = fileName;
|
|
2396
|
+
link.href = dataURL;
|
|
2397
|
+
});
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
/**
|
|
2403
|
+
* Initiates the download of the graph as an image with an optional name and type.
|
|
2404
|
+
* @param name The desired name for the downloaded image file (optional).
|
|
2405
|
+
* @param type The type of the image to download (optional, defaults to 'image/png').
|
|
2406
|
+
*/
|
|
2407
|
+
public downloadImage(name?: string, type?: DataURLType): void {
|
|
2408
|
+
const self = this;
|
|
2409
|
+
// self.stopAnimate();
|
|
2410
|
+
|
|
2411
|
+
const rendererType = this.rendererType;
|
|
2412
|
+
if (!type) type = 'image/png';
|
|
2413
|
+
|
|
2414
|
+
const fileName: string =
|
|
2415
|
+
(name || 'graph') +
|
|
2416
|
+
(rendererType === 'svg' ? '.svg' : type.split('/')[1]);
|
|
2417
|
+
|
|
2418
|
+
const link: HTMLAnchorElement = document.createElement('a');
|
|
2419
|
+
|
|
2420
|
+
self.asyncToDataUrl(type, (dataURL) => {
|
|
2421
|
+
this.dataURLToImage(dataURL, rendererType, link, fileName);
|
|
2422
|
+
const e = document.createEvent('MouseEvents');
|
|
2423
|
+
e.initEvent('click', false, false);
|
|
2424
|
+
link.dispatchEvent(e);
|
|
2425
|
+
});
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
/**
|
|
2429
|
+
* Initiates the download of the entire graph as an image with optional name, type, and padding configuration.
|
|
2430
|
+
* @param name The desired name for the downloaded image file (optional).
|
|
2431
|
+
* @param type The type of the image to download (optional, defaults to 'image/png').
|
|
2432
|
+
* @param imageConfig Configuration options for the image (optional).
|
|
2433
|
+
*/
|
|
2434
|
+
public downloadFullImage(
|
|
2435
|
+
name?: string,
|
|
2436
|
+
type?: DataURLType,
|
|
2437
|
+
imageConfig?: { padding?: number | number[] },
|
|
2438
|
+
): void {
|
|
2439
|
+
const self = this;
|
|
2440
|
+
|
|
2441
|
+
const rendererType = this.rendererType;
|
|
2442
|
+
if (!type) type = 'image/png';
|
|
2443
|
+
const fileName: string =
|
|
2444
|
+
(name || 'graph') +
|
|
2445
|
+
(rendererType === 'svg' ? '.svg' : type.split('/')[1]);
|
|
2446
|
+
const link: HTMLAnchorElement = document.createElement('a');
|
|
2447
|
+
|
|
2448
|
+
self.asyncToFullDataUrl(type, imageConfig, (dataURL) => {
|
|
2449
|
+
this.dataURLToImage(dataURL, rendererType, link, fileName);
|
|
2450
|
+
const e = document.createEvent('MouseEvents');
|
|
2451
|
+
e.initEvent('click', false, false);
|
|
2452
|
+
link.dispatchEvent(e);
|
|
2453
|
+
});
|
|
2454
|
+
}
|
|
2455
|
+
|
|
2456
|
+
/**
|
|
2457
|
+
* Asynchronously converts the canvas content to a Data URL of the specified type and invokes the provided callback.
|
|
2458
|
+
* @param type The type of the Data URL (optional, defaults to 'image/png').
|
|
2459
|
+
* @param callback A callback function to handle the Data URL (optional).
|
|
2460
|
+
*/
|
|
2461
|
+
protected asyncToDataUrl(type?: DataURLType, callback?: Function): void {
|
|
2462
|
+
let dataURL = '';
|
|
2463
|
+
if (!type) type = 'image/png';
|
|
2464
|
+
|
|
2465
|
+
setTimeout(async () => {
|
|
2466
|
+
await this.toDataURL(type).then((url) => {
|
|
2467
|
+
dataURL = url;
|
|
2468
|
+
});
|
|
2469
|
+
if (callback) callback(dataURL);
|
|
2470
|
+
}, 16);
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
/**
|
|
2474
|
+
* Asynchronously converts the entire canvas content to a Data URL of the specified type
|
|
2475
|
+
* with optional padding, and invokes the provided callback.
|
|
2476
|
+
* @param type The type of the Data URL (optional, defaults to 'image/png').
|
|
2477
|
+
* @param imageConfig Configuration options for the image (optional).
|
|
2478
|
+
* @param callback A callback function to handle the Data URL (optional).
|
|
2479
|
+
*/
|
|
2480
|
+
protected asyncToFullDataUrl(
|
|
2481
|
+
type?: DataURLType,
|
|
2482
|
+
imageConfig?: { padding?: number | number[] },
|
|
2483
|
+
callback?: (dataUrl: string) => void,
|
|
2484
|
+
): void {
|
|
2485
|
+
let dataURL = '';
|
|
2486
|
+
if (!type) type = 'image/png';
|
|
2487
|
+
|
|
2488
|
+
setTimeout(async () => {
|
|
2489
|
+
await this.toFullDataURL(type, imageConfig).then((url) => {
|
|
2490
|
+
dataURL = url;
|
|
2491
|
+
});
|
|
2492
|
+
if (callback) callback(dataURL);
|
|
2493
|
+
}, 16);
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2061
2496
|
// ===== history operations =====
|
|
2062
2497
|
|
|
2063
2498
|
/**
|