Skip to content

Instantly share code, notes, and snippets.

@emepyc
Last active January 12, 2016 16:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save emepyc/ae409eb069871e1be569 to your computer and use it in GitHub Desktop.
different labels in the same tree

TnT tree example showing how to render different labels in the same tree. The example first defines two different label renderers (two instances of tnt.tree.label.text). One to render internal labels taking the text from its branch length node property, and placing the labels in the upper left side of the node. The second to render leaf labels containing the name property of the node (default property) and placing them at the right of the node (default position). A third renderer (a general tnt.tree.label) uses the display and transform methods of one or the other depending on the node's type (leaf or not). This last label renderer is the one passed to the tree visualization.

<!DOCTYPE html>
<meta charset="utf-8">
<head>
<link rel="stylesheet" href="http://tntvis.github.io/tnt.tree/build/tnt.tree.css" type="text/css" />
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://tntvis.github.io/tnt.tree/build/tnt.tree.min.js"></script>
<script src="render.js"></script>
</head>
<body>
<div id="mytree"></div>
<script>
render (document.getElementById("mytree"));
</script>
</body>
var render = function (div) {
"use strict";
var internal_label = tnt.tree.label.text()
.text(function (node) {
return node.data().branch_length;
})
.fontweight("bold")
.fontsize(13)
.color("red")
.transform(function (node) {
return {
"translate" : [-50, -5],
"rotate" : -0
};
});
var leaf_label = tnt.tree.label.text()
.fontsize(14);
var node_label = tnt.tree.label()
.width(leaf_label.width())
.height(30)
.display(function (node) {
if (node.is_leaf()) {
return leaf_label.display().call(this, node, "vertical");
} else {
return internal_label.display().call(this, node, "vertical");
}
})
.transform(function (node) {
if (node.is_leaf()) {
return leaf_label.transform().call(this, node, "vertical");
} else {
return internal_label.transform().call(this, node, "vertical");
}
});
var tree = tnt.tree();
tree
.data (tnt.tree.parse_newick(newick))
.label (node_label)
.layout(tnt.tree.layout.vertical()
.width(950)
.scale(true)
);
tree(div);
};
var newick = "(((Crotalus_oreganus_oreganus_cytochrome_b:0.00800,Crotalus_horridus_cytochrome_b:0.05866):0.04732,(Thamnophis_elegans_terrestris_cytochrome_b:0.00366,Thamnophis_atratus_cytochrome_b:0.00172):0.06255):0.00555,(Pituophis_catenifer_vertebralis_cytochrome_b:0.00552,Lampropeltis_getula_cytochrome_b:0.02035):0.05762,((Diadophis_punctatus_cytochrome_b:0.06486,Contia_tenuis_cytochrome_b:0.05342):0.01037,Hypsiglena_torquata_cytochrome_b:0.05346):0.00779);";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment