# 4.x 迁移指南
¥4.x Migration Guide
Chart.js 4.0 引入了一些重大变化。我们尝试将重大更改的数量保持在最低限度。对于某些功能和错误修复,有必要打破向后兼容性,但我们的目标是仅在值得受益时才这样做。
¥Chart.js 4.0 introduces a number of breaking changes. We tried keeping the amount of breaking changes to a minimum. For some features and bug fixes it was necessary to break backwards compatibility, but we aimed to do so only when worth the benefit.
# 终端用户迁移
¥End user migration
# 图表
¥Charts
图表不会覆盖默认的工具提示回调,因此所有图表类型都具有外观相同的工具提示。
¥Charts don't override the default tooltip callbacks, so all chart types have the same-looking tooltips.
如果配置的比例以
x
/y
开头,则默认比例覆盖已被删除。在你的配置中定义xAxes
现在将创建第二个比例,而不是覆盖默认的x
轴。¥Default scale override has been removed if the configured scale starts with
x
/y
. DefiningxAxes
in your config will now create a second scale instead of overriding the defaultx
axis.
# 选项
¥Options
对传递给 Chart
构造函数的配置选项进行了一些更改。这些更改记录在下面。
¥A number of changes were made to the configuration options passed to the Chart
constructor. Those changes are documented below.
# 具体变化
¥Specific changes
radialLinear grid indexable 和 scriptable 选项不再减少指定网格线的索引。
¥The radialLinear grid indexable and scriptable options don't decrease the index of the specified grid line anymore.
destroy
插件钩子已被删除并替换为afterDestroy
。¥The
destroy
plugin hook has been removed and replaced withafterDestroy
.时间刻度上的滴答回调现在接收时间戳而不是格式化标签。
¥Ticks callback on time scale now receives timestamp instead of a formatted label.
scales[id].grid.drawBorder
已重命名为scales[id].border.display
。¥
scales[id].grid.drawBorder
has been renamed toscales[id].border.display
.scales[id].grid.borderWidth
已重命名为scales[id].border.width
。¥
scales[id].grid.borderWidth
has been renamed toscales[id].border.width
.scales[id].grid.borderColor
已重命名为scales[id].border.color
。¥
scales[id].grid.borderColor
has been renamed toscales[id].border.color
.scales[id].grid.borderDash
已重命名为scales[id].border.dash
。¥
scales[id].grid.borderDash
has been renamed toscales[id].border.dash
.scales[id].grid.borderDashOffset
已重命名为scales[id].border.dashOffset
。¥
scales[id].grid.borderDashOffset
has been renamed toscales[id].border.dashOffset
.比例尺边界的 z 索引现在是可配置的,而不是比网格 z 索引高 1。
¥The z index for the border of a scale is now configurable instead of being 1 higher as the grid z index.
如果最小值和最大值相同而不是
1
,则线性刻度现在将最大值的5%
添加和减去范围。¥Linear scales now add and subtracts
5%
of the max value to the range if the min and max are the same instead of1
.如果工具提示回调返回
undefined
,则将使用默认回调。¥If the tooltip callback returns
undefined
, then the default callback will be used.maintainAspectRatio
尊重容器高度。¥
maintainAspectRatio
respects container height.时间和时间序列尺度使用
ticks.stepSize
而不是time.stepSize
,后者已被删除。¥Time and timeseries scales use
ticks.stepSize
instead oftime.stepSize
, which has been removed.如果确定的最大刻度小于
maxTicksLimit
,则maxTickslimit
将不会用于autoSkip
中的刻度。¥
maxTickslimit
won't be used for the ticks inautoSkip
if the determined max ticks is less then themaxTicksLimit
.dist/chart.js
已被删除。¥
dist/chart.js
has been removed.dist/chart.min.js
已重命名为dist/chart.umd.js
。¥
dist/chart.min.js
has been renamed todist/chart.umd.js
.dist/chart.esm.js
已重命名为dist/chart.js
。¥
dist/chart.esm.js
has been renamed todist/chart.js
.
# 类型变化
¥Type changes
ChartMeta
参数的顺序已从<Element, DatasetElement, Type>
更改为<Type, Element, DatasetElement>
。¥The order of the
ChartMeta
parameters have been changed from<Element, DatasetElement, Type>
to<Type, Element, DatasetElement>
.
# 一般的
¥General
Chart.js 成为 仅 ESM 包 (opens new window) (UMD 包仍然可用)。要使用 Chart.js,你的项目也应该是一个 ES 模块。确保在你的
package.json
中有这个:¥Chart.js becomes an ESM-only package (opens new window) (the UMD bundle is still available). To use Chart.js, your project should also be an ES module. Make sure to have this in your
package.json
:{ "type": "module" }
如果你在使用 Jest (opens new window) 时遇到问题,请按照其 documentation (opens new window) 启用 ESM 支持。或者,我们可以建议你迁移到 Vitest (opens new window)。Vitest 具有开箱即用的 ESM 支持和 与 Jest 几乎相同的 API (opens new window)。见 迁移示例 (opens new window)。
¥If you are experiencing problems with Jest (opens new window), follow its documentation (opens new window) to enable the ESM support. Or, we can recommend you migrating to Vitest (opens new window). Vitest has the ESM support out of the box and almost the same API as Jest (opens new window). See an example of migration (opens new window).
删除了图例文本和删除线颜色的回退到
fontColor
。¥Removed fallback to
fontColor
for the legend text and strikethrough color.在填充插件中删除了
this.chart
的config._chart
回退。¥Removed
config._chart
fallback forthis.chart
in the filler plugin.删除了填充插件中的
this._chart
。¥Removed
this._chart
in the filler plugin.