Skip to content

Instantly share code, notes, and snippets.

@cgroll
Last active August 29, 2015 14:16
Show Gist options
  • Save cgroll/d84c1bab51dc8d524eae to your computer and use it in GitHub Desktop.
Save cgroll/d84c1bab51dc8d524eae to your computer and use it in GitHub Desktop.
Copula vine visualization: array of trees

This gist shows how a given copula vine decomposition in parent referencing notation could be visualized as array of trees.

// define margins
var margin = {top: 20, right: 80, bottom: 30, left: 150};
var nodeRadius = 18;
// graphics size without axis
var width = 960 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.text("vine.csv", function(text) {
var rawData = d3.csv.parseRows(text).map(function(row) {
return row.map(function(value) {
return +value;
});
});
// console.log(rawData);
var nVars = rawData.length;
var parVar;
var nd;
var treeData;
treeData = new Array;
for (ii = 0; ii < nVars; ii++) {
// create new tree
tr = new Array;
for (jj = 0; jj < nVars; jj++) {
parVar = rawData[jj][ii];
// console.log(parVar);
if (parVar == 0) {
nd = {"name": jj+1, "parent": null};
}
else {
nd = {"name": jj+1, "parent": parVar};
}
tr[jj] = nd;
}
treeData[ii] = tr;
}
});
// var data = [
// { "name": "3", "parent": "null" },
// { "name": "2", "parent": "3" },
// { "name": "1", "parent": "2" },
// { "name": "5", "parent": "2" },
// { "name": "6", "parent": "3" },
// { "name": "4", "parent": "3" },
// { "name": "7", "parent": "6" }
// ]
// var dataMap = data.reduce(function(map, node) {
// map[node.name] = node;
// return map;
// }, {});
// var treeData = [];
// data.forEach(function(node) {
// // add to parent
// var parent = dataMap[node.parent];
// if (parent) {
// // create child array if it doesn't exist
// (parent.copulaLink || (parent.copulaLink = []))
// // add node to child array
// .push(node);
// } else {
// // parent is null or missing
// treeData.push(node);
// }
// });
// var tree = d3.layout.tree()
// .sort(function comparator(a, b) {
// return +a.name - +b.name;
// })
// // .size([height, width - maxLabelLength*options.fontSize])
// .size([height, width])
// .children(function(d)
// {
// return (!d.copulaLink || d.copulaLink.length === 0) ? null : d.copulaLink;
// });
// var nodes = tree.nodes(treeData[0]);
// var links = tree.links(nodes);
// nodes.forEach(function(d) { d.y = d.depth * 200; });
// var link = d3.svg.diagonal()
// .projection(function(d)
// {
// return [d.x, d.y];
// });
// svg.selectAll("path.link")
// .data(links)
// .enter()
// .append("svg:path")
// .attr("class", "link")
// .attr("d", link);
// var nodeGroup = svg.selectAll("g.node")
// .data(nodes)
// .enter()
// .append("svg:g")
// .attr("class", "node")
// .attr("transform", function(d)
// {
// return "translate(" + d.x + "," + d.y + ")";
// });
// nodeGroup.append("svg:circle")
// .attr("class", "node-dot")
// .attr("r", nodeRadius)
// nodeGroup.append("svg:text")
// .attr("text-anchor", "middle")
// .attr("alignment-baseline", "central")
// .text(function(d) { return d.name; });
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 16px sans-serif;
}
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text { font: 16px sans-serif; }
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8">
</script>
<script src="assembly_code.js">
</script>
</body>
</html>
0 3 3 3 3 3
3 0 3 5 5 5
1 2 0 2 2 2
5 5 5 0 5 6
2 2 2 4 0 4
4 4 4 4 4 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment