Repository navigation
fix(d3-force): respect explicit center and force positions - #295
Open
chilingling wants to merge 1 commit into
Open
chilingling wants to merge 1 commit into
chilingling wants to merge 1 commit into
Conversation
Defaults such as width / 2, the center point and radius 100 were merged after the user-provided force objects, so explicit center, x, y and radial options were overwritten. Apply defaults first, then force objects, then shortcut fields.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
close #294
问题
D3ForceLayout在合并配置时,默认属性的合并优先级错误地高于用户传入的配置对象,导致以下自定义配置被默认值覆盖而无法生效:center: { x, y }、centerX/centerY:无论如何配置,中心点坐标始终回退为视口中心[width / 2, height / 2]x: { x }、y: { y }、radial: { x, y }:中心力开启时,其自定义坐标会被中心点坐标覆盖radial: { radius }:自定义半径被硬编码的默认值100覆盖详细复现步骤与原因分析见关联 Issue。
改动
修改集中在
src/algorithm/d3-force/index.ts,调整了getCenterOptions、getXForceOptions、getYForceOptions与getRadialOptions的合并逻辑,将参数合并优先级统一为:默认兜底值 → force 配置对象 → 扁平快捷属性。
manyBody、link、collide等其他力配置 getter 的现有规范保持一致。getCenterOptions补充对centerX/centerY快捷属性的解析(此前类型声明与文档均已提供,但代码实现中未实际读取)。影响
__tests__/snapshots/d3-force/force-options.svg。该用例显式配置了center: { x: 300, y: 300 }、x: { x: 300 }、y: { y: 300 }与radial: { x: 350, y: 350, radius: 120 }。此前由于参数被默认值覆盖,旧快照实际渲染使用的是 center/x/y 均为 350、径向半径为 100 的错误布局;本次修复后快照已更新为真实生效配置对应的正确图形。验证
__tests__/unit/d3-force.test.ts中新增center position测试套件(包含 5 个用例)。其中验证显式坐标的 4 个用例在v5旧代码上均失败,修复后全部通过;验证“回退到视口中心”的用例在修复前后均正常通过。npm test,47 个测试套件全部通过,1 个跳过(47 passed, 1 skipped)。npm run lint(tsc --noEmit),src/核心代码无错误,本 PR 未引入任何新增类型问题。(注:__tests__/中现存的 228 个报错系v5分支基线历史遗留问题,全新安装下还会触发@types/node@26与 TS 4.9 的兼容问题,均与本 PR 无关)。