UNPKG

phylogician-ts

Version:

Module to read, manipulate and write phylogenetic trees. Written in TypeScript

95 lines 3.27 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const d3 = __importStar(require("d3")); class TreeLayout { constructor(tree, svgParams) { this.tree = tree; this.svgParams = svgParams; } calcLabelDims() { this.tree.nodes.forEach(node => { const svg = d3.select('body').append('svg'); const text = this.renderNames(node, svg, true); // .attr('display', node.getShowLabel() ? 'block' : 'none'); const domEl = text.node(); if (!domEl) { throw new Error('Cannot find any DOM element with this selection.'); } const dim = domEl.getBBox(); const h = dim.height; const w = dim.width; node.setLabelHeight(h); node.setLabelWidth(w); svg.remove(); }); return this; } /** * Helper function to render names * * @param {TreeNode} node * @param {d3.Selection<any, any, any, any>} svg * @param {boolean} [tmp=false] * @returns * @memberof TreeLayout */ renderNames(node, svg, tmp = false) { const label = svg.append('text').attr('class', tmp ? 'labeltmp' : 'label'); this.updateNames(node, label); return label; } /** * Helper function to update names * * @param {TreeNode} node * @param {d3.Selection<any, any, any, any>} svg * @param {boolean} [tmp=false] * @returns * @memberof TreeLayout */ updateNames(node, label, duration = 0) { const chunks = node.getDisplayNameChunks(); if (chunks.length > 0) { label .selectAll('tspan') .data(chunks) // , function (d: IDisplayNameChunks) { return this.id }) .join(enter => enter .append('tspan') .text((l) => l.value) .attr('font-size', (l) => l.font.size) .attr('font-style', (l) => l.font.style) .attr('fill', (l) => l.color), update => update.call(u => u .transition() .duration(duration) .text((l) => l.value) .attr('font-size', (l) => l.font.size) .attr('font-style', (l) => l.font.style) .attr('fill', (l) => l.color)), exit => exit.call(e => e.remove())); } } /** * It gets the longest label. It skips labels marked to not show. * * @returns {number} * @memberof TreeLayout */ getLongestLabel() { let longestLabel = 0; this.tree.nodes.forEach(node => { if (node.getShowLabel()) { const labelWidth = node.getLabelWidth(); longestLabel = longestLabel < labelWidth ? labelWidth : longestLabel; } }); return longestLabel; } } exports.TreeLayout = TreeLayout; //# sourceMappingURL=TreeLayout.js.map