蓝色天空网mbsky.com · 知识 · 实践 · 创造Chart.js · 本地阅读预览
教程 / 前端开发 / Chart.js

Chart.js 使用:初始化、配置和实例生命周期

分别验证元素、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选项命名空间分开
responsiveboolean,默认true需要独立有尺寸的容器
maintainAspectRatioboolean,默认truefalse表示不保持宽高比
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)});}
Chart.js 使用:初始化、配置和实例生命周期:真实Canvas实验

所需环境:Chart.js4.6.1 / Edge156.0.4314.58;显式DPR2;实际Canvas及程序化事件

当前源码 SHA256:67b3c2f8666d26e9477d1fd49d6e838ee2b3832dc14aec35f6974a67dfc3e4a2

预期结果 · 待实际运行核验
  • 本章各图真实绘制,元数据点位/半径/弧角与数值映射相符
  • 修改数据使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)});}
2D上下文彩色柱形:独立变体

所需环境:Chart.js4.6.1 / Edge156.0.4314.58;显式DPR2;实际Canvas及程序化事件

当前源码 SHA256:67b3c2f8666d26e9477d1fd49d6e838ee2b3832dc14aec35f6974a67dfc3e4a2

预期结果 · 待实际运行核验
  • 对应图形真实渲染,各元素数量与坐标映射正确

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)});}
ID字符串入口:独立变体

所需环境:Chart.js4.6.1 / Edge156.0.4314.58;显式DPR2;实际Canvas及程序化事件

当前源码 SHA256:67b3c2f8666d26e9477d1fd49d6e838ee2b3832dc14aec35f6974a67dfc3e4a2

预期结果 · 待实际运行核验
  • 对应图形真实渲染,各元素数量与坐标映射正确

真实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)});}
真实jQuery集合入口:独立变体

所需环境:Chart.js4.6.1 / Edge156.0.4314.58;显式DPR2;实际Canvas及程序化事件

当前源码 SHA256:67b3c2f8666d26e9477d1fd49d6e838ee2b3832dc14aec35f6974a67dfc3e4a2

预期结果 · 待实际运行核验
  • 对应图形真实渲染,各元素数量与坐标映射正确

常见错误与边界

  • 只看type、颜色配置而未检查实际图元和像素。
  • 旧版本选项、类别/数值轴或比例含义混淆。

练习与解答

改变一处数据或显示条件,先预测图形与文字变化,再观察实际点位和更新。

展开参考解答

按类型分别检查坐标映射、半径或角度,核对文字表、真实像素和生命周期;真人键盘、读屏、触屏和全事实仍需独立复核。

规范与官方依据