-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
150 lines (131 loc) · 4.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { getNodes } from './model/nodes/nodes.js';
import { checkInterests, getEdges } from './model/edges/edges.js';
import {
setupD3,
defineArrowHeads,
createOwnershipCurve,
createControlCurve,
createUnknownCurve,
createControlText,
createOwnText,
createUnknownText,
setNodeLabelBkg,
injectSVGElements,
setZoomTransform,
removeMarkers,
setDashedLine,
createUnspecifiedNode,
} from './render/renderD3';
import { setupGraph, setEdges, setNodes } from './render/renderGraph';
import { setupUI, renderMessage, renderProperties } from './render/renderUI';
import './style.css';
// This sets up the basic format of the graph, such as direction, node and rank separation, and default label limits
const draw = ({
data,
container,
imagesPath,
labelLimit = 8,
rankDir = 'LR',
viewProperties = true,
useTippy = false,
}) => {
// Initialise D3 and graph
const { svg, inner } = setupD3(container);
const { g, render } = setupGraph(rankDir);
defineArrowHeads(svg);
// Extract the BODS data that is required for drawing the graph
const { edges } = getEdges(data);
const { nodes } = getNodes(data, edges);
// This section maps the incoming BODS data to the parameters expected by Dagre
setEdges(edges, g);
setNodes(nodes, g);
// Run the renderer. This is what draws the final graph.
try {
render(inner, g);
} catch (error) {
const message = 'Your data does not have any information that can be drawn.';
renderMessage(message);
console.error(error);
}
// Inject SVG images (for use e.g. as flags)
injectSVGElements(imagesPath, inner, g);
// Create white backgrounds for all of the node labels so that text legible
setNodeLabelBkg('white');
// stack unspecified nodes
nodes.forEach((node) => {
if (node?.class?.includes('unspecified')) {
const element = g.node(node.id).elem;
createUnspecifiedNode(element);
}
});
// calculate the new edges using control and ownership values
// this section could do with a refactor and move more of the logic into edges.js
edges.forEach((edge, index) => {
const {
source,
target,
interests,
interestRelationship,
shareStroke,
shareText,
controlStroke,
controlText,
unknownStroke,
config,
} = edge;
const unknownOffset = unknownStroke / 2;
const shareOffset = shareStroke / 2;
const controlOffset = -(controlStroke / 2);
const element = g.edge(source, target).elem;
if (interests.some((item) => item.category === 'ownership')) {
createOwnershipCurve(
element,
index,
config.share.positiveStroke,
config.share.strokeValue,
shareOffset,
checkInterests(interestRelationship),
config.share.arrowheadShape
);
}
if (interests.some((item) => item.category === 'control')) {
createControlCurve(
element,
index,
config.control.positiveStroke,
config.control.strokeValue,
controlOffset,
checkInterests(interestRelationship),
config.control.arrowheadShape
);
}
if (!interests.length || interests.some((item) => item.category === '')) {
createUnknownCurve(
element,
index,
config.unknown.positiveStroke,
config.unknown.strokeValue,
unknownOffset,
checkInterests(interestRelationship),
config.unknown.arrowheadShape
);
}
// this creates the edge labels, and will allow the labels to be turned off if the node count exceeds the labelLimit
const limitLabels = (createLabel) => g.nodeCount() < labelLimit && createLabel;
limitLabels(createControlText(svg, index, controlText));
limitLabels(createOwnText(svg, index, shareText));
// This removes the markers from any edges that have either ownership or control
if (
interests.some((item) => item.category === 'ownership') ||
interests.some((item) => item.category === 'control')
) {
removeMarkers(g, source, target);
}
});
const { zoom } = setZoomTransform(inner, svg);
setupUI(zoom, inner, svg);
if (viewProperties) {
renderProperties(inner, g, useTippy);
}
};
export { draw };