Chart.js 使用:初始化、配置和实例生命周期
分别验证元素、2D上下文、ID和jQuery集合入口,保留折线与彩色柱形实例。
学完这一章,你能够
- 分别验证元素、2D上下文、ID和jQuery集合入口,保留折线与彩色柱形实例。
- 分别验证元素、2D上下文、ID和jQuery集合入口,保留折线与彩色柱形实例。
选中真实Canvas
new Chart的首参可为Canvas元素、其2D上下文或有效ID;本例另用真实jQuery4.0.0集合保留旧入口对照。原生项目不必为了画一张图添加jQuery。必须保证元素已经存在,ID不重复。
四种入口分别使用不同Canvas。一个画布仍被现有Chart占用时再次创建会报错;重建前调用destroy,而不是无限new造成事件和观察器泄漏。
完整配置结构
配置对象由type、data、options、plugins等组成,各字段之间必须有逗号。data.labels用于类别标签,datasets为系列数组;options控制布局、轴和交互,plugins为实例插件集合,不能和options.plugins的插件选项混淆。
本章同时保留折线和多色柱形图。数据、颜色数组及标签的对应关系必须核对;图例名称应说明变量与单位,不把演示数字伪装成真实经营数据。
更新与清理
修改数据后调用update,update(none)用于本例立即重绘并便于核对。改变options也需更新;destroy释放事件和Canvas关联。getChart可确认是否仍有实例绑定。
按钮可增加第一项数值、调整容器宽度、隐藏系列、销毁和恢复;数据表同时刷新。Canvas渲染像素变化、实际点位及API状态分别记录,不能只看配置里写了一个属性。
配置、方法与有效条件
| 字段/API | 输入/作用 | 返回或边界 |
| new Chart(item,config) | Canvas/上下文/ID等与配置 | Chart实例;同Canvas占用须先destroy |
| type | 已注册图表类型 | 数据集type可覆盖默认 |
| data.labels / datasets | 类别及系列 | 标签/数值/颜色对应 |
| options | 轴、尺寸、交互等 | 各版本及类型不同 |
| plugins | 插件对象数组 | 与options.plugins选项命名空间分开 |
| responsive | boolean,默认true | 需要独立有尺寸的容器 |
| maintainAspectRatio | boolean,默认true | false表示不保持宽高比 |
| devicePixelRatio | 本例显式2 | 渲染缓冲像素与CSS显示尺寸不同 |
| chart.update(mode) | 数据或选项改变后重绘 | void;none使本例立即更新 |
| chart.destroy() | 释放事件/观察器及关联 | void;重建前调用 |
| Chart.getChart(item) | 查已绑定实例 | Chart或undefined |
| getDatasetMeta(index) | 取绘图元数据 | 调试/测试不替代业务数据模型 |
| getElementsAtEventForMode | 事件、模式、条件及最终位置选项 | 活动元素数组;依真实命中条件 |
本章独立核心源码与验证步骤
展开查看收起
javascript
import Chart from 'chart.js/auto';
import {mount} from './lab.js';
import $ from 'jquery';
// 本章配置与实际运行共用;独立虚构数据。
const specification = {
"charts": [
{
"id": "usage-element",
"title": "Canvas元素折线",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "element"
},
{
"id": "usage-context",
"title": "2D上下文彩色柱形",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "context"
},
{
"id": "usage-id",
"title": "ID字符串入口",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "id"
},
{
"id": "usage-jquery",
"title": "真实jQuery集合入口",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "jquery"
}
],
"key": "chartjs-usage",
"title": "Chart.js 使用:初始化、配置和实例生命周期"
};
export default function run(){window.mbskyJQuery=$.fn.jquery;mount(Chart,specification, {jquery:element=>$(element)});}
尝试一下 ↗ 在独立页面修改并运行
- 本章各图真实绘制,元数据点位/半径/弧角与数值映射相符
- 修改数据使Canvas像素和表格都变化;容器实际响应至360px
- 同Canvas占用被拒绝,销毁释放绑定,恢复创建新实例
2D上下文彩色柱形:独立变体
展开查看收起
javascript
import Chart from 'chart.js/auto';
import {mount} from './lab.js';
import $ from 'jquery';
// 本章配置与实际运行共用;独立虚构数据。
const specification = {
"charts": [
{
"id": "usage-element",
"title": "Canvas元素折线",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "element"
},
{
"id": "usage-context",
"title": "2D上下文彩色柱形",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "context"
},
{
"id": "usage-id",
"title": "ID字符串入口",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "id"
},
{
"id": "usage-jquery",
"title": "真实jQuery集合入口",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "jquery"
}
],
"key": "chartjs-usage",
"title": "Chart.js 使用:初始化、配置和实例生命周期"
};
export default function run(){window.mbskyJQuery=$.fn.jquery;mount(Chart,specification, {jquery:element=>$(element)});}
尝试一下 ↗ 在独立页面修改并运行
- 对应图形真实渲染,各元素数量与坐标映射正确
ID字符串入口:独立变体
展开查看收起
javascript
import Chart from 'chart.js/auto';
import {mount} from './lab.js';
import $ from 'jquery';
// 本章配置与实际运行共用;独立虚构数据。
const specification = {
"charts": [
{
"id": "usage-element",
"title": "Canvas元素折线",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "element"
},
{
"id": "usage-context",
"title": "2D上下文彩色柱形",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "context"
},
{
"id": "usage-id",
"title": "ID字符串入口",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "id"
},
{
"id": "usage-jquery",
"title": "真实jQuery集合入口",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "jquery"
}
],
"key": "chartjs-usage",
"title": "Chart.js 使用:初始化、配置和实例生命周期"
};
export default function run(){window.mbskyJQuery=$.fn.jquery;mount(Chart,specification, {jquery:element=>$(element)});}
尝试一下 ↗ 在独立页面修改并运行
- 对应图形真实渲染,各元素数量与坐标映射正确
真实jQuery集合入口:独立变体
展开查看收起
javascript
import Chart from 'chart.js/auto';
import {mount} from './lab.js';
import $ from 'jquery';
// 本章配置与实际运行共用;独立虚构数据。
const specification = {
"charts": [
{
"id": "usage-element",
"title": "Canvas元素折线",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "element"
},
{
"id": "usage-context",
"title": "2D上下文彩色柱形",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "context"
},
{
"id": "usage-id",
"title": "ID字符串入口",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "#2563eb",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "id"
},
{
"id": "usage-jquery",
"title": "真实jQuery集合入口",
"config": {
"type": "bar",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderColor": [
"#2563eb",
"#db2777",
"#059669",
"#d97706",
"#7c3aed",
"#0891b2",
"#64748b"
],
"borderWidth": 1
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
},
"y": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
}
}
}
},
"input_kind": "jquery"
}
],
"key": "chartjs-usage",
"title": "Chart.js 使用:初始化、配置和实例生命周期"
};
export default function run(){window.mbskyJQuery=$.fn.jquery;mount(Chart,specification, {jquery:element=>$(element)});}
尝试一下 ↗ 在独立页面修改并运行
- 对应图形真实渲染,各元素数量与坐标映射正确
本章附件与实例包
课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202611a495.zip
常见错误与边界检查
- 只看type、颜色配置而未检查实际图元和像素。
- 旧版本选项、类别/数值轴或比例含义混淆。
技术规范与官方资料
练习与参考解答
查看参考解答
按类型分别检查坐标映射、半径或角度,核对文字表、真实像素和生命周期;真人键盘、读屏、触屏和全事实仍需独立复核。