Repository navigation
fix(chart): count=1 时横轴刻度返回整数下标,不再产生 NaN - #24
Merged
Merged
Conversation
count=1 时 step=(length-1)/0=Infinity,Math.round(0*Infinity) 得 NaN,横轴标签会拿到不存在的坐标。现在 count<=0 返回空数组、count===1 返回 [0],并补齐 geometry 的单元测试。
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.
问题
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 passedcd app && npm test→ 183 passedcd app && npm run lint→ 无输出