function visualise(data, matches_dateExtent) {
let container = html`<div id="viz_container" style="width: 700px">`;
var gap_between_lines = 35,
branch_steepness = 15,
shunt_to_right = 40,
bar_colour = `rgba(${bar_colour_val}, 1)`,
bar_strokeWidth = 16,
text_strokeWidth = 3,
demo_margin = { top: 15, bottom: 30, left: 60, right: 0 },
max_width = 625,
viz_width =
width - demo_margin.left - demo_margin.right > max_width
? max_width
: width - demo_margin.left - demo_margin.right,
waterfall_stroke_width = 3;
const burney_link_colour = "rgba(41, 50, 65,0.5)",
bna_link_colour = burney_link_colour;
let data_length = 0;
data.forEach(function (d, i) {
if (d.connected_titles) {
data_length = data_length + d.connected_titles.length;
} else {
data_length++;
}
});
let demo_height =
data_length * gap_between_lines + demo_margin.top + demo_margin.bottom;
var x = d3
.scaleLinear()
.domain(matches_dateExtent)
.range([0, viz_width - 15]);
var y = d3.scaleLinear().domain([0, 200]).range([demo_height, 0]);
var x_axis = d3
.select(container)
.append("div")
.attr("class", "x_axis_wrapper")
.append("svg")
.attr("width", viz_width + demo_margin.left + demo_margin.right)
.attr("height", 30)
.append("g")
.attr(
"transform",
"translate(" + demo_margin.left + "," + demo_margin.top * 2 + ")"
);
x_axis
.append("g")
.attr("id", "axis_g")
.call(d3.axisTop(x).tickFormat(d3.format("d")));
x_axis.select("#axis_g").select("path").remove();
var svg = d3
.select(container)
.append("svg")
.attr("width", viz_width + 30)
.attr("height", demo_height);
svg.node().appendChild(gradient_defs);
var div = d3
.select(container)
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function make_x_gridlines() {
return d3.axisBottom(x);
}
svg
.append("g")
.attr("class", "grid")
.attr(
"transform",
"translate(" + demo_margin.left + "," + demo_height + ")"
)
.call(make_x_gridlines().tickSize(-demo_height).tickFormat(""));
var svg_graphs = svg
.append("g")
.attr("id", "graph_group")
.attr(
"transform",
"translate(" + demo_margin.left + "," + demo_margin.top + ")"
);
var nested_count = 0;
data.forEach(function (d, i) {
if ("connected_titles" in d) {
nested_count = nested_count + d["connected_titles"].length - 1;
d["connected_titles"].forEach(function (nested_d, nested_i) {
var horizontal_offset;
if (d["connected_titles"].length > 15) {
horizontal_offset = shunt_to_right + nested_i * 5;
} else {
horizontal_offset = shunt_to_right + nested_i * branch_steepness;
}
var vertical_offset =
(i + nested_i + nested_count - d["connected_titles"].length + 1) *
gap_between_lines;
var title_wrapper = svg_graphs
.append("g")
.attr("class", "title_wrapper")
.attr("transform", "translate(0," + vertical_offset + ")");
if (
!(nested_d.first_date_held == "" && nested_d.last_date_held == "")
) {
title_wrapper
.append("line")
.attr("class", "line_run")
.attr("x1", () =>
nested_d.first_date_held == ""
? x(+nested_d.last_date_held.substring(0, 4) - 1)
: nested_d.first_date_held == nested_d.last_date_held
? x(+nested_d.first_date_held.substring(0, 4) - 1)
: x(+nested_d.first_date_held.substring(0, 4))
)
.attr("x2", () =>
nested_d.last_date_held == "" || nested_d.last_date_held == null
? x(+nested_d.date_sort_last + 1)
: nested_d.last_date_held == "Continuing"
? x(new Date().getFullYear())
: x(+nested_d.last_date_held.substring(0, 4))
)
.attr("y1", gap_between_lines + -bar_strokeWidth / 2 + 6)
.attr("y2", gap_between_lines + -bar_strokeWidth / 2 + 6)
.attr("stroke", bar_colour)
.attr("stroke-width", bar_strokeWidth);
if (nested_i != d["connected_titles"].length - 1) {
title_wrapper
.append("line")
.attr("class", "line_run_drop")
.attr("x1", () =>
nested_d.last_date_held == ""
? x(+nested_d.date_sort_last) - waterfall_stroke_width / 2
: nested_d.last_date_held == "Continuing"
? x(new Date().getFullYear()) - waterfall_stroke_width / 2
: x(+nested_d.last_date_held) - waterfall_stroke_width / 2
)
.attr("x2", () =>
nested_d.last_date_held == ""
? x(+nested_d.date_sort_last) - waterfall_stroke_width / 2
: nested_d.last_date_held == "Continuing"
? x(new Date().getFullYear()) - waterfall_stroke_width / 2
: x(+nested_d.last_date_held) - waterfall_stroke_width / 2
)
.attr("y1", gap_between_lines + 6)
.attr("y2", gap_between_lines * 2 + 6)
.attr("stroke", "url(#e)")
.attr("stroke-width", waterfall_stroke_width);
}
}
if (nested_i != 0) {
let x1, x2;
if (d["connected_titles"].length > 15) {
x1 = shunt_to_right + 5 * (nested_i - 1);
x2 = shunt_to_right + 5 * nested_i;
} else {
x1 = shunt_to_right + branch_steepness * (nested_i - 1);
x2 = shunt_to_right + branch_steepness * nested_i;
}
title_wrapper
.append("line")
.attr("class", "connected_line")
.attr("x1", x1)
.attr("x2", x2)
.attr("y1", 0 + demo_margin.top - 10)
.attr("y2", gap_between_lines + demo_margin.top - 10);
}
var title_nested_offset_wrapper = title_wrapper
.append("g")
.attr("class", "title_nested_offset_wrapper")
.attr("transform", "translate(" + horizontal_offset + ",0)");
var title_text1 = title_nested_offset_wrapper
.append("text")
.attr("class", "title_name_wrapper")
.attr("transform", "translate(8,52)");
var title_text2 = title_nested_offset_wrapper
.append("text")
.attr("class", "title_name_wrapper")
.attr("transform", "translate(8,52)");
title_text1
.append("tspan")
.attr("class", "title_name")
.text(" " + nested_d["publication_title"])
.attr("stroke", "white")
.attr("opacity", 0.93)
.attr("stroke-width", text_strokeWidth);
let titleName_span = title_text2
.append("a")
.attr("href", nested_d["explore_link"])
.attr("target", "_blank")
.attr("cursor", "pointer")
.append("tspan")
.attr("class", "title_name")
.text(" " + nested_d["publication_title"])
.attr("fill", "black");
if (
nested_d["preceding_titles"] != null &&
nested_d["preceding_titles"] != ""
) {
title_text2
.append("tspan")
.text(" <")
.attr("id", "preceding")
.attr("class", "arrow")
.style("cursor", "default")
.on("mouseover", function () {
div.transition().duration(200).style("opacity", 0.98);
div
.html(nested_d["preceding_titles"])
.style("background", "rgb(219, 232, 237)")
.style("left", d3.event.pageX + 10 + "px")
.style("top", d3.event.pageY - 28 - window.scrollY + "px");
})
.on("mouseout", function () {
div.transition().duration(500).style("opacity", 0);
});
}
if (
nested_d["succeeding_titles"] != null &&
nested_d["succeeding_titles"] != ""
) {
title_text2
.append("tspan")
.text(" >")
.attr("id", "succeeding")
.attr("class", "arrow")
.style("cursor", "default")
.on("mouseover", function () {
div.transition().duration(200).style("opacity", 0.98);
div
.html(nested_d["succeeding_titles"])
.style("background", "rgb(219, 232, 237)")
.style("left", d3.event.pageX + 10 + "px")
.style("top", d3.event.pageY - 28 - window.scrollY + "px");
})
.on("mouseout", function () {
div.transition().duration(500).style("opacity", 0);
});
}
if (nested_d.online_status == "BURNEY") {
let burney_link = title_nested_offset_wrapper
.append("a")
.attr("class", "burney_a")
.attr(
"href",
"https://www.bl.uk/collection-guides/burney-collection"
)
.attr("target", "_blank")
.attr("opacity", 0.5)
.style(
"transform",
"translate(" + (-60 - horizontal_offset) + "px,22px)"
);
burney_link
.append("path")
.attr("d", "M -10 20 H 60 V 22 Q 50 20 45 36 H -10 V 20")
.style("transform", "translateY(-3px)")
.attr("class", "burney_link_rect")
.attr("x", 0)
.attr("y", gap_between_lines - 18)
.attr("width", 60)
.attr("height", 16)
.attr("fill", burney_link_colour)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 1);
});
burney_link
.append("text")
.attr("class", "burney_link")
.style("font-weight", 800)
.text("Burney")
.attr("fill", "white")
.attr("x", 1.5)
.attr("y", gap_between_lines - 5.8)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this.parentNode)
.select(".burney_link_rect")
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this.parentNode)
.select(".burney_link_rect")
.transition()
.duration(80)
.style("opacity", 1);
});
}
if (nested_d.link_to_british_newspaper_archive != "") {
let bna_link = title_nested_offset_wrapper
.append("a")
.attr("class", "bna_a")
.attr("href", nested_d.link_to_british_newspaper_archive)
.attr("target", "_blank")
.attr("opacity", 0.5)
.style(
"transform",
"translate(" + (-50 - horizontal_offset) + "px,22px)"
);
bna_link
.append("path")
.attr("class", "bna_link_rect")
.attr("d", "M -10 20 H 60 V 22 Q 50 20 45 36 H -10 V 20")
.style("transform", "translate(-9px, -3px)")
.attr("fill", bna_link_colour)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 1);
});
bna_link
.append("text")
.attr("class", "bna_link")
.style("font-weight", 800)
.text("BNA")
.attr("fill", "white")
.attr("x", 0)
.attr("y", gap_between_lines - 5.8)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this.parentNode)
.select(".bna_link_rect")
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this.parentNode)
.select(".bna_link_rect")
.transition()
.duration(80)
.style("opacity", 1);
});
}
if (nested_i != 0) {
let position_dividing_baselines;
if (d["connected_titles"].length > 15) {
position_dividing_baselines =
nested_d.last_date_held == ""
? x(+nested_d.date_sort_last) - shunt_to_right - 5 * nested_i
: nested_d.last_date_held == "Continuing"
? x(new Date().getFullYear()) - shunt_to_right - 5 * nested_i
: x(+nested_d.last_date_held) - shunt_to_right - 5 * nested_i;
} else {
position_dividing_baselines =
nested_d.last_date_held == ""
? x(+nested_d.date_sort_last) -
shunt_to_right -
branch_steepness * nested_i
: nested_d.last_date_held == "Continuing"
? x(new Date().getFullYear()) -
shunt_to_right -
branch_steepness * nested_i
: x(+nested_d.last_date_held.substring(0, 4)) -
shunt_to_right -
branch_steepness * nested_i;
}
title_nested_offset_wrapper
.append("line")
.attr("class", "connected_line")
.attr("x1", 0)
.attr("x2", position_dividing_baselines)
.attr("y1", 0)
.attr("y2", 0)
.attr("transform", "translate(0,40)");
title_nested_offset_wrapper
.append("line")
.attr("class", "connected_line2")
.attr("x1", 0)
.attr("x2", 100)
.attr("y1", 0)
.attr("y2", 0)
.attr(
"transform",
"translate(" + position_dividing_baselines + ",40)"
);
} else {
let position_dividing_baselines =
nested_d.last_date_held == ""
? x(+nested_d.date_sort_last) - shunt_to_right
: nested_d.last_date_held == "Continuing"
? x(new Date().getFullYear()) - shunt_to_right
: x(+nested_d.last_date_held) - shunt_to_right;
title_nested_offset_wrapper
.append("line")
.attr("class", "connected_line")
.attr("x1", -shunt_to_right)
.attr("x2", position_dividing_baselines)
.attr("y1", 0)
.attr("y2", 0)
.attr("transform", "translate(0,40)");
title_nested_offset_wrapper
.append("line")
.attr("class", "connected_line2")
.attr("x1", 0)
.attr("x2", 100)
.attr("y1", 0)
.attr("y2", 0)
.attr(
"transform",
"translate(" + position_dividing_baselines + ",40)"
);
}
});
} else {
var title_wrapper = svg_graphs
.append("g")
.attr("class", "title_wrapper")
.attr(
"transform",
"translate(0," + (i + nested_count) * gap_between_lines + ")"
);
if (!(d.first_date_held == "" && d.last_date_held == "")) {
title_wrapper
.append("line")
.attr("class", "line_run")
.attr("x1", () =>
d.first_date_held == ""
? x(+d.last_date_held - 1)
: d.first_date_held == d.last_date_held
? x(+d.first_date_held - 1)
: x(+d.first_date_held.substring(0, 4))
)
.attr("x2", () =>
+d.last_date_held == 0 || d.last_date_held == "Continuing"
? x(new Date().getFullYear())
: x(+d.last_date_held.substring(0, 4))
)
.attr("y1", gap_between_lines - bar_strokeWidth / 2 + 6)
.attr("y2", gap_between_lines - bar_strokeWidth / 2 + 6)
.attr("stroke", bar_colour)
.attr("stroke-width", bar_strokeWidth);
}
var title_text1 = title_wrapper
.append("text")
.attr("class", "title_name_wrapper")
.attr("transform", "translate(3,52)");
var title_text2 = title_wrapper
.append("a")
.attr("href", d["explore_link"])
.attr("target", "_blank")
.attr("cursor", "pointer")
.append("text")
.attr("class", "title_name_wrapper")
.attr("transform", "translate(3,52)");
title_text1
.append("tspan")
.attr("class", "title_name")
.text(" " + d["publication_title"])
.attr("stroke", "white")
.attr("opacity", 0.93)
.attr("stroke-width", text_strokeWidth);
let titleName_span = title_text2
.append("tspan")
.attr("class", "title_name")
.text(" " + d["publication_title"])
.attr("fill", "black");
title_text1
.append("tspan")
.attr("class", "title_name")
.text(" " + d["publication_title"])
.attr("stroke", "white")
.attr("opacity", 0.93)
.attr("stroke-width", text_strokeWidth);
title_text2
.append("tspan")
.attr("class", "title_name")
.text(" " + d["publication_title"])
.attr("fill", "black");
let position_dividing_baselines =
+d.last_date_held == 0 || d.last_date_held == "Continuing"
? x(new Date().getFullYear())
: x(+d.last_date_held.substring(0, 4));
title_wrapper
.append("line")
.attr("class", "connected_line")
.attr("x1", 0)
.attr("x2", position_dividing_baselines)
.attr("y1", 0)
.attr("y2", 0)
.attr("transform", "translate(0,40)");
title_wrapper
.append("line")
.attr("class", "connected_line2")
.attr("x1", 0)
.attr("x2", 100)
.attr("y1", 0)
.attr("y2", 0)
.attr("transform", "translate(" + position_dividing_baselines + ",40)");
if (d.online_status == "BURNEY") {
let burney_link = title_wrapper
.append("a")
.attr("class", "burney_a")
.attr("href", "https://www.bl.uk/collection-guides/burney-collection")
.attr("target", "_blank")
.attr("opacity", 0.5)
.style("transform", "translate(" + -60 + "px,22px)");
burney_link
.append("path")
.attr("d", "M -10 20 H 60 V 22 Q 50 20 45 36 H -10 V 20")
.style("transform", "translate(0px, -3px)")
.attr("class", "burney_link_rect")
.attr("fill", burney_link_colour)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 1);
});
burney_link
.append("text")
.attr("class", "burney_link")
.style("font-weight", 800)
.text("Burney")
.attr("fill", "white")
.attr("x", 1.5)
.attr("y", gap_between_lines - 5.8)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this.parentNode)
.select(".burney_link_rect")
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this.parentNode)
.select(".burney_link_rect")
.transition()
.duration(80)
.style("opacity", 1);
});
}
if (d.link_to_british_newspaper_archive != "") {
let bna_link = title_wrapper
.append("a")
.attr("class", "bna_a")
.attr("href", d.link_to_british_newspaper_archive)
.attr("target", "_blank")
.attr("opacity", 0.5)
.style("transform", "translate(" + -50 + "px,22px)");
bna_link
.append("path")
.attr("d", "M -10 20 H 60 V 22 Q 50 20 45 36 H -10 V 20")
.style("transform", "translate(-9px, -3px)")
.attr("class", "bna_link_rect")
.attr("fill", bna_link_colour)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this)
.transition()
.duration(80)
.style("opacity", 1);
});
bna_link
.append("text")
.attr("class", "bna_link")
.style("font-weight", 800)
.text("BNA")
.attr("fill", "white")
.attr("x", 0)
.attr("y", gap_between_lines - 5.8)
.style("cursor", "pointer")
.on("mouseover", function (d) {
return d3
.select(this.parentNode)
.select(".bna_link_rect")
.transition()
.duration(80)
.style("opacity", 0.5);
})
.on("mouseout", function (d) {
return d3
.select(this.parentNode)
.select(".bna_link_rect")
.transition()
.duration(80)
.style("opacity", 1);
});
}
}
});
return container;
}