{
const svg = d3
.create("svg")
.attr("viewBox", [-100, -100, width + 200, height + 200]);
const gx = svg
.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(30, 0)")
.call(d3.axisLeft().scale(yScale));
const gy = svg
.append("g")
.attr("class", "y-axis")
.attr("transform", "translate(0, 480)")
.call(d3.axisBottom().scale(xScale));
const tooltip = d3
.select("#tooltip")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "black")
.style("border-radius", "5px")
.style("padding", "10px")
.style("color", "white");
const showTooltip = function(d) {
tooltip.transition().duration(100);
tooltip
.style("opacity", 1)
.html("Name: " + d.Name)
.style("left", d3.mouse(this)[0] + 40 + "px")
.style("top", d3.mouse(this)[1] + 40 + "px");
};
const moveTooltip = function(d) {
tooltip
.style("left", d3.mouse(this)[0] + 40 + "px")
.style("top", d3.mouse(this)[1] + 40 + "px");
};
const hideTooltip = function(d) {
tooltip
.transition()
.duration(200)
.style("opacity", 0);
};
svg
.append("g")
.attr("stroke-width", 1.5)
.selectAll("dot")
.data(filteredData)
.enter()
.append("circle")
.attr("cx", d => xScale(d["Attack"]))
.attr("cy", d => yScale(d["Sp. Atk"]))
.attr("fill", d => colorScale(d["Type 1"]))
.attr("r", 7)
.on("mouseover", showTooltip)
.on("mousemove", moveTooltip)
.on("mouseleave", hideTooltip);
svg
.append("text")
.style("text-anchor", "middle")
.attr("x", 500)
.attr("y", -20)
.text("Attack vs Special Attack Power of Pokemon");
svg
.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height + 30)
.text("Attack");
svg
.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", -200)
.attr("y", -20)
.attr("transform", "rotate(-90)")
.text("Special Attack");
svg
.append("g")
.append(() => type_legend)
.attr("x", 50)
.attr("y", 550);
function zoomed({ transform }) {
const zx = transform.rescaleX(xScale).interpolate(d3.interpolateRound);
const zy = transform.rescaleY(yScale).interpolate(d3.interpolateRound);
svg.selectAll("circle").attr("transform", transform);
gx.call(xAxis, zx);
gy.call(yAxis, zy);
}
const zoom = d3
.zoom()
.scaleExtent([0.5, 32])
.on("zoom", zoomed);
svg.call(zoom).call(zoom.transform, d3.zoomIdentity);
return svg.node();
}