Skip to content

Instantly share code, notes, and snippets.

@armollica
Last active December 9, 2015 03:17
Show Gist options
  • Save armollica/dddf616d4f71475c4611 to your computer and use it in GitHub Desktop.
Save armollica/dddf616d4f71475c4611 to your computer and use it in GitHub Desktop.
Fantasy Football Scores
var draw = {};
draw.own = function(selection, data, scale) {
var axis = selection.append("g").attr("class", "y axis")
.call(d3.svg.axis().scale(scale.y).orient("left"));
axis.selectAll("text")
.text(function(d) { return d == 1 ? "Week 1" : d});
var bars = selection.selectAll(".bar").data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", scale.x(0))
.attr("y", function(d) { return scale.y(d.week); })
.attr("width", function(d) { return scale.x(d.score); })
.attr("height", scale.y.rangeBand())
.style("fill", function(d) { return scale.color(d.score); });
var x = d3.mean(data, function(d) { return d.score; });
var line = selection.append("path")
.datum([[scale.x(x), scale.y(1)], [scale.x(x), scale.y(13) + scale.y.rangeBand()]])
.attr("class", "mean")
.attr("d", d3.svg.line());
var average = selection.append("text")
.attr("class", "average")
.attr("x", scale.x(x))
.attr("y", scale.y(13) + scale.y.rangeBand())
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.text("Avg. " + Math.round(x));
var labels = selection.selectAll(".label").data(data)
.enter().append("text")
.attr("class", "label")
.attr("x", function(d) { return scale.x(d.score); })
.attr("y", function(d) { return scale.y(d.week); })
.attr("dx", 4)
.attr("dy", "1em")
.text(function(d) { return d.score + (d.week == 1 ? " pts" : ""); });
};
draw.opponent = function(selection, data, scale) {
var axis = {
y: d3.svg.axis().scale(scale.y).orient("left")
};
var axis = selection.append("g").attr("class", "y axis")
.call(d3.svg.axis().scale(scale.y).orient("left"));
axis.selectAll("text")
.text(function(week) {
var opponent = data
.filter(function(d) { return d.week == week; })[0].opponent_name;
return opponent;
});
var bars = selection.selectAll(".bar").data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", scale.x(0))
.attr("y", function(d) { return scale.y(d.week); })
.attr("width", function(d) { return scale.x(d.opponent_score); })
.attr("height", scale.y.rangeBand())
.style("fill", function(d) { return scale.color(d.opponent_score); });
var x = d3.mean(data, function(d) { return d.opponent_score; });
var line = selection.append("path")
.datum([[scale.x(x), scale.y(1)], [scale.x(x), scale.y(13) + scale.y.rangeBand()]])
.attr("class", "mean")
.attr("d", d3.svg.line());
var average = selection.append("text")
.attr("class", "average")
.attr("x", scale.x(x))
.attr("y", scale.y(13) + scale.y.rangeBand())
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.text("Avg. " + Math.round(x));
var labels = selection.selectAll(".label").data(data)
.enter().append("text")
.attr("class", "label")
.attr("x", function(d) { return scale.x(d.opponent_score); })
.attr("y", function(d) { return scale.y(d.week); })
.attr("dx", 4)
.attr("dy", "1em")
.text(function(d) { return d.opponent_score + (d.week == 1 ? " pts" : ""); });
};
draw.margin = function(selection, data, scale) {
var negatives = data
.filter(function(d) { return d.margin < 0; })
.map(function(d) { return d.week; });
var axis = selection.append("g").attr("class", "y axis")
.call(d3.svg.axis().scale(scale.y).orient("left"))
.attr("transform", "translate(" + scale.x(0) + ",0)");
axis.selectAll("text")
.attr("x", function(week) { return negatives.indexOf(week) == -1 ? -9 : 9; })
.style("text-anchor", function(week) { return negatives.indexOf(week) == -1 ? "end" : "start"; })
.text(function(week) { return "Week " + week; });
axis.selectAll("line")
.attr("x2", function(week) { return negatives.indexOf(week) == -1 ? -6 : 6; });
var bars = selection.selectAll(".bar").data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return d.margin >= 0 ? scale.x(0) : scale.x(d.margin);
})
.attr("y", function(d) { return scale.y(d.week); })
.attr("width", function(d) {
return d.margin >= 0 ?
scale.x(d.margin) - scale.x(0) :
scale.x(0) - scale.x(d.margin);
})
.attr("height", scale.y.rangeBand())
.style("fill", function(d) { return scale.color(d.margin); });
var x = d3.mean(data, function(d) { return d.margin; });
var line = selection.append("path")
.datum([[scale.x(x), scale.y(1)], [scale.x(x), scale.y(13) + scale.y.rangeBand()]])
.attr("class", "mean")
.attr("d", d3.svg.line());
var average = selection.append("text")
.attr("class", "average")
.attr("x", scale.x(x))
.attr("y", scale.y(13) + scale.y.rangeBand())
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.text("Avg. " + Math.round(x));
var labels = selection.selectAll(".label").data(data)
.enter().append("text")
.attr("class", "label")
.attr("x", function(d) { return scale.x(d.margin); })
.attr("y", function(d) { return scale.y(d.week); })
.attr("dx", function(d) {
return d.margin >= 0 ? 4 : -4;
})
.attr("dy", "1em")
.style("text-anchor", function(d) {
return d.margin >= 0 ? "start" : "end";
})
.text(function(d) { return d.margin + (d.week == 1 ? " pts" : ""); });
};
[{"opponent_owner":"Joey Glocke","owner":"Andy Stoughton","week":1,"opponent_score":49,"score":94,"home":true},{"opponent_owner":"Spencer Morris","owner":"Corey Larson","week":1,"opponent_score":87,"score":89,"home":true},{"opponent_owner":"Justin Orrick","owner":"Nicholas Patri","week":1,"opponent_score":100,"score":104,"home":true},{"opponent_owner":"Anthony Swichtenberg","owner":"Blake Martin","week":1,"opponent_score":83,"score":101,"home":true},{"opponent_owner":"Andrew Mollica","owner":"Joel Schmocker","week":1,"opponent_score":106,"score":84,"home":true},{"opponent_owner":"Zach Droeszler","owner":"Nate Miller","week":1,"opponent_score":100,"score":67,"home":true},{"opponent_owner":"Spencer Morris","owner":"Joey Glocke","week":2,"opponent_score":124,"score":65,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Justin Orrick","week":2,"opponent_score":57,"score":104,"home":true},{"opponent_owner":"Corey Larson","owner":"Anthony Swichtenberg","week":2,"opponent_score":41,"score":80,"home":true},{"opponent_owner":"Nicholas Patri","owner":"Andrew Mollica","week":2,"opponent_score":86,"score":100,"home":true},{"opponent_owner":"Blake Martin","owner":"Zach Droeszler","week":2,"opponent_score":56,"score":95,"home":true},{"opponent_owner":"Joel Schmocker","owner":"Nate Miller","week":2,"opponent_score":73,"score":118,"home":true},{"opponent_owner":"Joey Glocke","owner":"Justin Orrick","week":3,"opponent_score":99,"score":89,"home":true},{"opponent_owner":"Anthony Swichtenberg","owner":"Spencer Morris","week":3,"opponent_score":132,"score":82,"home":true},{"opponent_owner":"Andrew Mollica","owner":"Andy Stoughton","week":3,"opponent_score":86,"score":87,"home":true},{"opponent_owner":"Zach Droeszler","owner":"Corey Larson","week":3,"opponent_score":106,"score":87,"home":true},{"opponent_owner":"Nate Miller","owner":"Nicholas Patri","week":3,"opponent_score":96,"score":91,"home":true},{"opponent_owner":"Joel Schmocker","owner":"Blake Martin","week":3,"opponent_score":82,"score":107,"home":true},{"opponent_owner":"Anthony Swichtenberg","owner":"Joey Glocke","week":4,"opponent_score":79,"score":70,"home":true},{"opponent_owner":"Justin Orrick","owner":"Andrew Mollica","week":4,"opponent_score":56,"score":128,"home":true},{"opponent_owner":"Spencer Morris","owner":"Zach Droeszler","week":4,"opponent_score":80,"score":67,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Nate Miller","week":4,"opponent_score":85,"score":61,"home":true},{"opponent_owner":"Corey Larson","owner":"Joel Schmocker","week":4,"opponent_score":47,"score":88,"home":true},{"opponent_owner":"Nicholas Patri","owner":"Blake Martin","week":4,"opponent_score":74,"score":104,"home":true},{"opponent_owner":"Joey Glocke","owner":"Andrew Mollica","week":5,"opponent_score":70,"score":82,"home":true},{"opponent_owner":"Zach Droeszler","owner":"Anthony Swichtenberg","week":5,"opponent_score":91,"score":95,"home":true},{"opponent_owner":"Nate Miller","owner":"Justin Orrick","week":5,"opponent_score":84,"score":64,"home":true},{"opponent_owner":"Joel Schmocker","owner":"Spencer Morris","week":5,"opponent_score":95,"score":127,"home":true},{"opponent_owner":"Blake Martin","owner":"Andy Stoughton","week":5,"opponent_score":68,"score":109,"home":true},{"opponent_owner":"Nicholas Patri","owner":"Corey Larson","week":5,"opponent_score":81,"score":88,"home":true},{"opponent_owner":"Zach Droeszler","owner":"Joey Glocke","week":6,"opponent_score":82,"score":83,"home":true},{"opponent_owner":"Andrew Mollica","owner":"Nate Miller","week":6,"opponent_score":119,"score":120,"home":true},{"opponent_owner":"Anthony Swichtenberg","owner":"Joel Schmocker","week":6,"opponent_score":60,"score":111,"home":true},{"opponent_owner":"Justin Orrick","owner":"Blake Martin","week":6,"opponent_score":61,"score":100,"home":true},{"opponent_owner":"Spencer Morris","owner":"Nicholas Patri","week":6,"opponent_score":122,"score":88,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Corey Larson","week":6,"opponent_score":58,"score":61,"home":true},{"opponent_owner":"Joey Glocke","owner":"Nate Miller","week":7,"opponent_score":100,"score":120,"home":true},{"opponent_owner":"Joel Schmocker","owner":"Zach Droeszler","week":7,"opponent_score":93,"score":96,"home":true},{"opponent_owner":"Blake Martin","owner":"Andrew Mollica","week":7,"opponent_score":84,"score":102,"home":true},{"opponent_owner":"Nicholas Patri","owner":"Anthony Swichtenberg","week":7,"opponent_score":89,"score":107,"home":true},{"opponent_owner":"Corey Larson","owner":"Justin Orrick","week":7,"opponent_score":63,"score":86,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Spencer Morris","week":7,"opponent_score":91,"score":44,"home":true},{"opponent_owner":"Joel Schmocker","owner":"Joey Glocke","week":8,"opponent_score":84,"score":83,"home":true},{"opponent_owner":"Nate Miller","owner":"Blake Martin","week":8,"opponent_score":79,"score":94,"home":true},{"opponent_owner":"Zach Droeszler","owner":"Nicholas Patri","week":8,"opponent_score":95,"score":61,"home":true},{"opponent_owner":"Andrew Mollica","owner":"Corey Larson","week":8,"opponent_score":88,"score":77,"home":true},{"opponent_owner":"Anthony Swichtenberg","owner":"Andy Stoughton","week":8,"opponent_score":125,"score":87,"home":true},{"opponent_owner":"Justin Orrick","owner":"Spencer Morris","week":8,"opponent_score":88,"score":68,"home":true},{"opponent_owner":"Joey Glocke","owner":"Blake Martin","week":9,"opponent_score":124,"score":87,"home":true},{"opponent_owner":"Nicholas Patri","owner":"Joel Schmocker","week":9,"opponent_score":116,"score":72,"home":true},{"opponent_owner":"Corey Larson","owner":"Nate Miller","week":9,"opponent_score":91,"score":132,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Zach Droeszler","week":9,"opponent_score":51,"score":103,"home":true},{"opponent_owner":"Spencer Morris","owner":"Andrew Mollica","week":9,"opponent_score":122,"score":90,"home":true},{"opponent_owner":"Justin Orrick","owner":"Anthony Swichtenberg","week":9,"opponent_score":91,"score":68,"home":true},{"opponent_owner":"Nicholas Patri","owner":"Joey Glocke","week":10,"opponent_score":131,"score":111,"home":true},{"opponent_owner":"Blake Martin","owner":"Corey Larson","week":10,"opponent_score":87,"score":82,"home":true},{"opponent_owner":"Joel Schmocker","owner":"Andy Stoughton","week":10,"opponent_score":73,"score":62,"home":true},{"opponent_owner":"Nate Miller","owner":"Spencer Morris","week":10,"opponent_score":81,"score":81,"home":true},{"opponent_owner":"Zach Droeszler","owner":"Justin Orrick","week":10,"opponent_score":62,"score":54,"home":true},{"opponent_owner":"Andrew Mollica","owner":"Anthony Swichtenberg","week":10,"opponent_score":46,"score":108,"home":true},{"opponent_owner":"Joey Glocke","owner":"Corey Larson","week":11,"opponent_score":78,"score":89,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Nicholas Patri","week":11,"opponent_score":99,"score":78,"home":true},{"opponent_owner":"Spencer Morris","owner":"Blake Martin","week":11,"opponent_score":81,"score":23,"home":true},{"opponent_owner":"Justin Orrick","owner":"Joel Schmocker","week":11,"opponent_score":84,"score":85,"home":true},{"opponent_owner":"Anthony Swichtenberg","owner":"Nate Miller","week":11,"opponent_score":99,"score":53,"home":true},{"opponent_owner":"Andrew Mollica","owner":"Zach Droeszler","week":11,"opponent_score":93,"score":46,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Joey Glocke","week":12,"opponent_score":133,"score":91,"home":true},{"opponent_owner":"Corey Larson","owner":"Spencer Morris","week":12,"opponent_score":103,"score":50,"home":true},{"opponent_owner":"Nicholas Patri","owner":"Justin Orrick","week":12,"opponent_score":104,"score":98,"home":true},{"opponent_owner":"Blake Martin","owner":"Anthony Swichtenberg","week":12,"opponent_score":126,"score":53,"home":true},{"opponent_owner":"Joel Schmocker","owner":"Andrew Mollica","week":12,"opponent_score":100,"score":81,"home":true},{"opponent_owner":"Nate Miller","owner":"Zach Droeszler","week":12,"opponent_score":81,"score":92,"home":true},{"opponent_owner":"Joey Glocke","owner":"Spencer Morris","week":13,"opponent_score":94,"score":110,"home":true},{"opponent_owner":"Justin Orrick","owner":"Andy Stoughton","week":13,"opponent_score":94,"score":87,"home":true},{"opponent_owner":"Anthony Swichtenberg","owner":"Corey Larson","week":13,"opponent_score":76,"score":78,"home":true},{"opponent_owner":"Andrew Mollica","owner":"Nicholas Patri","week":13,"opponent_score":137,"score":124,"home":true},{"opponent_owner":"Zach Droeszler","owner":"Blake Martin","week":13,"opponent_score":82,"score":60,"home":true},{"opponent_owner":"Nate Miller","owner":"Joel Schmocker","week":13,"opponent_score":106,"score":107,"home":true},{"opponent_owner":"Andy Stoughton","owner":"Joey Glocke","week":1,"opponent_score":94,"score":49,"home":false},{"opponent_owner":"Corey Larson","owner":"Spencer Morris","week":1,"opponent_score":89,"score":87,"home":false},{"opponent_owner":"Nicholas Patri","owner":"Justin Orrick","week":1,"opponent_score":104,"score":100,"home":false},{"opponent_owner":"Blake Martin","owner":"Anthony Swichtenberg","week":1,"opponent_score":101,"score":83,"home":false},{"opponent_owner":"Joel Schmocker","owner":"Andrew Mollica","week":1,"opponent_score":84,"score":106,"home":false},{"opponent_owner":"Nate Miller","owner":"Zach Droeszler","week":1,"opponent_score":67,"score":100,"home":false},{"opponent_owner":"Joey Glocke","owner":"Spencer Morris","week":2,"opponent_score":65,"score":124,"home":false},{"opponent_owner":"Justin Orrick","owner":"Andy Stoughton","week":2,"opponent_score":104,"score":57,"home":false},{"opponent_owner":"Anthony Swichtenberg","owner":"Corey Larson","week":2,"opponent_score":80,"score":41,"home":false},{"opponent_owner":"Andrew Mollica","owner":"Nicholas Patri","week":2,"opponent_score":100,"score":86,"home":false},{"opponent_owner":"Zach Droeszler","owner":"Blake Martin","week":2,"opponent_score":95,"score":56,"home":false},{"opponent_owner":"Nate Miller","owner":"Joel Schmocker","week":2,"opponent_score":118,"score":73,"home":false},{"opponent_owner":"Justin Orrick","owner":"Joey Glocke","week":3,"opponent_score":89,"score":99,"home":false},{"opponent_owner":"Spencer Morris","owner":"Anthony Swichtenberg","week":3,"opponent_score":82,"score":132,"home":false},{"opponent_owner":"Andy Stoughton","owner":"Andrew Mollica","week":3,"opponent_score":87,"score":86,"home":false},{"opponent_owner":"Corey Larson","owner":"Zach Droeszler","week":3,"opponent_score":87,"score":106,"home":false},{"opponent_owner":"Nicholas Patri","owner":"Nate Miller","week":3,"opponent_score":91,"score":96,"home":false},{"opponent_owner":"Blake Martin","owner":"Joel Schmocker","week":3,"opponent_score":107,"score":82,"home":false},{"opponent_owner":"Joey Glocke","owner":"Anthony Swichtenberg","week":4,"opponent_score":70,"score":79,"home":false},{"opponent_owner":"Andrew Mollica","owner":"Justin Orrick","week":4,"opponent_score":128,"score":56,"home":false},{"opponent_owner":"Zach Droeszler","owner":"Spencer Morris","week":4,"opponent_score":67,"score":80,"home":false},{"opponent_owner":"Nate Miller","owner":"Andy Stoughton","week":4,"opponent_score":61,"score":85,"home":false},{"opponent_owner":"Joel Schmocker","owner":"Corey Larson","week":4,"opponent_score":88,"score":47,"home":false},{"opponent_owner":"Blake Martin","owner":"Nicholas Patri","week":4,"opponent_score":104,"score":74,"home":false},{"opponent_owner":"Andrew Mollica","owner":"Joey Glocke","week":5,"opponent_score":82,"score":70,"home":false},{"opponent_owner":"Anthony Swichtenberg","owner":"Zach Droeszler","week":5,"opponent_score":95,"score":91,"home":false},{"opponent_owner":"Justin Orrick","owner":"Nate Miller","week":5,"opponent_score":64,"score":84,"home":false},{"opponent_owner":"Spencer Morris","owner":"Joel Schmocker","week":5,"opponent_score":127,"score":95,"home":false},{"opponent_owner":"Andy Stoughton","owner":"Blake Martin","week":5,"opponent_score":109,"score":68,"home":false},{"opponent_owner":"Corey Larson","owner":"Nicholas Patri","week":5,"opponent_score":88,"score":81,"home":false},{"opponent_owner":"Joey Glocke","owner":"Zach Droeszler","week":6,"opponent_score":83,"score":82,"home":false},{"opponent_owner":"Nate Miller","owner":"Andrew Mollica","week":6,"opponent_score":120,"score":119,"home":false},{"opponent_owner":"Joel Schmocker","owner":"Anthony Swichtenberg","week":6,"opponent_score":111,"score":60,"home":false},{"opponent_owner":"Blake Martin","owner":"Justin Orrick","week":6,"opponent_score":100,"score":61,"home":false},{"opponent_owner":"Nicholas Patri","owner":"Spencer Morris","week":6,"opponent_score":88,"score":122,"home":false},{"opponent_owner":"Corey Larson","owner":"Andy Stoughton","week":6,"opponent_score":61,"score":58,"home":false},{"opponent_owner":"Nate Miller","owner":"Joey Glocke","week":7,"opponent_score":120,"score":100,"home":false},{"opponent_owner":"Zach Droeszler","owner":"Joel Schmocker","week":7,"opponent_score":96,"score":93,"home":false},{"opponent_owner":"Andrew Mollica","owner":"Blake Martin","week":7,"opponent_score":102,"score":84,"home":false},{"opponent_owner":"Anthony Swichtenberg","owner":"Nicholas Patri","week":7,"opponent_score":107,"score":89,"home":false},{"opponent_owner":"Justin Orrick","owner":"Corey Larson","week":7,"opponent_score":86,"score":63,"home":false},{"opponent_owner":"Spencer Morris","owner":"Andy Stoughton","week":7,"opponent_score":44,"score":91,"home":false},{"opponent_owner":"Joey Glocke","owner":"Joel Schmocker","week":8,"opponent_score":83,"score":84,"home":false},{"opponent_owner":"Blake Martin","owner":"Nate Miller","week":8,"opponent_score":94,"score":79,"home":false},{"opponent_owner":"Nicholas Patri","owner":"Zach Droeszler","week":8,"opponent_score":61,"score":95,"home":false},{"opponent_owner":"Corey Larson","owner":"Andrew Mollica","week":8,"opponent_score":77,"score":88,"home":false},{"opponent_owner":"Andy Stoughton","owner":"Anthony Swichtenberg","week":8,"opponent_score":87,"score":125,"home":false},{"opponent_owner":"Spencer Morris","owner":"Justin Orrick","week":8,"opponent_score":68,"score":88,"home":false},{"opponent_owner":"Blake Martin","owner":"Joey Glocke","week":9,"opponent_score":87,"score":124,"home":false},{"opponent_owner":"Joel Schmocker","owner":"Nicholas Patri","week":9,"opponent_score":72,"score":116,"home":false},{"opponent_owner":"Nate Miller","owner":"Corey Larson","week":9,"opponent_score":132,"score":91,"home":false},{"opponent_owner":"Zach Droeszler","owner":"Andy Stoughton","week":9,"opponent_score":103,"score":51,"home":false},{"opponent_owner":"Andrew Mollica","owner":"Spencer Morris","week":9,"opponent_score":90,"score":122,"home":false},{"opponent_owner":"Anthony Swichtenberg","owner":"Justin Orrick","week":9,"opponent_score":68,"score":91,"home":false},{"opponent_owner":"Joey Glocke","owner":"Nicholas Patri","week":10,"opponent_score":111,"score":131,"home":false},{"opponent_owner":"Corey Larson","owner":"Blake Martin","week":10,"opponent_score":82,"score":87,"home":false},{"opponent_owner":"Andy Stoughton","owner":"Joel Schmocker","week":10,"opponent_score":62,"score":73,"home":false},{"opponent_owner":"Spencer Morris","owner":"Nate Miller","week":10,"opponent_score":81,"score":81,"home":false},{"opponent_owner":"Justin Orrick","owner":"Zach Droeszler","week":10,"opponent_score":54,"score":62,"home":false},{"opponent_owner":"Anthony Swichtenberg","owner":"Andrew Mollica","week":10,"opponent_score":108,"score":46,"home":false},{"opponent_owner":"Corey Larson","owner":"Joey Glocke","week":11,"opponent_score":89,"score":78,"home":false},{"opponent_owner":"Nicholas Patri","owner":"Andy Stoughton","week":11,"opponent_score":78,"score":99,"home":false},{"opponent_owner":"Blake Martin","owner":"Spencer Morris","week":11,"opponent_score":23,"score":81,"home":false},{"opponent_owner":"Joel Schmocker","owner":"Justin Orrick","week":11,"opponent_score":85,"score":84,"home":false},{"opponent_owner":"Nate Miller","owner":"Anthony Swichtenberg","week":11,"opponent_score":53,"score":99,"home":false},{"opponent_owner":"Zach Droeszler","owner":"Andrew Mollica","week":11,"opponent_score":46,"score":93,"home":false},{"opponent_owner":"Joey Glocke","owner":"Andy Stoughton","week":12,"opponent_score":91,"score":133,"home":false},{"opponent_owner":"Spencer Morris","owner":"Corey Larson","week":12,"opponent_score":50,"score":103,"home":false},{"opponent_owner":"Justin Orrick","owner":"Nicholas Patri","week":12,"opponent_score":98,"score":104,"home":false},{"opponent_owner":"Anthony Swichtenberg","owner":"Blake Martin","week":12,"opponent_score":53,"score":126,"home":false},{"opponent_owner":"Andrew Mollica","owner":"Joel Schmocker","week":12,"opponent_score":81,"score":100,"home":false},{"opponent_owner":"Zach Droeszler","owner":"Nate Miller","week":12,"opponent_score":92,"score":81,"home":false},{"opponent_owner":"Spencer Morris","owner":"Joey Glocke","week":13,"opponent_score":110,"score":94,"home":false},{"opponent_owner":"Andy Stoughton","owner":"Justin Orrick","week":13,"opponent_score":87,"score":94,"home":false},{"opponent_owner":"Corey Larson","owner":"Anthony Swichtenberg","week":13,"opponent_score":78,"score":76,"home":false},{"opponent_owner":"Nicholas Patri","owner":"Andrew Mollica","week":13,"opponent_score":124,"score":137,"home":false},{"opponent_owner":"Blake Martin","owner":"Zach Droeszler","week":13,"opponent_score":60,"score":82,"home":false},{"opponent_owner":"Joel Schmocker","owner":"Nate Miller","week":13,"opponent_score":107,"score":106,"home":false}]
rm(list = ls())
library(rvest)
library(dplyr)
library(tidyr)
library(stringr)
library(jsonlite)
table <- read_html("http://games.espn.go.com/ffl/schedule?leagueId=275298") %>%
html_nodes("table.tableBody") %>%
.[[1]] %>%
html_table()
names(table) <- c("away_team", "away_owner", "at", "home_team", "home_owner", "result")
# Add "week" variable for each game
week <- 0
for (i in 1:nrow(table)) {
away_team <- table[i,"away_team"]
if (str_detect(away_team, "WEEK ")) { week <- week + 1 }
table[i,"week"] <- week
}
table <- table %>%
filter(!is.na(away_owner), away_team != "AWAY TEAM", result != "Preview") %>%
select(-at) %>%
mutate(away_score = (result %>% str_split("-") %>%
sapply(function(d) return(d[1]) %>% as.numeric)),
home_score = (result %>% str_split("-") %>%
sapply(function(d) return(d[2]) %>% as.numeric))) %>%
select(-result)
table_home <- table %>%
rename(team = home_team, owner = home_owner, score = home_score,
opponent_team = away_team, opponent_owner = away_owner, opponent_score = away_score) %>%
mutate(home = TRUE)
table_away <- table %>%
rename(team = away_team, owner = away_owner, score = away_score,
opponent_team = home_team, opponent_owner = home_owner, opponent_score = home_score) %>%
mutate(home = FALSE)
removeRecord <- function(team_name) {
i <- team_name %>% str_locate("\\(") %>% .[,"start"]
return(team_name %>%
str_sub(start = 1, end = i-2))
}
table <- rbind(table_home, table_away) %>%
mutate(team = removeRecord(team),
opponent_team = removeRecord(opponent_team)) %>%
select(-team, -opponent_team) # drop these; they're not used
write(toJSON(table), "data.json")
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>The Justice League</title>
<style>
body {
font-family: sans-serif;
width: 960px;
margin: auto;
}
.player div {
display: inline;
float: left;
height: 320px;
}
h3 {
margin-top: 1.5em;
margin-bottom: 0em;
}
p {
text-align: center;
margin-top: 2.8em;
margin-bottom: 0;
font-size: 12px;
}
text {
font-size: 11px;
}
.mean {
stroke: white;
stroke-width: 1px;
}
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis path {
display: none;
}
.x.axis {
display: none;
}
.margin .y.axis {
display: none;
}
.highlight {
fill: orange !important;
}
</style>
</head>
<body>
<h1>The Justice League</h1>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="charts.js"></script>
<script>
var margin = {top: 10, left: 50, bottom: 30, right: 30},
width = (960/3) - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
var scale = {
x: d3.scale.linear().range([0, width]),
y: d3.scale.ordinal().rangeBands([height, 0], .1),
color: d3.scale.linear().range(["brown", "lightgrey", "steelblue"])
.interpolate(d3.interpolateLab)
};
var svg = d3.select("body")
d3.json("data.json", ready);
function ready(error, data) {
if (error) throw error;
data = data
.sort(function(a,b) { return b.week - a.week; })
.map(function(d) {
d.margin = d.score - d.opponent_score;
d.name = d.owner.split(" ")[0];
d.opponent_name = d.opponent_owner.split(" ")[0];
return d;
});
var nest = d3.nest()
.key(function(d) { return d.name; })
.entries(data);
scale.x.domain([0, d3.max(data, function(d) { return d.score; })]);
scale.y.domain(data.map(function(d) { return d.week; }));
scale.color.domain([0, 0, d3.max(data, function(d) { return d.score; })]);
d3.select("body").selectAll(".player").data(nest)
.enter().append("div")
.attr("class", "player")
.each(function(player) {
var div = d3.select(this);
div.append("div").attr("class", "own").append("h3").text(player.key);
div.append("div").attr("class", "opponent").append("p").text("Opponent");
div.append("div").attr("class", "margin").append("p").text("Point Margin");
scale.y.domain(data.map(function(d) { return d.week; }));
scale.x.domain([0, d3.max(data, function(d) { return d.score; })]);
scale.color.domain([0, 0, d3.max(data, function(d) { return d.score; })]);
// own chart
div.select("div.own").call(appendSvg, "own").select(".own g")
.call(draw.own, player.values, scale);
// opponent chart
div.select("div.opponent").call(appendSvg, "opponent").select(".opponent g")
.call(draw.opponent, player.values, scale);
scale.x.domain(d3.extent(data, function(d) { return d.margin; }));
scale.color.domain([
d3.min(data, function(d) { return d.margin; }), 0,
d3.max(data, function(d) { return d.margin; })
]);
// point margin chart
div.select("div.margin").call(appendSvg, "margin").select(".margin g")
.call(draw.margin, player.values, scale, height);
});
d3.selectAll(".bar")
.on("mouseenter", function(hovered) {
d3.selectAll(".bar")
.classed("highlight", function(d) { return d.week == hovered.week; })
})
.on("mouseleave", function(hovered) {
d3.selectAll(".bar")
.classed("highlight", false);
});
}
function appendSvg(selection, c) {
return selection.append("svg")
.attr("class", c)
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
}
d3.select(self.frameElement)
.style("height", 3920 + "px");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment