-
Notifications
You must be signed in to change notification settings - Fork 286
/
LoadLayoutFeaturesSampleGraph.js
220 lines (203 loc) · 7.04 KB
/
LoadLayoutFeaturesSampleGraph.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/****************************************************************************
** @license
** This demo file is part of yFiles for HTML 2.6.
** Copyright (c) 2000-2024 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for HTML functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for HTML version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for HTML powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for HTML
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
import {
Color,
DefaultEdgePathCropper,
DefaultLabelStyle,
FreeEdgeLabelModel,
GraphBuilder,
GroupNodeLabelModel,
GroupNodeStyle,
HorizontalTextAlignment,
IArrow,
IEdgeStyle,
IGraph,
ILabelStyle,
INodeStyle,
Insets,
InteriorLabelModel,
LabelShape,
PolylineEdgeStyle,
ShapeNodeStyle,
ShapeNodeStyleRenderer,
SolidColorFill,
Stroke,
VerticalTextAlignment
} from 'yfiles'
/**
* @yjs:keep = nodeList,edgeList
* @param {!IGraph} graph
* @param {!string} fileName
* @returns {!Promise}
*/
export async function loadLayoutSampleGraph(graph, fileName) {
const response = await fetch(fileName)
const data = await response.json()
initStyles(graph)
const builder = new GraphBuilder(graph)
const nodesSource = builder.createNodesSource({
data: data.nodeList.filter((node) => !node.isGroup),
id: 'id',
tag: 'tag',
layout: 'layout',
parentId: 'parent'
})
const groupSource = builder.createGroupNodesSource({
data: data.nodeList.filter((node) => node.isGroup),
id: 'id',
tag: 'tag',
layout: 'layout'
})
const nodeCreator = nodesSource.nodeCreator
nodeCreator.styleProvider = (data) => getNodeStyle(data)
const nodeLabelCreator = nodeCreator.createLabelsSource((data) => data.labels || []).labelCreator
nodeLabelCreator.textProvider = (data) => data.text
nodeLabelCreator.layoutParameterProvider = (data) => InteriorLabelModel.CENTER
nodeLabelCreator.styleProvider = (data) => getLabelStyle(2.5, data)
const groupCreator = groupSource.nodeCreator
groupCreator.styleProvider = (data) => getGroupNodeStyle(data)
const groupLabelCreator = groupCreator.createLabelsSource(
(data) => data.labels || []
).labelCreator
groupLabelCreator.textProvider = (data) => data.text
groupLabelCreator.layoutParameterProvider = (data) =>
new GroupNodeLabelModel().createTabBackgroundParameter()
groupLabelCreator.styleProvider = (data) => getGroupLabelStyle()
const edgesSource = builder.createEdgesSource({
data: data.edgeList,
id: 'id',
sourceId: 'source',
targetId: 'target',
bends: 'bends',
tag: 'tag'
})
const edgeCreator = edgesSource.edgeCreator
edgeCreator.styleProvider = (data) => getEdgeStyle(data)
const edgeLabelCreator = edgeCreator.createLabelsSource((data) => data.labels || []).labelCreator
edgeLabelCreator.textProvider = (data) => data.text || ''
edgeLabelCreator.layoutParameterProvider = (data) =>
new FreeEdgeLabelModel().createDefaultParameter()
edgeLabelCreator.styleProvider = (data) => getLabelStyle(2.0, data)
builder.buildGraph()
}
/**
* @param {!IGraph} graph
*/
function initStyles(graph) {
graph.nodeDefaults.style = getNodeStyle(null)
graph.edgeDefaults.style = getEdgeStyle(null)
graph.decorator.portDecorator.edgePathCropperDecorator.setImplementation(
new DefaultEdgePathCropper({ cropAtPort: false, extraCropLength: 1.0 })
)
graph.nodeDefaults.labels.style = getLabelStyle(2.5, null)
graph.edgeDefaults.labels.style = getLabelStyle(2.0, null)
graph.groupNodeDefaults.style = getGroupNodeStyle(null)
graph.groupNodeDefaults.labels.style = getGroupLabelStyle()
graph.groupNodeDefaults.labels.layoutParameter =
new GroupNodeLabelModel().createTabBackgroundParameter()
}
/**
* @returns {!ILabelStyle}
*/
function getGroupLabelStyle() {
return new DefaultLabelStyle({
verticalTextAlignment: 'center',
horizontalTextAlignment: 'left',
textFill: '#9CC5CF'
})
}
/**
* @param {*} data
* @returns {!INodeStyle}
*/
function getGroupNodeStyle(data) {
const fill = data && data.fill ? data.fill : '#0B7189'
return new GroupNodeStyle({
tabFill: fill,
stroke: `2px solid ${fill}`,
tabPosition: 'top'
})
}
/**
* @param {*} data
* @returns {!INodeStyle}
*/
function getNodeStyle(data) {
const shapeNodeStyle = new ShapeNodeStyle({
shape: data && data.shape ? data.shape : 'round-rectangle',
fill: data && data.fill ? data.fill : '#FF6C00',
stroke: data && data.stroke ? data.stroke : '1.5px #662b00'
})
shapeNodeStyle.renderer.roundRectArcRadius = 3.5
return shapeNodeStyle
}
/**
* @param {*} data
* @returns {!IEdgeStyle}
*/
function getEdgeStyle(data) {
const stroke = Stroke.from((data && data.stroke) || '1.5px #662b00')
return new PolylineEdgeStyle({
stroke: stroke,
sourceArrow: getArrow(data ? data.sourceArrow : null, stroke),
targetArrow: getArrow(data ? data.targetArrow : null, stroke)
})
}
/**
* @param {?string} dataArrow
* @param {!Stroke} stroke
* @returns {!IArrow}
*/
function getArrow(dataArrow, stroke) {
if (!dataArrow) {
return IArrow.NONE
}
if (dataArrow === 'default') {
//the arrow is default, so we take the color from the edge stroke
const color = stroke.fill instanceof SolidColorFill ? stroke.fill.color : Color.from('#662b00')
return IArrow.from(`rgba(${color.r},${color.g},${color.b},${color.a}) small triangle`)
}
//custom arrow from json data, use it directly
return IArrow.from(dataArrow)
}
/**
* @param {number} rounding
* @param {*} data
* @returns {!ILabelStyle}
*/
function getLabelStyle(rounding, data) {
const labelStyle = new DefaultLabelStyle()
labelStyle.shape = LabelShape.ROUND_RECTANGLE
labelStyle.backgroundFill = data && data.fill ? data.fill : '#FFC398'
labelStyle.textFill = data && data.textFill ? data.textFill : '#662b00'
labelStyle.verticalTextAlignment = VerticalTextAlignment.CENTER
labelStyle.horizontalTextAlignment = HorizontalTextAlignment.CENTER
labelStyle.insets = new Insets(4, 2, 4, 1)
return labelStyle
}