Skip to content

Instantly share code, notes, and snippets.

@mathias-goebel
Created March 8, 2016 10:59
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 mathias-goebel/2b4a9336fd19ff2ae598 to your computer and use it in GitHub Desktop.
Save mathias-goebel/2b4a9336fd19ff2ae598 to your computer and use it in GitHub Desktop.
sunburst tree for TEI data
<html>
<meta charset="utf-8"/>
<style>
path {
stroke: #fff;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"/>
<script>
<![CDATA[
var para=window.location.href.split('=')[1];
console.log(para);
if(para != ""){
var jsonurl= "TEI-JSON.xq?tguri="+para;
}
else {
var jsonurl= "TEI-JSON.xq";
}
console.log(jsonurl);
var width = 960,
height = 700,
radius = (Math.min(width, height) / 2) - 10;
var formatNumber = d3.format(",d");
var x = d3.scale.linear()
.range([0, 2 * Math.PI]);
var y = d3.scale.sqrt()
.range([0, radius]);
var color = d3.scale.category20c();
var partition = d3.layout.partition()
.value(function(d) { return d.size; });
var arc = d3.svg.arc()
.startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
.endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
.innerRadius(function(d) { return Math.max(0, y(d.y)); })
.outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + (height / 2) + ")");
d3.json(jsonurl, function(error, root) {
if (error) throw error;
svg.selectAll("path")
.data(partition.nodes(root))
.enter().append("path")
.attr("d", arc)
.style("fill", function(d) { return color((d.children ? d : d.parent).name); })
.on("click", click)
.append("title")
.text(function(d) { return d.name + "\n" + formatNumber(d.value); });
});
function click(d) {
svg.transition()
.duration(750)
.tween("scale", function() {
var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
yd = d3.interpolate(y.domain(), [d.y, 1]),
yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
return function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); };
})
.selectAll("path")
.attrTween("d", function(d) { return function() { return arc(d); }; });
}
d3.select(self.frameElement).style("height", height + "px");
]]>
</script>
</body>
</html>
xquery version "3.0";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare option exist:serialize "method=text media-type=text/plain omit-xml-declaration=yes";
declare function local:recurse($node as node()*) as xs:string* {
for $n in $node
return(
'{' ||
string-join(
('"name": "', $n/local-name(), '"',
if( not($n/*) )
then ',"size":'||sum( $n//string-length(.) )
else (),
if($n/*) then ',"children": [' ||
string-join(
local:recurse($n/*)
, ',')
|| ']'
else ()
)
)
|| '}'
)};
let
$tguri := request:get-parameter('tguri', 'mr3x'),
$doc := doc('https://textgridlab.org/1.0/tgcrud-public/rest/textgrid:'|| $tguri ||'/data')
return
for $node in $doc/tei:*
return local:recurse($node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment