Possible plugin lifecycle regression in Chart.js 4.5.1 causing chartjs-plugin-datalabels to draw before internal labels are prepared

未关闭
#12,269 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
38/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
活跃
技术栈
javascript
领域
frontend

调研方向

从 src/core/core.plugins.js 开始,比较 Chart.js 4.5.0 和 4.5.1 之间 PluginService._init 及 notification 行为的差异。尝试根据报告中的配置构建一个最小的 chartjs-plugin-datalabels 2.2.0 复现,并跟踪 setup、update 和 draw hooks。确认或排除生命周期回归,并记录一个可复现的失败或一个针对性回归测试,即视为完成。

由索引模型根据 Issue 内容生成。

描述

type: bug
Expected behavior

After upgrading from Chart.js 4.5.0 to 4.5.1, charts using chartjs-plugin-datalabels 2.2.0 can fail during render with:

TypeError: can't access property "length", labels is undefined

The stack points into chartjs-plugin-datalabels.js during its draw/layout step, where it expects a prepared labels array.

The same chart configuration works with Chart.js 4.5.0.

Suspicious Chart.js change
The likely related change is in src/core/core.plugins.js.

In 4.5.0, PluginService initialized _init as an empty array:

constructor() {
this._init = [];
}

In 4.5.1, _init is initialized as undefined, and plugin notifications return early if beforeInit has not populated it:

constructor() {
this._init = undefined;
}

notify(chart, hook, args, filter) {
if (hook === 'beforeInit') {
this._init = this._createDescriptors(chart, true);
this._notify(this._init, chart, 'install');
}

if (this._init === undefined) {
return;
}

...
}

Hypothesis
Some plugin lifecycle notifications may be skipped, so chartjs-plugin-datalabels does not receive the update hooks it uses to build its internal label layout, but later still reaches its draw hook. That leaves its internal labels undefined and causes the crash.

Environment
Chart.js: 4.5.1
Working version: 4.5.0
Plugin: chartjs-plugin-datalabels 2.2.0
Browser: include browser/version if known
Chart type: include the failing chart type, e.g. bar/pie

Minimal symptom
The failing plugin line is equivalent to:

for (i = 0, ilen = labels.length; i < ilen; ++i) {

where labels is unexpectedly undefined.

Current behavior

Question
Was the PluginService._init === undefined guard intended to skip all plugin hooks before beforeInit, and could it cause inline/per-chart plugins such as ChartDataLabels to miss setup hooks but still receive draw hooks?

I tried to create a minimal CodePen using Chart.js 4.5.1 and chartjs-plugin-datalabels 2.2.0, but I have not been able to reproduce the error outside our REDCap page yet. The issue may depend on our page lifecycle or how the charts are created in that environment.

I understand this may not be actionable without a reproduction, but I wanted to ask whether the PluginService._init change in 4.5.1 could plausibly cause plugin setup hooks to be skipped while later draw hooks still run.

Reproducible sample

https://www.chartjs.org/dist/master/chart.umd.min.js

Optional extra steps/info to reproduce

No response

Possible solution

No response

Context

No response

chart.js version

v4.5.1

Browser name and version

No response

Link to your project

No response

主要语言
JavaScript
星标
67.8k
派生
12k
平均合并
6 小时 15 分钟
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

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

chartjs/Chart.js 的其他 Issue

查看 chartjs/Chart.js 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

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