Skip to content

fix(model): use the global textStyle font in getTextRect - #21874

Open
codechrl wants to merge 1 commit into
apache:masterfrom
codechrl:fix/get-text-rect-global-font
Open

codechrl wants to merge 1 commit into
apache:masterfrom
codechrl:fix/get-text-rect-global-font

Conversation

@codechrl

Copy link
Copy Markdown

Brief Information

This pull request is in the type of:

  • bug fixing
  • new feature
  • others

What does this PR do?

Makes getTextRect fall back to the global textStyle font, as text rendering does.

Fixed issues

#21873

Details

Before: What was the problem?

getTextRect in src/model/mixin/textStyle.ts reads fontStyle, fontWeight, fontSize and fontFamily with getShallow only. createTextStyle falls back to the global textStyle for these keys. getTextRect measures a label drawn in a global font with the default font.

With grid.containLabel: true, legacyContainLabel.ts measures axis labels with getTextRect. The reserved space then does not match the drawn labels. The treemap breadcrumb and the visualMap handle label measure text the same way.

A local fontSize also drops the global fontFamily. With axisLabel.fontSize: 10 and a global fontFamily: 'monospace', getTextRect measures the label as 10px sans-serif.

After: How does it behave after the fixing?

Each of the four font keys falls back to ecModel.option.textStyle, the source createTextStyle reads. zrender still builds the font string from the separate keys, so a fontSize with a unit such as '25px' keeps working.

On Windows, the default global fontFamily is 'Microsoft YaHei'. getTextRect now measures default labels in that font there, matching the drawn text.

Added test/ut/spec/model/textStyle.test.ts. Two of its four cases fail without the change in textStyle.ts.

Document Info

One of the following should be checked.

  • This PR doesn't relate to document changes
  • The document should be updated later
  • The document changes have been made in apache/echarts-doc#xxx

Misc

Security Checking

  • This PR uses security-sensitive Web APIs.

ZRender Changes

  • This PR depends on ZRender changes (ecomfe/zrender#xxx).

Related test cases or examples to use the new APIs

test/ut/spec/model/textStyle.test.ts

Merging options

  • Please squash the commits into a single one when merging.

Other information

getFont appends px to any fontSize. It turns '25px' into '25pxpx', so getTextRect does not reuse it.

`getTextRect` reads `fontStyle`, `fontWeight`, `fontSize` and
`fontFamily` from the model only. `createTextStyle` falls back to the
global `textStyle` for these keys. `getTextRect` measures such a label
in the default font.

Each font key now falls back to `ecModel.option.textStyle`, as in
`createTextStyle`. zrender still builds the font string from the
separate keys, so a `fontSize` with a unit such as `'25px'` keeps
working.
@echarts-bot

echarts-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

Thanks for your contribution!
The community will review it ASAP. In the meanwhile, please checkout the coding standard and Wiki about How to make a pull request.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant