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

Chart.js 折线图:点样式、交互及转置坐标

保留基础、增强与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选项命名空间分开
responsiveboolean,默认true需要独立有尺寸的容器
maintainAspectRatioboolean,默认truefalse表示不保持宽高比
devicePixelRatio本例显式2渲染缓冲像素与CSS显示尺寸不同
chart.update(mode)数据或选项改变后重绘void;none使本例立即更新
chart.destroy()释放事件/观察器及关联void;重建前调用
Chart.getChart(item)查已绑定实例Chart或undefined
getDatasetMeta(index)取绘图元数据调试/测试不替代业务数据模型
getElementsAtEventForMode事件、模式、条件及最终位置选项活动元素数组;依真实命中条件

本章参数与分支

属性本例值作用/边界
fillfalse不填线下区域
tension0.1视觉插值,不是观测中间值
pointStylecircle点轮廓
pointRadius6正常显示像素半径
pointHoverRadius10实际活动点半径另验
backgroundColorrgba颜色与fill及点样式共同解释
interaction.intersectfalse非仅精确落点才交互
scales.*.titledisplay / text转置后类别y与数值x标题对应

本章独立核心源码与验证步骤

javascript · 阅读源码
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.js 折线图:点样式、交互及转置坐标:真实Canvas实验

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

当前源码 SHA256:ce8f38e538a8fedd8d91fa0e38707aac34b729b96bd1261644712c323856fc27

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

点样式和交互:独立变体

javascript · 阅读源码
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

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

类别沿纵轴:独立变体

javascript · 阅读源码
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、颜色配置而未检查实际图元和像素。
  • 旧版本选项、类别/数值轴或比例含义混淆。

练习与解答

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

展开参考解答

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

规范与官方依据