-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfigtree_figtree.js.html
422 lines (360 loc) · 14.1 KB
/
figtree_figtree.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: figtree/figtree.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: figtree/figtree.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
import {select,easeCubic,scaleLinear} from "d3";
import uuid from "uuid";
import { mergeDeep} from "../utilities";
import 'd3-selection-multi';
import {GeoLayout} from "../layout/_deprecatedClasses/geoLayout";
import {BaubleManager} from "../features/baubleManager"
import p from "../_privateConstants.js"
import extent from "d3-array/src/extent";
/** @module figtree */
/**
* The FigTree class
*
* A class that takes a tree and draws it into the the given SVG root element. Has a range of methods
* for adding interactivity to the tree (e.g., mouse-over labels, rotating nodes and rerooting on branches).
* The figure updates with animated transitions when the tree is updated.
*/
export class FigTree {
static DEFAULT_SETTINGS() {
return {
xScale: {
scale: scaleLinear,
revisions: {
origin: null,
reverseAxis: false,
branchScale: 1,
offset: 0,
hedge: 0
}
},
yScale: {
scale: scaleLinear,
revisions: {
origin: null,
reverseAxis: false,
offset: 0,
hedge: 0
}
},
}
}
/**
* The constructor. All parameters are optional can be set with setters after construction.
* @param {DOM.node} [svg=null] - the svg that will hold the figure.
* @param margins {Object} [margins={top:10,bottom:60,left:30,right:60}] The margins around the tree figure. Axis will go in these spaces if applicable
* @param tree {Tree} [null] - the tree
* @param settings {Object} [settings={width:null,height:null}] sets the size for drawing the figure. If not provided, the size of the svg will be used.
* @returns {FigTree}
*/
constructor(svg=null, margins={top:10,bottom:60,left:30,right:60},tree=null, settings = {}) {
// this[p.layout] = layout;
this._margins = margins;
this.settings = mergeDeep(FigTree.DEFAULT_SETTINGS(),settings);
this._transitions= {
transitionDuration: 500,
transitionEase: easeCubic
};
this[p.svg]=svg;
this[p.tree] = tree;
this[p.tree].subscribeCallback( () => {
this.update();
});
setupSVG.call(this);
this.axes=[];
this._features=[];
this.nodeManager = new BaubleManager()
.class("node")
.layer("nodes-layer")
.figure(this);
this.nodeBackgroundManager = new BaubleManager()
.class("node-background")
.layer("node-backgrounds-layer")
.figure(this);
this.branchManager = new BaubleManager()
.class("branch")
.layer("branches-layer")
.figure(this);
// this.setupUpdaters();
return this;
}
/**
* Setter/getter for transition setting.
* @param t {Object} [t={transitionEase,transitionDuration:} - sets the transition ease and duration (in milliseconds) and returns the figtree instance
* if nothing is provided it returns the current settings.
* @returns {{transitionEase: cubicInOut, transitionDuration: number}|*}
*/
transitions(t=null){
if(t){
this._transitions={...this._transitions,...t};
}else{
return this._transitions;
}
}
/**
* Setter/getter for updating the margins.
* @param m {Object} [margins={top:10,bottom:60,left:30,right:60}] -any provided object will be merged with the current settings.
* If nothing is provided returns current margins.
* @returns {*}
*/
margins(m=null){
if(m!==null){
this._margins = {...this._margins,...m}
return this;
}else{
return this._margins
}
}
/**
* Updates the figure when the tree has changed. You can call this to force an update.
* Returns the figure.
*/
update() {
const {vertices,edges} = this[p.layout](this[p.tree]);
select(`#${this.svgId}`)
.attr("transform",`translate(${this._margins.left},${this._margins.top})`);
setUpScales.call(this,vertices,edges);
updateNodePositions.call(this,vertices);
updateBranchPositions.call(this,edges);
for(const feature of this._features){
feature.update({vertices,edges})
}
return this;
}
/**
* Adds an element to the node update cycle. The element's update method will be called for each node selection.
* Used to insert the visible elements mapped to the nodes
* @param elements
* @return {FigTree}
*/
nodes(...elements){
for(const element of elements){
this.nodeManager.element(element);
}
this.update();
return this;
}
/**
* Adds an element to the branch update cycle.The element's update method will be called for each branch selection.
* Used to insert the visible elements mapped to the nodes
* @param elements
* @return {FigTree}
*/
branches(...elements){
for(const element of elements){
this.branchManager.element(element);
}
this.update();
return this;
}
/**
* Adds an element to the node background update cycle. The element's update method will be called for each node selection.
* Used to insert the visible elements mapped to the nodes
* @param elements
* @return {FigTree}
*/
nodeBackgrounds(...elements){
for(const element of elements){
this.nodeBackgroundManager.element(element);
}
this.nodeBackgroundManager.update();
return this;
}
/**
* Registers some text to appear in a popup box when the mouse hovers over the selection.
*
* @param selection - {string} - passed to the d3 select. Adds an event listener to this selection to trigger the tooltip
* @param text - {string} - text to display in the tooltip.
*/
addToolTip(selection, text) {
this.svgSelection.selectAll(selection).on("mouseover",
function (selected) {
let tooltip = document.getElementById("tooltip");
if (typeof text === typeof "") {
tooltip.innerHTML = text;
} else {
tooltip.innerHTML = text(selected.node);
}
tooltip.style.display = "block";
tooltip.style.left =event.pageX + 10 + "px";
tooltip.style.top = event.pageY + 10 + "px";
}
);
this.svgSelection.selectAll(selection).on("mouseout", function () {
let tooltip = document.getElementById("tooltip");
tooltip.style.display = "none";
});
return this;
}
// setters and getters
/**
* Get or set SVG
* @param svg - optional parameter.
* @return {FigTree|svg}
*/
svg(svg=null){
if(svg===null){
return this[p.svg]
}
else {
this[p.svg] = svg;
return this;
}
}
/**
* Get or set tree
* @param tree
* @return {FigTree|Tree}
*/
tree(tree=null){
if(tree===null){
return this[p.tree]
}else{
this[p.tree] = tree;
this[p.tree].subscribeCallback( () => {
this.update();
});
this.update();
return this;
}
}
/**
* Get or set layout function
* @param layout
* @return {FigTree|*}
*/
layout(layout=null){
if(layout===null){
return this[p.layout]
}else{
this[p.layout] = layout;
this.update();
return this;
}
}
/**
* Add a feature to the update cycle. Also sets the figure of the feature to this figtree instance
* The feature's update cycle will be called
* with an an object containing the vertices and edges.
* @param feature
* @return {FigTree}
*/
feature(f){
f.figure(this);
this._features = this._features.concat(f);
this.update();
return this;
}
// axis(){
// const a = axis();
// this.feature(a);
// return a;
// }
// scaleBar(){
// const sb=scaleBar();
// this.feature(sb);
// return sb;
// }
// legend(){
// const l = legend();
// this.feature(l);
// return l;
// }
}
function setupSVG(){
this.svgId = `g-${uuid.v4()}`;
select(this[p.svg]).select(`#${this.svgId}`).remove();
// add a group which will contain the new tree
select(this[p.svg]).append("g")
.attr("id",this.svgId)
.attr("transform",`translate(${this._margins.left},${this._margins.top})`);
//to selecting every time
this.svgSelection = select(this[p.svg]).select(`#${this.svgId}`);
this.svgSelection.append("g").attr("class","annotation-layer");
this.svgSelection.append("g").attr("class", "axes-layer");
this.svgSelection.append("g").attr("class", "cartoon-layer");
this.svgSelection.append("g").attr("class", "branches-layer");
this.svgSelection.append("g").attr("class", "node-backgrounds-layer");
this.svgSelection.append("g").attr("class", "nodes-layer");
}
/**
* A helper function that sets the positions of the node and nodebackground groups in the svg and then calls update
* functions of the node and node background elements.
* @param vertices
*/
function updateNodePositions(vertices) {
this.nodeManager.update(vertices);
this.nodeBackgroundManager.update(vertices);
}
/**
* A helper function that sets the postions of the branch groups and calls the update functions of the branch elements.
* @param edges
*/
function updateBranchPositions(edges){
this.branchManager.update(edges)
}
function setUpScales(vertices,edges){
let width,height;
if(Object.keys(this.settings).indexOf("width")>-1){
width =this.settings.width;
}else{
width = this[p.svg].getBoundingClientRect().width;
}
if(Object.keys(this.settings).indexOf("height")>-1){
height =this.settings.height;
}else{
height = this[p.svg].getBoundingClientRect().height;
}
// create the scales
let xScale,yScale;
let projection=null;
if(this.layout instanceof GeoLayout){
xScale = scaleLinear();
yScale = scaleLinear();
projection = this.layout.projection;
}
else{
const xdomain = extent(vertices.map(d=>d.x).concat(edges.reduce((acc,e)=> acc.concat([e.v1.x,e.v0.x]),[])));
// almost always the same except when the trendline is added as an edge without vertices
const ydomain = extent(vertices.map(d=>d.y).concat(edges.reduce((acc,e)=>acc.concat([e.v1.y,e.v0.y]),[])));
xScale = this.settings.xScale.scale()
.domain(xdomain)
.range([0, width - this._margins.right-this._margins.left]);
yScale = this.settings.yScale.scale()
.domain(ydomain)
.range([height -this._margins.bottom-this._margins.top,0]);
}
this.scales = {x:xScale, y:yScale, width, height, projection};
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bauble.html">bauble</a></li><li><a href="module-figtree.html">figtree</a></li><li><a href="module-tree.html">tree</a></li></ul><h3>Classes</h3><ul><li><a href="Axis.html">Axis</a></li><li><a href="BaubleManager.html">BaubleManager</a></li><li><a href="CircleBauble.html">CircleBauble</a></li><li><a href="Decoration.html">Decoration</a></li><li><a href="Label.html">Label</a></li><li><a href="Legend.html">Legend</a></li><li><a href="module-bauble.Bauble.html">Bauble</a></li><li><a href="module-figtree.FigTree.html">FigTree</a></li><li><a href="module-tree.Tree.html">Tree</a></li><li><a href="module-tree-Node.html">Node</a></li><li><a href="ScaleBar.html">ScaleBar</a></li></ul><h3>Global</h3><ul><li><a href="global.html#branch">branch</a></li><li><a href="global.html#branchLabel">branchLabel</a></li><li><a href="global.html#branchPathGenerator">branchPathGenerator</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#dateToDecimal">dateToDecimal</a></li><li><a href="global.html#decimalToDate">decimalToDate</a></li><li><a href="global.html#equalAngleLayout">equalAngleLayout</a></li><li><a href="global.html#internalNodeLabel">internalNodeLabel</a></li><li><a href="global.html#label">label</a></li><li><a href="global.html#leapYear">leapYear</a></li><li><a href="global.html#leastSquares">leastSquares</a></li><li><a href="global.html#legend">legend</a></li><li><a href="global.html#makeEdges">makeEdges</a></li><li><a href="global.html#makeVertexFromNode">makeVertexFromNode</a></li><li><a href="global.html#rectangle">rectangle</a></li><li><a href="global.html#rectangularHilightedLayout">rectangularHilightedLayout</a></li><li><a href="global.html#scaleBar">scaleBar</a></li><li><a href="global.html#tipLabel">tipLabel</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Sat Mar 21 2020 11:15:18 GMT+0000 (Greenwich Mean Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>