@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.
Files changed (77) hide show
  1. package/LICENSE +43 -43
  2. package/README.md +256 -256
  3. package/dist/assets/dagre-B-z4SP0u.js.map +1 -1
  4. package/dist/assets/graphviz-DQ0E8zfY.js.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.umd.cjs +1 -1
  8. package/dist/index.umd.cjs.map +1 -1
  9. package/package.json +6 -6
  10. package/src/AdjacencyGraph.ts +224 -224
  11. package/src/Edge.css +23 -23
  12. package/src/Edge.ts +257 -257
  13. package/src/Graph.css +18 -18
  14. package/src/Graph.ts +1077 -1077
  15. package/src/GraphData.ts +187 -187
  16. package/src/GraphLayouts.ts +214 -214
  17. package/src/Sankey.css +46 -46
  18. package/src/Sankey.ts +304 -304
  19. package/src/Subgraph.css +10 -10
  20. package/src/Subgraph.ts +165 -165
  21. package/src/Vertex.css +3 -3
  22. package/src/Vertex.ts +282 -282
  23. package/src/__package__.ts +3 -3
  24. package/src/__tests__/data.ts +444 -444
  25. package/src/__tests__/index.ts +1 -1
  26. package/src/__tests__/test1.ts +18 -18
  27. package/src/__tests__/test2.ts +80 -80
  28. package/src/__tests__/test3.ts +46 -46
  29. package/src/__tests__/test4.ts +66 -66
  30. package/src/__tests__/test5.ts +85 -85
  31. package/src/common/graphT.css +38 -38
  32. package/src/common/graphT.ts +1363 -1363
  33. package/src/common/index.ts +3 -3
  34. package/src/common/layouts/circle.ts +37 -37
  35. package/src/common/layouts/dagre.ts +145 -145
  36. package/src/common/layouts/dagreWorker.ts +24 -24
  37. package/src/common/layouts/forceDirected.ts +117 -117
  38. package/src/common/layouts/forceDirectedWorker.ts +22 -22
  39. package/src/common/layouts/geoForceDirected.ts +112 -112
  40. package/src/common/layouts/graphviz.ts +137 -137
  41. package/src/common/layouts/graphvizWorker.ts +27 -27
  42. package/src/common/layouts/index.ts +7 -7
  43. package/src/common/layouts/layout.ts +147 -147
  44. package/src/common/layouts/null.ts +39 -39
  45. package/src/common/layouts/placeholders.ts +113 -113
  46. package/src/common/layouts/tree.ts +326 -326
  47. package/src/common/layouts/workers/dagre.ts +46 -46
  48. package/src/common/layouts/workers/dagreOptions.ts +35 -35
  49. package/src/common/layouts/workers/forceDirected.ts +38 -38
  50. package/src/common/layouts/workers/forceDirectedOptions.ts +30 -30
  51. package/src/common/layouts/workers/graphviz.ts +225 -225
  52. package/src/common/layouts/workers/graphvizOptions.ts +70 -70
  53. package/src/common/liteMap.ts +72 -72
  54. package/src/common/liteSVGZooom.ts +61 -61
  55. package/src/common/sankeyGraph.css +45 -45
  56. package/src/common/sankeyGraph.ts +345 -345
  57. package/src/html/annotation.ts +71 -71
  58. package/src/html/component.ts +18 -18
  59. package/src/html/edge.ts +15 -15
  60. package/src/html/graphHtml.ts +11 -11
  61. package/src/html/graphHtmlT.ts +117 -117
  62. package/src/html/icon.ts +64 -64
  63. package/src/html/image.ts +26 -26
  64. package/src/html/imageChar.ts +18 -18
  65. package/src/html/index.ts +8 -8
  66. package/src/html/intersection.ts +110 -110
  67. package/src/html/shape.ts +141 -141
  68. package/src/html/text.ts +59 -59
  69. package/src/html/textBox.ts +45 -45
  70. package/src/html/vertex.ts +67 -67
  71. package/src/index.ts +10 -10
  72. package/src/react/dataGraph.ts +345 -345
  73. package/src/react/graphReact.ts +177 -177
  74. package/src/react/graphReactT.ts +44 -44
  75. package/src/react/index.ts +4 -4
  76. package/src/react/subgraph.tsx +30 -30
  77. package/src/react/vertex.tsx +31 -31
