@hpcc-js/graph 3.6.5 → 3.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +43 -43
- package/README.md +256 -256
- package/dist/assets/dagre-B-z4SP0u.js.map +1 -1
- package/dist/assets/graphviz-DQ0E8zfY.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +1 -1
- package/dist/index.umd.cjs.map +1 -1
- package/package.json +6 -6
- package/src/AdjacencyGraph.ts +224 -224
- package/src/Edge.css +23 -23
- package/src/Edge.ts +257 -257
- package/src/Graph.css +18 -18
- package/src/Graph.ts +1077 -1077
- package/src/GraphData.ts +187 -187
- package/src/GraphLayouts.ts +214 -214
- package/src/Sankey.css +46 -46
- package/src/Sankey.ts +304 -304
- package/src/Subgraph.css +10 -10
- package/src/Subgraph.ts +165 -165
- package/src/Vertex.css +3 -3
- package/src/Vertex.ts +282 -282
- package/src/__package__.ts +3 -3
- package/src/__tests__/data.ts +444 -444
- package/src/__tests__/index.ts +1 -1
- package/src/__tests__/test1.ts +18 -18
- package/src/__tests__/test2.ts +80 -80
- package/src/__tests__/test3.ts +46 -46
- package/src/__tests__/test4.ts +66 -66
- package/src/__tests__/test5.ts +85 -85
- package/src/common/graphT.css +38 -38
- package/src/common/graphT.ts +1363 -1363
- package/src/common/index.ts +3 -3
- package/src/common/layouts/circle.ts +37 -37
- package/src/common/layouts/dagre.ts +145 -145
- package/src/common/layouts/dagreWorker.ts +24 -24
- package/src/common/layouts/forceDirected.ts +117 -117
- package/src/common/layouts/forceDirectedWorker.ts +22 -22
- package/src/common/layouts/geoForceDirected.ts +112 -112
- package/src/common/layouts/graphviz.ts +137 -137
- package/src/common/layouts/graphvizWorker.ts +27 -27
- package/src/common/layouts/index.ts +7 -7
- package/src/common/layouts/layout.ts +147 -147
- package/src/common/layouts/null.ts +39 -39
- package/src/common/layouts/placeholders.ts +113 -113
- package/src/common/layouts/tree.ts +326 -326
- package/src/common/layouts/workers/dagre.ts +46 -46
- package/src/common/layouts/workers/dagreOptions.ts +35 -35
- package/src/common/layouts/workers/forceDirected.ts +38 -38
- package/src/common/layouts/workers/forceDirectedOptions.ts +30 -30
- package/src/common/layouts/workers/graphviz.ts +225 -225
- package/src/common/layouts/workers/graphvizOptions.ts +70 -70
- package/src/common/liteMap.ts +72 -72
- package/src/common/liteSVGZooom.ts +61 -61
- package/src/common/sankeyGraph.css +45 -45
- package/src/common/sankeyGraph.ts +345 -345
- package/src/html/annotation.ts +71 -71
- package/src/html/component.ts +18 -18
- package/src/html/edge.ts +15 -15
- package/src/html/graphHtml.ts +11 -11
- package/src/html/graphHtmlT.ts +117 -117
- package/src/html/icon.ts +64 -64
- package/src/html/image.ts +26 -26
- package/src/html/imageChar.ts +18 -18
- package/src/html/index.ts +8 -8
- package/src/html/intersection.ts +110 -110
- package/src/html/shape.ts +141 -141
- package/src/html/text.ts +59 -59
- package/src/html/textBox.ts +45 -45
- package/src/html/vertex.ts +67 -67
- package/src/index.ts +10 -10
- package/src/react/dataGraph.ts +345 -345
- package/src/react/graphReact.ts +177 -177
- package/src/react/graphReactT.ts +44 -44
- package/src/react/index.ts +4 -4
- package/src/react/subgraph.tsx +30 -30
- package/src/react/vertex.tsx +31 -31
package/src/common/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "../react/dataGraph.ts";
|
|
2
|
-
export * from "./graphT.ts";
|
|
3
|
-
export * from "./sankeyGraph.ts";
|
|
1
|
+
export * from "../react/dataGraph.ts";
|
|
2
|
+
export * from "./graphT.ts";
|
|
3
|
+
export * from "./sankeyGraph.ts";
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { Layout } from "./layout.ts";
|
|
2
|
-
|
|
3
|
-
const rads = (degrees: number) => degrees * Math.PI / 180;
|
|
4
|
-
const radius = (vertexCount: number, sideLength: number) => sideLength / (2 * Math.sin(rads(180 / vertexCount)));
|
|
5
|
-
|
|
6
|
-
export class Circle extends Layout {
|
|
7
|
-
|
|
8
|
-
constructor(graph, readonly sideLength = 60) {
|
|
9
|
-
super(graph);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
start(): Promise<this> {
|
|
13
|
-
return super.start().then(() => {
|
|
14
|
-
const size = this._graph.size();
|
|
15
|
-
const data = this._graph.graphData();
|
|
16
|
-
const vertices = data.allVertices();
|
|
17
|
-
const edges = data.allEdges();
|
|
18
|
-
edges.forEach(e => delete e.points);
|
|
19
|
-
|
|
20
|
-
const r = radius(vertices.length, this.sideLength);
|
|
21
|
-
const angle = 360 / vertices.length;
|
|
22
|
-
vertices.forEach((v, i) => {
|
|
23
|
-
delete v.fx;
|
|
24
|
-
delete v.fy;
|
|
25
|
-
v.x = size.width / 2 + Math.cos(rads(i * angle)) * r;
|
|
26
|
-
v.y = size.height / 2 + Math.sin(rads(i * angle)) * r;
|
|
27
|
-
});
|
|
28
|
-
this._graph
|
|
29
|
-
.moveVertices(true)
|
|
30
|
-
.moveEdges(true)
|
|
31
|
-
;
|
|
32
|
-
this.stop();
|
|
33
|
-
this._running = false;
|
|
34
|
-
return this;
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
}
|
|
1
|
+
import { Layout } from "./layout.ts";
|
|
2
|
+
|
|
3
|
+
const rads = (degrees: number) => degrees * Math.PI / 180;
|
|
4
|
+
const radius = (vertexCount: number, sideLength: number) => sideLength / (2 * Math.sin(rads(180 / vertexCount)));
|
|
5
|
+
|
|
6
|
+
export class Circle extends Layout {
|
|
7
|
+
|
|
8
|
+
constructor(graph, readonly sideLength = 60) {
|
|
9
|
+
super(graph);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
start(): Promise<this> {
|
|
13
|
+
return super.start().then(() => {
|
|
14
|
+
const size = this._graph.size();
|
|
15
|
+
const data = this._graph.graphData();
|
|
16
|
+
const vertices = data.allVertices();
|
|
17
|
+
const edges = data.allEdges();
|
|
18
|
+
edges.forEach(e => delete e.points);
|
|
19
|
+
|
|
20
|
+
const r = radius(vertices.length, this.sideLength);
|
|
21
|
+
const angle = 360 / vertices.length;
|
|
22
|
+
vertices.forEach((v, i) => {
|
|
23
|
+
delete v.fx;
|
|
24
|
+
delete v.fy;
|
|
25
|
+
v.x = size.width / 2 + Math.cos(rads(i * angle)) * r;
|
|
26
|
+
v.y = size.height / 2 + Math.sin(rads(i * angle)) * r;
|
|
27
|
+
});
|
|
28
|
+
this._graph
|
|
29
|
+
.moveVertices(true)
|
|
30
|
+
.moveEdges(true)
|
|
31
|
+
;
|
|
32
|
+
this.stop();
|
|
33
|
+
this._running = false;
|
|
34
|
+
return this;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,145 +1,145 @@
|
|
|
1
|
-
import { curveBasis as d3CurveBasis, line as d3Line } from "d3-shape";
|
|
2
|
-
import { dagre, Options } from "./dagreWorker.ts";
|
|
3
|
-
import { Layout, Point } from "./layout.ts";
|
|
4
|
-
import { EdgePlaceholder } from "./placeholders.ts";
|
|
5
|
-
|
|
6
|
-
const lineBasis = d3Line<Point>()
|
|
7
|
-
.x(d => d[0])
|
|
8
|
-
.y(d => d[1])
|
|
9
|
-
.curve(d3CurveBasis)
|
|
10
|
-
;
|
|
11
|
-
|
|
12
|
-
const clusterID = (id: string | number) => `cluster_${id}`;
|
|
13
|
-
const rClusterID = (id: string) => id.substring(8);
|
|
14
|
-
|
|
15
|
-
function distance(x1, y1, x2, y2) {
|
|
16
|
-
const a = x1 - x2;
|
|
17
|
-
const b = y1 - y2;
|
|
18
|
-
return Math.sqrt(a * a + b * b);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class Dagre extends Layout {
|
|
22
|
-
|
|
23
|
-
constructor(graph, readonly _options: Options) {
|
|
24
|
-
super(graph);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
start() {
|
|
28
|
-
super.start();
|
|
29
|
-
const size = this._graph.size();
|
|
30
|
-
const data = this._graph.graphData();
|
|
31
|
-
|
|
32
|
-
return dagre({
|
|
33
|
-
subgraphs: data.allSubgraphs().map(s => ({
|
|
34
|
-
...s.props,
|
|
35
|
-
id: clusterID(s.id)
|
|
36
|
-
})),
|
|
37
|
-
nodes: data.allVertices().map(v => {
|
|
38
|
-
delete v.fx;
|
|
39
|
-
delete v.fy;
|
|
40
|
-
const bbox = v.element.node().getBBox();
|
|
41
|
-
return {
|
|
42
|
-
width: bbox.width,
|
|
43
|
-
height: bbox.height,
|
|
44
|
-
...v.props,
|
|
45
|
-
id: String(v.id)
|
|
46
|
-
};
|
|
47
|
-
}),
|
|
48
|
-
links: data.allEdges().map(e => {
|
|
49
|
-
return {
|
|
50
|
-
id: String(e.props.id),
|
|
51
|
-
source: {
|
|
52
|
-
id: String(e.source.props.id),
|
|
53
|
-
text: e.source.props.text,
|
|
54
|
-
},
|
|
55
|
-
target: {
|
|
56
|
-
id: String(e.target.props.id),
|
|
57
|
-
text: e.target.props.text,
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
}),
|
|
61
|
-
hierarchy: [
|
|
62
|
-
...data.allSubgraphs()
|
|
63
|
-
.filter(s => !!data.subgraphParent(s.id))
|
|
64
|
-
.map(s => ({
|
|
65
|
-
parent: clusterID(data.subgraphParent(s.id).props.id),
|
|
66
|
-
child: clusterID(s.id)
|
|
67
|
-
})),
|
|
68
|
-
...data.allVertices()
|
|
69
|
-
.filter(v => data.vertexParent(v.id) !== undefined)
|
|
70
|
-
.map(v => ({
|
|
71
|
-
parent: clusterID(data.vertexParent(v.id).props.id),
|
|
72
|
-
child: String(v.id)
|
|
73
|
-
}))
|
|
74
|
-
]
|
|
75
|
-
}, this._options).response.then((response: any) => {
|
|
76
|
-
if (this.running()) {
|
|
77
|
-
response.subgraphs.forEach(n => {
|
|
78
|
-
const sg = data.subgraph(rClusterID(n.id));
|
|
79
|
-
sg.x = n.x + size.width / 2;
|
|
80
|
-
sg.y = n.y + size.height / 2;
|
|
81
|
-
sg.props.width = n.width;
|
|
82
|
-
sg.props.height = n.height;
|
|
83
|
-
});
|
|
84
|
-
response.nodes.forEach(n => {
|
|
85
|
-
const v = data.vertex(n.id);
|
|
86
|
-
v.x = n.x + size.width / 2;
|
|
87
|
-
v.y = n.y + size.height / 2;
|
|
88
|
-
});
|
|
89
|
-
response.links.forEach(l => {
|
|
90
|
-
const e = data.edge(l.id);
|
|
91
|
-
const sourceDist = distance(e.source.x, e.source.y, l.points[0][0] + size.width / 2, l.points[0][1] + size.height / 2);
|
|
92
|
-
const targetDist = distance(e.target.x, e.target.y, l.points[0][0] + size.width / 2, l.points[0][1] + size.height / 2);
|
|
93
|
-
e.points = [
|
|
94
|
-
sourceDist < targetDist ? [e.source.x, e.source.y] : [e.target.x, e.target.y],
|
|
95
|
-
...l.points.map(p => [p[0] + size.width / 2, p[1] + size.height / 2]),
|
|
96
|
-
sourceDist < targetDist ? [e.target.x, e.target.y] : [e.source.x, e.source.y]
|
|
97
|
-
];
|
|
98
|
-
});
|
|
99
|
-
this._graph
|
|
100
|
-
.moveVertices(true)
|
|
101
|
-
.moveSubgraphs(true)
|
|
102
|
-
.moveEdges(true)
|
|
103
|
-
;
|
|
104
|
-
this.stop();
|
|
105
|
-
}
|
|
106
|
-
return this;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
edgePath(ep: EdgePlaceholder, curveDepth: number): { path: string, labelPos: Point } {
|
|
111
|
-
let points = [];
|
|
112
|
-
let hasNaN = false;
|
|
113
|
-
if (ep.points) {
|
|
114
|
-
const line = this.edgeLine(ep);
|
|
115
|
-
points = ep.points.map((p, idx) => {
|
|
116
|
-
let x = NaN;
|
|
117
|
-
let y = NaN;
|
|
118
|
-
if (idx === 0) {
|
|
119
|
-
x = this._graph.rproject(line.source.x);
|
|
120
|
-
y = this._graph.rproject(line.source.y);
|
|
121
|
-
} else if (idx === ep.points.length - 1) {
|
|
122
|
-
x = this._graph.rproject(line.target.x);
|
|
123
|
-
y = this._graph.rproject(line.target.y);
|
|
124
|
-
} else {
|
|
125
|
-
x = p[0];
|
|
126
|
-
y = p[1];
|
|
127
|
-
}
|
|
128
|
-
x = this._graph.project(x, false);
|
|
129
|
-
y = this._graph.project(y, false);
|
|
130
|
-
if (isNaN(x) || isNaN(y)) {
|
|
131
|
-
hasNaN = true;
|
|
132
|
-
}
|
|
133
|
-
return [x, y];
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
if (hasNaN || points.length < 2) {
|
|
137
|
-
return super.edgePath(ep, curveDepth);
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
path: lineBasis(points),
|
|
141
|
-
labelPos: this.center(points)
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
}
|
|
1
|
+
import { curveBasis as d3CurveBasis, line as d3Line } from "d3-shape";
|
|
2
|
+
import { dagre, Options } from "./dagreWorker.ts";
|
|
3
|
+
import { Layout, Point } from "./layout.ts";
|
|
4
|
+
import { EdgePlaceholder } from "./placeholders.ts";
|
|
5
|
+
|
|
6
|
+
const lineBasis = d3Line<Point>()
|
|
7
|
+
.x(d => d[0])
|
|
8
|
+
.y(d => d[1])
|
|
9
|
+
.curve(d3CurveBasis)
|
|
10
|
+
;
|
|
11
|
+
|
|
12
|
+
const clusterID = (id: string | number) => `cluster_${id}`;
|
|
13
|
+
const rClusterID = (id: string) => id.substring(8);
|
|
14
|
+
|
|
15
|
+
function distance(x1, y1, x2, y2) {
|
|
16
|
+
const a = x1 - x2;
|
|
17
|
+
const b = y1 - y2;
|
|
18
|
+
return Math.sqrt(a * a + b * b);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Dagre extends Layout {
|
|
22
|
+
|
|
23
|
+
constructor(graph, readonly _options: Options) {
|
|
24
|
+
super(graph);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
start() {
|
|
28
|
+
super.start();
|
|
29
|
+
const size = this._graph.size();
|
|
30
|
+
const data = this._graph.graphData();
|
|
31
|
+
|
|
32
|
+
return dagre({
|
|
33
|
+
subgraphs: data.allSubgraphs().map(s => ({
|
|
34
|
+
...s.props,
|
|
35
|
+
id: clusterID(s.id)
|
|
36
|
+
})),
|
|
37
|
+
nodes: data.allVertices().map(v => {
|
|
38
|
+
delete v.fx;
|
|
39
|
+
delete v.fy;
|
|
40
|
+
const bbox = v.element.node().getBBox();
|
|
41
|
+
return {
|
|
42
|
+
width: bbox.width,
|
|
43
|
+
height: bbox.height,
|
|
44
|
+
...v.props,
|
|
45
|
+
id: String(v.id)
|
|
46
|
+
};
|
|
47
|
+
}),
|
|
48
|
+
links: data.allEdges().map(e => {
|
|
49
|
+
return {
|
|
50
|
+
id: String(e.props.id),
|
|
51
|
+
source: {
|
|
52
|
+
id: String(e.source.props.id),
|
|
53
|
+
text: e.source.props.text,
|
|
54
|
+
},
|
|
55
|
+
target: {
|
|
56
|
+
id: String(e.target.props.id),
|
|
57
|
+
text: e.target.props.text,
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}),
|
|
61
|
+
hierarchy: [
|
|
62
|
+
...data.allSubgraphs()
|
|
63
|
+
.filter(s => !!data.subgraphParent(s.id))
|
|
64
|
+
.map(s => ({
|
|
65
|
+
parent: clusterID(data.subgraphParent(s.id).props.id),
|
|
66
|
+
child: clusterID(s.id)
|
|
67
|
+
})),
|
|
68
|
+
...data.allVertices()
|
|
69
|
+
.filter(v => data.vertexParent(v.id) !== undefined)
|
|
70
|
+
.map(v => ({
|
|
71
|
+
parent: clusterID(data.vertexParent(v.id).props.id),
|
|
72
|
+
child: String(v.id)
|
|
73
|
+
}))
|
|
74
|
+
]
|
|
75
|
+
}, this._options).response.then((response: any) => {
|
|
76
|
+
if (this.running()) {
|
|
77
|
+
response.subgraphs.forEach(n => {
|
|
78
|
+
const sg = data.subgraph(rClusterID(n.id));
|
|
79
|
+
sg.x = n.x + size.width / 2;
|
|
80
|
+
sg.y = n.y + size.height / 2;
|
|
81
|
+
sg.props.width = n.width;
|
|
82
|
+
sg.props.height = n.height;
|
|
83
|
+
});
|
|
84
|
+
response.nodes.forEach(n => {
|
|
85
|
+
const v = data.vertex(n.id);
|
|
86
|
+
v.x = n.x + size.width / 2;
|
|
87
|
+
v.y = n.y + size.height / 2;
|
|
88
|
+
});
|
|
89
|
+
response.links.forEach(l => {
|
|
90
|
+
const e = data.edge(l.id);
|
|
91
|
+
const sourceDist = distance(e.source.x, e.source.y, l.points[0][0] + size.width / 2, l.points[0][1] + size.height / 2);
|
|
92
|
+
const targetDist = distance(e.target.x, e.target.y, l.points[0][0] + size.width / 2, l.points[0][1] + size.height / 2);
|
|
93
|
+
e.points = [
|
|
94
|
+
sourceDist < targetDist ? [e.source.x, e.source.y] : [e.target.x, e.target.y],
|
|
95
|
+
...l.points.map(p => [p[0] + size.width / 2, p[1] + size.height / 2]),
|
|
96
|
+
sourceDist < targetDist ? [e.target.x, e.target.y] : [e.source.x, e.source.y]
|
|
97
|
+
];
|
|
98
|
+
});
|
|
99
|
+
this._graph
|
|
100
|
+
.moveVertices(true)
|
|
101
|
+
.moveSubgraphs(true)
|
|
102
|
+
.moveEdges(true)
|
|
103
|
+
;
|
|
104
|
+
this.stop();
|
|
105
|
+
}
|
|
106
|
+
return this;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
edgePath(ep: EdgePlaceholder, curveDepth: number): { path: string, labelPos: Point } {
|
|
111
|
+
let points = [];
|
|
112
|
+
let hasNaN = false;
|
|
113
|
+
if (ep.points) {
|
|
114
|
+
const line = this.edgeLine(ep);
|
|
115
|
+
points = ep.points.map((p, idx) => {
|
|
116
|
+
let x = NaN;
|
|
117
|
+
let y = NaN;
|
|
118
|
+
if (idx === 0) {
|
|
119
|
+
x = this._graph.rproject(line.source.x);
|
|
120
|
+
y = this._graph.rproject(line.source.y);
|
|
121
|
+
} else if (idx === ep.points.length - 1) {
|
|
122
|
+
x = this._graph.rproject(line.target.x);
|
|
123
|
+
y = this._graph.rproject(line.target.y);
|
|
124
|
+
} else {
|
|
125
|
+
x = p[0];
|
|
126
|
+
y = p[1];
|
|
127
|
+
}
|
|
128
|
+
x = this._graph.project(x, false);
|
|
129
|
+
y = this._graph.project(y, false);
|
|
130
|
+
if (isNaN(x) || isNaN(y)) {
|
|
131
|
+
hasNaN = true;
|
|
132
|
+
}
|
|
133
|
+
return [x, y];
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
if (hasNaN || points.length < 2) {
|
|
137
|
+
return super.edgePath(ep, curveDepth);
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
path: lineBasis(points),
|
|
141
|
+
labelPos: this.center(points)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { Data, type Hierarchy, type Options } from "./workers/dagreOptions.ts";
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
import DagreWorker from "./workers/dagre.ts?worker&inline";
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
type Hierarchy,
|
|
7
|
-
type Options
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export function dagre(data: Data, options: Options) {
|
|
11
|
-
|
|
12
|
-
const worker = new DagreWorker();
|
|
13
|
-
const response = new Promise<string>(resolve => {
|
|
14
|
-
worker.onmessage = event => {
|
|
15
|
-
resolve(event.data);
|
|
16
|
-
worker.terminate();
|
|
17
|
-
};
|
|
18
|
-
worker.postMessage([data, options]);
|
|
19
|
-
});
|
|
20
|
-
return {
|
|
21
|
-
terminate: () => worker.terminate(),
|
|
22
|
-
response
|
|
23
|
-
};
|
|
24
|
-
}
|
|
1
|
+
import { Data, type Hierarchy, type Options } from "./workers/dagreOptions.ts";
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import DagreWorker from "./workers/dagre.ts?worker&inline";
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
type Hierarchy,
|
|
7
|
+
type Options
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function dagre(data: Data, options: Options) {
|
|
11
|
+
|
|
12
|
+
const worker = new DagreWorker();
|
|
13
|
+
const response = new Promise<string>(resolve => {
|
|
14
|
+
worker.onmessage = event => {
|
|
15
|
+
resolve(event.data);
|
|
16
|
+
worker.terminate();
|
|
17
|
+
};
|
|
18
|
+
worker.postMessage([data, options]);
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
terminate: () => worker.terminate(),
|
|
22
|
+
response
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
import { forceCenter as d3ForceCenter, forceLink as d3ForceLink, forceManyBody as d3ForceManyBody, forceSimulation as d3ForceSimulation, forceX as d3ForceX, forceY as d3ForceY } from "d3-force";
|
|
2
|
-
|
|
3
|
-
import { Layout } from "./layout.ts";
|
|
4
|
-
|
|
5
|
-
import { Options } from "./forceDirectedWorker.ts";
|
|
6
|
-
|
|
7
|
-
// Non worker ---
|
|
8
|
-
|
|
9
|
-
export class ForceDirectedBase extends Layout {
|
|
10
|
-
|
|
11
|
-
protected _links;
|
|
12
|
-
protected _charge;
|
|
13
|
-
protected _center;
|
|
14
|
-
protected _simulation;
|
|
15
|
-
|
|
16
|
-
constructor(graph, readonly _options: Options) {
|
|
17
|
-
super(graph);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
start() {
|
|
21
|
-
return super.start().then(() => {
|
|
22
|
-
|
|
23
|
-
const size = this._graph.size();
|
|
24
|
-
const data = this._graph.graphData();
|
|
25
|
-
const vertices = data.allVertices();
|
|
26
|
-
const edges = data.allEdges();
|
|
27
|
-
edges.forEach(e => delete e.points);
|
|
28
|
-
|
|
29
|
-
this._links = d3ForceLink(edges)
|
|
30
|
-
.id(d => d.id)
|
|
31
|
-
.distance(this._options.linkDistance)
|
|
32
|
-
.strength(this._options.linkStrength)
|
|
33
|
-
;
|
|
34
|
-
|
|
35
|
-
this._charge = d3ForceManyBody()
|
|
36
|
-
.strength(this._options.repulsionStrength)
|
|
37
|
-
.distanceMin(this._options.distanceMin)
|
|
38
|
-
.distanceMax(this._options.distanceMax)
|
|
39
|
-
;
|
|
40
|
-
|
|
41
|
-
this._center = d3ForceCenter(size.width / 2, size.height / 2);
|
|
42
|
-
|
|
43
|
-
const fx = d3ForceX()
|
|
44
|
-
.strength(this._options.forceStrength)
|
|
45
|
-
;
|
|
46
|
-
const fy = d3ForceY()
|
|
47
|
-
.strength(this._options.forceStrength)
|
|
48
|
-
;
|
|
49
|
-
|
|
50
|
-
this._simulation = d3ForceSimulation(vertices.map(v => {
|
|
51
|
-
const { width, height } = v.element.node().getBBox();
|
|
52
|
-
v.fx = (this._options.pinCentroid && v.props.centroid) ? size.width / 2 : undefined;
|
|
53
|
-
v.fy = (this._options.pinCentroid && v.props.centroid) ? size.height / 2 : undefined;
|
|
54
|
-
v["width"] = width;
|
|
55
|
-
v["height"] = height;
|
|
56
|
-
return v;
|
|
57
|
-
}))
|
|
58
|
-
.force("link", this._links)
|
|
59
|
-
.force("charge", this._charge)
|
|
60
|
-
.force("center", this._center)
|
|
61
|
-
.force("x", fx)
|
|
62
|
-
.force("y", fy)
|
|
63
|
-
.alpha(this._options.alpha / 2)
|
|
64
|
-
.alphaMin(this._options.alphaMin)
|
|
65
|
-
.alphaDecay(this._options.alphaDecay)
|
|
66
|
-
.velocityDecay(this._options.velocityDecay)
|
|
67
|
-
.stop()
|
|
68
|
-
;
|
|
69
|
-
|
|
70
|
-
return this;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
stop() {
|
|
75
|
-
this._simulation.stop();
|
|
76
|
-
return super.stop();
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export class ForceDirected extends ForceDirectedBase {
|
|
81
|
-
|
|
82
|
-
start() {
|
|
83
|
-
return super.start().then(() => {
|
|
84
|
-
this._simulation.tick(this._options.iterations);
|
|
85
|
-
this.stop();
|
|
86
|
-
this._graph
|
|
87
|
-
.moveVertices(false)
|
|
88
|
-
.moveEdges(false)
|
|
89
|
-
;
|
|
90
|
-
return this;
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export class ForceDirectedAnimated extends ForceDirectedBase {
|
|
96
|
-
|
|
97
|
-
start() {
|
|
98
|
-
return super.start().then(() => {
|
|
99
|
-
return new Promise<this>(resolve => {
|
|
100
|
-
this._simulation
|
|
101
|
-
.on("tick", () => {
|
|
102
|
-
this._graph
|
|
103
|
-
.moveVertices(false)
|
|
104
|
-
.moveEdges(false)
|
|
105
|
-
;
|
|
106
|
-
this._graph.progress("layout-tick");
|
|
107
|
-
})
|
|
108
|
-
.on("end", () => {
|
|
109
|
-
this._running = false;
|
|
110
|
-
resolve(this);
|
|
111
|
-
})
|
|
112
|
-
.restart()
|
|
113
|
-
;
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
1
|
+
import { forceCenter as d3ForceCenter, forceLink as d3ForceLink, forceManyBody as d3ForceManyBody, forceSimulation as d3ForceSimulation, forceX as d3ForceX, forceY as d3ForceY } from "d3-force";
|
|
2
|
+
|
|
3
|
+
import { Layout } from "./layout.ts";
|
|
4
|
+
|
|
5
|
+
import { Options } from "./forceDirectedWorker.ts";
|
|
6
|
+
|
|
7
|
+
// Non worker ---
|
|
8
|
+
|
|
9
|
+
export class ForceDirectedBase extends Layout {
|
|
10
|
+
|
|
11
|
+
protected _links;
|
|
12
|
+
protected _charge;
|
|
13
|
+
protected _center;
|
|
14
|
+
protected _simulation;
|
|
15
|
+
|
|
16
|
+
constructor(graph, readonly _options: Options) {
|
|
17
|
+
super(graph);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
start() {
|
|
21
|
+
return super.start().then(() => {
|
|
22
|
+
|
|
23
|
+
const size = this._graph.size();
|
|
24
|
+
const data = this._graph.graphData();
|
|
25
|
+
const vertices = data.allVertices();
|
|
26
|
+
const edges = data.allEdges();
|
|
27
|
+
edges.forEach(e => delete e.points);
|
|
28
|
+
|
|
29
|
+
this._links = d3ForceLink(edges)
|
|
30
|
+
.id(d => d.id)
|
|
31
|
+
.distance(this._options.linkDistance)
|
|
32
|
+
.strength(this._options.linkStrength)
|
|
33
|
+
;
|
|
34
|
+
|
|
35
|
+
this._charge = d3ForceManyBody()
|
|
36
|
+
.strength(this._options.repulsionStrength)
|
|
37
|
+
.distanceMin(this._options.distanceMin)
|
|
38
|
+
.distanceMax(this._options.distanceMax)
|
|
39
|
+
;
|
|
40
|
+
|
|
41
|
+
this._center = d3ForceCenter(size.width / 2, size.height / 2);
|
|
42
|
+
|
|
43
|
+
const fx = d3ForceX()
|
|
44
|
+
.strength(this._options.forceStrength)
|
|
45
|
+
;
|
|
46
|
+
const fy = d3ForceY()
|
|
47
|
+
.strength(this._options.forceStrength)
|
|
48
|
+
;
|
|
49
|
+
|
|
50
|
+
this._simulation = d3ForceSimulation(vertices.map(v => {
|
|
51
|
+
const { width, height } = v.element.node().getBBox();
|
|
52
|
+
v.fx = (this._options.pinCentroid && v.props.centroid) ? size.width / 2 : undefined;
|
|
53
|
+
v.fy = (this._options.pinCentroid && v.props.centroid) ? size.height / 2 : undefined;
|
|
54
|
+
v["width"] = width;
|
|
55
|
+
v["height"] = height;
|
|
56
|
+
return v;
|
|
57
|
+
}))
|
|
58
|
+
.force("link", this._links)
|
|
59
|
+
.force("charge", this._charge)
|
|
60
|
+
.force("center", this._center)
|
|
61
|
+
.force("x", fx)
|
|
62
|
+
.force("y", fy)
|
|
63
|
+
.alpha(this._options.alpha / 2)
|
|
64
|
+
.alphaMin(this._options.alphaMin)
|
|
65
|
+
.alphaDecay(this._options.alphaDecay)
|
|
66
|
+
.velocityDecay(this._options.velocityDecay)
|
|
67
|
+
.stop()
|
|
68
|
+
;
|
|
69
|
+
|
|
70
|
+
return this;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
stop() {
|
|
75
|
+
this._simulation.stop();
|
|
76
|
+
return super.stop();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export class ForceDirected extends ForceDirectedBase {
|
|
81
|
+
|
|
82
|
+
start() {
|
|
83
|
+
return super.start().then(() => {
|
|
84
|
+
this._simulation.tick(this._options.iterations);
|
|
85
|
+
this.stop();
|
|
86
|
+
this._graph
|
|
87
|
+
.moveVertices(false)
|
|
88
|
+
.moveEdges(false)
|
|
89
|
+
;
|
|
90
|
+
return this;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class ForceDirectedAnimated extends ForceDirectedBase {
|
|
96
|
+
|
|
97
|
+
start() {
|
|
98
|
+
return super.start().then(() => {
|
|
99
|
+
return new Promise<this>(resolve => {
|
|
100
|
+
this._simulation
|
|
101
|
+
.on("tick", () => {
|
|
102
|
+
this._graph
|
|
103
|
+
.moveVertices(false)
|
|
104
|
+
.moveEdges(false)
|
|
105
|
+
;
|
|
106
|
+
this._graph.progress("layout-tick");
|
|
107
|
+
})
|
|
108
|
+
.on("end", () => {
|
|
109
|
+
this._running = false;
|
|
110
|
+
resolve(this);
|
|
111
|
+
})
|
|
112
|
+
.restart()
|
|
113
|
+
;
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|