-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlayoutHelpers.js.html
189 lines (162 loc) · 5.5 KB
/
layoutHelpers.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: layoutHelpers.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: layoutHelpers.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import {mean} from "d3";
import {Type} from "../tree";
import p from "../_privateConstants.js";
/** @module layout */
function getVertexClassesFromNode(node){
let classes = [(!node.children ? "external-node" : "internal-node")];
const tree = node.tree;
if (node.annotations) {
classes = [
...classes,
...Object.entries(node.annotations)
.filter(([key]) => {
return tree.annotations[key] &&
(tree.annotations[key].type === Type.DISCRETE ||
tree.annotations[key].type === Type.BOOLEAN ||
tree.annotations[key].type === Type.INTEGER);
})
.map(([key, value]) =>{
if(tree.annotations[key].type===Type.DISCRETE || tree.annotations[key].type === Type.INTEGER){
return `${key}-${value}`;
}else if(tree.annotations[key].type === Type.BOOLEAN && value ){
return `${key}`
}
})];
}
return classes;
}
// TODO update this to handel location for other layouts that aren't left to right
/**
* Makes a vertex from a node in a tree.
* anatomy of a vertex
* {
name:node.name,
length:node.length,
height:node.height,
divergence:node.divergence,
level:node.level,
label:node.label,
annotations:node.annotations,
key: node.id,
id:node.id,
parent:node.parent?node.parent.id:null,
children:node.children?node.children.map(child=>child.id):null,
degree: (node.children ? node.children.length + 1 : 1),// the number of edges (including stem)
textLabel:{
labelBelow:labelBelow,
x:leftLabel?"-6":"12",
y:leftLabel?(labelBelow ? "-8": "8" ):"0",
alignmentBaseline: leftLabel?(labelBelow ? "bottom": "hanging" ):"middle",
textAnchor:leftLabel?"end":"start",
},
classes: getVertexClassesFromNode(node),
[p.node]:node,
};
*
* @param node
* @returns vertex
*/
export function makeVertexFromNode(node){
const leftLabel= !!node.children;
const labelBelow= (!!node.children && (!node.parent || node.parent.children[0] !== node));
return {
name:node.name,
length:node.length,
height:node.height,
divergence:node.divergence,
level:node.level,
label:node.label,
annotations:node.annotations,
key: node.id,
id:node.id,
parent:node.parent?node.parent.id:null,
children:node.children?node.children.map(child=>child.id):null,
degree: (node.children ? node.children.length + 1 : 1),// the number of edges (including stem)
textLabel:{
labelBelow:labelBelow,
x:leftLabel?"-6":"12",
y:leftLabel?(labelBelow ? "-8": "8" ):"0",
alignmentBaseline: leftLabel?(labelBelow ? "bottom": "hanging" ):"middle",
textAnchor:leftLabel?"end":"start",
},
classes: getVertexClassesFromNode(node),
[p.node]:node,
};
}
/**
* Makes edges from an array of vertices.
*
* Edge {
v0: parent vertex,
v1: target vertex,
key: vertex.key,
id:vertex.id,
classes:vertex.classes,
x:x position,
y:y.position,
textLabel:{ label postions
x:,
y: -6,
alignmentBaseline: "bottom",
textAnchor:"middle",
},
* @param vertices
* @returns {*}
*/
export function makeEdges(vertices){
const nodeMap = new Map(vertices.map(v=>[v[p.node],v]));
return vertices.filter(v=>v[p.node].parent).map(v=>{
return {
v0: nodeMap.get(v[p.node].parent),
v1: v,
key: v.key,
id:v.id,
classes:v.classes,
x:nodeMap.get(v[p.node].parent).x,
y:v.y,
textLabel:{
x:mean([v.x,nodeMap.get(v[p.node].parent).x]),
y: -6,
alignmentBaseline: "bottom",
textAnchor:"middle",
},
}
})
}
export const layoutFactory=makeVertices=>tree=>{
const vertices = makeVertices(tree);
const edges = makeEdges(vertices);
return {vertices,edges}
};</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-layout.html">layout</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Wed Mar 18 2020 15:04:49 GMT+0000 (Greenwich Mean Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>