Skip to content

Instantly share code, notes, and snippets.

@kaleguy
Last active March 13, 2017 00:22
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/e569d9b51d4469e922749792ed8a4469 to your computer and use it in GitHub Desktop.
Save kaleguy/e569d9b51d4469e922749792ed8a4469 to your computer and use it in GitHub Desktop.
Radial Tree from Leo File
license: gpl-3.0
border: no
height: 750

This is a demo of using Leo (open source outline editor + IDE) to create content for a radial tree 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 Radial Dendrogram

<?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" a="E"><vh>North America</vh>
<v t="josephorr.20170228225033.1"><vh>Canada</vh></v>
<v t="josephorr.20170228225040.1"><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.20170310204423.1"><vh>Venezuala</vh></v>
<v t="josephorr.20170310204437.1"><vh>Chile</vh></v>
<v t="josephorr.20170310204442.1"><vh>Peru</vh></v>
<v t="josephorr.20170310204446.1"><vh>Ecuador</vh></v>
<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>
<t tx="josephorr.20170310204423.1"></t>
<t tx="josephorr.20170310204437.1"></t>
<t tx="josephorr.20170310204442.1"></t>
<t tx="josephorr.20170310204446.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;
}
</style>
<svg width="700" height="700"></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 data = resultDocument.textContent;
data = data.replace(/,\s+$/,''); // kludge to get rid of trailing comma
treeData = JSON.parse(data);
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + (height / 2 + 20) + ")");
var cluster = d3.cluster()
.size([360, width / 2 - 120]);
var root = d3.hierarchy(treeData, 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", function(d) {
return "M" + project(d.x, d.y)
+ "C" + project(d.x, (d.y + d.parent.y) / 2)
+ " " + project(d.parent.x, (d.y + d.parent.y) / 2)
+ " " + project(d.parent.x, d.parent.y);
});
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(" + project(d.x, d.y) + ")"; });
node.append("circle")
.attr("r", 2.5);
node.append("text")
.attr("dy", "0.31em")
.attr("x", function(d) { return d.x < 180 === !d.children ? 6 : -6; })
.style("text-anchor", function(d) { return d.x < 180 === !d.children ? "start" : "end"; })
.attr("transform", function(d) { return "rotate(" + (d.x < 180 ? d.x - 90 : d.x + 90) + ")"; })
.text(function(d) { return d.data.name });
function project(x, y) {
var angle = (x - 90) / 180 * Math.PI, radius = y;
return [radius * Math.cos(angle), radius * Math.sin(angle)];
}
</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