2026/1/12 7:28:12
网站建设
项目流程
寻找网站建设公司,企业网站建设公司价格,网站过期就可以抢注,网站编辑是网页制作么Highcharts 动态图#xff08;实时更新图表#xff09;详解
Highcharts 的动态图#xff08;也称为实时图表#xff09;是其最受欢迎的功能之一#xff0c;能够在页面不刷新的情况下#xff0c;通过 JavaScript 动态添加、修改或删除数据点#xff0c;并带有平滑动画效…Highcharts 动态图实时更新图表详解Highcharts 的动态图也称为实时图表是其最受欢迎的功能之一能够在页面不刷新的情况下通过 JavaScript 动态添加、修改或删除数据点并带有平滑动画效果。非常适合实时监控系统CPU、内存、温度、流量等股票/加密货币价格走势IoT 传感器数据展示网站访问统计任何需要持续更新数据的场景核心动态更新方法方法用途示例代码series.addPoint(point, redraw?, shift?, animation?)添加单个新数据点series.addPoint([x, y], true, true, true)series.setData(data, redraw?)替换整个系列数据series.setData(新数组)point.update(value, redraw?)更新单个现有数据点points[0].update(新值)chart.addSeries(options)添加新系列chart.addSeries({name: 新系列, data: [...]})series.remove(redraw?)删除系列chart.series[0].remove()chart.update(options)批量修改图表配置如切换类型chart.update({chart: {type: column}})1. 经典实时折线图示例每秒更新自动滚动!DOCTYPEhtmlhtmllangzhheadmetacharsetUTF-8titleHighcharts 实时动态图示例/titlescriptsrchttps://code.highcharts.com/highcharts.js/script/headbodydividcontainerstylewidth:900px;height:500px;margin:50px auto;/divscriptHighcharts.chart(container,{chart:{type:spline,// 推荐 spline 或 areaspline曲线更平滑animation:Highcharts.svg,marginRight:10,events:{load:function(){constseriesthis.series[0];setInterval(function(){constx(newDate()).getTime();// 当前时间戳constyMath.random()*8020;// 模拟随机数据series.addPoint([x,y],true,true,true);// 参数说明// [x, y] : 新数据点// true : 立即重绘// true : shift - 超出显示范围时自动删除最旧点// true : 开启动画},1000);// 每秒更新一次}}},time:{useUTC:false},// 使用本地时间title:{text:实时数据监控仪表盘},subtitle:{text:每秒自动更新},xAxis:{type:datetime,tickPixelInterval:150,labels:{format:{value:%H:%M:%S}}},yAxis:{title:{text:数值},min:0,max:120,plotLines:[{value:80,color:#ff0000,width:2,dashStyle:Dash,label:{text:警戒线}}]},tooltip:{formatter:function(){returnbHighcharts.dateFormat(%Y-%m-%d %H:%M:%S,this.x)/bbr/数值: bthis.y.toFixed(2)/b;}},legend:{enabled:false},exporting:{enabled:false},series:[{name:实时数据,color:#7cb5ec,data:(function(){// 初始化显示最近 30 个点constdata[];consttime(newDate()).getTime();for(leti-29;i0;i1){data.push({x:timei*1000,y:Math.random()*8020});}returndata;}())}]});/script/body/html2. 多系列实时更新常见监控场景load:function(){constchartthis;setInterval(function(){constx(newDate()).getTime();chart.series[0].addPoint([x,Math.random()*100],true,true);chart.series[1].addPoint([x,Math.random()*6020],true,true);chart.series[2].addPoint([x,Math.random()*4040],true,true);},1500);}3. 从后端接口获取真实数据Ajax 示例setInterval(function(){fetch(/api/sensor-data)// 替换为你的真实接口.then(responseresponse.json()).then(data{constx(newDate()).getTime();chart.series[0].addPoint([x,data.temperature],true,true);chart.series[1].addPoint([x,data.humidity],true,true);});},5000);// 每5秒请求一次4. 其他实用技巧动态切换图表类型按钮切换折线 ↔ 柱状document.getElementById(switchBtn).addEventListener(click,function(){constnewTypechart.options.chart.typespline?column:spline;chart.update({chart:{type:newType}});});性能优化建议大数据量时引入 Boost 模块支持百万级点scriptsrchttps://code.highcharts.com/modules/boost.js/script使用shift: true限制显示点数关闭动画animation: false5. 推荐实时图表类型spline/areaspline最常用视觉平滑line简单高效column柱子生长动画很酷scatter/bubble适合轨迹追踪gauge仪表盘需额外模块Highcharts 的动态更新性能极佳几乎所有图表类型都支持实时动画是构建专业仪表盘的首选。如果你有具体需求比如多条曲线实时监控WebSocket 实时推送股票K线实时图Highcharts Stock在 Vue/React 中封装动态组件告诉我你的应用场景我可以提供完整可运行的代码示例