Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Last active August 29, 2015 14:14
Show Gist options
  • Save tonyfast/2fb83e94a3b487a283e5 to your computer and use it in GitHub Desktop.
Save tonyfast/2fb83e94a3b487a283e5 to your computer and use it in GitHub Desktop.
d3+handlebars+bootstrap

An interactive version of a Reingold–Tilford tree. Click on the nodes to expand or collapse.

Modifications

Very minor tweaks were made to create a collapsible tree of a dict-like object.

  • The dict is converted to the flare.json tree structure with a default size.
  • I added a subset of a gist API response because that is what I am working on.
  • I added lodash because the tree converter was already written.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<style></style>
</head>
<body>
<div class="files container"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.min.js"></script>
<script>
d3.json("./user.json", function( e, d ) {
if (e) return console.warn(e);
data = _.merge( d, {} );
files = d3.select('.files').selectAll('.gist')
.data( data ).enter();
// html
files.append( 'div')
.classed( {
'gist' : true,
'row' : true,
})
.html( render );
}); //d3.json
function render( d,i ){
var template = Handlebars.compile( d3.select('#gist').html() );
if ( i < 100){
return template( d );
} else {
this.remove();
return null;
}
}
</script>
<script id="gist" type="text/x-handlebars-template">
<div class="panel panel-default">
<div class="panel-heading">
<a href="{{html_url}}">{{description}}</a>
</div>
<div class="panel-body">
{{#each files}}
<a href="{{raw_url}}">{{filename}} :: {{size}}</a>&nbsp;
{{/each}}
</div>
<div class="panel-footer">
</div>
</div>
</script>
</body>
[
{
"url": "https://api.github.com/gists/f7c69bda4418117bae72",
"forks_url": "https://api.github.com/gists/f7c69bda4418117bae72/forks",
"commits_url": "https://api.github.com/gists/f7c69bda4418117bae72/commits",
"id": "f7c69bda4418117bae72",
"git_pull_url": "https://gist.github.com/f7c69bda4418117bae72.git",
"git_push_url": "https://gist.github.com/f7c69bda4418117bae72.git",
"html_url": "https://gist.github.com/f7c69bda4418117bae72",
"files": {
"query.js": {
"filename": "query.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f7c69bda4418117bae72/raw/5a759e6b982974963c9f6aa8dad3bf818e053113/query.js",
"size": 265
}
},
"public": true,
"created_at": "2014-12-16T20:14:27Z",
"updated_at": "2014-12-16T20:14:28Z",
"description": "Get Query Parameters from url",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/f7c69bda4418117bae72/comments",
"owner": {
"login": "tonyfast",
"id": 4236275,
"avatar_url": "https://avatars.githubusercontent.com/u/4236275?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/tonyfast",
"html_url": "https://github.com/tonyfast",
"followers_url": "https://api.github.com/users/tonyfast/followers",
"following_url": "https://api.github.com/users/tonyfast/following{/other_user}",
"gists_url": "https://api.github.com/users/tonyfast/gists{/gist_id}",
"starred_url": "https://api.github.com/users/tonyfast/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/tonyfast/subscriptions",
"organizations_url": "https://api.github.com/users/tonyfast/orgs",
"repos_url": "https://api.github.com/users/tonyfast/repos",
"events_url": "https://api.github.com/users/tonyfast/events{/privacy}",
"received_events_url": "https://api.github.com/users/tonyfast/received_events",
"type": "User",
"site_admin": false
}
},
{
"url": "https://api.github.com/gists/f2d83a03551d26bc57f2",
"forks_url": "https://api.github.com/gists/f2d83a03551d26bc57f2/forks",
"commits_url": "https://api.github.com/gists/f2d83a03551d26bc57f2/commits",
"id": "f2d83a03551d26bc57f2",
"git_pull_url": "https://gist.github.com/f2d83a03551d26bc57f2.git",
"git_push_url": "https://gist.github.com/f2d83a03551d26bc57f2.git",
"html_url": "https://gist.github.com/f2d83a03551d26bc57f2",
"files": {
"README.md": {
"filename": "README.md",
"type": "text/plain",
"language": "Markdown",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/86092a26be3e9c11a0a7091cc42c1ddaae77ff67/README.md",
"size": 66
},
"handlebars.js": {
"filename": "handlebars.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/f826bbfd3879d006111ddaf0def6a05a2c329078/handlebars.js",
"size": 100900
},
"index.html": {
"filename": "index.html",
"type": "text/html",
"language": "HTML",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/beeb556b545ea42be2fa1ded9b5bd2fcd98b31f4/index.html",
"size": 318
},
"jquery.js": {
"filename": "jquery.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/9f7b3d38958726758eddc89880d50db98e11cfac/jquery.js",
"size": 247351
},
"jsyaml.js": {
"filename": "jsyaml.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/f518f133dd07d9f6e2873ac34c7ccac26b6ff98c/jsyaml.js",
"size": 92370
},
"main.js": {
"filename": "main.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/868332bd4330189dcdb19a088481573dc2c47edc/main.js",
"size": 2258
},
"markdown.js": {
"filename": "markdown.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/7412794ae3df971e2abb66fc99015bd62a826f0a/markdown.js",
"size": 57341
},
"readme.md": {
"filename": "readme.md",
"type": "text/plain",
"language": "Markdown",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/3a0c1566bdb2e299cd4b4025c01e4404602898be/readme.md",
"size": 144
},
"require.js": {
"filename": "require.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/77a5bb1d3be75c2f6e0d7f8484d00b890319da0f/require.js",
"size": 83083
},
"underscore.js": {
"filename": "underscore.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/tonyfast/f2d83a03551d26bc57f2/raw/b4f49a0204cae8f267d9ae0f3009d9a485c1ba46/underscore.js",
"size": 47559
}
},
"public": true,
"created_at": "2014-12-15T03:49:02Z",
"updated_at": "2014-12-20T19:06:02Z",
"description": "Load Structured Data in Gist and Parse the Markdownified text with Moustache notation",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/f2d83a03551d26bc57f2/comments",
"owner": {
"login": "tonyfast",
"id": 4236275,
"avatar_url": "https://avatars.githubusercontent.com/u/4236275?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/tonyfast",
"html_url": "https://github.com/tonyfast",
"followers_url": "https://api.github.com/users/tonyfast/followers",
"following_url": "https://api.github.com/users/tonyfast/following{/other_user}",
"gists_url": "https://api.github.com/users/tonyfast/gists{/gist_id}",
"starred_url": "https://api.github.com/users/tonyfast/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/tonyfast/subscriptions",
"organizations_url": "https://api.github.com/users/tonyfast/orgs",
"repos_url": "https://api.github.com/users/tonyfast/repos",
"events_url": "https://api.github.com/users/tonyfast/events{/privacy}",
"received_events_url": "https://api.github.com/users/tonyfast/received_events",
"type": "User",
"site_admin": false
}
},
{
"url": "https://api.github.com/gists/242af9bda2517a9179c0",
"forks_url": "https://api.github.com/gists/242af9bda2517a9179c0/forks",
"commits_url": "https://api.github.com/gists/242af9bda2517a9179c0/commits",
"id": "242af9bda2517a9179c0",
"git_pull_url": "https://gist.github.com/242af9bda2517a9179c0.git",
"git_push_url": "https://gist.github.com/242af9bda2517a9179c0.git",
"html_url": "https://gist.github.com/242af9bda2517a9179c0",
"files": {
"blog-to-data.ipynb": {
"filename": "blog-to-data.ipynb",
"type": "text/plain",
"language": null,
"raw_url": "https://gist.githubusercontent.com/tonyfast/242af9bda2517a9179c0/raw/cee90a51fc1b8fa66e3ea463205c6000f7fd0e8b/blog-to-data.ipynb",
"size": 18955
},
"readme.md": {
"filename": "readme.md",
"type": "text/plain",
"language": "Markdown",
"raw_url": "https://gist.githubusercontent.com/tonyfast/242af9bda2517a9179c0/raw/57d320fcc671f49106b64ece601870dc33c03c97/readme.md",
"size": 114
}
},
"public": true,
"created_at": "2014-12-01T19:09:24Z",
"updated_at": "2014-12-03T13:08:50Z",
"description": "Using Jekyll blog posts as data in Python",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/242af9bda2517a9179c0/comments",
"owner": {
"login": "tonyfast",
"id": 4236275,
"avatar_url": "https://avatars.githubusercontent.com/u/4236275?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/tonyfast",
"html_url": "https://github.com/tonyfast",
"followers_url": "https://api.github.com/users/tonyfast/followers",
"following_url": "https://api.github.com/users/tonyfast/following{/other_user}",
"gists_url": "https://api.github.com/users/tonyfast/gists{/gist_id}",
"starred_url": "https://api.github.com/users/tonyfast/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/tonyfast/subscriptions",
"organizations_url": "https://api.github.com/users/tonyfast/orgs",
"repos_url": "https://api.github.com/users/tonyfast/repos",
"events_url": "https://api.github.com/users/tonyfast/events{/privacy}",
"received_events_url": "https://api.github.com/users/tonyfast/received_events",
"type": "User",
"site_admin": false
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment