Skip to content

Instantly share code, notes, and snippets.

@bperel
Forked from anonymous/composite.js
Last active December 17, 2015 08:29
Show Gist options
  • Save bperel/5580185 to your computer and use it in GitHub Desktop.
Save bperel/5580185 to your computer and use it in GitHub Desktop.
var force = d3.layout.force()
.gravity(0.2)
.charge(-150)
.linkDistance(300)
.size([800, 600]);
var svg = d3.select("body").append("svg:svg")
.attr("id","graph")
.attr("width", 800)
.attr("height", 600);
var g = svg.append("svg:g");
var mainObjects = [{name: "a"}, {name: "a2"}];
var secondaryObjects = [{name: "b", belongsTo: "a"}, {name: "b1", belongsTo: "a2"}];
var n=d3.values(mainObjects).concat(d3.values(secondaryObjects));
var l=[{source: 0, target: 1}];
var firstRectangles = g.append("svg:g").selectAll("rect.table")
.data(d3.values(mainObjects))
.enter().append("svg:rect")
.attr("name", function(d) { return d.name;})
.call(force.drag);
var secondRectangles = g.append("svg:g").selectAll("rect.table")
.data(d3.values(secondaryObjects))
.enter().append("svg:rect")
.attr("name", function(d) { return d.name;})
.attr("stroke", "black")
.attr("fill", "transparent")
.call(force.drag);
force
.nodes(n)
.links(l)
.on("tick", tick)
.start();
function tick() {
firstRectangles.each(function(d,i) {
var x = d.x;
var y = d.y;
d3.select(this)
.attr("width",40+50*i)
.attr("height",80+10*i)
.attr("x", this.x = x)
.attr("y", this.y = y)
.attr("transform", "translate(-"+(40+50*i)/2+" -"+(80+10*i)/2+")");
secondRectangles
.filter(function(d2,i2) { return d2.belongsTo === d.name; })
.attr("width",40+50*i)
.attr("height",80+10*i)
.attr("x",x+40+50*i)
.attr("y",y+80+10*i)
.attr("transform", "translate(-"+(40+50*i)/2+" -"+(80+10*i)/2+")");
});
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>test</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
</head>
<body>
<script type="text/javascript" src="composite.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment