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

Chart.js:图表类型、许可与现代浏览器

在Canvas中绘制真实数据,固定4.6.1版本并区分库能力、数据语义与可访问展示。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

从数据到Canvas

Chart.js是开源JavaScript图表库,用Canvas2D绘图;图表会随数据、配置和交互改变,而不是一张固定图片。Canvas是网页绘制表面,并不自动提供数据来源或业务分析。

本教程固定4.6.1,并在当前Edge中实际运行。早期2.x/3.x示例的浏览器描述不能直接用于4.x;不再声称IE11可运行现代版本。不同设备和浏览器仍需按项目目标测试。

八种基本类型与组合

常见基本类型为bar、line、pie、doughnut、polarArea、radar、bubble和scatter。混合图是在数据集层组合类型,不是第九个固定字符串,也不一定需要scatter作为总类型。

本页提供八种类型选择;数据表与图形同步,改变一个值后重新绘图,切换时销毁旧实例,避免同一Canvas重复占用。响应式需有明确尺寸的父容器,并考虑渲染像素与显示尺寸的区别。

许可与学习入口

Chart.js采用MIT许可,允许符合许可条件的商业使用,并非只能个人学习或非商业使用。打包时保留版权及许可;第三方插件和数据可能有独立条款。

官方文档、源码和示例分别用于查API、追踪实现及观察配置。Canvas的像素不会自动变成可理解的文本,本站实例提供有名称的画布与同步数据表;读屏、键盘及实际颜色辨识仍需人工验收。

配置、方法与有效条件

字段/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';