@@ -1,345 +1,345 @@
1
- import { Palette, SVGWidget, Utility, select as d3Select } from "@hpcc-js/common";
2
- import { compare2 } from "@hpcc-js/util";
3
- import { sankey as d3Sankey, sankeyLinkHorizontal as d3SankeyLinkHorizontal } from "d3-sankey";
4
- import { AnnotationColumn, toJsonObj } from "../react/dataGraph.ts";
5
-
6
- import "./sankeyGraph.css";
7
- import { EdgeBaseProps, VertexBaseProps } from "./graphT.ts";
8
-
9
- export class SankeyGraph extends SVGWidget {
10
-
11
- protected _d3Sankey: any;
12
- protected _selection: any;
13
-
14
- constructor() {
15
- super();
16
- Utility.SimpleSelectionMixin.call(this, false);
17
-
18
- this._drawStartPos = "origin";
19
- }
20
-
21
- private _prevVertices: readonly VertexBaseProps[] = [];
22
- private _masterVertices: VertexBaseProps[] = [];
23
- private _masterVerticesMap: { [key: string]: VertexBaseProps } = {};
24
- mergeVertices() {
25
- const columns = this.vertexColumns();
26
- const annotationColumns = this.vertexAnnotationColumns();
27
- const catIdx = this.indexOf(columns, this.vertexCategoryColumn(), "category");
28
- const idIdx = this.indexOf(columns, this.vertexIDColumn(), "id");
29
- const labelIdx = this.indexOf(columns, this.vertexLabelColumn(), "label");
30
- const centroidIdx = this.indexOf(columns, this.vertexCentroidColumn(), "centroid");
31
- const vertexTooltipIdx = this.indexOf(columns, this.vertexTooltipColumn(), "tooltip");
32
- const annotationIdxs = annotationColumns.map(ac => this.indexOf(columns, ac.columnID(), ""));
33
- const vertices: VertexBaseProps[] = this.vertices().map((v): VertexBaseProps => {
34
- return {
35
- categoryID: "" + v[catIdx],
36
- id: "" + v[idIdx],
37
- text: "" + v[labelIdx],
38
- tooltip: "" + v[vertexTooltipIdx],
39
- origData: toJsonObj(v, columns),
40
- centroid: !!v[centroidIdx],
41
- annotationIDs: annotationIdxs.map((ai, i) => !!v[ai] ? annotationColumns[i].annotationID() : undefined).filter(a => !!a)
42
- };
43
- });
44
- const diff = compare2(this._prevVertices, vertices, d => d.id);
45
- diff.exit.forEach(item => {
46
- this._masterVertices = this._masterVertices.filter(i => i.id !== item.id);
47
- });
48
- diff.enter.forEach(item => {
49
- this._masterVertices.push(item);
50
- this._masterVerticesMap[item.id] = item;
51
- });
52
- this._prevVertices = vertices;
53
- }
54
-
55
- indexOf(columns: readonly string[], column: string, defColumn: string = ""): number {
56
- const retVal = columns.indexOf(column);
57
- return retVal >= 0 ? retVal : columns.indexOf(defColumn);
58
- }
59
-
60
- protected _prevEdges: readonly EdgeBaseProps[] = [];
61
- protected _masterEdges: EdgeBaseProps[] = [];
62
- mergeEdges() {
63
- const columns = this.edgeColumns();
64
- const idIdx = this.indexOf(columns, this.edgeIDColumn(), "id");
65
- const sourceIdx = this.indexOf(columns, this.edgeSourceColumn(), "source");
66
- const targetIdx = this.indexOf(columns, this.edgeTargetColumn(), "target");
67
- const labelIdx = this.indexOf(columns, this.edgeLabelColumn(), "label");
68
- const weightIdx = this.indexOf(columns, this.edgeWeightColumn(), "weight");
69
- const edges: EdgeBaseProps[] = this.edges().map(e => {
70
- const source = this._masterVerticesMap["" + e[sourceIdx]];
71
- if (!source) console.error(`Invalid edge source entity "${e[sourceIdx]}" does not exist.`);
72
- const target = this._masterVerticesMap["" + e[targetIdx]];
73
- if (!target) console.error(`Invalid edge target entity "${e[targetIdx]}" does not exist.`);
74
- return {
75
- type: "edge",
76
- id: idIdx >= 0 ? "" + e[idIdx] : "" + e[sourceIdx] + "->" + e[targetIdx],
77
- source,
78
- target,
79
- value: +e[weightIdx] || 0,
80
- label: labelIdx >= 0 ? ("" + e[labelIdx]) : "",
81
- origData: toJsonObj(e, columns)
82
- };
83
- }).filter(e => e.source && e.target);
84
- const diff = compare2(this._masterEdges, edges, d => d.id);
85
- diff.exit.forEach(item => {
86
- this._masterEdges = this._masterEdges.filter(i => i.id !== item.id);
87
- });
88
- diff.enter.forEach(item => {
89
- this._masterEdges.push(item);
90
- });
91
- this._prevEdges = edges;
92
- }
93
-
94
- sankeyData() {
95
- this.mergeVertices();
96
- this.mergeEdges();
97
- return {
98
- vertices: this._masterVertices,
99
- edges: this._masterEdges
100
- };
101
- }
102
-
103
- enter(domNode, element) {
104
- super.enter(domNode, element);
105
-
106
- this._d3Sankey = d3Sankey();
107
- this._selection.widgetElement(element);
108
- }
109
-
110
- update(domNode, element) {
111
- super.update(domNode, element);
112
-
113
- this._palette = this._palette.switch(this.paletteID());
114
-
115
- const strokeWidth = this.vertexStrokeWidth();
116
-
117
- const sankeyData = this.sankeyData();
118
- this._d3Sankey
119
- .nodeId(d => d.id)
120
- .extent([
121
- [0, 0],
122
- [this.width(), this.height()]
123
- ])
124
- // .nodeAlign(sankeyCenter)
125
- // .nodeWidth(this.vertexWidth())
126
- // .nodePadding(this.vertexPadding())
127
- ;
128
- if (sankeyData.vertices.length > 0) {
129
- this._d3Sankey({
130
- nodes: sankeyData.vertices,
131
- links: sankeyData.edges
132
- });
133
- }
134
- const context = this;
135
-
136
- // Links ---
137
- const link = element.selectAll(".link").data(sankeyData.edges);
138
- link.enter().append("path")
139
- .attr("class", "link")
140
- .each(function (this: HTMLElement) {
141
- d3Select(this)
142
- .append("title")
143
- ;
144
- })
145
- .merge(link)
146
- .attr("d", d3SankeyLinkHorizontal())
147
- .style("stroke-width", (d: any) => Math.max(1, d.width))
148
- // .sort(function (a, b) { return b.width - a.width; })
149
- .select("title")
150
- .text(function (d) {
151
- return d.source.text + " → " + d.target.text + "\n" + d.value;
152
- })
153
- ;
154
- link.exit().remove();
155
-
156
- // Nodes ---
157
- const node = element.selectAll(".node").data(sankeyData.vertices);
158
- node.enter().append("g")
159
- .attr("class", "node")
160
- .call(this._selection.enter.bind(this._selection))
161
- .on("click", function (this: HTMLElement, d) {
162
- context.click(d.origData, "", context._selection.selected(this));
163
- })
164
- .on("dblclick", function (this: HTMLElement, d) {
165
- context.dblclick(d.origData, "", context._selection.selected(this));
166
- })
167
- .each(function (this: HTMLElement) {
168
- const gElement = d3Select(this);
169
- gElement.append("rect");
170
- gElement.append("text");
171
- })
172
- /*
173
- .call(d3.behavior.drag()
174
- .origin(function (d) { return d; })
175
- .on("dragstart", function () {
176
- this.parentNode.appendChild(this);
177
- })
178
- .on("drag", dragmove)
179
- )
180
- */
181
- .merge(node)
182
- .attr("transform", function (d) {
183
- let _x = 0;
184
- let _y = 0;
185
- if (d.x0) _x = d.x0;
186
- if (d.y0) _y = d.y0;
187
- return "translate(" + (_x + strokeWidth) + "," + (_y + strokeWidth) + ")";
188
- })
189
- .each(function (this: HTMLElement) {
190
- const n = d3Select(this);
191
- n.select("rect")
192
- .attr("height", (d: any) => {
193
- return d.y1 - d.y0;
194
- })
195
- .attr("width", (d: any) => d.x1 - d.x0)
196
- .style("fill", function (d: any) { return context._palette(d.categoryID); })
197
- .style("stroke", function (d: any) { return context.vertexStrokeColor(); })
198
- .style("stroke-width", function (d: any) { return strokeWidth; })
199
- .style("cursor", (context.xAxisMovement() || context.yAxisMovement()) ? null : "default")
200
- ;
201
- n.select("text")
202
- .attr("x", -6)
203
- .attr("y", function (d: any) {
204
- return (d.y1 - d.y0) / 2;
205
- })
206
- .attr("dy", ".35em")
207
- .attr("text-anchor", "end")
208
- .attr("transform", null)
209
- .text(function (d: any) {
210
- return d.text;
211
- })
212
- .filter(function (d: any) {
213
- return d.x0 < context.width() / 2;
214
- })
215
- .attr("x", 6 + context._d3Sankey.nodeWidth())
216
- .attr("text-anchor", "start")
217
- ;
218
- });
219
- node.exit().remove();
220
-
221
- /*
222
- function dragmove(d) {
223
- var gElement = d3.select(this);
224
- if (context.xAxisMovement()) {
225
- d.x = Math.max(0, Math.min(context.width() - d.dx, d3.event.x));
226
- }
227
- if (context.yAxisMovement()) {
228
- d.y = Math.max(0, Math.min(context.height() - d.dy, d3.event.y));
229
- }
230
- gElement.attr("transform", "translate(" + d.x + "," + d.y + ")");
231
- context._d3Sankey.relayout();
232
- link.attr("d", context._d3SankeyPath);
233
-
234
- gElement.select("text")
235
- .attr("x", -6)
236
- .attr("y", function (d) { return d.dy / 2; })
237
- .attr("dy", ".35em")
238
- .attr("text-anchor", "end")
239
- .attr("transform", null)
240
- .text(function (d) { return d.name; })
241
- .filter(function (d) { return d.x < context.width() / 2; })
242
- .attr("x", 6 + context._d3Sankey.nodeWidth())
243
- .attr("text-anchor", "start")
244
- ;
245
- }
246
- */
247
- }
248
-
249
- exit(domNode, element) {
250
- super.exit(domNode, element);
251
- }
252
-
253
- // Events ---
254
- click(row, column, selected) {
255
- }
256
-
257
- dblclick(row, column, selected) {
258
- }
259
- }
260
- SankeyGraph.prototype._class += " graph_SankeyGraph";
261
- SankeyGraph.prototype.mixin(Utility.SimpleSelectionMixin);
262
-
263
- SankeyGraph.prototype._palette = Palette.ordinal("category10");
264
-
265
- export interface SankeyGraph {
266
- _palette: any;
267
- vertexColumns(): string[];
268
- vertexColumns(_: string[]): this;
269
- vertices(): Array<Array<string | number | boolean>>;
270
- vertices(_: Array<Array<string | number | boolean>>): this;
271
- vertexCategoryColumn(): string;
272
- vertexCategoryColumn(_: string): this;
273
- vertexIDColumn(): string;
274
- vertexIDColumn(_: string): this;
275
- vertexLabelColumn(): string;
276
- vertexLabelColumn(_: string): this;
277
- vertexCentroidColumn(): string;
278
- vertexCentroidColumn(_: string): this;
279
- vertexFAChar(): string;
280
- vertexFAChar(_: string): this;
281
- vertexFACharColumn(): string;
282
- vertexFACharColumn(_: string): this;
283
- vertexTooltipColumn(): string;
284
- vertexTooltipColumn(_: string): this;
285
- vertexAnnotationColumns(): AnnotationColumn[];
286
- vertexAnnotationColumns(_: AnnotationColumn[]): this;
287
-
288
- edgeColumns(): string[];
289
- edgeColumns(_: string[]): this;
290
- edges(): Array<Array<string | number | boolean>>;
291
- edges(_: Array<Array<string | number | boolean>>): this;
292
- edgeIDColumn(): string;
293
- edgeIDColumn(_: string): this;
294
- edgeLabelColumn(): string;
295
- edgeLabelColumn(_: string): this;
296
- edgeSourceColumn(): string;
297
- edgeSourceColumn(_: string): this;
298
- edgeTargetColumn(): string;
299
- edgeTargetColumn(_: string): this;
300
- edgeWeightColumn(): string;
301
- edgeWeightColumn(_: string): this;
302
-
303
- paletteID(): string;
304
- paletteID(_: string): this;
305
- vertexStrokeWidth(): number;
306
- vertexStrokeWidth(_: number): this;
307
- vertexStrokeColor(): string;
308
- vertexStrokeColor(_: string): this;
309
- vertexWidth(): number;
310
- vertexWidth(_: number): this;
311
- vertexPadding(): number;
312
- vertexPadding(_: number): this;
313
- xAxisMovement(): boolean;
314
- xAxisMovement(_: boolean): this;
315
- yAxisMovement(): boolean;
316
- yAxisMovement(_: boolean): this;
317
- }
318
-
319
- SankeyGraph.prototype.publish("paletteID", "category10", "set", "Color palette for this widget", SankeyGraph.prototype._palette.switch());
320
-
321
- SankeyGraph.prototype.publish("xAxisMovement", false, "boolean", "Enable x-axis movement");
322
- SankeyGraph.prototype.publish("yAxisMovement", false, "boolean", "Enable y-axis movement");
323
-
324
- SankeyGraph.prototype.publish("vertexStrokeWidth", 1, "number", "Vertex Stroke Width");
325
- SankeyGraph.prototype.publish("vertexStrokeColor", "darkgray", "string", "Vertex Stroke Color");
326
- SankeyGraph.prototype.publish("vertexWidth", 36, "number", "Vertex Width");
327
- SankeyGraph.prototype.publish("vertexPadding", 20, "number", "Vertex Padding");
328
- SankeyGraph.prototype.publish("vertexColumns", [], "any", "Vertex Columns", null, { internal: true });
329
- SankeyGraph.prototype.publish("vertices", [], "any", "Vertices (Nodes)", null, { internal: true });
330
- SankeyGraph.prototype.publish("vertexCategoryColumn", "", "set", "Vertex Category ID column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
331
- SankeyGraph.prototype.publish("vertexIDColumn", "", "set", "Vertex ID column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
332
- SankeyGraph.prototype.publish("vertexLabelColumn", "", "set", "Vertex label column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
333
- SankeyGraph.prototype.publish("vertexCentroidColumn", "", "set", "Vertex centroid column (boolean)", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
334
- SankeyGraph.prototype.publish("vertexFAChar", "?", "string", "Vertex default FAChar");
335
- SankeyGraph.prototype.publish("vertexFACharColumn", "", "set", "Vertex FAChar column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
336
- SankeyGraph.prototype.publish("vertexTooltipColumn", "", "set", "Vertex tooltip column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
337
- SankeyGraph.prototype.publish("vertexAnnotationColumns", [], "propertyArray", "Annotations", null, { autoExpand: AnnotationColumn });
338
-
339
- SankeyGraph.prototype.publish("edgeColumns", [], "any", "Edge columns", null, { internal: true });
340
- SankeyGraph.prototype.publish("edges", [], "any", "Edges (Edges)", null, { internal: true });
341
- SankeyGraph.prototype.publish("edgeIDColumn", "", "set", "Edge ID column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
342
- SankeyGraph.prototype.publish("edgeLabelColumn", "", "set", "Edge label column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
343
- SankeyGraph.prototype.publish("edgeSourceColumn", "", "set", "Edge source ID column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
344
- SankeyGraph.prototype.publish("edgeTargetColumn", "", "set", "Edge target ID column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
345
- SankeyGraph.prototype.publish("edgeWeightColumn", "", "set", "Edge weight column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
1
+ import { Palette, SVGWidget, Utility, select as d3Select } from "@hpcc-js/common";
2
+ import { compare2 } from "@hpcc-js/util";
3
+ import { sankey as d3Sankey, sankeyLinkHorizontal as d3SankeyLinkHorizontal } from "d3-sankey";
4
+ import { AnnotationColumn, toJsonObj } from "../react/dataGraph.ts";
5
+
6
+ import "./sankeyGraph.css";
7
+ import { EdgeBaseProps, VertexBaseProps } from "./graphT.ts";
8
+
9
+ export class SankeyGraph extends SVGWidget {
10
+
11
+ protected _d3Sankey: any;
12
+ protected _selection: any;
13
+
14
+ constructor() {
15
+ super();
16
+ Utility.SimpleSelectionMixin.call(this, false);
17
+
18
+ this._drawStartPos = "origin";
19
+ }
20
+
21
+ private _prevVertices: readonly VertexBaseProps[] = [];
22
+ private _masterVertices: VertexBaseProps[] = [];
23
+ private _masterVerticesMap: { [key: string]: VertexBaseProps } = {};
24
+ mergeVertices() {
25
+ const columns = this.vertexColumns();
26
+ const annotationColumns = this.vertexAnnotationColumns();
27
+ const catIdx = this.indexOf(columns, this.vertexCategoryColumn(), "category");
28
+ const idIdx = this.indexOf(columns, this.vertexIDColumn(), "id");
29
+ const labelIdx = this.indexOf(columns, this.vertexLabelColumn(), "label");
30
+ const centroidIdx = this.indexOf(columns, this.vertexCentroidColumn(), "centroid");
31
+ const vertexTooltipIdx = this.indexOf(columns, this.vertexTooltipColumn(), "tooltip");
32
+ const annotationIdxs = annotationColumns.map(ac => this.indexOf(columns, ac.columnID(), ""));
33
+ const vertices: VertexBaseProps[] = this.vertices().map((v): VertexBaseProps => {
34
+ return {
35
+ categoryID: "" + v[catIdx],
36
+ id: "" + v[idIdx],
37
+ text: "" + v[labelIdx],
38
+ tooltip: "" + v[vertexTooltipIdx],
39
+ origData: toJsonObj(v, columns),
40
+ centroid: !!v[centroidIdx],
41
+ annotationIDs: annotationIdxs.map((ai, i) => !!v[ai] ? annotationColumns[i].annotationID() : undefined).filter(a => !!a)
42
+ };
43
+ });
44
+ const diff = compare2(this._prevVertices, vertices, d => d.id);
45
+ diff.exit.forEach(item => {
46
+ this._masterVertices = this._masterVertices.filter(i => i.id !== item.id);
47
+ });
48
+ diff.enter.forEach(item => {
49
+ this._masterVertices.push(item);
50
+ this._masterVerticesMap[item.id] = item;
51
+ });
52
+ this._prevVertices = vertices;
53
+ }
54
+
55
+ indexOf(columns: readonly string[], column: string, defColumn: string = ""): number {
56
+ const retVal = columns.indexOf(column);
57
+ return retVal >= 0 ? retVal : columns.indexOf(defColumn);
58
+ }
59
+
60
+ protected _prevEdges: readonly EdgeBaseProps[] = [];
61
+ protected _masterEdges: EdgeBaseProps[] = [];
62
+ mergeEdges() {
63
+ const columns = this.edgeColumns();
64
+ const idIdx = this.indexOf(columns, this.edgeIDColumn(), "id");
65
+ const sourceIdx = this.indexOf(columns, this.edgeSourceColumn(), "source");
66
+ const targetIdx = this.indexOf(columns, this.edgeTargetColumn(), "target");
67
+ const labelIdx = this.indexOf(columns, this.edgeLabelColumn(), "label");
68
+ const weightIdx = this.indexOf(columns, this.edgeWeightColumn(), "weight");
69
+ const edges: EdgeBaseProps[] = this.edges().map(e => {
70
+ const source = this._masterVerticesMap["" + e[sourceIdx]];
71
+ if (!source) console.error(`Invalid edge source entity "${e[sourceIdx]}" does not exist.`);
72
+ const target = this._masterVerticesMap["" + e[targetIdx]];
73
+ if (!target) console.error(`Invalid edge target entity "${e[targetIdx]}" does not exist.`);
74
+ return {
75
+ type: "edge",
76
+ id: idIdx >= 0 ? "" + e[idIdx] : "" + e[sourceIdx] + "->" + e[targetIdx],
77
+ source,
78
+ target,
79
+ value: +e[weightIdx] || 0,
80
+ label: labelIdx >= 0 ? ("" + e[labelIdx]) : "",
81
+ origData: toJsonObj(e, columns)
82
+ };
83
+ }).filter(e => e.source && e.target);
84
+ const diff = compare2(this._masterEdges, edges, d => d.id);
85
+ diff.exit.forEach(item => {
86
+ this._masterEdges = this._masterEdges.filter(i => i.id !== item.id);
87
+ });
88
+ diff.enter.forEach(item => {
89
+ this._masterEdges.push(item);
90
+ });
91
+ this._prevEdges = edges;
92
+ }
93
+
94
+ sankeyData() {
95
+ this.mergeVertices();
96
+ this.mergeEdges();
97
+ return {
98
+ vertices: this._masterVertices,
99
+ edges: this._masterEdges
100
+ };
101
+ }
102
+
103
+ enter(domNode, element) {
104
+ super.enter(domNode, element);
105
+
106
+ this._d3Sankey = d3Sankey();
107
+ this._selection.widgetElement(element);
108
+ }
109
+
110
+ update(domNode, element) {
111
+ super.update(domNode, element);
112
+
113
+ this._palette = this._palette.switch(this.paletteID());
114
+
115
+ const strokeWidth = this.vertexStrokeWidth();
116
+
117
+ const sankeyData = this.sankeyData();
118
+ this._d3Sankey
119
+ .nodeId(d => d.id)
120
+ .extent([
121
+ [0, 0],
122
+ [this.width(), this.height()]
123
+ ])
124
+ // .nodeAlign(sankeyCenter)
125
+ // .nodeWidth(this.vertexWidth())
126
+ // .nodePadding(this.vertexPadding())
127
+ ;
128
+ if (sankeyData.vertices.length > 0) {
129
+ this._d3Sankey({
130
+ nodes: sankeyData.vertices,
131
+ links: sankeyData.edges
132
+ });
133
+ }
134
+ const context = this;
135
+
136
+ // Links ---
137
+ const link = element.selectAll(".link").data(sankeyData.edges);
138
+ link.enter().append("path")
139
+ .attr("class", "link")
140
+ .each(function (this: HTMLElement) {
141
+ d3Select(this)
142
+ .append("title")
143
+ ;
144
+ })
145
+ .merge(link)
146
+ .attr("d", d3SankeyLinkHorizontal())
147
+ .style("stroke-width", (d: any) => Math.max(1, d.width))
148
+ // .sort(function (a, b) { return b.width - a.width; })
149
+ .select("title")
150
+ .text(function (d) {
151
+ return d.source.text + " → " + d.target.text + "\n" + d.value;
152
+ })
153
+ ;
154
+ link.exit().remove();
155
+
156
+ // Nodes ---
157
+ const node = element.selectAll(".node").data(sankeyData.vertices);
158
+ node.enter().append("g")
159
+ .attr("class", "node")
160
+ .call(this._selection.enter.bind(this._selection))
161
+ .on("click", function (this: HTMLElement, d) {
162
+ context.click(d.origData, "", context._selection.selected(this));
163
+ })
164
+ .on("dblclick", function (this: HTMLElement, d) {
165
+ context.dblclick(d.origData, "", context._selection.selected(this));
166
+ })
167
+ .each(function (this: HTMLElement) {
168
+ const gElement = d3Select(this);
169
+ gElement.append("rect");
170
+ gElement.append("text");
171
+ })
172
+ /*
173
+ .call(d3.behavior.drag()
174
+ .origin(function (d) { return d; })
175
+ .on("dragstart", function () {
176
+ this.parentNode.appendChild(this);
177
+ })
178
+ .on("drag", dragmove)
179
+ )
180
+ */
181
+ .merge(node)
182
+ .attr("transform", function (d) {
183
+ let _x = 0;
184
+ let _y = 0;
185
+ if (d.x0) _x = d.x0;
186
+ if (d.y0) _y = d.y0;
187
+ return "translate(" + (_x + strokeWidth) + "," + (_y + strokeWidth) + ")";
188
+ })
189
+ .each(function (this: HTMLElement) {
190
+ const n = d3Select(this);
191
+ n.select("rect")
192
+ .attr("height", (d: any) => {
193
+ return d.y1 - d.y0;
194
+ })
195
+ .attr("width", (d: any) => d.x1 - d.x0)
196
+ .style("fill", function (d: any) { return context._palette(d.categoryID); })
197
+ .style("stroke", function (d: any) { return context.vertexStrokeColor(); })
198
+ .style("stroke-width", function (d: any) { return strokeWidth; })
199
+ .style("cursor", (context.xAxisMovement() || context.yAxisMovement()) ? null : "default")
200
+ ;
201
+ n.select("text")
202
+ .attr("x", -6)
203
+ .attr("y", function (d: any) {
204
+ return (d.y1 - d.y0) / 2;
205
+ })
206
+ .attr("dy", ".35em")
207
+ .attr("text-anchor", "end")
208
+ .attr("transform", null)
209
+ .text(function (d: any) {
210
+ return d.text;
211
+ })
212
+ .filter(function (d: any) {
213
+ return d.x0 < context.width() / 2;
214
+ })
215
+ .attr("x", 6 + context._d3Sankey.nodeWidth())
216
+ .attr("text-anchor", "start")
217
+ ;
218
+ });
219
+ node.exit().remove();
220
+
221
+ /*
222
+ function dragmove(d) {
223
+ var gElement = d3.select(this);
224
+ if (context.xAxisMovement()) {
225
+ d.x = Math.max(0, Math.min(context.width() - d.dx, d3.event.x));
226
+ }
227
+ if (context.yAxisMovement()) {
228
+ d.y = Math.max(0, Math.min(context.height() - d.dy, d3.event.y));
229
+ }
230
+ gElement.attr("transform", "translate(" + d.x + "," + d.y + ")");
231
+ context._d3Sankey.relayout();
232
+ link.attr("d", context._d3SankeyPath);
233
+
234
+ gElement.select("text")
235
+ .attr("x", -6)
236
+ .attr("y", function (d) { return d.dy / 2; })
237
+ .attr("dy", ".35em")
238
+ .attr("text-anchor", "end")
239
+ .attr("transform", null)
240
+ .text(function (d) { return d.name; })
241
+ .filter(function (d) { return d.x < context.width() / 2; })
242
+ .attr("x", 6 + context._d3Sankey.nodeWidth())
243
+ .attr("text-anchor", "start")
244
+ ;
245
+ }
246
+ */
247
+ }
248
+
249
+ exit(domNode, element) {
250
+ super.exit(domNode, element);
251
+ }
252
+
253
+ // Events ---
254
+ click(row, column, selected) {
255
+ }
256
+
257
+ dblclick(row, column, selected) {
258
+ }
259
+ }
260
+ SankeyGraph.prototype._class += " graph_SankeyGraph";
261
+ SankeyGraph.prototype.mixin(Utility.SimpleSelectionMixin);
262
+
263
+ SankeyGraph.prototype._palette = Palette.ordinal("category10");
264
+
265
+ export interface SankeyGraph {
266
+ _palette: any;
267
+ vertexColumns(): string[];
268
+ vertexColumns(_: string[]): this;
269
+ vertices(): Array<Array<string | number | boolean>>;
270
+ vertices(_: Array<Array<string | number | boolean>>): this;
271
+ vertexCategoryColumn(): string;
272
+ vertexCategoryColumn(_: string): this;
273
+ vertexIDColumn(): string;
274
+ vertexIDColumn(_: string): this;
275
+ vertexLabelColumn(): string;
276
+ vertexLabelColumn(_: string): this;
277
+ vertexCentroidColumn(): string;
278
+ vertexCentroidColumn(_: string): this;
279
+ vertexFAChar(): string;
280
+ vertexFAChar(_: string): this;
281
+ vertexFACharColumn(): string;
282
+ vertexFACharColumn(_: string): this;
283
+ vertexTooltipColumn(): string;
284
+ vertexTooltipColumn(_: string): this;
285
+ vertexAnnotationColumns(): AnnotationColumn[];
286
+ vertexAnnotationColumns(_: AnnotationColumn[]): this;
287
+
288
+ edgeColumns(): string[];
289
+ edgeColumns(_: string[]): this;
290
+ edges(): Array<Array<string | number | boolean>>;
291
+ edges(_: Array<Array<string | number | boolean>>): this;
292
+ edgeIDColumn(): string;
293
+ edgeIDColumn(_: string): this;
294
+ edgeLabelColumn(): string;
295
+ edgeLabelColumn(_: string): this;
296
+ edgeSourceColumn(): string;
297
+ edgeSourceColumn(_: string): this;
298
+ edgeTargetColumn(): string;
299
+ edgeTargetColumn(_: string): this;
300
+ edgeWeightColumn(): string;
301
+ edgeWeightColumn(_: string): this;
302
+
303
+ paletteID(): string;
304
+ paletteID(_: string): this;
305
+ vertexStrokeWidth(): number;
306
+ vertexStrokeWidth(_: number): this;
307
+ vertexStrokeColor(): string;
308
+ vertexStrokeColor(_: string): this;
309
+ vertexWidth(): number;
310
+ vertexWidth(_: number): this;
311
+ vertexPadding(): number;
312
+ vertexPadding(_: number): this;
313
+ xAxisMovement(): boolean;
314
+ xAxisMovement(_: boolean): this;
315
+ yAxisMovement(): boolean;
316
+ yAxisMovement(_: boolean): this;
317
+ }
318
+
319
+ SankeyGraph.prototype.publish("paletteID", "category10", "set", "Color palette for this widget", SankeyGraph.prototype._palette.switch());
320
+
321
+ SankeyGraph.prototype.publish("xAxisMovement", false, "boolean", "Enable x-axis movement");
322
+ SankeyGraph.prototype.publish("yAxisMovement", false, "boolean", "Enable y-axis movement");
323
+
324
+ SankeyGraph.prototype.publish("vertexStrokeWidth", 1, "number", "Vertex Stroke Width");
325
+ SankeyGraph.prototype.publish("vertexStrokeColor", "darkgray", "string", "Vertex Stroke Color");
326
+ SankeyGraph.prototype.publish("vertexWidth", 36, "number", "Vertex Width");
327
+ SankeyGraph.prototype.publish("vertexPadding", 20, "number", "Vertex Padding");
328
+ SankeyGraph.prototype.publish("vertexColumns", [], "any", "Vertex Columns", null, { internal: true });
329
+ SankeyGraph.prototype.publish("vertices", [], "any", "Vertices (Nodes)", null, { internal: true });
330
+ SankeyGraph.prototype.publish("vertexCategoryColumn", "", "set", "Vertex Category ID column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
331
+ SankeyGraph.prototype.publish("vertexIDColumn", "", "set", "Vertex ID column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
332
+ SankeyGraph.prototype.publish("vertexLabelColumn", "", "set", "Vertex label column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
333
+ SankeyGraph.prototype.publish("vertexCentroidColumn", "", "set", "Vertex centroid column (boolean)", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
334
+ SankeyGraph.prototype.publish("vertexFAChar", "?", "string", "Vertex default FAChar");
335
+ SankeyGraph.prototype.publish("vertexFACharColumn", "", "set", "Vertex FAChar column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
336
+ SankeyGraph.prototype.publish("vertexTooltipColumn", "", "set", "Vertex tooltip column", function (this: SankeyGraph) { return this.vertexColumns(); }, { optional: true });
337
+ SankeyGraph.prototype.publish("vertexAnnotationColumns", [], "propertyArray", "Annotations", null, { autoExpand: AnnotationColumn });
338
+
339
+ SankeyGraph.prototype.publish("edgeColumns", [], "any", "Edge columns", null, { internal: true });
340
+ SankeyGraph.prototype.publish("edges", [], "any", "Edges (Edges)", null, { internal: true });
341
+ SankeyGraph.prototype.publish("edgeIDColumn", "", "set", "Edge ID column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
342
+ SankeyGraph.prototype.publish("edgeLabelColumn", "", "set", "Edge label column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
343
+ SankeyGraph.prototype.publish("edgeSourceColumn", "", "set", "Edge source ID column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
344
+ SankeyGraph.prototype.publish("edgeTargetColumn", "", "set", "Edge target ID column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });
345
+ SankeyGraph.prototype.publish("edgeWeightColumn", "", "set", "Edge weight column", function (this: SankeyGraph) { return this.edgeColumns(); }, { optional: true });