Skip to content

fix(chart): count=1 时横轴刻度返回整数下标,不再产生 NaN - #24

Merged
JasonBuildAI merged 1 commit into
mainfrom
fix/even-indices-count-one
Nov 26, 2026
Merged

JasonBuildAI merged 1 commit into
mainfrom
fix/even-indices-count-one

Conversation

@JasonBuildAI

Copy link
Copy Markdown
Owner

问题

evenIndices(length, count) 用来给横轴均匀取刻度位置。当 count === 1 时:

  • step = (length - 1) / (count - 1) = (length - 1) / 0 = Infinity;
  • Math.round(0 * Infinity) = NaN。

于是横轴标签拿到 NaN 坐标,刻度被画到 SVG 之外(或直接消失)。窄屏上刻度数是按宽度算出来的,很容易降到 1。

做法

  • count <= 0 直接返回 [](原本只判空数据);
  • count === 1 返回 [0],即只放首个位置;
  • 其余路径不变。

测试

新增 app/src/components/charts/__tests__/geometry.test.ts(13 个用例,覆盖 evenIndices / paddedDomain / linePath / areaPath / niceTicks),其中包含 count=1、count<=0,以及对多种 length×count 组合的「下标在范围内且递增」性质检查。

验证

  • cd app && npx vitest run src/components/charts/__tests__/geometry.test.ts → 13 passed
  • cd app && npm test → 183 passed
  • cd app && npm run lint → 无输出

count=1 时 step=(length-1)/0=Infinity,Math.round(0*Infinity) 得 NaN,横轴标签会拿到不存在的坐标。现在 count<=0 返回空数组、count===1 返回 [0],并补齐 geometry 的单元测试。
@JasonBuildAI
JasonBuildAI merged commit da111bb into main Nov 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant