Skip to content

Instantly share code, notes, and snippets.

@erikhazzard
Forked from poezn/_.md
Created January 18, 2013 08:08
Show Gist options
  • Save erikhazzard/4563077 to your computer and use it in GitHub Desktop.
Save erikhazzard/4563077 to your computer and use it in GitHub Desktop.
Tributary Inlet Leaderboard
{"description":"Tributary Inlet Leaderboard","endpoint":"","display":"html","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"tab":"edit","display_percent":0.6150630391506304,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"hidepanel":false}
var display = d3.select("#display")
.style("overflow", "scroll");
var users_url = '/api/users';
var created_url = '/api/latest/created'
//grab the users
if(!tributary.users) {
d3.json(users_url, function(err, res) {
tributary.users = {};
res.forEach(function(u) {
tributary.users[u.login] = u;
})
render();
})
}
display.append("input")
.attr({
id: "fetch",
type: "button",
value: "fetch"
})
.on("click", function() {
d3.json(created_url, function(err, res) {
tributary.inlets = res;
render();
})
})
function render() {
if(!tributary.inlets) return;
var leaderboardData = _.chain(tributary.inlets)
.groupBy(function(d, i) { return d.user.login })
.map(function(inlets, id) { return { count: inlets.length, userId: id }})
.sortBy(function(d, i) { return -1 * d.count})
.value()
;
var inletsSel = display.selectAll("div.inlet")
.data(leaderboardData);
var inlets = inletsSel
.enter()
.append("div")
.classed("inlet", true)
inlets
.append("div")
.classed('rank', true)
.text(function(d, i) { return i + 1 + "." })
.style({
"font-weight": function(d, i) {
return i < 3 ? "bold" : "normal";
}
});
inlets.append("img")
.attr({
src: function(d) {
console.log(d.userId);
if (tributary.users) {
return tributary.users[d.userId].avatar_url;
} else {
return "";
}
},
width: function(d, i) {
return i < 3 ? 50 : 25;
},
height: function(d, i) {
return i < 3 ? 50 : 25;
}
});
inlets
.append("div")
.classed('user', true)
.text(function(d) { return d.userId });
};
render();
if(!tributary.inlets || !tributary.inlets.length) {
d3.json(created_url, function(err, res) {
tributary.inlets = res;
render();
})
}
#fetch {
position:relative;
margin-top: 20px;
right: 40px;
font-size:29px;
float:right;
}
.inlet {
margin-left: 10px;
margin-top: 20px;
}
.inlet img {
margin-right: 10px;
// margin-top: 5px;
}
.inlet .user, .inlet .rank {
display: inline;
}
.inlet .rank {
margin-right: 10px;
font-size: 20px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment