[Bug] String formatter templates only replace the first occurrence of `{a}`/`{b}`/`{c}`
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 80/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- data-visualization
Hướng nghiên cứu
Bắt đầu trong src/util/format.ts: formatTpl viết lại {a} thành {a0} rồi thay thế giá trị bằng tpl.replace(wrapVar(alias), ...), và replace với mẫu chuỗi chỉ tác động lên kết quả khớp đầu tiên; formatTplSimple có cùng lỗi. Hãy đổi cả hai sang một replace-all giữ nguyên giá trị được chèn dưới dạng literal để cách xử lý $ vẫn đúng, sau đó bao phủ {b}/{c} lặp lại, các biến có chỉ số lặp lại và formatTplSimple trong các bài kiểm thử đơn vị jest và kiểm tra rằng một formatter nhãn series render ra Mon: 120 | Mon: 120. Lưu ý rằng người báo cáo nói đã có sẵn một bản vá kèm kiểm thử, nên xác nhận với maintainer trước khi nhân bản nó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 67.7k
- Fork
- 19.9k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/echarts
-
[Bug] Inverse axis collapses the grid with outerBoundsContain when a tick is near the axis edgeCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởen pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
en pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
en pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
[Bug] The zoom cursor in the toolbox area is incorrectCó thể đã có người làm @waterWang đã nhận 98 ngày trước. Đang mởbug en pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
en pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Tất cả issue của apache/echarts
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
pollen-robotics/reachy-mini-desktop-app#341 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
mike-north/AgentMonitors#648 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Position: a popup placed again from between two layout steps lands a fraction of a pixel offĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
vercel-labs/ui#700 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 2 ngày