plotly / plotly/plotly.py

[BUG]: Text extents are not recomputed after web font loading

未关闭
#5,656 4 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@emilykl 已经在做这个了。

开始于 2026年7月22日。

bug P2 size: 5
主要语言
Python
星标
18.8k
派生
2.8k
平均合并
16 小时 26 分钟
30 天内合并 PR
21

描述

Description

When using custom web fonts loaded via @font-face or external stylesheets, Plotly computes text extents before the configured font is available and therefore uses fallback font metrics.

Once the web font has loaded, the SVG text elements correctly update to the configured font-family, but the previously computed text extents are not recomputed.

As a result, text may become clipped or incorrectly positioned when the web font has significantly different metrics from the fallback font, which is particularly noticeable with monospace fonts.

The issue only occurs on the first page load. A subsequent refresh uses the already-loaded web font, and the text extents are calculated correctly.

Expected behavior: Text extents should either be computed after the configured font has loaded or recomputed once the font becomes available.

Screenshots/Video

Output of example below:

Image
Steps to reproduce

Minimal example using a slightly modified version of the bar chart example

Plotly version: v6.9.0

import plotly.express as px

long_df = px.data.medals_long()

fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")

fig.update_layout(
    font=dict(
        family="JetBrains Mono",
    ),
    legend=dict(
        borderwidth=1
    )
)
fig.add_annotation(
    x=1.05,
    y=0.0,
    xref="paper",
    yref="paper",
    xanchor="left",
    yanchor="bottom",
    text="Source: Coyote | \u00a9 ACME",
    textangle=-90,
    showarrow=False,
    bgcolor="rgba(0,0,0,0.2)",
)

fig.write_html("plotly_bar_jetbrains_mono.html", include_plotlyjs="cdn")

# Inject the following stylesheet into the head section of the html
# <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource/jetbrains-mono@5.0.20/index.css">
Notes

Tested with:
Firefox 152.0.5
Edge 150.0.4078.65
Chrome 150.0.7871.115

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。