fix(matrix): fix matrix label formatter does not work#21410
Conversation
|
Thanks for your contribution! Please DO NOT commit the files in dist, i18n, and ssr/client/dist folders in a non-release pull request. These folders are for release use only. |
|
The changes brought by this PR can be previewed at: https://echarts.apache.org/examples/editor?version=PR-21410@819208a |
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug where the matrix.x.label.formatter and matrix.y.label.formatter options were being ignored. The fix adds proper formatter support for matrix axis labels by implementing both string template and function formatters.
Key Changes
- Added
MatrixLabelOptionandMatrixLabelFormatterParamsinterfaces to define the formatter types - Implemented formatter logic in
createMatrixCellfunction to handle both string and function formatters - Added comprehensive test cases covering function formatters, string template formatters, and nested structures
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/coord/matrix/MatrixModel.ts | Added MatrixLabelOption interface extending LabelOption with custom formatter signature, and MatrixLabelFormatterParams interface defining the parameters passed to formatter functions |
| src/component/matrix/MatrixView.ts | Implemented formatter logic by checking for formatter configuration, creating params object, and applying either function or string template formatting to cell text values |
| test/matrix-label-formatter.html | Added three test cases verifying function formatters with emojis, string template formatters with heatmap series, and formatters with nested data structures |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (isFunction(formatter)) { | ||
| const formattedText = formatter(params); | ||
| if (formattedText != null) { | ||
| text = formattedText + ''; | ||
| } | ||
| } | ||
| else if (isString(formatter)) { | ||
| text = formatTplSimple(formatter, params); | ||
| } |
There was a problem hiding this comment.
[nitpick] The order of checks should be isString(formatter) first, then isFunction(formatter) to maintain consistency with the rest of the codebase. See similar patterns in src/component/axisPointer/viewHelper.ts:181-184, src/component/tooltip/TooltipView.ts:837-847, and src/component/calendar/CalendarView.ts:232-237.
| if (isFunction(formatter)) { | |
| const formattedText = formatter(params); | |
| if (formattedText != null) { | |
| text = formattedText + ''; | |
| } | |
| } | |
| else if (isString(formatter)) { | |
| text = formatTplSimple(formatter, params); | |
| } | |
| if (isString(formatter)) { | |
| text = formatTplSimple(formatter, params); | |
| } | |
| else if (isFunction(formatter)) { | |
| const formattedText = formatter(params); | |
| if (formattedText != null) { | |
| text = formattedText + ''; | |
| } | |
| } |
There was a problem hiding this comment.
A nice-to-have suggestion. Not a must.
| name: string; | ||
| value: unknown; | ||
| xyLocator: MatrixXYLocator[]; | ||
| $vars: readonly ['name', 'value', 'xyLocator']; |
There was a problem hiding this comment.
[nitpick] The readonly modifier on $vars is inconsistent with other FormatterParams interfaces in the codebase. For consistency with interfaces like ToolboxTooltipFormatterParams, LegendTooltipFormatterParams, and GeoTooltipFormatterParams, this should be $vars: ['name', 'value', 'xyLocator'] without the readonly modifier.
| $vars: readonly ['name', 'value', 'xyLocator']; | |
| $vars: ['name', 'value', 'xyLocator']; |
There was a problem hiding this comment.
I see other places using readonly, although not $vars, and it's reasonable to use it here.
| }, | ||
| }; | ||
|
|
||
| var chart = testHelper.create(echarts, 'main0', { |
There was a problem hiding this comment.
Unused variable chart.
| var chart = testHelper.create(echarts, 'main0', { | |
| testHelper.create(echarts, 'main0', { |
| } | ||
| }; | ||
|
|
||
| var chart = testHelper.create(echarts, 'main1', { |
There was a problem hiding this comment.
Unused variable chart.
| var chart = testHelper.create(echarts, 'main1', { | |
| testHelper.create(echarts, 'main1', { |
| } | ||
| }; | ||
|
|
||
| var chart = testHelper.create(echarts, 'main2', { |
There was a problem hiding this comment.
Unused variable chart.
| var chart = testHelper.create(echarts, 'main2', { | |
| testHelper.create(echarts, 'main2', { |
Ovilia
left a comment
There was a problem hiding this comment.
Checked visual tests of matrix and are all good.
| if (isFunction(formatter)) { | ||
| const formattedText = formatter(params); | ||
| if (formattedText != null) { | ||
| text = formattedText + ''; | ||
| } | ||
| } | ||
| else if (isString(formatter)) { | ||
| text = formatTplSimple(formatter, params); | ||
| } |
There was a problem hiding this comment.
A nice-to-have suggestion. Not a must.
| name: text, | ||
| value: textValue as unknown, | ||
| xyLocator: xyLocator.slice() as MatrixXYLocator[], | ||
| $vars: ['name', 'value', 'xyLocator'] as const |
There was a problem hiding this comment.
I would suggest using 'coord' instead of 'xyLocatoer' to align with MatrixBodyCornerCellOption.coord.
| name: string; | ||
| value: unknown; | ||
| xyLocator: MatrixXYLocator[]; | ||
| $vars: readonly ['name', 'value', 'xyLocator']; |
There was a problem hiding this comment.
I see other places using readonly, although not $vars, and it's reasonable to use it here.
|
Congratulations! Your PR has been merged. Thanks for your contribution! 👍 |
Brief Information
This pull request is in the type of:
What does this PR do?
Fix matrix label formatter not working.

demo
Fixed issues
Details
Before: What was the problem?
matrix.x.label.formatterandmatrix.y.label.formatterwere ignored.After: How does it behave after the fixing?
Label formatter now works correctly for matrix axes.
Document Info
Misc
Security Checking
ZRender Changes
Related test cases or examples to use the new APIs
test/matrix-label-formatter.htmlMerging options
Other information