chart4 = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg.append("g").call(xAxis);
svg.append("g").call(yAxis);
svg.append("g").call(legendBodyType);
const players = svg
.append("g")
.selectAll("circle")
.data(sample_data)
.join("circle")
.attr("cx", d => x(d.age))
.attr("cy", d => y(d.wage_eur))
.attr("r", d => r(d.overall))
.attr("stroke", d => color(d.body_type))
.attr("fill", d => color(d.body_type))
.attr("opacity", 0.70)
.on('mouseenter', mouseEnter)
.on('mouseleave', mouseLeave);
const tooltip = svg.append('g').attr('visibility', 'hidden');
const tooltipHeight = 25;
const tooltipRect = tooltip
.append('rect')
.attr('fill', 'white')
.attr('rx', 5)
.attr('height', tooltipHeight);
const tooltipText = tooltip
.append('text')
.attr('fill', 'black')
.attr('font-family', 'Ariel, Helvetica, sans-serif')
.attr('font-size', 10)
.attr('dy', 2)
.attr('dx', 2)
.attr('dominant-baseline', 'hanging');
function mouseEnter(event, d) {
d3.select(this)
.attr("r", d => r(d.overall) * 2)
.attr("stroke", "black")
.attr("opacity", 0.9);
tooltipText.html(d.short_name);
const labelWidth = tooltipText.node().getComputedTextLength();
tooltipRect.attr('width', labelWidth + 4);
const xPos = x(d.age) + r(d.overall) * 3;
const yPos = y(d.wage_eur) - tooltipHeight / 2;
tooltip
.attr('transform', `translate(${xPos},${yPos})`)
.attr('visibility', 'visible');
}
function mouseLeave(event, d) {
d3.select(this)
.attr("r", d => r(d.overall))
.attr("stroke", d => color(d.body_type))
.attr("opacity", 0.70);
tooltip.attr('visibility', 'hidden');
}
return svg.node();
}