function draw_tourney(links, opts = {}) {
let {
games,
sep = function (a, b) {
return a.parent == b.parent ? 1 : 2;
},
w = 800,
h = 0.625 * w,
margin = 60,
game_height = 30,
team_width = 120
} = opts;
let div = d3
.create("div")
.style("width", `${w}px`)
.attr("height", `${h}px`)
.style("position", "relative");
let svg = div
.append("svg")
.attr("width", w)
.attr("height", h)
.style("overflow", "visible");
let g = svg.append("g");
let scale = d3
.scaleLinear()
.domain([0, w])
.range([w - margin, margin]);
let root = d3.hierarchy(d3.stratify()(links));
d3.tree().separation(sep).size([h, w])(root);
let link_display = g.append("g").attr("id", "links");
link_display
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", function (o) {
let x1 = scale(o.source.y);
let y1 = o.source.x;
let x2 = scale(o.target.y);
let y2 = o.target.x;
let line = d3.line()([
[x1, y1],
[x1, y2],
[x2, y2]
]);
return line;
})
.attr("fill", "none")
.attr("stroke", "#222")
.attr("stroke-width", 1.5);
let game_containers = div
.selectAll("div.game")
.data(root.descendants())
.enter()
.append(function (d) {
d.left = scale(d.y) - team_width / 2 + "px";
d.top = d.x - game_height / 2 + "px";
return game_container(d, {
game_height,
team_width,
game_data: games ? games.get(d.data.data.id) : null
});
});
return div.node();
function get_linked_team(link) {
let teams = [];
let source_links = link.source.data;
let source_data = games.get(link.source.data.data.id);
let target_data = games.get(link.target.data.data.id);
let link_team;
if (source_data && target_data) {
let source_teams = [];
if (source_data.top_team) {
source_teams.push(source_data.top_team);
}
if (source_data.bot_team) {
source_teams.push(source_data.bot_team);
}
let target_teams = [];
if (target_data.top_team) {
target_teams.push(target_data.top_team);
}
if (target_data.bot_team) {
target_teams.push(target_data.bot_team);
}
link_team = _.intersection(source_teams, target_teams);
}
if (link_team && link_team.length == 1) {
return link_team[0].replace(/ /g, "_");
}
}
}