fix(axis): fix chart does not work when using customValues with formatter in time axis label#21352
Conversation
|
Thanks for your contribution! |
…ter on time axis
Root Cause:
- customValues creates basic ScaleTick objects without time property
- TimeScale formatter code assumed tick.time always exists
- Accessing tick.time.level and tick.time.upperTimeUnit without null checks
Solution Applied:
1. src/util/time.ts:346 - Added null check: level: tick.time ? tick.time.level : 0
2. src/scale/Time.ts:208-212 - Wrapped tick.time access in conditional: if (tick.time) { ... }
Testing:
- Created test/customFormat.html with enhanced styling and custom labels
- Verified fixes work correctly with customValues + formatter combination
Impact:
- Resolves GitHub issue with customValues + formatter combination
- Maintains backward compatibility for existing time axis functionality
- Enables proper custom tick positioning with formatted labels on time axes
0b5860d to
3010cb8
Compare
| each(ticks, tick => { | ||
| upperUnitIndex = Math.min(upperUnitIndex, indexOf(primaryTimeUnits, tick.time.upperTimeUnit)); | ||
| maxLevel = Math.max(maxLevel, tick.time.level); | ||
| if (tick.time) { |
There was a problem hiding this comment.
I noticed that TypeScript suppresses this TypeError as per the current tsconfig.json, where the strictNullChecks option is disabled. But if we enable it, there are many errors to be fixed. For simplicity, I agree with your current fix by adding the safeguard.
@100pah How do you think about this?
|
The changes brought by this PR can be previewed at: https://echarts.apache.org/examples/editor?version=PR-21352@3010cb8 |
- Moved test case from standalone customFormat.html to existing axis-customTicks.html - Added test demonstrating time axis with customValues + formatter combination - Verifies the fix for TypeError when accessing tick.time.level
customValues with formatter in axis label
|
Congratulations! Your PR has been merged. Thanks for your contribution! 👍 |
customValues with formatter in axis labelcustomValues with formatter in time axis label
fix(time-axis): resolve TypeError when using customValues with formatter on time axis
Brief Information
This pull request is in the type of:
What does this PR do?
Fixes TypeError "Cannot read properties of undefined (reading 'level')" when using customValues with formatter function on time axis.
Fixed issues
Details
Before: What was the problem?
When using
customValueswith aformatterfunction on a time axis, ECharts threw a TypeError: "Cannot read properties of undefined (reading 'level')". This occurred because:After: How does it behave after the fixing?
The time axis now properly handles customValues with formatter functions without throwing errors. The fix:
level: tick.time ? tick.time.level : 0if (tick.time) { ... }Testing:
Impact:
Document Info
Misc
Security Checking
ZRender Changes
Related test cases or examples to use the new APIs
test/customFormat.html demonstrates the fix with customValues + formatter combination
Merging options
Other information
Files Modified:
I am attaching a video for the solution i have added
https://github.com/user-attachments/assets/29d9d047-edab-414d-9512-6dae1cf455ff