Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handling gaps #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 102 additions & 11 deletions epl_trends/process_json_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function load_data() {
"10-11","11-12","12-13","13-14","14-15"];
//"https://raw.githubusercontent.com/jokecamp/FootballData/master/EPL%201992%20-%202015/tables/epl-92-93.json"
var baseurl = "https://raw.githubusercontent.com/jokecamp/FootballData/master/EPL%201992%20-%202015/tables/epl";
//console.log(seasons);
var q = d3.queue();
seasons.forEach(function(s) {
url = baseurl + "-" + s + ".json";
Expand Down Expand Up @@ -106,14 +105,28 @@ function setupPlot(plot, data, yvar) {

// setup line function
var buildLine = d3.line()//.curve(d3.curveCardinal)
.x(function(d) { return xScale(d.startyear); })
.x(function(d) { return xScale(d.startyear); })
.y(function(d) { return yScale(d[yvar]); });

// organize data by team
var databyteam = d3.nest()
.key(function(d) { return d.team; })
.entries(data);

.entries(data);
var sequentialdata = databyteam.map(function(d) {
var filteredvalues = d.values.filter(isNotGap);
return({ // need to return new object b/c d.values = d.values.filter() overwrites databyteam
key: d.key,
values: filteredvalues,
});
});
var gapdata = databyteam.map(function(d) {
var filteredvalues = d.values.filter(isGap);
return({ // need to return new object b/c d.values = d.values.filter() overwrites databyteam
key: d.key,
values: filteredvalues,
});
});

// add points
var pts_g = plot.append("g");
pts_g.selectAll(".datapt")
Expand All @@ -126,10 +139,22 @@ function setupPlot(plot, data, yvar) {
.attr("cy", function(d) { return yScale(d[yvar]); })
.style("fill", function(d) { return colorScale(d.team); });

// add lines
// add line styled for gaps
var gaps_g = plot.append("g");
gaps_g.selectAll(".teamlinegap")
.data(gapdata)
.enter()
.append("path")
.attr("class", "teamlinegap")
.attr("d", function(d) { return buildLine(d.values); })
.style("stroke", function(d) { return colorScale(d.key); })
.style("stroke-dasharray", "3,3")
.style("fill", "none");

// add lines overtop of gaps
var lines_g = plot.append("g");
lines_g.selectAll(".teamline")
.data(databyteam)
.data(sequentialdata)
.enter()
.append("path")
.attr("class", "teamline")
Expand All @@ -151,15 +176,13 @@ function setupPlot(plot, data, yvar) {
// update phase????
hoverlines_g.selectAll(".teamlineinvisible")
.on("mouseover", function(d) {
console.log(d);
//var this_color = d3.select(this).style("stroke");
//d3.selectAll(".teamline").style("stroke", "grey");
//d3.select(this).style("stroke", this_color);
d3.select(this).style('stroke-width', "6px")
.style("stroke", function(d) { return colorScale(d.key); }); // determine location of mouse
var x_val = d3.event.pageX; //xScale(d.startyear); //
var y_val = d3.event.pageY; //yScale(d[yvar]); //
console.log(x_val, y_val);
// add text element
d3.select("#tooltip")
.style("display", "block")
Expand All @@ -180,7 +203,6 @@ function setupPlot(plot, data, yvar) {
.style("pointer-events", "none");
})

//console.log(databyteam);
return plot;
}

Expand Down Expand Up @@ -235,7 +257,21 @@ function updatePlot(plot, data, yvar) {
// organize data by team
var databyteam = d3.nest()
.key(function(d) { return d.team; })
.entries(data);
.entries(data);
var sequentialdata = databyteam.map(function(d) {
var filteredvalues = d.values.filter(isNotGap);
return({ // need to return new object b/c d.values = d.values.filter() overwrites databyteam
key: d.key,
values: filteredvalues,
});
});
var gapdata = databyteam.map(function(d) {
var filteredvalues = d.values.filter(isGap);
return({ // need to return new object b/c d.values = d.values.filter() overwrites databyteam
key: d.key,
values: filteredvalues,
});
});

plot.selectAll(".datapt")
.data(data)
Expand All @@ -247,8 +283,19 @@ function updatePlot(plot, data, yvar) {
.attr("cy", function(d) { return yScale(d[yvar]); })
.style("fill", function(d) { return colorScale(d.team); });

// add line styled for gaps
plot.selectAll(".teamlinegap")
.data(gapdata)
.transition()
.duration(duration_time)
.delay(function(d, i) { return i * 10; })
.attr("d", function(d) { return buildLine(d.values); })
.style("stroke", function(d) { return colorScale(d.key); })
.style("stroke-dasharray", "3,3")
.style("fill", "none");

plot.selectAll(".teamline")
.data(databyteam)
.data(sequentialdata)
.transition()
.duration(duration_time)
.delay(function(d, i) { return i * 10; })
Expand All @@ -266,7 +313,51 @@ function updatePlot(plot, data, yvar) {

plot.selectAll(".datapt").exit().remove();
plot.selectAll(".teamline").exit().remove();
plot.selectAll(".teamlinegap").exit().remove();
plot.selectAll(".teamlineinvisible").exit().remove();

return plot;
}

function isGap(d, i, j){
if(j.length == 1){ // skip everything if theres' only one value (can't have gaps!)
return false;
} else if(i == 0){ // first value in array
var future = j[i+1];
var diff_after = future.startyear - d.startyear;
return diff_after != 1;
} else if(i < (j.length-1)){ //need to use j.length-1 since arrays are 0-indexed
var prev = j[i-1];
var future = j[i+1];
var diff_before = d.startyear - prev.startyear,
diff_after = future.startyear - d.startyear;
return diff_before != 1 || diff_after != 1;
} else if (i == (j.length-1)){ // last value in array
var prev = j[i-1];
var diff_before = d.startyear - prev.startyear;
return diff_before != 1;
}
}

function isNotGap(d, i, j){
//can't just do !isGap because we need to include the outer points, not exclude them
// e.g if data is [1998, 1999, 2007], isGap will filter to [1999, 2007]
// but !isGap will only return [1998], but we need [1998, 1999] to create the line.
if(j.length == 1){ // skip everything if theres' only one value (can't have gaps!)
return true;
} else if(i == 0){ // first value in array
var future = j[i+1];
var diff_after = future.startyear - d.startyear;
return diff_after == 1;
} else if(i < (j.length-1)){ //need to use j.length-1 since arrays are 0-indexed
var prev = j[i-1];
var future = j[i+1];
var diff_before = d.startyear - prev.startyear,
diff_after = future.startyear - d.startyear;
return diff_before == 1 || diff_after == 1;
} else if (i == (j.length-1)){ // last value in array
var prev = j[i-1];
var diff_before = d.startyear - prev.startyear;
return diff_before == 1;
}
}