Skip to content

Instantly share code, notes, and snippets.

@kaleguy
Last active March 11, 2017 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaleguy/37ebcad3eff35e01db0ad83e81f438bc to your computer and use it in GitHub Desktop.
Save kaleguy/37ebcad3eff35e01db0ad83e81f438bc to your computer and use it in GitHub Desktop.
Tidy Tree and Dendrogram from Leo File
license: MIT
border: no
height: 620

This is a demo of using Leo (open source outline editor + IDE) to create content for a tree/dendrogram diagram (using D3 version 4). This snippet just displays the outline portion of a Leo file.

This version can handle outlines with clone nodes.

The code is based on Mike Bostock's Tidy Tree vs. Dendrogram

Viewable at bl.ocks.org

<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1" >
<leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
<globals body_outline_ratio="0.5" body_secondary_ratio="0.5">
<global_window_position top="50" left="50" height="500" width="700"/>
<global_log_window_position top="0" left="0" height="0" width="0"/>
</globals>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="josephorr.20170228222411.2" a="E"><vh>Top</vh>
<v t="josephorr.20170228222452.1" a="E"><vh>Regions</vh>
<v t="josephorr.20170228222513.1"><vh>North America</vh>
<v t="josephorr.20170228225033.1"><vh>Canada</vh></v>
<v t="josephorr.20170228225040.1" a="E"><vh>USA</vh>
<v t="josephorr.20170310195304.1" a="E"><vh>New York</vh>
<v t="josephorr.20170310195357.1"><vh>New York City</vh></v>
<v t="josephorr.20170310195406.1"><vh>Albany</vh></v>
<v t="josephorr.20170310195411.1"><vh>Kingston</vh></v>
</v>
<v t="josephorr.20170310195315.1"><vh>Oregon</vh></v>
<v t="josephorr.20170310195328.1"><vh>New Jersey</vh></v>
<v t="josephorr.20170310195310.1"><vh>California</vh></v>
</v>
</v>
<v t="josephorr.20170228222521.1" a="E"><vh>South America</vh>
<v t="josephorr.20170228224939.1"><vh>Bolivia</vh></v>
<v t="josephorr.20170228224946.1"><vh>Brazil</vh></v>
</v>
<v t="josephorr.20170228222526.1" a="E"><vh>Europe</vh>
<v t="josephorr.20170228224925.1" a="E"><vh>France</vh>
<v t="josephorr.20170310204019.1"><vh>Paris</vh></v>
<v t="josephorr.20170310204028.1"><vh>Arles</vh></v>
</v>
<v t="josephorr.20170228224930.1" a="E"><vh>Italy</vh>
<v t="josephorr.20170310204033.1"><vh>Rome</vh></v>
<v t="josephorr.20170310204038.1"><vh>Turin</vh></v>
</v>
</v>
</v>
<v t="josephorr.20170228222534.1" a="E"><vh>Vegetables</vh>
<v t="josephorr.20170310203838.1" a="E"><vh>Leafy</vh>
<v t="josephorr.20170310203853.1"><vh>Kale</vh></v>
<v t="josephorr.20170228222548.1"><vh>Spinach</vh></v>
</v>
<v t="josephorr.20170310203842.1" a="E"><vh>Other</vh>
<v t="josephorr.20170310195247.1"><vh>Cabbage</vh></v>
<v t="josephorr.20170228222538.1"><vh>Broccoli</vh></v>
</v>
</v>
<v t="josephorr.20170310195424.1" a="E"><vh>Cities</vh>
<v t="josephorr.20170310195411.1"></v>
<v t="josephorr.20170310195406.1"></v>
<v t="josephorr.20170310195357.1"></v>
</v>
</v>
</vnodes>
<tnodes>
<t tx="josephorr.20170228222411.2"></t>
<t tx="josephorr.20170228222452.1"></t>
<t tx="josephorr.20170228222513.1"></t>
<t tx="josephorr.20170228222521.1"></t>
<t tx="josephorr.20170228222526.1"></t>
<t tx="josephorr.20170228222534.1"></t>
<t tx="josephorr.20170228222538.1"></t>
<t tx="josephorr.20170228222548.1"></t>
<t tx="josephorr.20170228224925.1"></t>
<t tx="josephorr.20170228224930.1"></t>
<t tx="josephorr.20170228224939.1"></t>
<t tx="josephorr.20170228224946.1"></t>
<t tx="josephorr.20170228225033.1"></t>
<t tx="josephorr.20170228225040.1"></t>
<t tx="josephorr.20170310195247.1"></t>
<t tx="josephorr.20170310195304.1"></t>
<t tx="josephorr.20170310195310.1"></t>
<t tx="josephorr.20170310195315.1"></t>
<t tx="josephorr.20170310195328.1"></t>
<t tx="josephorr.20170310195357.1"></t>
<t tx="josephorr.20170310195406.1"></t>
<t tx="josephorr.20170310195411.1"></t>
<t tx="josephorr.20170310195424.1"></t>
<t tx="josephorr.20170310203838.1"></t>
<t tx="josephorr.20170310203842.1"></t>
<t tx="josephorr.20170310203853.1"></t>
<t tx="josephorr.20170310204019.1"></t>
<t tx="josephorr.20170310204028.1"></t>
<t tx="josephorr.20170310204033.1"></t>
<t tx="josephorr.20170310204038.1"></t>
</tnodes>
</leo_file>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node circle {
fill: #999;
}
.node text {
font: 10px sans-serif;
}
.node--internal circle {
fill: #555;
}
.node--internal text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #555;
stroke-opacity: 0.4;
stroke-width: 1.5px;
}
form {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
position: absolute;
left: 10px;
top: 10px;
}
label {
display: block;
}
</style>
<form>
<label><input type="radio" name="mode" value="cluster" checked> Dendrogram</label>
<label><input type="radio" name="mode" value="tree"> Tree</label>
</form>
<svg width="960" height="600"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
function loadXMLDoc(filename, type){
var xhttp = new XMLHttpRequest();
xhttp.open('GET', filename, false); // synchronous
xhttp.send('');
return xhttp['response' + type];
}
var xmlString = loadXMLDoc('example.leo', 'Text');
var oParser = new DOMParser();
var xml = oParser.parseFromString(xmlString,'text/xml');
var xsl = loadXMLDoc('leo.xsl', 'XML');
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
var resultDocument = xsltProcessor.transformToFragment(xml, document);
var ldata = resultDocument.textContent;
ldata = ldata.replace(/,\s+$/,''); // kludge to get rid of trailing comma
ldata = JSON.parse(ldata);
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(40,0)");
var tree = d3.tree()
.size([height, width - 160]);
var cluster = d3.cluster()
.size([height, width - 160]);
var root = d3.hierarchy(ldata, function(d){return d.children});
cluster(root);
var link = g.selectAll(".link")
.data(root.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
node.append("circle")
.attr("r", 2.5);
node.append("text")
.attr("dy", 3)
.attr("x", function(d) { return d.children ? -8 : 8; })
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.data.name; });
d3.selectAll("input")
.on("change", changed);
var timeout = setTimeout(function() {
d3.select("input[value=\"tree\"]")
.property("checked", true)
.dispatch("change");
}, 1000);
function changed() {
timeout = clearTimeout(timeout);
(this.value === "tree" ? tree : cluster)(root);
var t = d3.transition().duration(750);
node.transition(t).attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
link.transition(t).attr("d", diagonal);
}
function diagonal(d) {
return "M" + d.y + "," + d.x
+ "C" + (d.parent.y + 100) + "," + d.x
+ " " + (d.parent.y + 100) + "," + d.parent.x
+ " " + d.parent.y + "," + d.parent.x;
}
</script>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="v">
<xsl:variable name="t" select="@t"/>
<xsl:variable name="nodeSet" select="//v[@t=$t]"/>
{
"t": "<xsl:value-of select="@t"/>",
"name":"<xsl:value-of select="$nodeSet[1]/vh"/>",
"text":"<xsl:value-of select="//t[@tx=$t]"/>",
"children":[<xsl:apply-templates select="$nodeSet[1]/v"/>]
}
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>
<xsl:template match="text()"><xsl:value-of select="."/></xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment