Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Forked from bollwyvl/README.md
Last active August 29, 2015 14:14
Show Gist options
  • Save tonyfast/303745ea9672172ab31a to your computer and use it in GitHub Desktop.
Save tonyfast/303745ea9672172ab31a to your computer and use it in GitHub Desktop.
d3+flexbox table
<head>
<meta charset="utf-8">
<style>
@import "//cdn.jsdelivr.net/g/bootstrap(css/bootstrap.min.css)";
* {
-webkit-border-radius: 0 !important;
-moz-border-radius: 0 !important;
border-radius: 0 !important;
}
button{
opacity: .9;
}
body {
margin-top: 60px;
}
.container {
width: 95%;
z-index: 10;
}
div.table {
margin-top: 50px;
}
div.row {
display: flex;
align-items: flex-end;
}
div.cell{
flex: 1;
}
div.requests{
flex-direction: column;
display: flex;
z-index: 0;
position: fixed;
height: 800px;
}
div.request{
flex-shrink: 1;
opacity: .4;
}
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="url to json" id="user">
</div>
</div>
<p><i>
Currenly viewing ``<a class="url" href=""></a>``
</i></p>
</div>
</nav>
<div class="alert alert-primary" style="background-color: blue;">
<button type="button" class="close">click to close</button>
Click on the blue cells to move through the json tree.
</div>
<script src="//cdn.jsdelivr.net/g/d3js,lodash"></script>
<div class="container">
<div id="preview" class="well">&nbsp;</div>
</div>
<script>
;( function(){
// riot.mount('ttable')
function get( url ){
d3.json( url, render );// d3.json
d3.select('a.url')
.text(url)
.attr('href',url);
}
function render(d){
d3.select('div.table').remove();
d3.select('div.requests').remove();
// there is no need to store teh javascript variable if
// it is attached to the DOM ever
d3.select('body')
.append('div')
.attr( 'class', 'requests')
.selectAll('div.request')
.data(Array(d))
.call( function(s){
s.enter()
.append('div')
.attr('class','request')
.append('pre').append('code')
.text( function(d){
return JSON.stringify( d );
});
});
d3.select('body')
.append( 'div')
.classed( { 'container' : true })
.append( 'div' )
.attr( 'class', 'table');
h = d3.layout.tree( );
n = h( make_tree( d ) );
update( n.slice( 0, 1 ) );
}
// update row
function update( D ){
// data join
// append new data to the old nodes if they have been made
d3.select( 'div.table')
.selectAll('div.row')
.data( D )
.each( appendrow )
.call( function(s){
s.enter()
.append('div')
.attr('class', 'row btn-group btn-group-justified')
.each( appendrow );
s.exit().remove();
})
d3.select('.table')
.selectAll('button')
// hover behaviors
.on( 'mouseenter', function(d){
var k = 'size';
if ( d.hasOwnProperty( k ) ){
d3.select('#preview')
.html( function(){
return d['name'] + ' : ' + d['size'];
});
} else {
d3.select(this)
.text( d['children'].length );
d3.select('#preview')
.html( function(){
return d['name'] + ' : ' + d['children'].length + ' items';
});
};
})
.on( 'mouseleave', function( d ){
var k = 'size';
if ( d.hasOwnProperty( k ) ){
} else {
d3.select(this)
.text( d['name'] );
};
})
.filter( function(d){
return d['children'] ? this : null;
})
// click behavior
.on('click', function(d){
if ( D.length == d['depth'] ){
D.push( d );
} else {
D = D.slice( 0, d['depth'] );
D.push( d );
}
update(D);
});// click object
};//render
function appendrow(d){
// Append rows to the flexbox with data
d3.select(this)
.selectAll('div.cell')
.data( d['children'])
// update data
.call(function(cd){
// create cells from new data
cd.enter()
.append('div')
.attr('class', 'cell btn-group')
.append('button')
})//call
// css decisions
.call(function(cd){
cd.select('button')
.text(function(d){ return d['name'];})
.attr( 'class', function(d){
return 'btn '+ ( d.hasOwnProperty('children') ? 'btn-primary' : 'btn-default' );
})
})
// kill the old stuff
.exit().remove()
};//appendrow
// Wire Up listener
var input = d3.select("#user")
.on("change", function(){
get(input.property("value"));
});
// start everything running
get('user.json');
// functions to unroll an object into d3 hierarchy layouts
function make_tree( d ){
return {
'name' : 'root',
'children' : _.map( d, make_node )
};
}
function make_node( d, k ){
// numerical name are arrays
if ( _.isObject( d ) ){
return {
'name' : k,
'children' : _.map( d, make_node )
};
} else {
return {'name' : k,
'size' : d
};
}
}
// dismissible aleart
var alert = d3.select('.alert');
alert.call( function(s){
s.select('.close')
.on('click', function(s){
alert.remove();
});
});
})();//main
</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