drawBubbleMapWithUpdateAxisWithBrush = {
d3.selectAll('.brush>.overlay').remove();
const [svgNode, svg] = getSVG();
const maxRad = 60;
const xScale = d3["scale" + scaleOption]()
.range([0, W])
.nice();
const yScale = d3
.scaleLinear()
.range([H, 0])
.nice();
const radScale = d3
.scaleSqrt()
.range([2, maxRad])
.nice();
const colorScale = d3
.scaleOrdinal()
.domain(continents)
.range(d3.schemeCategory10);
const yAxis = d3.axisLeft().scale(yScale);
svg
.append("g")
.attr("class", "y-axis")
.call(yAxis);
const xAxis = d3.axisBottom().scale(xScale);
svg
.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(0,${H})`)
.call(xAxis);
const bubbles = svg.append("g");
mutable feedback = "enter";
const xScaleBrush = d3
.scaleLinear()
.domain([0, years.length - 1])
.range([0, W + 10]);
const brush = d3
.brushX()
.extent([[0, H], [W + 10, H + margin.bottom]])
.on("brush", brushMove);
const defaultSelection = [W - 10, W + 10];
const slider = svg
.append("g")
.attr("width", W + 10)
.attr("height", margin.bottom)
.attr("class", "brush")
.call(brush)
.call(brush.move, defaultSelection);
slider.selectAll(".resize").remove();
update(groupeByYear.get(recentYear));
function brushMove(event) {
const s = event.selection;
if (s) {
let yearIndex = Math.round(xScaleBrush.invert((s[0] + s[1]) / 2));
mutable feedback = yearIndex;
yearIndex =
yearIndex > years.length - 1
? years.length - 1
: yearIndex < 0
? 0
: yearIndex;
update(groupeByYear.get(years[yearIndex]));
}
}
function update(newData) {
xScale.domain(d3.extent(newData, d => d.gdpPercap));
yScale.domain(d3.extent(newData, d => d.lifeExp));
radScale.domain([0, d3.max(newData, d => d.pop)]);
svg.select(".x-axis").call(d3.axisBottom().scale(xScale));
svg.select(".y-axis").call(d3.axisLeft().scale(yScale));
bubbles
.selectAll("circle")
.data(newData)
.join("circle")
.attr(
'transform',
d => `translate(${xScale(d.gdpPercap)},${yScale(d.lifeExp)})`
)
.attr("r", d => radScale(d.pop))
.style("fill", d => colorScale(d.continent))
.style("opacity", 0.5)
.append("title")
.text(d => Object.entries(d).join("\n"));
}
return svgNode;
}