脉冲指标环

数据可视化组件HTML/CSS已复制 0 次

给实时读数用的精密仪表:60 格刻度、渐变进度弧、发光端点和涟漪都只服务于「现在是多少、比刚才如何」这一件事。右侧同时给趋势线与峰值/均值/P95,光效集中在弧的端点,不撒到整张卡上。

参数覆盖文案、单位、量程、实时/固定数值模式、刻度密度、环尺寸、刷新间隔,以及趋势线、统计值、端点光效、涟漪和品牌位的开关。样例数值均实时生成并标注为演示数据。

组件预览右侧改参数,这里和下方代码同步更新

演示页加载中…

演示在 iframe 里独立运行(1280×720 虚拟视口,等比缩放 40%)。滚动类交互要在框内滚动才看得到。

复制代码

「使用示例」跟随右侧参数实时生成;源码与样式是定值

<!-- pulse-metric · 来自 UI Bits 组件库。HTML + 内联脚本直接贴进页面;配套样式见「样式 CSS」Tab。头像为示例素材,替换 AV 数组里的地址即可。 -->
<div class="page">
  <div class="eyebrow">UI Bits · Data Display</div>
  <div class="h1">脉冲指标环 <span>pulse-metric</span></div>
  <div class="stage" id="stage">
    <div class="metric-card" id="card">
      <div class="ring-wrap" id="ringWrap">
        <svg viewBox="0 0 240 240" aria-hidden="true">
          <g class="ring-ticks" id="ticks"></g>
          <circle class="ring-track" cx="120" cy="120" r="96"></circle>
          <g id="ripples"></g>
          <circle class="ring-prog" id="ringProg" cx="120" cy="120" r="96"></circle>
          <circle class="ring-head" id="ringHead" cx="120" cy="24" r="5"></circle>
        </svg>
        <div class="ring-center"><div class="val" id="val">—</div><div class="unit" id="unit">REQ/S</div></div>
      </div>
      <div class="info">
        <div class="eyebrow2">Observability / Live feed</div>
        <h3 id="title">实时吞吐</h3>
        <div class="sub" id="subtitle">过去 30 分钟 · 采集节点 A-12</div>
        <div class="delta" id="delta">▲ 0.0%</div>
        <svg class="spark" viewBox="0 0 320 52" preserveAspectRatio="none" aria-hidden="true"><defs><linearGradient id="sparkFill" x1="0" y1="0" x2="0" y2="1"><stop offset="0%" stop-color="var(--mc)" stop-opacity=".28"></stop><stop offset="100%" stop-color="var(--mc)" stop-opacity="0"></stop></linearGradient></defs><path class="area" id="sparkArea"></path><path class="line" id="sparkLine"></path></svg>
        <div class="mini-stats" id="stats"><div><b id="peak">—</b><span>峰值</span></div><div><b id="avg">—</b><span>均值</span></div><div><b id="p95">—</b><span>P95</span></div></div>
      </div>
    </div>
  </div>
  
  <div class="ub-credit" id="credit"><a href="https://uibits.cn" target="_blank" rel="noopener"><span class="mk">UI Bits</span><span class="hair"></span><span>uibits.cn</span><span>↗</span></a><span>组件来自 UI Bits 组件库</span></div>
