D3力导向图鱼眼效果在仿真未稳定前不生效问题咨询
带鱼眼效果的力导向图实现方案
问题根因
- 力导向模拟中
node.x/node.y是用于物理计算的原始坐标,你之前实现的自定义力直接修改该坐标,相当于改变了节点在力场中的真实物理位置,因此会出现节点随光标逃逸的异常。 - 原有代码中
tick事件和mousemove事件同时修改节点渲染坐标,tick每次运行都会将坐标覆盖为未做鱼眼转换的原始值,因此力模拟运行时鱼眼效果会被覆盖。
实现思路
鱼眼属于纯渲染层的坐标变换,不要和力模拟的物理计算逻辑耦合:
- 所有力模拟逻辑保留,不修改原始的
node.x/node.y计算结果 - 所有渲染环节统一将原始坐标传入鱼眼工具,用转换后的坐标绘制节点和连线
- 鼠标移动时仅更新鱼眼的焦点位置,触发重绘即可
- 节点拖拽直接修改原始物理坐标,渲染层自动完成鱼眼转换,无需额外适配
核心修正代码
d3.json("https://gist.githubusercontent.com/mbostock/4062045/raw/5916d145c8c048a6e3086915a6be464467391c62/miserables.json").then(draw); function draw(data) { const fisheye = fisheyeO.circular() .radius(100) .distortion(5); const height = 400; const width = 500; const simulation = d3.forceSimulation(data.nodes) .alphaDecay(0.0125) .alphaMin(0.01) .force("link", d3.forceLink(data.links).id(d => d.id)) .force("charge", d3.forceManyBody()) .force("x", d3.forceX(width/2)) .force("y", d3.forceY(height/2)); const svg = d3.select("body").append("svg") .attr("viewBox", [0, 0, width, height]) const link = svg.append("g") .attr("stroke", "#999") .attr("stroke-opacity", 0.6) .selectAll("line") .data(data.links) .join("line") .attr("stroke-width", 2); const node = svg.append("g") .attr("stroke", "#fff") .attr("stroke-width", 1.5) .selectAll("circle") .data(data.nodes) .join("circle") .attr("r", 5) .attr("fill", "black") // 增加节点拖拽支持 .call(d3.drag() .on("start", function(event, d) { if (!event.active) simulation.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; }) .on("drag", function(event, d) { d.fx = event.x; d.fy = event.y; }) .on("end", function(event, d) { if (!event.active) simulation.alphaTarget(0); d.fx = null; d.fy = null; }) ); // 统一的重绘函数 function redraw() { node.each(function(d) { d.fisheye = fisheye(d); }) .attr("cx", d => d.fisheye.x) .attr("cy", d => d.fisheye.y) .attr("r", d => d.fisheye.z * 4.5); link.attr("x1", d => d.source.fisheye.x) .attr("y1", d => d.source.fisheye.y) .attr("x2", d => d.target.fisheye.x) .attr("y2", d => d.target.fisheye.y); } svg.on("mousemove", function(event) { // 更新鱼眼焦点,d3 v5版本请替换为d3.mouse(this) fisheye.focus(d3.pointer(event, this)); // 鼠标移动时主动触发重绘,解决模拟冷却后不更新的问题 redraw(); }) simulation.on("tick", () => { // 每次tick统一走鱼眼转换后重绘 redraw(); }); } // 原有鱼眼工具代码保留 const fisheyeO = { circular: () => { var radius = 200, distortion = 2, k0, k1, focus = [0, 0]; function fisheye(d) { var dx = d.x - focus[0], dy = d.y - focus[1], dd = Math.sqrt(dx * dx + dy * dy); if (!dd || dd >= radius) return {x: d.x, y: d.y, z: dd >= radius ? 1 : 10}; var k = k0 * (1 - Math.exp(-dd * k1)) / dd * .75 + .25; return {x: focus[0] + dx * k, y: focus[1] + dy * k, z: Math.min(k, 10)}; } function rescale() { k0 = Math.exp(distortion); k0 = k0 / (k0 - 1) * radius; k1 = distortion / radius; return fisheye; } fisheye.radius = function(_) { if (!arguments.length) return radius; radius = +_; return rescale(); }; fisheye.distortion = function(_) { if (!arguments.length) return distortion; distortion = +_; return rescale(); }; fisheye.focus = function(_) { if (!arguments.length) return focus; focus = _; return fisheye; }; return rescale(); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.8.5/d3.min.js"></script>
内容的提问来源于stack exchange,提问作者xy2_
相关产品推荐
相关产品推荐

