Files
NODEDC_MISSION_CORE/plugins/vesc/frontend/src/SignalPlot.tsx
T
DCCONSTRUCTIONS f32c7b400b feat(vesc): extend motor diagnostics, configuration and transport recovery
Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
2026-10-29 09:09:59 +03:00

37 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {useEffect,useMemo,useRef,useState} from 'react';
import {Description,Icon,IconButton,SettingsCard} from '@nodedc/ui-react';
export interface SignalPoint {at:number;value:number|null}
const number=(n:number|null)=>n===null?'—':n.toLocaleString('ru-RU',{maximumFractionDigits:3});
export function signalView(points:readonly SignalPoint[],from:number,to:number,height=180,width=800){
const span=Math.max(1,width-84);
const visible=points.filter(p=>p.at>=from&&p.at<=to),valid=visible.filter(p=>p.value!==null&&Number.isFinite(p.value));
let min:number|null=null,max:number|null=null,mean=0;
for(const p of valid){min=min===null?p.value!:Math.min(min,p.value!);max=max===null?p.value!:Math.max(max,p.value!);mean+=p.value!/valid.length;}
const pad=Math.max(((max??1)-(min??0))*.1,Math.abs(max??0)*.01,.001),low=(min??0)-pad,high=(max??1)+pad;
const segments:SignalPoint[][]=[];let line:SignalPoint[]=[];
const flush=()=>{if(line.length)segments.push(line);line=[];};
for(const p of visible){if(p.value===null||!Number.isFinite(p.value))flush();else line.push(p);}flush();
// Min/max per pixel bucket preserves spikes; gaps are never joined.
const render=segments.map(segment=>{const out:SignalPoint[]=[];let bucket:SignalPoint[]=[];let previous=-1;
const emit=()=>{if(!bucket.length)return;const values=[bucket[0],bucket.reduce((a,b)=>a.value!<b.value!?a:b),bucket.reduce((a,b)=>a.value!>b.value!?a:b),bucket.at(-1)!];out.push(...[...new Set(values)].sort((a,b)=>a.at-b.at));bucket=[];};
for(const p of segment){const x=Math.floor((p.at-from)/Math.max(Number.EPSILON,to-from)*span);if(x!==previous){emit();previous=x;}bucket.push(p);}emit();
return out.map(p=>({x:64+(p.at-from)/Math.max(Number.EPSILON,to-from)*span,y:20+(1-(p.value!-low)/(high-low))*height}));});
return {segments:render,min,max,mean:valid.length?mean:null,count:valid.length,low,high};
}
export function SignalPlot({title,unit,points,from,to,xUnit='с',xScale=1000,cursors=[],step=false}:{title:string;unit:string;points:readonly SignalPoint[];from:number;to:number;xUnit?:string;xScale?:number;cursors?:readonly number[];step?:boolean}){
const [expanded,setExpanded]=useState(false),[width,setWidth]=useState(800),height=expanded?384:160,span=Math.max(1,width-84);
const svg=useRef<SVGSVGElement>(null);
useEffect(()=>{const element=svg.current;if(!element)return;const observer=new ResizeObserver(entries=>{const next=Math.max(160,Math.round(entries[0].contentRect.width));setWidth(old=>old===next?old:next);});observer.observe(element);return()=>observer.disconnect();},[]);
const view=useMemo(()=>signalView(points,from,to,height,width),[points,from,to,height,width]);
return <SettingsCard title={title} description={`Минимум: ${number(view.min)} · максимум: ${number(view.max)} · среднее: ${number(view.mean)} ${unit} · ${view.count} измерений`}
actions={<div className="nodedc-viewport-controls"><IconButton label={expanded?`Свернуть: ${title}`:`Развернуть: ${title}`} onClick={()=>setExpanded(v=>!v)}><Icon name={expanded?'minimize':'expand'}/></IconButton></div>}>
<svg ref={svg} style={{height:height+56}} className={`vesc-trend-plot${expanded?' vesc-trend-plot--expanded':''}`} viewBox={`0 0 ${width} ${height+56}`} role="img" aria-label={`${title}, ${unit}; ${view.count} измерений`}>
{[0,.5,1].map(f=><g key={f}><line className="vesc-trend-grid" x1="64" x2={width-20} y1={20+f*height} y2={20+f*height}/><text x="56" y={24+f*height} textAnchor="end">{number(view.high-f*(view.high-view.low))}</text></g>)}
{view.segments.map((line,i)=>line.length===1?<circle key={i} className="vesc-trend-point" cx={line[0].x} cy={line[0].y} r="2"/>:<polyline key={i} className="vesc-trend-line" points={line.flatMap((p,j)=>step&&j?[`${p.x},${line[j-1].y}`,`${p.x},${p.y}`]:[`${p.x},${p.y}`]).join(' ')}/>)}
{cursors.map((at,i)=>at>=from&&at<=to?<g key={i}><line className="vesc-trend-cursor" x1={64+(at-from)/Math.max(Number.EPSILON,to-from)*span} x2={64+(at-from)/Math.max(Number.EPSILON,to-from)*span} y1="20" y2={height+20}/><text x={64+(at-from)/Math.max(Number.EPSILON,to-from)*span} y="14" textAnchor="middle">{i+1}</text></g>:null)}
<text x="64" y={height+47}>{number(from/xScale)} {xUnit}</text><text x={width-20} y={height+47} textAnchor="end">{number(to/xScale)} {xUnit}</text>
</svg>
{!view.count&&<Description>В этом интервале нет измерений. Пропуски не заменяются нулями.</Description>}
</SettingsCard>;
}