当前位置: 首页 > news >正文

多维度图表:带自定义入场动画的折线图|Highcharts 代码示列

这个折线图演示了如何用自定义入口动画展示与美国通货膨胀相关的统计数据。

这是一段基于 Highcharts 实现的高级图表动画定制代码,核心功能是给折线图、坐标轴、刻度标签、绘图线添加自定义入场动画,让图表加载更有视觉层次感。

(function (H) { const animateSVGPath = (svgElem, animation, callback = void 0) => { const length = svgElem.element.getTotalLength(); svgElem.attr({ 'stroke-dasharray': length, 'stroke-dashoffset': length, opacity: 1 }); svgElem.animate({ 'stroke-dashoffset': 0 }, animation, callback); }; H.seriesTypes.line.prototype.animate = function (init) { const series = this, animation = H.animObject(series.options.animation); if (!init) { animateSVGPath(series.graph, animation); } }; H.addEvent(H.Axis, 'afterRender', function () { const axis = this, chart = axis.chart, animation = H.animObject(chart.renderer.globalAnimation); axis.axisGroup // Init .attr({ opacity: 0, rotation: -3, scaleY: 0.9 }) // Animate .animate({ opacity: 1, rotation: 0, scaleY: 1 }, animation); if (axis.horiz) { axis.labelGroup // Init .attr({ opacity: 0, rotation: 3, scaleY: 0.5 }) // Animate .animate({ opacity: 1, rotation: 0, scaleY: 1 }, animation); } else { axis.labelGroup // Init .attr({ opacity: 0, rotation: 3, scaleX: -0.5 }) // Animate .animate({ opacity: 1, rotation: 0, scaleX: 1 }, animation); } if (axis.plotLinesAndBands) { axis.plotLinesAndBands.forEach(plotLine => { const animation = H.animObject(plotLine.options.animation); // Init plotLine.label.attr({ opacity: 0 }); // Animate animateSVGPath( plotLine.svgElem, animation, function () { plotLine.label.animate({ opacity: 1 }); } ); }); } }); }(Highcharts)); Highcharts.chart('container', { chart: { type: 'spline' }, title: { text: 'United States of America\'s Inflation-related statistics' }, subtitle: { text: 'Source: <a href="https://www.worldbank.org/en/home">The World Bank</a>' }, data: { csv: document.getElementById('csv').innerHTML }, yAxis: [{ title: { text: 'Inflation' }, plotLines: [{ color: 'var(--highcharts-neutral-color-100, black)', width: 2, value: 13.5492019749684, animation: { duration: 1000, defer: 4000 }, label: { text: 'Max Inflation', align: 'right', x: -20 } }] }, { title: { text: 'Claims on central government, etc.' } }, { opposite: true, title: { text: 'Net foreign assets' } }, { opposite: true, title: { text: 'Net domestic credit' } }], plotOptions: { series: { animation: { duration: 1000 }, marker: { enabled: false }, lineWidth: 2 } }, series: [{ yAxis: 0 }, { yAxis: 1, animation: { defer: 1000 } }, { yAxis: 2, animation: { defer: 2000 } }, { yAxis: 3, animation: { defer: 3000 } }], responsive: { rules: [{ condition: { maxWidth: 500 }, chartOptions: { yAxis: [{ tickAmount: 2, title: { x: 15, reserveSpace: false } }, { tickAmount: 2, title: { x: 20, reserveSpace: false } }, { tickAmount: 2, title: { x: -20, reserveSpace: false } }, { tickAmount: 2, title: { x: -20, reserveSpace: false } }] } }] } });

demo链接:演示地址

核心功能拆解

1. 自定义 SVG 路径动画函数animateSVGPath

这是通用工具函数,实现线条描边绘制动画(像手写一样逐渐显示):

  • 获取 SVG 路径总长度
  • 设置stroke-dasharraystroke-dashoffset让线条初始隐藏
  • 动画将偏移量归零,实现线条逐步绘制
  • 支持动画完成回调

2. 重写折线图动画line.prototype.animate

覆盖 Highcharts 默认的折线动画,让所有折线 / 曲线使用手写描边动画,替代默认的拉伸动画。

3. 坐标轴渲染后动画Axis afterRender

坐标轴、刻度标签添加入场动画:

  • 初始状态:透明、轻微旋转、缩放
  • 动画:渐显 + 旋转复位 + 缩放复位
  • 区分水平 / 垂直坐标轴,给标签不同的动画效果

4. 绘图线(plotLines)动画

给 Y 轴的最大值标记线添加动画:

  • 线条先执行手写绘制动画
  • 动画完成后,标签渐显
  • 支持自定义延迟、时长

5. 图表配置

  • 多 Y 轴图表(4 个 Y 轴)
  • 系列依次延迟动画(0/1000/2000/3000ms)
  • 隐藏数据标记、统一线条宽度
  • 响应式适配小屏幕设备
  • 绑定 CSV 数据源
http://www.jsqmd.com/news/719636/

相关文章:

  • 2026年遵义央国企笔试面试培训机构优选 专注本土考情且服务有保障 - 深度智识库
  • 三步构建企业级开源CRM系统:EspoCRM全栈部署实战
  • QLVideo:深度解析macOS非原生视频格式的终极预览解决方案
  • 别再为mxnet安装报错头疼了!手把手教你用conda虚拟环境搞定版本兼容
  • 魔兽争霸3终极优化工具:WarcraftHelper 5分钟快速上手指南
  • 什么泥膜清洁毛孔效果好?12天解锁素颜柔光感干净肤质 - 全网最美
  • 南昌好的医疗纠纷代理律师推荐:为何律师的医法双背景更受信赖 - 品牌2025
  • 猫抓Cat-Catch:浏览器资源嗅探扩展的终极免费解决方案
  • 清洁毛孔泥膜哪个牌子好?12天告别面部灰蒙蒙打造原生透光肌 - 全网最美
  • 中国信通院启动“模数共振”行动:构建“高质量数据—高效能模型—高价值应用”良性循环,赋能新型工业化
  • 2026年AI毕业论文工具深度实测|7款AI毕业论文写作工具横评,这款AI领衔毕业安全线 - 逢君学术-AI论文写作
  • 前端性能优化:移动端优化详解
  • Highcharts的不规则间隔的时间数据图表示例|制作时间序列积雪深度对比图
  • 2026年装甲门厂家怎么选?从行业痛点看高端入户门的真正差异 - 企师傅推荐官
  • Barrier终极指南:如何用一套键鼠无缝控制Windows、macOS和Linux三台电脑?[特殊字符]
  • 2026年福田区靠谱GEO优化公司推荐技术实力与服务价值拆解 - 奔跑123
  • 生信总监,为何高薪裸辞?
  • 2026南昌医疗纠纷律师怎么选?医法双背景的律师提供专业代理方案 - 品牌2025
  • Mac Mouse Fix:5分钟让你的普通鼠标在macOS上超越触控板体验
  • 2026年云南昆明曲靖代理记账与工商变更全生命周期企业财税服务深度横评 - 优质企业观察收录
  • 2026年昆明代理记账与工商变更全生命周期服务深度横评——云南企业财税合规一站式解决方案 - 优质企业观察收录
  • 算网LLaMA-Factory镜像:零代码轻松微调百余种大模型
  • 3分钟看懂一个AI概念:卷积到底在干什么?
  • 2026年昆明代理记账与工商变更全生命周期服务深度横评|曲靖企业财税合规指南 - 优质企业观察收录
  • 【官方权威】2026年5月卡地亚售后服务中心地址变更通告:全国45城网点+营业时间全新发布+最新统一维修热线400-106-3365 - 速递信息
  • 别再让中文用户名坑了你!手把手教你解决Keil工程路径报错(附D盘迁移指南)
  • 用Python和NumPy手把手实现数据白化:从协方差矩阵到PCA降维实战
  • CircuitJS1 Desktop Mod:免费离线电路仿真,让电子学习变得简单
  • 保姆级教程:用Python+Kalman滤波手把手实现一个简易的RTK定位引擎
  • 2026年资产管理系统平台合集,国资私有化部署与不动产厂商精选 - 品牌2026