update = {
const g = d3.select(chart).select(".gDrawing");
const isBothSelected =
Home_Road.includes("Home") && Home_Road.includes("Road");
let filteredData;
if (isBothSelected) {
const tempFiltered = data.filter(
(d) => d.Season === currSeason && Home_Road.includes(d.TEAM_GAME_LOCATION)
);
const groupedData = d3.group(tempFiltered, (d) => d.TEAM_NAME);
filteredData = Array.from(groupedData, ([team, values]) => {
let teamData = {
TEAM_NAME: team,
img: values[0].img
};
teamData[xAttr] = d3.mean(values, (v) => v[xAttr]);
teamData[yAttr] = d3.mean(values, (v) => v[yAttr]);
return teamData;
});
} else {
filteredData = data.filter(
(d) => d.Season === currSeason && Home_Road.includes(d.TEAM_GAME_LOCATION)
);
}
// (d) => d.Season === currSeason && d.TEAM_GAME_LOCATION === Home_Road[0]
// );
const x = d3
.scaleLinear()
.domain([
d3.min(filteredData, (d) => d[xAttr]),
d3.max(filteredData, (d) => d[xAttr])
])
.range([0, width - margin.left - margin.right])
.nice();
const y = d3
.scaleLinear()
.domain([
d3.min(filteredData, (d) => d[yAttr]),
d3.max(filteredData, (d) => d[yAttr])
])
.range([height - margin.top - margin.bottom, 0])
.nice();
const medianX = (x.domain()[0] + x.domain()[1]) / 2;
const medianY = (y.domain()[0] + y.domain()[1]) / 2;
const xAxisYPosition = y(medianY);
const yAxisXPosition = x(medianX);
// Remove any existing axes before redrawing them
g.selectAll(".xAxis").remove();
g.selectAll(".yAxis").remove();
g.append("g")
.attr("class", "xAxis")
.attr("transform", `translate(0, ${xAxisYPosition})`)
.append("text")
.attr("class", "axisLabel")
.style("fill", "black")
.attr("transform", `translate(${width - margin.left - margin.right}, 30)`)
.style("text-anchor", "end");
g.append("g")
.attr("class", "yAxis")
.attr("transform", `translate(${yAxisXPosition}, 0)`)
.append("text")
.attr("class", "axisLabel")
.style("fill", "black")
.attr("transform", `translate(0, -10)`)
.style("text-anchor", "middle");
g.select(".xAxis").call(d3.axisBottom(x)).select(".axisLabel").text(xAttr);
g.select(".yAxis").call(d3.axisLeft(y)).select(".axisLabel").text(yAttr);
const t = g.transition().duration(3000);
const move = (sel) =>
sel
.attr("x", (d) => x(d[xAttr]) - imgWidth / 2)
.attr("y", (d) => y(d[yAttr]) - imgHeight / 2);
const rows = g
.selectAll(".rows")
.data(filteredData, (d) => d.TEAM_NAME)
.join(
(enter) =>
enter
.append("image")
.attr("class", "rows")
.attr("href", (d) => d.img)
.attr("width", imgWidth)
.attr("height", imgHeight)
.call((enter) => enter.transition(t)),
(update) => update.call((update) => update.transition(t).call(move)),
(exit) =>
exit.call((exit) => exit.transition(t).style("opacity", 0).remove())
);
// tooltipBehavior(g.selectAll(".rows"), tooltip);
rows
.on("mouseover", (event, d) => {
tooltip.style("opacity", 1);
d3.select(event.currentTarget)
.raise() // This brings the current element to the front
.transition()
.duration(200)
.style("stroke", "black")
.style("stroke-width", 2);
})
.on("mousemove", (event, d) => {
tooltip
.html(
`<b>Team:</b> ${d.TEAM_NAME}<br><b>${xAttr}:</b> ${d[xAttr]}<br><b>${yAttr}:</b> ${d[yAttr]}`
)
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY - 28 + "px");
})
.on("mouseout", () => {
tooltip.style("opacity", 0);
d3.selectAll("image")
.transition()
.duration(200)
.style("stroke", "none")
.style("stroke-width", 0);
});
}