Skip to content

Instantly share code, notes, and snippets.

@widged
Last active December 15, 2015 04:39
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 widged/5203275 to your computer and use it in GitHub Desktop.
Save widged/5203275 to your computer and use it in GitHub Desktop.
D3js simple grouping
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 grouping</title>
<script src="http://d3js.org/d3.v2.min.js"></script>
</head>
<body>
<style type="text/css">
div.group {
background-color: blue;
color: white;
margin: 20px;
padding: 9px;
}
div.node {
background-color: orange;
margin: 3px;
}
</style>
<div id='viz'></div>
<script>
var data = [
[1,2,3],
[1,2,3,4],
[1,2,3,4,5]
];
var viz = d3.select("#viz");
var groups = viz.selectAll("div.group")
.data(data, function(d, index) {
return d.key;
})
.enter()
.append("div")
.classed('group', true)
.text(function(d, i) { return 'group ' + i});
var nodes = groups
.selectAll("div.node")
.data(function(d, index){
console.log("d",d);
return d;
})
.enter().append("div")
.classed('node', true)
.text(function(d, i) { return d});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment