chart = {
const zoom = d3.zoom()
.scaleExtent([0.5, 32])
.on("zoom", zoomed);
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const gGrid = svg.append("g");
const gDot = svg.append("g")
.attr("fill", "none")
.attr("stroke-linecap", "round");
const dot = svg.append("g")
.attr("class", "toolTip")
.attr("display", "none");
dot.append("text")
.attr("font-family", "Helvetica")
.attr("font-size", 30)
.attr("text-anchor", "middle");
gDot.selectAll("path")
.data(data)
.join("path")
.attr("d", d => `M${x(d.val[0])},${y(d.val[1])}h0`)
.attr("stroke", d => z(d.val[2]))
.on('mousemove', function (d) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '0.5');
dot.attr("transform", `translate(${x(d.val[0])},${y(d.val[1])})`);
dot.attr("display", null)
.attr("class", "toolTip");
dot.select('text').text(d.word);
})
.on('mouseout', function (d) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '1');
dot.attr("display", "none");
});
const gx = svg.append("g");
const gy = svg.append("g");
svg.call(zoom).call(zoom.transform, d3.zoomIdentity);
dot.call(zoom).call(zoom.transform, d3.zoomIdentity);
function zoomed() {
const transform = d3.event.transform;
const zx = transform.rescaleX(x).interpolate(d3.interpolateRound);
const zy = transform.rescaleY(y).interpolate(d3.interpolateRound);
gDot.attr("transform", transform).attr("stroke-width", 20 / transform.k);
gx.call(xAxis, zx);
gy.call(yAxis, zy);
gGrid.call(grid, zx, zy);
}
return Object.assign(svg.node(), {
reset() {
svg.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity);
}
});
}