function make_salary_comparison_bar_plot(cohort, rank, cost_adjust_flag) {
let ids = cohorts.filter((o) => o.cohort == cohort).map((o) => o.UNITID);
let filtered_data = salary_data.filter((o) => ids.indexOf(o.UnitID) > -1);
let tidy_data = get_single_value(rank.rank.toLowerCase());
let avg_data = tidy_data.map(avg);
let plot =
width < 900
? Plot.plot({
y: {
label: null,
domain: d3
.sort(avg_data, (d) =>
cost_adjust_flag ? -d.cost_adjusted_value : -d.value
)
.map((d) => d.UnitID),
tickFormat: (d) => get_initials(d)
},
width: width,
marks: [
Plot.barX(avg_data, {
y: "UnitID",
x: cost_adjust_flag ? "cost_adjusted_value" : "value",
fill: (d) => (d.UnitID == "199111" ? "#003DA5" : "lightblue"),
title: "UnitID"
}),
Plot.ruleX([0])
]
})
: Plot.plot({
x: {
label: null,
domain: d3
.sort(avg_data, (d) =>
cost_adjust_flag ? -d.cost_adjusted_value : -d.value
)
.map((d) => d.UnitID),
tickFormat: (d) => get_initials(d)
},
width: width,
height: 0.4 * width,
marginLeft: 70,
marks: [
Plot.barY(avg_data, {
x: "UnitID",
y: cost_adjust_flag ? "cost_adjusted_value" : "value",
fill: (d) => (d.UnitID == "199111" ? "#003DA5" : "lightblue"),
title: "UnitID"
}),
Plot.ruleY([0])
]
});
let nodes = d3
.select(plot)
.selectAll("rect")
.on("pointerenter", function () {
d3.select(this).attr("stroke-width", "2px").attr("stroke", "black");
})
.on("pointerleave", function () {
d3.select(this).attr("stroke", null);
})
.nodes()
.forEach(function (bar) {
let title = d3.select(bar).select("title").text();
tippy(bar, {
content: `${get_name(title)}: $${parseInt(get_salary(title))} ${
cost_adjust_flag ? "(adjusted)" : ""
}`,
theme: "light"
});
});
d3.select(plot).selectAll("rect").select("title").remove();
return plot;
function get_single_value(column_start) {
let columns = Object.keys(filtered_data[0]).filter(
(s) => s.slice(0, column_start.length) == column_start
);
let tidy_data = filtered_data.map(function (d) {
let row = columns.map((key) => ({
year: parseInt(key.match(find_year_re)[0].slice(-4)),
value: parseInt(d[key]),
cost_adjusted_value: cost_adjust(parseInt(d[key]), d.UnitID),
UnitID: d.UnitID
}));
row = d3.sort(row, (o) => o.year);
return row;
});
return tidy_data;
}
function get_salary(uid) {
let row = avg_data.filter((o) => o.UnitID == uid)[0];
if (cost_adjust_flag) {
return Math.round(row.cost_adjusted_value);
} else {
return row.value;
}
}
}