Chart.js 折线图:点样式、交互及转置坐标
保留基础、增强与indexAxis:y三种实例,准确对应类别、数值轴和点半径。
学完这一章,你能够
- 保留基础、增强与indexAxis:y三种实例,准确对应类别、数值轴和点半径。
- 保留基础、增强与indexAxis:y三种实例,准确对应类别、数值轴和点半径。
连线表达的关系
line通过顺序连接数据点显示趋势;类别等距不代表真实时间间隔相同。真实不规则时间应采用适当的时间尺度、日期适配器和数据语义,不能拿类别月名就证明时间连续性。
本例基础线设置fill:false、borderColor及tension:0.1。曲线插值只是视觉连接,不意味着中间值经过观测;缺失数据和断线策略要独立处理。
增强样式与交互
pointStyle:circle、pointRadius:6和pointHoverRadius:10分别控制形状、正常及活动半径。backgroundColor用于点/填色的具体效果需结合fill;设置颜色不表示必定填满线下区域。
增强实例使用interaction.intersect:false,显示类别/数值轴标题,并通过真实鼠标事件核对活动点及提示。配置存在不等于交互已触发,实际元素半径另行观察。
indexAxis:y的准确解释
将indexAxis改为y是把类别排列到纵轴,数值放到横轴,并非把数值轴仍称作日期。此时类别轴y的标题为学习日期,数值轴x的标题为次数,零基线也放在x。
三种实例独立保留入口并使用相同七项数据,逐个检查点位与轴映射。方向变化不会自动让不等间隔时间数据获得正确尺度。
配置、方法与有效条件
| 字段/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 | 事件、模式、条件及最终位置选项 | 活动元素数组;依真实命中条件 |
本章参数与分支
| 属性 | 本例值 | 作用/边界 |
|---|---|---|
| fill | false | 不填线下区域 |
| tension | 0.1 | 视觉插值,不是观测中间值 |
| pointStyle | circle | 点轮廓 |
| pointRadius | 6 | 正常显示像素半径 |
| pointHoverRadius | 10 | 实际活动点半径另验 |
| backgroundColor | rgba颜色 | 与fill及点样式共同解释 |
| interaction.intersect | false | 非仅精确落点才交互 |
| scales.*.title | display / text | 转置后类别y与数值x标题对应 |
本章独立核心源码与验证步骤
import Chart from 'chart.js/auto';
import {mount} from './lab.js';
// 本章配置与实际运行共用;独立虚构数据。
const specification = {
"charts": [
{
"id": "line-basic",
"title": "基础折线",
"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": "line-enhanced",
"title": "点样式和交互",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "rgba(179,0,33,.5)",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1,
"pointStyle": "circle",
"pointRadius": 6,
"pointHoverRadius": 10
}
]
},
"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": "次数"
}
}
},
"interaction": {
"intersect": false
}
}
},
"input_kind": "element"
},
{
"id": "line-transposed",
"title": "类别沿纵轴",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "rgba(179,0,33,.5)",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1,
"pointStyle": "circle",
"pointRadius": 6,
"pointHoverRadius": 10
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
},
"y": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
}
},
"interaction": {
"intersect": false
},
"indexAxis": "y"
}
},
"input_kind": "element"
}
],
"key": "chartjs-line",
"title": "Chart.js 折线图:点样式、交互及转置坐标"
};
export default function run(){mount(Chart,specification);}
所需环境:Chart.js4.6.1 / Edge156.0.4314.58;显式DPR2;实际Canvas及程序化事件
当前源码 SHA256:ce8f38e538a8fedd8d91fa0e38707aac34b729b96bd1261644712c323856fc27
- 本章各图真实绘制,元数据点位/半径/弧角与数值映射相符
- 修改数据使Canvas像素和表格都变化;容器实际响应至360px
- 同Canvas占用被拒绝,销毁释放绑定,恢复创建新实例
点样式和交互:独立变体
import Chart from 'chart.js/auto';
import {mount} from './lab.js';
// 本章配置与实际运行共用;独立虚构数据。
const specification = {
"charts": [
{
"id": "line-basic",
"title": "基础折线",
"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": "line-enhanced",
"title": "点样式和交互",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "rgba(179,0,33,.5)",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1,
"pointStyle": "circle",
"pointRadius": 6,
"pointHoverRadius": 10
}
]
},
"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": "次数"
}
}
},
"interaction": {
"intersect": false
}
}
},
"input_kind": "element"
},
{
"id": "line-transposed",
"title": "类别沿纵轴",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "rgba(179,0,33,.5)",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1,
"pointStyle": "circle",
"pointRadius": 6,
"pointHoverRadius": 10
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
},
"y": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
}
},
"interaction": {
"intersect": false
},
"indexAxis": "y"
}
},
"input_kind": "element"
}
],
"key": "chartjs-line",
"title": "Chart.js 折线图:点样式、交互及转置坐标"
};
export default function run(){mount(Chart,specification);}
所需环境:Chart.js4.6.1 / Edge156.0.4314.58;显式DPR2;实际Canvas及程序化事件
当前源码 SHA256:ce8f38e538a8fedd8d91fa0e38707aac34b729b96bd1261644712c323856fc27
- 对应图形真实渲染,各元素数量与坐标映射正确
类别沿纵轴:独立变体
import Chart from 'chart.js/auto';
import {mount} from './lab.js';
// 本章配置与实际运行共用;独立虚构数据。
const specification = {
"charts": [
{
"id": "line-basic",
"title": "基础折线",
"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": "line-enhanced",
"title": "点样式和交互",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "rgba(179,0,33,.5)",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1,
"pointStyle": "circle",
"pointRadius": 6,
"pointHoverRadius": 10
}
]
},
"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": "次数"
}
}
},
"interaction": {
"intersect": false
}
}
},
"input_kind": "element"
},
{
"id": "line-transposed",
"title": "类别沿纵轴",
"config": {
"type": "line",
"data": {
"labels": [
"第1月",
"第2月",
"第3月",
"第4月",
"第5月",
"第6月",
"第7月"
],
"datasets": [
{
"label": "蓝色天空网:练习次数",
"data": [
18,
31,
24,
42,
35,
50,
47
],
"backgroundColor": "rgba(179,0,33,.5)",
"borderColor": "#2563eb",
"borderWidth": 1,
"fill": false,
"tension": 0.1,
"pointStyle": "circle",
"pointRadius": 6,
"pointHoverRadius": 10
}
]
},
"options": {
"responsive": true,
"maintainAspectRatio": false,
"animation": false,
"devicePixelRatio": 2,
"plugins": {
"title": {
"display": true,
"text": "蓝色天空网 · 虚构学习数据"
}
},
"scales": {
"x": {
"type": "linear",
"beginAtZero": true,
"title": {
"display": true,
"text": "次数"
}
},
"y": {
"type": "category",
"title": {
"display": true,
"text": "学习日期"
}
}
},
"interaction": {
"intersect": false
},
"indexAxis": "y"
}
},
"input_kind": "element"
}
],
"key": "chartjs-line",
"title": "Chart.js 折线图:点样式、交互及转置坐标"
};
export default function run(){mount(Chart,specification);}
所需环境:Chart.js4.6.1 / Edge156.0.4314.58;显式DPR2;实际Canvas及程序化事件
当前源码 SHA256:ce8f38e538a8fedd8d91fa0e38707aac34b729b96bd1261644712c323856fc27
- 对应图形真实渲染,各元素数量与坐标映射正确
常见错误与边界
- 只看type、颜色配置而未检查实际图元和像素。
- 旧版本选项、类别/数值轴或比例含义混淆。
练习与解答
改变一处数据或显示条件,先预测图形与文字变化,再观察实际点位和更新。
展开参考解答
按类型分别检查坐标映射、半径或角度,核对文字表、真实像素和生命周期;真人键盘、读屏、触屏和全事实仍需独立复核。