# 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. DefiningxAxesin your config will now create a second scale instead of overriding the defaultxaxis.
# 选项
¥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
destroyplugin 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.drawBorderhas been renamed toscales[id].border.display.scales[id].grid.borderWidth已重命名为scales[id].border.width。¥
scales[id].grid.borderWidthhas been renamed toscales[id].border.width.scales[id].grid.borderColor已重命名为scales[id].border.color。¥
scales[id].grid.borderColorhas been renamed toscales[id].border.color.scales[id].grid.borderDash已重命名为scales[id].border.dash。¥
scales[id].grid.borderDashhas been renamed toscales[id].border.dash.scales[id].grid.borderDashOffset已重命名为scales[id].border.dashOffset。¥
scales[id].grid.borderDashOffsethas 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尊重容器高度。¥
maintainAspectRatiorespects container height.时间和时间序列尺度使用
ticks.stepSize而不是time.stepSize,后者已被删除。¥Time and timeseries scales use
ticks.stepSizeinstead oftime.stepSize, which has been removed.如果确定的最大刻度小于
maxTicksLimit,则maxTickslimit将不会用于autoSkip中的刻度。¥
maxTickslimitwon't be used for the ticks inautoSkipif the determined max ticks is less then themaxTicksLimit.dist/chart.js已被删除。¥
dist/chart.jshas been removed.dist/chart.min.js已重命名为dist/chart.umd.min.js(4.5.0 之前的版本为dist/chart.umd.js)。¥
dist/chart.min.jshas been renamed todist/chart.umd.min.js(and before 4.5.0dist/chart.umd.js).dist/chart.esm.js已重命名为dist/chart.js。¥
dist/chart.esm.jshas been renamed todist/chart.js.
# 类型变化
¥Type changes
ChartMeta参数的顺序已从<Element, DatasetElement, Type>更改为<Type, Element, DatasetElement>。¥The order of the
ChartMetaparameters 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
fontColorfor the legend text and strikethrough color.在填充插件中删除了
this.chart的config._chart回退。¥Removed
config._chartfallback forthis.chartin the filler plugin.删除了填充插件中的
this._chart。¥Removed
this._chartin the filler plugin.