[Bug] String formatter templates only replace the first occurrence of `{a}`/`{b}`/`{c}`
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 80/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- typescript
调研方向
从 src/util/format.ts 开始:formatTpl 会把 {a} 改写成 {a0},然后用 tpl.replace(wrapVar(alias), ...) 替换值,而字符串模式的 replace 只命中第一个匹配;formatTplSimple 有同样的缺陷。把两者都换成 replace-all,并保持插入值是字面量,这样 $ 的处理仍然成立,然后在 jest 单元测试中覆盖重复的 {b}/{c}、重复的索引变量和 formatTplSimple,并检查某个 series 标签格式化器会渲染出 Mon: 120 | Mon: 120。注意报告者说已经有带测试的补丁,所以在重复它之前先与维护者确认。
由索引模型根据 Issue 内容生成。
描述
Version
6.1.0 (also reproduced on current master 34f4d92 and release 8ca03d4)
Link to Minimal Reproduction
Steps to Reproduce
- Create a chart with this option:
option = {
xAxis: { type: 'category', data: ['Mon', 'Tue'] },
yAxis: {},
series: [{
type: 'bar',
name: 'Sales',
data: [120, 200],
label: { show: true, formatter: '{b}: {c} | {b}: {c}' }
}]
};
- Look at the bar labels.
The same thing happens with the public helper:
echarts.format.formatTpl('{b}: {c} | {b}: {c}', {
$vars: ['seriesName', 'name', 'value'], seriesName: 'Sales', name: 'Mon', value: 120
});
// 6.1.0 returns "Mon: 120 | {b}: {c}"
Rendered label text for three formatters, read back from the chart in the repo's jest unit-test setup. The first and third are controls (each variable used once, and a repeated {@[0]}):
"{b}: {c}" -> ["Mon: 120","Tue: 200"]
"{b}: {c} | {b}: {c}" -> ["Mon: 120 | {b}: {c}","Tue: 200 | {b}: {c}"]
"{@[0]} | {@[0]}" -> ["120 | 120","200 | 200"]
Current Behavior
Only the first occurrence of each template variable is substituted. Later occurrences of the same variable are shown literally, e.g. the label reads Mon: 120 | {b}: {c}. Indexed variables in multi-series templates behave the same way: '{a0}={c0}, {a1}={c1}; {a0}={c0}, {a1}={c1}' gives A=1, B=2; {a0}={c0}, {a1}={c1}.
Expected Behavior
Every occurrence of a variable is replaced: Mon: 120 | Mon: 120. The option manual for series label.formatter lists {a}, {b}, {c}, {@xxx} and {@[n]} as string template variables (and tooltip.formatter lists {a} to {e} plus {a0}, {a1}, ...), and it does not say a variable can only be used once. In the same formatter, {@xxx} dimension variables are already replaced at every occurrence, and so are the time tokens ({yyyy}, {MM}, ...) in time axis label templates.
Environment
- OS: macOS 26 (arm64)
- Browser: not browser specific (reproduced in Node.js 24 with dist/echarts.js 6.1.0, and in the repo's jest/jsdom unit tests)
- Framework: none
Any additional comments?
Root cause: formatTpl in src/util/format.ts calls tpl.replace(wrapVar(alias), ...) with a string pattern, both when it rewrites {a} to {a0} and when it inserts the values. String.prototype.replace with a string pattern replaces only the first match (ECMA-262). formatTplSimple, which is used for the calendar and matrix label formatters, has the same pattern. formatTpl is used for series label formatters, string tooltip.formatter templates and echarts.format.formatTpl.
Suggested fix: replace all occurrences in both functions, for example with a small str.split(search).join(value + '') helper. That keeps the inserted value literal, so the $ handling from #21751 still works. I have a patch with unit tests (repeated {b}/{c}, repeated indexed variables, formatTplSimple, a series label check) and can open a PR if that helps.
This issue was drafted with Claude Code (claude-opus-5-5); the results above are from the runs described.
- 主要语言
- TypeScript
- 星标
- 67.7k
- 派生
- 19.9k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/echarts 的其他 Issue
-
[Bug] Inverse axis collapses the grid with outerBoundsContain when a tick is near the axis edge可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭en pending
难度 2/5 1-3 小时 新手友好度 82/100
-
en pending
难度 2/5 1-3 小时 新手友好度 78/100
-
en pending
难度 2/5 1-3 小时 新手友好度 78/100
-
[Bug] The zoom cursor in the toolbox area is incorrect可能已有人在做 @waterWang 于 98 天前认领。 未关闭bug en pending
难度 2/5 1-3 小时 新手友好度 70/100
-
en pending
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
Stop retrying permanent GitHub API failures in retry.sh可能已有人在做 @muspi-merol-review 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
promplate/code-review#113 · 1 条评论 ·
维护者通常 1 天内回复
-
triage
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
サムネイルの Storage のバケット(`thumbnails`)に、大きさの上限(1 MiB)と許可する種類(JPEG、PNG、WebP)を設定する可能已有人在做 @ykzts 今天认领。 未关闭tech: supabase
难度 2/5 1-3 小时 新手友好度 78/100
inabagumi/shinju-date#7147 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
elecnix/your-attention#129 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
umami-software/umami#4780 ·
维护者通常 3 天内回复