</div>
<script>
(function(){
  'use strict'
  var KEY='pulseMetric'
  var SPEC=[
    {key:'title',label:'标题',type:'text',def:'实时吞吐'},
    {key:'subtitle',label:'副标题',type:'text',def:'过去 30 分钟 · 采集节点 A-12'},
    {key:'unit',label:'单位',type:'text',def:'REQ/S'},
    {key:'valueMode',label:'数值模式',type:'select',def:'live',options:['live','fixed']},
    {key:'fixedValue',label:'固定值',type:'range',def:6842,min:0,max:20000,step:1},
    {key:'maxValue',label:'量程上限',type:'range',def:10000,min:1000,max:30000,step:500},
    {key:'accent',label:'主色',type:'color',def:'#5B9DFF'},
    {key:'ticks',label:'刻度数',type:'range',def:60,min:24,max:96,step:6},
    {key:'ringSize',label:'环尺寸',type:'range',def:240,min:180,max:280,step:10},
    {key:'interval',label:'刷新间隔 ms',type:'range',def:1400,min:400,max:4000,step:100},
    {key:'showTrend',label:'趋势线',type:'toggle',def:true},
    {key:'showStats',label:'统计值',type:'toggle',def:true},
    {key:'glow',label:'端点光效',type:'toggle',def:true},
    {key:'ripple',label:'涟漪',type:'toggle',def:true},
    {key:'credit',label:'品牌位',type:'toggle',def:true}
  ]
  var S={};SPEC.forEach(function(s){S[s.key]=s.def})
  try{var q=new URLSearchParams(location.search).get('p');if(q){var o=JSON.parse(q);Object.keys(o).forEach(function(k){if(k in S)S[k]=o[k]})}}catch(e){}
  var reduce=matchMedia('(prefers-reduced-motion: reduce)').matches
  var el=function(id){return document.getElementById(id)}
  /* 在详情页的预览窗里收起自带控件:右侧已经有一套,重复一套只会挤占场地 */
  var ctlBox=el('ctl');if(ctlBox&&window.self!==window.top)ctlBox.style.display='none'
  var card=el('card'),ring=el('ringProg'),head=el('ringHead'),ticks=el('ticks'),ripples=el('ripples'),val=el('val')
  var line=el('sparkLine'),area=el('sparkArea'),delta=el('delta'),stats=el('stats'),hist=[],display=0,anim=null,timer=null
  var C=2*Math.PI*96
  function clamp(v,a,b){return Math.max(a,Math.min(b,v))}
  function buildTicks(){
    ticks.innerHTML=''
    for(var i=0;i<S.ticks;i++){
      var a=i/S.ticks*Math.PI*2,major=i%5===0,r1=major?110:113
      var ln=document.createElementNS('http://www.w3.org/2000/svg','line')
      ln.setAttribute('x1',(120+Math.cos(a)*r1).toFixed(2));ln.setAttribute('y1',(120+Math.sin(a)*r1).toFixed(2))
      ln.setAttribute('x2',(120+Math.cos(a)*118).toFixed(2));ln.setAttribute('y2',(120+Math.sin(a)*118).toFixed(2))
      if(major)ln.setAttribute('class','major');ticks.appendChild(ln)
    }
  }
  function drawSpark(){
    if(!S.showTrend||hist.length<2){line.setAttribute('d','');area.setAttribute('d','');return}
    var w=320,h=52,min=Math.min.apply(null,hist),max=Math.max.apply(null,hist)
    if(max-min<1)max=min+1
    var d=hist.map(function(v,i){return (i?'L':'M')+(i/(hist.length-1)*w).toFixed(1)+' '+(h-4-((v-min)/(max-min))*(h-11)).toFixed(1)}).join(' ')
    line.setAttribute('d',d);area.setAttribute('d',d+' L 320 52 L 0 52 Z')
  }
  function animate(to){
    if(anim)cancelAnimationFrame(anim)
    if(reduce){display=to;val.textContent=Math.round(to).toLocaleString();return}
    var from=display,start=performance.now(),dur=750;
    (function step(now){var k=clamp((now-start)/dur,0,1),e=1-Math.pow(1-k,3)
      display=from+(to-from)*e;val.textContent=Math.round(display).toLocaleString()
      if(k<1)anim=requestAnimationFrame(step)})(start)
  }
  function emit(){
    if(!S.ripple||reduce)return
    var c=document.createElementNS('http://www.w3.org/2000/svg','circle')
    c.setAttribute('class','ripple');c.setAttribute('cx','120');c.setAttribute('cy','120');c.setAttribute('r','96')
    ripples.appendChild(c);setTimeout(function(){c.remove()},1250)
  }
  function refresh(){
    var mx=S.maxValue,v=S.valueMode==='fixed'?S.fixedValue:Math.round(mx*(.42+Math.random()*.5))
    var prev=hist.length?hist[hist.length-1]:v,pct=(v-prev)/prev*100
    hist.push(v);if(hist.length>28)hist.shift()
    drawSpark();animate(v)
    delta.textContent=(pct>=0?'▲ ':'▼ ')+Math.abs(pct).toFixed(1)+'%';delta.className='delta'+(pct<0?' down':'')
    var ratio=clamp(v/mx,0,1)
    ring.style.strokeDashoffset=(C*(1-ratio)).toFixed(1)
    var a=ratio*Math.PI*2-Math.PI/2
    head.setAttribute('cx',(120+Math.cos(a)*96).toFixed(2));head.setAttribute('cy',(120+Math.sin(a)*96).toFixed(2))
    emit()
    el('peak').textContent=Math.round(Math.max.apply(null,hist)).toLocaleString()
    el('avg').textContent=Math.round(hist.reduce(function(a,b){return a+b},0)/hist.length).toLocaleString()
    el('p95').textContent=Math.round(48+Math.random()*30)+' ms'
  }
  function schedule(){if(timer)clearInterval(timer);timer=setInterval(refresh,S.interval)}
  function apply(){
    card.style.setProperty('--mc',S.accent)
    el('ringWrap').style.setProperty('--ring',S.ringSize+'px')
    card.style.setProperty('--ring',S.ringSize+'px')
    document.documentElement.style.setProperty('--accent',S.accent)
    document.documentElement.style.setProperty('--fs',Math.round(S.ringSize*.19)+'px')
    document.documentElement.style.setProperty('--ring',S.ringSize+'px')
    el('title').textContent=S.title;el('subtitle').textContent=S.subtitle;el('unit').textContent=S.unit
    el('stats').style.display=S.showStats?'flex':'none'
    el('credit').style.display=S.credit?'flex':'none'
    document.documentElement.style.setProperty('--mc',S.accent)
    if(!S.glow)head.style.filter='none';else head.style.filter=''
    buildTicks();drawSpark()
  }
  SPEC.forEach(function(s){
    var lab=document.createElement('label'),txt=document.createElement('span'),inp
    txt.textContent=s.label
    if(s.type==='toggle'){inp=document.createElement('input');inp.type='checkbox';lab.className='ck'}
    else if(s.type==='select'){inp=document.createElement('select');s.options.forEach(function(o){var op=document.createElement('option');op.value=o;op.textContent=o;inp.appendChild(op)})}
    else{inp=document.createElement('input');inp.type=s.type;if(s.min!==undefined){inp.min=s.min;inp.max=s.max;inp.step=s.step}}
    inp.value=String(S[s.key]);if(s.type==='toggle')inp.checked=!!S[s.key]
    var out=document.createElement('b');out.textContent=String(S[s.key])
    inp.addEventListener('input',function(){
      var v=s.type==='toggle'?inp.checked:(s.type==='range'?+inp.value:inp.value)
      S[s.key]=v;out.textContent=String(v);apply()
      if(s.key==='interval')schedule()
      if(s.key==='valueMode'&&v==='fixed')refresh()
    })
    lab.appendChild(txt);lab.appendChild(inp);if(s.type!=='toggle')lab.appendChild(out);el('ctl').appendChild(lab)
  })
  apply()
  for(var i=0;i<18;i++)hist.push(Math.round(S.maxValue*(.5+Math.random()*.35)))
  refresh();schedule()
  window.addEventListener('message',function(e){
    if(!e.data||e.data.type!=='ub:props'||e.data.key!==KEY||!e.data.props)return
    Object.keys(e.data.props).forEach(function(k){if(k in S)S[k]=e.data.props[k]})
    var box=el('ctl');if(box)Array.prototype.forEach.call(box.children,function(lab,i){var s=SPEC[i],v=S[s.key],inp=lab.querySelector('input,select'),out=lab.querySelector('b')
      if(!inp)return;if(s.type==='toggle')inp.checked=!!v;else inp.value=String(v);if(out)out.textContent=String(v)})
    apply();if(S.valueMode==='fixed')refresh()
  })
})();
</script>

速览

类别
数据可视化
层级
组件
框架
HTML/CSS
复制
0 次

安装

复制「HTML/CSS」与「样式 CSS」两份代码即可,无第三方依赖;改配置对象里的值即可换文案与配色。

能力

  • 支持深色
  • 无障碍友好
  • 带动效
  • 响应式

自定义参数

15 项默认

其它参数

尚未归类的可调项
6842
10000
60
240
1400

使用提示

  1. 先在上方调参数,预览和下方「使用示例」会一起变; 复制那一段贴进页面即可。
  2. 「HTML/CSS」是结构与文案,「样式 CSS」是它用到的规则(含变量与响应式, 只挑了这个组件用到的部分)。两份一起贴。
  3. 代码里的素材是本站绝对地址,换成自己的图只需改 src。

相似组件

按标签重合度推荐,不是运营手配的榜单。

聚光跟随浮层 截图

聚光跟随浮层

浮层 · 组件

磁吸时间刻度尺 截图

磁吸时间刻度尺

时间 · 组件

分层堆叠 截图

分层堆叠

布局 · 区块

通知堆叠 截图

通知堆叠

反馈 · 组件