Highcharts仪表:角度、实心、时钟和双窗格
分开配置pane、轴、单位、数值更新与定时器。
学完这一章,你能够
- 分开配置pane、轴、单位、数值更新与定时器。
- 角度仪表与分段背景
坐标映射和模块
gauge依赖more,solidgauge还需要专用模块;常见拼写guage/solidguage是错误。pane规定角度和背景,yAxis规定值域,series的点驱动指针或进度弧。
角度仪表、双实心仪表、UTC时钟、双单位速度表、双窗格VU各自有独立入口;两套轴共享物理量时必须精确换算,不能仅靠看起来对齐。
更新不是真实硬件
本轮数值均为模拟,不访问麦克风或设备。单步、周期更新、停止及销毁分别观察,定时器句柄按对象保存,重复开始先清理旧句柄。
时钟每次取当前UTC日期,小时mod12并考虑分秒,跨日及停止另验。工程的模拟仪表不能当音频采集、车载传感器或生产安全告警已经开发完成。
角度仪表与分段背景
正确类型是gauge,不是guage。highcharts-more提供仪表,pane起止角和yAxis的0..200值域共同映射指针角度;三段plotBands独立显示阈值。
按钮可以单步或按300毫秒启动本站模拟更新,数值不超值域。定时器不是传感器输入;停止及销毁清理各自句柄,导出/重建不重复启动后台循环。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。

两套实心仪表
正确类型为solidgauge,先加载核心、more及solid-gauge模块。两张图分别保留0..200和0..5的值域;pane背景弧、stops阈值色及HTML数值标签各有作用。
本版本默认刻度取整会把max5扩成6,例子显式endOnTick:false保持5。两个实例分别更新,数字缩放单位与实际标签一致,不把倍率冒称实际转速设备。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。

时钟、跨日与生命周期
gauge时钟把一周映射为0..12,时针用UTC小时mod12加分秒,分针与秒针也按12刻度换算。三个点用稳定id逐个更新,回调先定义后传递。
使用当前Date重新取时而不是每次tick累加秒,减少漂移误差;23:59:59到00:00:00真实核对三针归零。本站明确UTC,停止清理句柄,不依赖jQuery easing或未赋值回调。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。

双单位同一物理量
两个圆弧刻度使用同一pane,第一轴km/h,第二轴mph。使用1mile=1.609344km精确换算,第二轴max设为200/1.609344而不是近似124。
标签同时显示80.0km/h和49.7mph,模拟更新保持值域。两套刻度要指向同一物理角度,不能把独立任意范围画成看似换算一致。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。

双窗格和独立轴
两个pane左右分布,每个yAxis指定自己的pane,每条series绑定对应轴。数值范围-20..6,0..6有红色警示区,指针长度与背景分别设置。
本站用确定的模拟序列更新两个通道并只重画一次,不申请麦克风。检查两通道实际变化及边界,停止和销毁都需清理句柄;仪表外观不代表音频测量已经实现。
本站本例完成对应操作后的实际截图,与本例代码和状态绑定,不计为来源原图。