// 本章配置与实际运行共用;独立虚构数据。
const specification = {
  "charts": [
    {
      "id": "overview",
      "title": "八类图表切换",
      "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": "element"
    }
  ],
  "modes": {
    "bar": {
      "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": "次数"
            }
          }
        }
      }
    },
    "line": {
      "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": "次数"
            }
          }
        }
      }
    },
    "pie": {
      "type": "pie",
      "data": {
        "labels": [
          "阅读",
          "实作",
          "复习"
        ],
        "datasets": [
          {
            "label": "蓝色天空网:练习次数",
            "data": [
              30,
              12,
              18
            ],
            "backgroundColor": [
              "#2563eb",
              "#db2777",
              "#059669",
              "#d97706",
              "#7c3aed",
              "#0891b2",
              "#64748b"
            ],
            "borderColor": [
              "#2563eb",
              "#db2777",
              "#059669",
              "#d97706",
              "#7c3aed",
              "#0891b2",
              "#64748b"
            ],
            "borderWidth": 1,
            "hoverOffset": 4
          }
        ]
      },
      "options": {
        "responsive": true,
        "maintainAspectRatio": false,
        "animation": false,
        "devicePixelRatio": 2,
        "plugins": {
          "title": {
            "display": true,
            "text": "蓝色天空网 · 虚构学习数据"
          }
        },
        "cutout": 0
      }
    },
    "doughnut": {
      "type": "doughnut",
      "data": {
        "labels": [
          "阅读",
          "实作",
          "复习"
        ],
        "datasets": [
          {
            "label": "蓝色天空网:练习次数",
            "data": [
              30,
              12,
              18
            ],
            "backgroundColor": [
              "#2563eb",
              "#db2777",
              "#059669",
              "#d97706",
              "#7c3aed",
              "#0891b2",
              "#64748b"
            ],
            "borderColor": [
              "#2563eb",
              "#db2777",
              "#059669",
              "#d97706",
              "#7c3aed",
              "#0891b2",
              "#64748b"
            ],
            "borderWidth": 1,
            "hoverOffset": 4
          }
        ]
      },
      "options": {
        "responsive": true,
        "maintainAspectRatio": false,
        "animation": false,
        "devicePixelRatio": 2,
        "plugins": {
          "title": {
            "display": true,
            "text": "蓝色天空网 · 虚构学习数据"
          }
        },
        "cutout": "50%"
      }
    },
    "polarArea": {
      "type": "polarArea",
      "data": {
        "labels": [
          "阅读",
          "实作",
          "复习",
          "讨论",
          "测验"
        ],
        "datasets": [
          {
            "label": "蓝色天空网:练习次数",
            "data": [
              11,
              16,
              7,
              3,
              14
            ],
            "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": {
          "r": {
            "min": 0,
            "max": 20
          }
        }
      }
    },
    "radar": {
      "type": "radar",
      "data": {
        "labels": [
          "理解",
          "练习",
          "检查",
          "表达",
          "调试",
          "整理",
          "回顾"
        ],
        "datasets": [
          {
            "label": "蓝色天空网:练习次数",
            "data": [
              65,
              59,
              90,
              81,
              56,
              55,
              40
            ],
            "backgroundColor": "rgba(37,99,235,.2)",
            "borderColor": "#2563eb",
            "fill": true,
            "pointBackgroundColor": "#2563eb",
            "pointBorderColor": "#ffffff",
            "pointHoverBackgroundColor": "#ffffff",
            "pointHoverBorderColor": "#2563eb"
          },
          {
            "label": "第二组虚构记录",
            "data": [
              28,
              48,
              40,
              19,
              96,
              27,
              100
            ],
            "backgroundColor": "rgba(219,39,119,.2)",
            "borderColor": "#db2777",
            "fill": true,
            "pointBackgroundColor": "#db2777",
            "pointBorderColor": "#ffffff",
            "pointHoverBackgroundColor": "#ffffff",
            "pointHoverBorderColor": "#db2777"
          }
        ]
      },
      "options": {
        "responsive": true,
        "maintainAspectRatio": false,
        "animation": false,
        "devicePixelRatio": 2,
        "plugins": {
          "title": {
            "display": true,
            "text": "蓝色天空网 · 虚构学习数据"
          }
        },
        "scales": {
          "r": {
            "min": 0,
            "max": 100
          }
        },
        "elements": {
          "line": {
            "borderWidth": 3
          }
        }
      }
    },
    "bubble": {
      "type": "bubble",
      "data": {
        "datasets": [
          {
            "label": "蓝色天空网:练习次数",
            "data": [
              {
                "x": 12,
                "y": 28,
                "r": 8
              },
              {
                "x": 25,
                "y": 18,
                "r": 14
              },
              {
                "x": 39,
                "y": 9,
                "r": 20
              }
            ],
            "backgroundColor": "#db2777",
            "borderColor": [
              "#2563eb",
              "#db2777",
              "#059669",
              "#d97706",
              "#7c3aed",
              "#0891b2",
              "#64748b"
            ],
            "borderWidth": 1
          }
        ]
      },
      "options": {
        "responsive": true,
        "maintainAspectRatio": false,
        "animation": false,
        "devicePixelRatio": 2,
        "plugins": {
          "title": {
            "display": true,
            "text": "蓝色天空网 · 虚构学习数据"
          }
        },
        "elements": {
          "point": {
            "radius": 0
          }
        },
        "scales": {
          "x": {
            "type": "linear"
          },
          "y": {
            "type": "linear"
          }
        }
      }
    },
    "scatter": {
      "type": "scatter",
      "data": {
        "datasets": [
          {
            "label": "蓝色天空网:练习次数",
            "data": [
              {
                "x": -8,
                "y": 2
              },
              {
                "x": 0,
                "y": 11
              },
              {
                "x": 7,
                "y": 6
              },
              {
                "x": 0.5,
                "y": 5.5
              }
            ],
            "backgroundColor": "#db2777",
            "borderColor": [
              "#2563eb",
              "#db2777",
              "#059669",
              "#d97706",
              "#7c3aed",
              "#0891b2",
              "#64748b"
            ],
            "borderWidth": 1,
            "showLine": false
          }
        ]
      },
      "options": {
        "responsive": true,
        "maintainAspectRatio": false,
        "animation": false,
        "devicePixelRatio": 2,
        "plugins": {
          "title": {
            "display": true,
            "text": "蓝色天空网 · 虚构学习数据"
          }
        },
        "scales": {
          "x": {
            "type": "linear",
            "position": "bottom"
          },
          "y": {
            "type": "linear"
          }
        }
      }
    }
  },
  "key": "chartjs-tutorial",
  "title": "Chart.js:图表类型、许可与现代浏览器"
};
export default function run(){mount(Chart,specification);}
Chart.js:图表类型、许可与现代浏览器:真实Canvas实验

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

当前源码 SHA256:cc2b4e5a746d664a83956631f452ae6b0305c28348813e08c10e5565c19318be

预期结果 · 待实际运行核验
  • 本章各图真实绘制,元数据点位/半径/弧角与数值映射相符
  • 修改数据使Canvas像素和表格都变化;容器实际响应至360px
  • 同Canvas占用被拒绝,销毁释放绑定,恢复创建新实例

常见错误与边界

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

练习与解答

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

展开参考解答

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

规范与官方依据