运行代码文件:gauges.js
下列代码与本章独立工程实际脚本一致;容器、本地数据和模块随工程包保留。
"use strict";
window.mbskyErrors=[];window.addEventListener("error",e=>mbskyErrors.push(e.message));window.addEventListener("unhandledrejection",e=>mbskyErrors.push(String(e.reason)));
const plots={},timers=new Map(),steps=new Map();const base={chart:{animation:false},title:{text:"蓝色天空网 · mbsky.com"},subtitle:{text:"本站虚构教学数据"},plotOptions:{series:{animation:false}},colors:["#2563eb","#f97316","#10b981","#8b5cf6"],accessibility:{enabled:true}};
function make(id,opts){return plots[id]=Highcharts.chart(id,Highcharts.merge(base,opts));}
const o3={enabled:true,alpha:15,beta:15,depth:50,viewDistance:25};
make("column3d",{chart:{type:"column",options3d:o3,margin:70},plotOptions:{column:{depth:25}},series:[{name:"练习",data:[3,5,7,6,9,10,12,8,9,7,6,5]}]});
for(const key of ["alpha","beta"])document.querySelector(`#${key}`).oninput=e=>{const value=Number(e.target.value);plots.column3d.update({chart:{options3d:{[key]:value}}},true,false);document.querySelector(`#${key}-value`).textContent=String(value);};
make("null3d",{chart:{type:"column",options3d:{...o3,alpha:10,beta:25,depth:70},margin:70},xAxis:{categories:Array.from({length:10},(_,i)=>`阶段${i+1}`)},series:[{name:"练习",data:[2,3,null,4,0,5,1,4,6,3]}]});
make("stack3d",{chart:{type:"column",options3d:{...o3,depth:40},marginTop:70,marginRight:40},xAxis:{categories:["一","二","三","四","五"]},yAxis:{min:0,allowDecimals:false},tooltip:{pointFormat:"{series.name}:{point.y} / 本组{point.stackTotal}"},plotOptions:{column:{stacking:"normal",depth:40}},series:[{name:"甲一",stack:"甲",data:[5,3,4,7,2]},{name:"甲二",stack:"甲",data:[3,4,4,2,5]},{name:"乙一",stack:"乙",data:[2,5,6,2,1]},{name:"乙二",stack:"乙",data:[3,0,4,4,3]}]});
const pie=()=>[40,25,15,10,7,3].map((y,i)=>({name:`练习${i+1}`,y,...(i===2?{selected:true,sliced:true}:{})}));
make("pie3d",{chart:{type:"pie",options3d:{enabled:true,alpha:45,beta:0}},plotOptions:{pie:{depth:35,allowPointSelect:true,cursor:"pointer",dataLabels:{enabled:true,format:"{point.name}"}}},series:[{name:"练习组成",data:pie()}]});
make("donut3d",{chart:{type:"pie",options3d:{enabled:true,alpha:45}},plotOptions:{pie:{innerSize:100,depth:45}},series:[{name:"练习组成",data:pie()}]});
const pane={startAngle:-150,endAngle:150,background:[{backgroundColor:{linearGradient:{x1:0,y1:0,x2:0,y2:1},stops:[[0,"#fff"],[1,"#526078"]]},borderWidth:0,outerRadius:"109%"},{borderWidth:1,outerRadius:"107%"},{backgroundColor:"#eef4ff"},{backgroundColor:"#dce6f3",borderWidth:0,outerRadius:"105%",innerRadius:"103%"}]};
make("gauge",{chart:{type:"gauge"},pane,yAxis:{min:0,max:200,minorTickInterval:"auto",minorTickLength:10,minorTickPosition:"inside",tickLength:10,tickPosition:"inside",labels:{step:2,rotation:"auto"},title:{text:"虚构练习量"},plotBands:[{from:0,to:120,color:"#55bf3b"},{from:120,to:160,color:"#dddf0d"},{from:160,to:200,color:"#df5353"}]},series:[{name:"虚构练习量",data:[80]}]});
for(const [id,max,y,unit]of [["solid-speed",200,80,"练习量"],["solid-rpm",5,1,"虚构倍率"]])make(id,{chart:{type:"solidgauge"},pane:{center:["50%","80%"],size:"130%",startAngle:-90,endAngle:90,background:{backgroundColor:"#eef4ff",innerRadius:"60%",outerRadius:"100%",shape:"arc"}},tooltip:{enabled:false},yAxis:{min:0,max,endOnTick:false,startOnTick:false,stops:[[.1,"#55bf3b"],[.5,"#dddf0d"],[.9,"#df5353"]],lineWidth:0,minorTickInterval:null,tickWidth:0,tickPixelInterval:400,title:{text:unit,y:-60},labels:{y:16}},plotOptions:{solidgauge:{dataLabels:{borderWidth:0,y:5,useHTML:true}}},series:[{name:unit,data:[y],dataLabels:{format:`<div style="text-align:center"><strong>{y:.1f}</strong><br>${unit}</div>`}}]});
function clockValues(date){return {hour:date.getUTCHours()%12+date.getUTCMinutes()/60+date.getUTCSeconds()/3600,minute:date.getUTCMinutes()/5+date.getUTCSeconds()/300,second:date.getUTCSeconds()/5,text:date.toISOString().slice(11,19)+" UTC"};}
make("clock",{chart:{type:"gauge"},pane:{startAngle:0,endAngle:360,background:[{}, {backgroundColor:{radialGradient:{cx:.5,cy:-.4,r:1.9},stops:[[.5,"rgba(255,255,255,.2)"],[.5,"rgba(200,200,200,.2)"]]}}]},yAxis:{min:0,max:12,tickInterval:1,lineWidth:0,showFirstLabel:false,minorTickInterval:"auto",minorTickLength:5,labels:{distance:-20},title:{text:"蓝色天空网 · UTC",y:10}},tooltip:{formatter(){return document.querySelector("#clock-time").textContent;}},series:[{name:"UTC时钟",data:[{id:"hour",y:0,dial:{radius:"60%",baseWidth:4,baseLength:"95%",rearLength:0}},{id:"minute",y:0,dial:{baseLength:"95%",rearLength:0}},{id:"second",y:0,dial:{radius:"100%",baseWidth:1,rearLength:"20%"}}],dataLabels:{enabled:false},animation:false}]});
function setClock(date){const v=clockValues(date);for(const key of ["hour","minute","second"])plots.clock.get(key).update(v[key],false,false);plots.clock.redraw(false);document.querySelector("#clock-time").textContent=v.text;return v;}
function stop(key){if(timers.has(key))clearInterval(timers.get(key));timers.delete(key);document.querySelector("#timer-status").textContent=`活动定时器:${timers.size}`;}
function start(key,fn,period){stop(key);timers.set(key,setInterval(fn,period));document.querySelector("#timer-status").textContent=`活动定时器:${timers.size}`;}
document.querySelector("#clock-live").onclick=()=>{setClock(new Date());start("clock",()=>setClock(new Date()),1000);};document.querySelector("#clock-stop").onclick=()=>stop("clock");document.querySelector("#clock-boundary").onclick=()=>{stop("clock");setClock(new Date("2026-01-01T23:59:59Z"));};document.querySelector("#clock-wrap").onclick=()=>{stop("clock");setClock(new Date("2026-01-02T00:00:00Z"));};setClock(new Date());
const KM_PER_MILE=1.609344;
make("dual-gauge",{chart:{type:"gauge"},pane:{startAngle:-150,endAngle:150},yAxis:[{min:0,max:200,endOnTick:false,lineColor:"#2563eb",lineWidth:2,offset:-25,labels:{distance:-20,rotation:"auto"},title:{text:"km/h"}},{min:0,max:200/KM_PER_MILE,endOnTick:false,lineColor:"#e11d48",lineWidth:2,tickPosition:"outside",minorTickPosition:"outside",offset:-20,labels:{distance:12,rotation:"auto"},title:{text:"mph"}}],series:[{name:"虚构速度",data:[80],dataLabels:{formatter(){return `${this.y.toFixed(1)} km/h<br>${(this.y/KM_PER_MILE).toFixed(1)} mph`;}}}]});
make("vu",{chart:{type:"gauge",height:350,plotBorderWidth:1,plotBackgroundColor:{linearGradient:{x1:0,y1:0,x2:0,y2:1},stops:[[0,"#fff4c6"],[.3,"white"],[1,"#fff4c6"]]}},pane:[{startAngle:-45,endAngle:45,background:null,center:["25%","100%"],size:270},{startAngle:-45,endAngle:45,background:null,center:["75%","100%"],size:270}],yAxis:[0,1].map(i=>({min:-20,max:6,pane:i,tickPosition:"outside",minorTickPosition:"outside",labels:{rotation:"auto",distance:20},plotBands:[{from:0,to:6,color:"#c02316",innerRadius:"100%",outerRadius:"105%"}],title:{text:`VU · ${i?"B":"A"}(模拟)`,y:-40}})),plotOptions:{gauge:{dataLabels:{enabled:false},dial:{radius:"100%"}}},series:[{name:"通道A",data:[-20],yAxis:0},{name:"通道B",data:[-20],yAxis:1}]});
function tick(key){const c=plots[key];if(!c?.series){stop(key);return;}const i=(steps.get(key)||0)+1;steps.set(key,i);if(key==="vu"){c.series[0].points[0].update(-20+(i*2)%27,false,false);c.series[1].points[0].update(-20+(i*3)%27,false,false);}else{const max=c.yAxis[0].max;c.series[0].points[0].update((i*(max===5?.2:7))%max,false,false);}c.redraw(false);}
const moving=["gauge","solid-speed","solid-rpm","dual-gauge","vu"];
document.querySelector("#start").onclick=()=>moving.forEach(key=>{if(plots[key]?.series)start(key,()=>tick(key),300);});document.querySelector("#stop").onclick=()=>moving.forEach(stop);document.querySelector("#step").onclick=()=>moving.forEach(tick);document.querySelector("#gauge-destroy").onclick=()=>{stop("gauge");plots.gauge.destroy();document.querySelector("#gauge").textContent="此仪表已销毁;重新打开页面可恢复。";};window.addEventListener("pagehide",()=>[...timers.keys()].forEach(stop));
window.mbskyGauges={plots,timers,steps,start,stop,tick,clockValues,setClock,KM_PER_MILE};
仪表配置
| 配置或接口 | 类型/写法 | 用途与准确条件 | 本例或迁移说明 |
|---|---|---|---|
| gauge / solidgauge | 准确类型字符串 | 指针 / 实心进度弧 | more / more+solid-gauge |
| pane | 起止角、中心、大小及背景 | 规定圆弧位置 | 多轴用pane索引 |
| yAxis.min / max / endOnTick | 数值 / 数值 / boolean | 值域及刻度取整 | max5需防被取整到6 |
| stops / plotBands | 比例颜色 / 数值区间 | 实心阈值色 / 背景分段 | 两者不同 |
| Clock hour/minute/second | 0..12数值 | UTC时钟三针 | mod12和跨日分别核对 |
本章独立核心源码与验证步骤
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>角度仪表与分段背景 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-gauge" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>角度仪表与分段背景</h1><p>正确类型是gauge,不是guage。highcharts-more提供仪表,pane起止角和yAxis的0..200值域共同映射指针角度;三段plotBands独立显示阈值。</p><iframe title="角度仪表与分段背景的实际运行图表" src="../gauges.html#case-gauge" style="width:100%;height:650px;border:0"></iframe><p><a href="../gauges.html#case-gauge" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
所需环境:Windows/Edge156独立会话,Highcharts13.1.1;具体命名检查已有记录,逐来源语义仍待核验
当前源码 SHA256:4a5548773b9e492bcb3ca014c4ddc30e920ffc5537b3b348e9ed2356d1a58864
- 正确类型是gauge,不是guage。highcharts-more提供仪表,pane起止角和yAxis的0..200值域共同映射指针角度;三段plotBands独立显示阈值。
- 按钮可以单步或按300毫秒启动本站模拟更新,数值不超值域。定时器不是传感器输入;停止及销毁清理各自句柄,导出/重建不重复启动后台循环。
两套实心仪表
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>两套实心仪表 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-solid" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>两套实心仪表</h1><p>正确类型为solidgauge,先加载核心、more及solid-gauge模块。两张图分别保留0..200和0..5的值域;pane背景弧、stops阈值色及HTML数值标签各有作用。</p><iframe title="两套实心仪表的实际运行图表" src="../gauges.html#case-solid" style="width:100%;height:650px;border:0"></iframe><p><a href="../gauges.html#case-solid" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
所需环境:Windows/Edge156独立会话,Highcharts13.1.1;具体命名检查已有记录,逐来源语义仍待核验
当前源码 SHA256:5cb887c4c5a1b0989f01aa0bd9bd1743a2a4b91207b65a7a4990c37d86409f80
- 正确类型为solidgauge,先加载核心、more及solid-gauge模块。两张图分别保留0..200和0..5的值域;pane背景弧、stops阈值色及HTML数值标签各有作用。
- 本版本默认刻度取整会把max5扩成6,例子显式endOnTick:false保持5。两个实例分别更新,数字缩放单位与实际标签一致,不把倍率冒称实际转速设备。
时钟、跨日与生命周期
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>时钟、跨日与生命周期 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-clock" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>时钟、跨日与生命周期</h1><p>gauge时钟把一周映射为0..12,时针用UTC小时mod12加分秒,分针与秒针也按12刻度换算。三个点用稳定id逐个更新,回调先定义后传递。</p><iframe title="时钟、跨日与生命周期的实际运行图表" src="../gauges.html#case-clock" style="width:100%;height:650px;border:0"></iframe><p><a href="../gauges.html#case-clock" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
所需环境:Windows/Edge156独立会话,Highcharts13.1.1;具体命名检查已有记录,逐来源语义仍待核验
当前源码 SHA256:7be7f4d9c944595859e9db4a166398d2ef44b8ec9300379398e749ac2d3894fd
- gauge时钟把一周映射为0..12,时针用UTC小时mod12加分秒,分针与秒针也按12刻度换算。三个点用稳定id逐个更新,回调先定义后传递。
- 使用当前Date重新取时而不是每次tick累加秒,减少漂移误差;23:59:59到00:00:00真实核对三针归零。本站明确UTC,停止清理句柄,不依赖jQuery easing或未赋值回调。
双单位同一物理量
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>双单位同一物理量 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-dual-gauge" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>双单位同一物理量</h1><p>两个圆弧刻度使用同一pane,第一轴km/h,第二轴mph。使用1mile=1.609344km精确换算,第二轴max设为200/1.609344而不是近似124。</p><iframe title="双单位同一物理量的实际运行图表" src="../gauges.html#case-dual-gauge" style="width:100%;height:650px;border:0"></iframe><p><a href="../gauges.html#case-dual-gauge" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
所需环境:Windows/Edge156独立会话,Highcharts13.1.1;具体命名检查已有记录,逐来源语义仍待核验
当前源码 SHA256:350046bc379706c48c105a4cf7d076452b759d6c22b3a679e59f56c8120688f7
- 两个圆弧刻度使用同一pane,第一轴km/h,第二轴mph。使用1mile=1.609344km精确换算,第二轴max设为200/1.609344而不是近似124。
- 标签同时显示80.0km/h和49.7mph,模拟更新保持值域。两套刻度要指向同一物理角度,不能把独立任意范围画成看似换算一致。
双窗格和独立轴
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="robots" content="noindex,nofollow"><title>双窗格和独立轴 | 蓝色天空网 mbsky.com</title><link rel="stylesheet" href="../bootstrap.css"><script defer src="../lesson-view.js"></script></head><body data-section="case-vu" data-mode=""><header>蓝色天空网 · mbsky.com</header><main><h1>双窗格和独立轴</h1><p>两个pane左右分布,每个yAxis指定自己的pane,每条series绑定对应轴。数值范围-20..6,0..6有红色警示区,指针长度与背景分别设置。</p><iframe title="双窗格和独立轴的实际运行图表" src="../gauges.html#case-vu" style="width:100%;height:650px;border:0"></iframe><p><a href="../gauges.html#case-vu" target="_blank" rel="noopener">新窗口打开完整实验</a></p></main></body></html>
所需环境:Windows/Edge156独立会话,Highcharts13.1.1;具体命名检查已有记录,逐来源语义仍待核验
当前源码 SHA256:52ba31ad828fb2b94e19fd468de504b7f207850496fb5546d9ed4db80a12acde
- 两个pane左右分布,每个yAxis指定自己的pane,每条series绑定对应轴。数值范围-20..6,0..6有红色警示区,指针长度与背景分别设置。
- 本站用确定的模拟序列更新两个通道并只重画一次,不申请麦克风。检查两通道实际变化及边界,停止和销毁都需清理句柄;仪表外观不代表音频测量已经实现。
常见错误与边界
- 把配置名、SVG节点或成功加载当成全部教学行为。
- 把本站合成数据、重写实例及自制图称为原始采集成功。
练习与解答
改变本例一个有效值或边界,先写出单位、状态和图形预期,再独立运行核对。
展开参考解答
分别核对数据、轴/系列映射、可见几何、交互和资源清理,不只看退出码。