@canplot/react 0.1.22 → 0.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/canplot.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const k=require("react/jsx-runtime"),v=require("react"),ee=require("zustand"),B=(s,t,e)=>Math.min(Math.max(s,t),e),ke=s=>s.reduce((t,e)=>t+(e??0),0),Re=(s,t)=>{let e=0;for(let n=0;n<s.length;n++)Math.abs(s[n]-t)<Math.abs(s[e]-t)&&(e=n);return e},ge=(s,t,e,n)=>{const{min:o,max:i,origin:r}=I(s,e),d=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,h=(r==="x"?d.width:d.height)/(i-o);return t/h},I=(s,t)=>{const e=s.scales.find(n=>n.id===t);if(!e)throw new Error(`Scale ${t} not found`);return e},A=(s,t)=>{const e=window.devicePixelRatio||1;s.lineCap=t?.lineCap??"butt",s.lineDashOffset=e*(t?.lineDashOffset??0),s.lineJoin=t?.lineJoin??"miter",s.lineWidth=e*(t?.lineWidth??1),s.miterLimit=e*(t?.miterLimit??10),s.strokeStyle=t?.strokeStyle??"black",s.fillStyle=t?.fillStyle??s.strokeStyle,s.font=t?.font??`${10*e}px sans-serif`,s.textAlign=t?.textAlign??"start",s.direction=t?.direction??"inherit",s.textBaseline=t?.textBaseline??"alphabetic",s.fontKerning=t?.fontKerning??"auto"},te=(s,t,e,n)=>{const o=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,{min:i,max:r,origin:d}=I(s,e),h=(d==="x"?o.width:o.height)/(r-i);return t*h},j=(s,t,e,n)=>{const{min:o,origin:i}=I(s,e),r=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,d=te(s,t-o,e,n);return i==="x"?B(r.x+d,r.x-10*r.width,r.x+11*r.width):B(r.y+r.height-d,r.y-10*r.height,r.y+11*r.height)},Ae=(s,t,e)=>{const{min:n,max:o}=I(s,e);return B(t,n,o)},Y=(s,t,e)=>{const n=e==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS;return B(t,n.x,n.x+n.width)},z=(s,t,e)=>{const n=e==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS;return B(t,n.y,n.y+n.height)},Z=(s,t,e)=>{const{min:n,max:o}=I(s,e);return t>=n&&t<=o},_=(s,t,e,n)=>{const{min:o,max:i,origin:r}=I(s,e),d=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,h=r==="x"?(t-d.x)/d.width:(d.height-t+d.y)/d.height;return o+h*(i-o)},Le=s=>{const{ctx:t,scales:e}=s;for(const n of e){if(!n.axis)continue;t.save(),A(t,{strokeStyle:"black",fillStyle:"black",lineWidth:1,...n.axis.style});const o=n.axis.canvasRect;if(n.origin==="x"){if(n.axis.position==="bottom")t.beginPath(),t.moveTo(o.x,o.y),t.lineTo(o.x+o.width,o.y),t.stroke();else if(n.axis.position==="top"){const i=o.y+o.height;t.beginPath(),t.moveTo(o.x,i),t.lineTo(o.x+o.width,i),t.stroke()}}else if(n.axis.position==="left"){const i=o.x+o.width;t.beginPath(),t.moveTo(i,o.y),t.lineTo(i,o.y+o.height),t.stroke()}else n.axis.position==="right"&&(t.beginPath(),t.moveTo(o.x,o.y),t.lineTo(o.x,o.y+o.height),t.stroke());t.restore()}},J={TOP:400,MIDDLE:300,BOTTOM:200,BACKGROUND:100},De=()=>ee.createStore((s,t)=>{const e=()=>{const n=t()._frame;if(!n)throw new Error("No frame set in frame store");return n};return{_frame:null,getFrame:e,getCtx:()=>e().ctx,clampXPosToChartArea:(n,o)=>Y(e(),n,o??"canvas"),clampYPosToChartArea:(n,o)=>z(e(),n,o??"canvas"),valToPos:(n,o,i)=>j(e(),n,o,i??"canvas"),valToPxDistance:(n,o,i)=>te(e(),n,o,i??"canvas"),valFits:(n,o)=>Z(e(),n,o),getScale:n=>I(e(),n),_listeners:[],_subscribe:(n,o)=>(t()._listeners.push([o,n]),t()._listeners.sort((i,r)=>i[0]-r[0]),()=>{const i=t()._listeners.findIndex(([,r])=>r===n);i!==-1&&t()._listeners.splice(i,1)}),_notifyListeners:n=>{for(const[,o]of t()._listeners)o(n)}}}),_e=()=>ee.createStore(s=>({notify:()=>{s(t=>({version:t.version+1}))},version:0})),de=v.createContext(null),Se=v.createContext(null),O=(s,t,e)=>{const n=v.useContext(de),o=v.useContext(Se);if(!n||!o)throw new Error("useFrame must be used within a CanPlot component");const i=v.useRef(t);i.current=t,v.useLayoutEffect(()=>(Promise.resolve().then(()=>{i.current(n.getState())}),n.getState()._subscribe(r=>{r._frame&&i.current(r)},typeof s=="number"?s:J[s])),[n,s]),v.useEffect(()=>{o.getState().notify()},[o,...e])},se=s=>{const t=v.useContext(de);if(!t)throw new Error("useFrame must be used within a CanPlot component");return ee.useStore(t,s??(e=>e.getFrame()))};function Ie(s){return t=>{for(const e of s)ue(e,t)}}function Ue(s){return t=>{const e=[];for(const n of s){const o=ue(n,t),i=typeof o=="function";e.push(i?o:()=>ue(n,null))}return()=>{for(const n of e)n()}}}function ue(s,t){if(typeof s=="function")return s(t);s&&(s.current=t)}var Xe=parseInt(v.version.split(".")[0],10)>=19?Ue:Ie;const Oe=v.forwardRef(({configuration:s,children:t,style:e,className:n},o)=>{const i=v.useRef(null),r=v.useRef(null),d=Ke(r),h=v.useMemo(De,[]),u=v.useMemo(_e,[]);v.useLayoutEffect(()=>{h.setState({_frame:Ye(s,d,i.current)});const c=h.getState();c._notifyListeners(c)},[s,d,i,h]),v.useLayoutEffect(()=>h.getState()._subscribe(c=>{const f=c._frame?.ctx;f&&f.clearRect(0,0,f.canvas.width,f.canvas.height)},J.BACKGROUND),[h]),v.useLayoutEffect(()=>h.getState()._subscribe(c=>{c._frame&&Le(c._frame)},J.BOTTOM),[h]),v.useLayoutEffect(()=>{let c=!1;return u.subscribe(()=>{c||(c=!0,window.requestAnimationFrame(()=>{c=!1,h.setState(a=>({_frame:a._frame?{...a._frame}:null}));const f=h.getState();f._notifyListeners(f)}))})},[u,h]);const m=window.devicePixelRatio||1;return k.jsxs("div",{ref:Xe([o,r]),className:n,style:{position:"relative",overflow:"hidden",...e},"data-canplotroot":!0,children:[k.jsx("canvas",{ref:i,width:d.width*m,height:d.height*m,style:{inset:0,position:"absolute",width:`${d.width}px`,height:`${d.height}px`}}),k.jsx(Fe,{frameStore:h,updateRequestStore:u,children:t})]})}),Fe=({frameStore:s,updateRequestStore:t,children:e})=>ee.useStore(s,o=>!!o._frame)?k.jsx(Se.Provider,{value:t,children:k.jsx(de.Provider,{value:s,children:e})}):null,Ke=s=>{const[t,e]=v.useState({width:0,height:0}),[n]=v.useState(()=>new ResizeObserver(o=>{for(const i of o){const r=Math.round(i.contentRect.width),d=Math.round(i.contentRect.height);e(h=>h.width!==r||h.height!==d?{...h,width:r,height:d}:h)}}));return v.useLayoutEffect(()=>{if(!s.current)return;const o=s.current.clientWidth,i=s.current.clientHeight;return e(r=>r.width!==o||r.height!==i?{...r,width:o,height:i}:r),n.observe(s.current,{box:"border-box"}),()=>n.disconnect()},[n,s]),t},Ye=(s,t,e)=>{const n=e?.getContext("2d");if(!n)return null;const o=window.devicePixelRatio||1;if(t.width===0||t.height===0)return null;const i={x:s.padding.left,y:s.padding.top,width:t.width-s.padding.left-s.padding.right,height:t.height-s.padding.top-s.padding.bottom};for(const a of s.scales)a.axis&&(a.origin==="x"?(a.axis.position==="bottom"||a.axis.position==="top")&&(i.height=Math.max(0,i.height-a.axis.size),a.axis.position==="top"&&(i.y+=a.axis.size)):(a.axis.position==="left"||a.axis.position==="right")&&(i.width=Math.max(0,i.width-a.axis.size),a.axis.position==="left"&&(i.x+=a.axis.size)));const r={x:i.x*o,y:i.y*o,width:i.width*o,height:i.height*o},d=[];let h=s.padding.left*o,u=n.canvas.width-s.padding.right*o,m=n.canvas.height-s.padding.bottom*o,c=s.padding.top*o;for(const a of s.scales){if(!a.axis){d.push({...a,axis:null});continue}let l;if(a.origin==="x")switch(a.axis.position){case"bottom":m-=a.axis.size*o,l={x:i.x,y:m/o,width:i.width,height:a.axis.size};break;case"top":c+=a.axis.size*o,l={x:i.x,y:c/o-a.axis.size,width:i.width,height:a.axis.size};break;case"left":case"right":throw new Error("Invalid axis position for x origin")}else switch(a.axis.position){case"left":h+=a.axis.size*o,l={x:h/o-a.axis.size,y:i.y,width:a.axis.size,height:i.height};break;case"right":u-=a.axis.size*o,l={x:u/o,y:i.y,width:a.axis.size,height:i.height};break;case"top":case"bottom":throw new Error("Invalid axis position for y origin")}const p={x:l.x*o,y:l.y*o,width:l.width*o,height:l.height*o};d.push({...a,axis:{...a.axis,cssRect:l,canvasRect:p}})}return{ctx:n,dpr:o,padding:s.padding,scales:d,chartAreaCSS:i,chartAreaCanvasPX:r}},ze=({layer:s="MIDDLE",data:t,xScaleId:e,yScaleId:n,style:o})=>(O(s,({getCtx:i,clampXPosToChartArea:r,clampYPosToChartArea:d,valToPos:h})=>{const u=i();u.save(),u.beginPath(),A(u,o);for(const m of t){const c=r(h(m.x,e)),f=d(h(m.y,n));u.lineTo(c,f)}u.stroke(),u.restore()},[t,e,n,o]),null),Be=({layer:s="MIDDLE",data:t,xScaleId:e,yScaleId:n,radius:o=5,style:i,globalAlpha:r})=>(O(s,({getCtx:d,valToPos:h,valFits:u})=>{const m=d();m.save(),m.beginPath();const c=new Path2D;A(m,i),r!==void 0&&(m.globalAlpha=r);for(const f of t){if(!u(f.x,e)||!u(f.y,n))continue;const a=h(f.x,e),l=h(f.y,n);c.moveTo(a+o,l),c.arc(a,l,o,0,Math.PI*2)}m.fill(c),m.stroke(c),m.restore()},[t,e,n,o,i,r]),null),je=({data:s,xScaleId:t,yScaleId:e,style:n,barWidth:o,xPositionOffset:i,radius:r,layer:d="MIDDLE"})=>(O(d,({getCtx:h,valToPxDistance:u,valToPos:m,clampXPosToChartArea:c,clampYPosToChartArea:f})=>{if(s.length===0)return;const a=h();a.save(),A(a,n);const l=u(o,t);a.beginPath();for(const p of s){const x=m(p.x,t)-l/2+i*l,S=f(m(0,e)),y=f(m(p.y,e)),w=S-y,T=c(x),b=c(x+l)-T;r?a.roundRect(T,y,b,w,r):a.rect(T,y,b,w)}a.closePath(),a.fill(),n?.strokeStyle&&a.stroke(),a.restore()},[s,t,e,n,o,i,r]),null),Ne=({layer:s="MIDDLE",data:t,xScaleId:e,yScaleId:n,style:o})=>(O(s,({getCtx:i,clampXPosToChartArea:r,clampYPosToChartArea:d,valToPos:h})=>{const u=[];for(const c of t){const f=r(h(c.x,e)),a=d(h(c.y[0],n)),l=d(h(c.y[1],n));u.push({x:f,y:a}),u.unshift({x:f,y:l})}const m=i();if(u.length>0){m.save(),m.beginPath(),A(m,o),m.moveTo(u[0].x,u[0].y);for(const c of u)m.lineTo(c.x,c.y);m.closePath(),m.fill(),m.restore()}},[t,e,n,o]),null),$e=({layer:s="MIDDLE",data:t,stroked:e,xScaleId:n,yScaleId:o,style:i})=>(O(s,({getCtx:r,clampXPosToChartArea:d,clampYPosToChartArea:h,valToPos:u})=>{const m=[],c=r();for(const p of t){const g=d(u(p.x,n)),x=h(u(p.y,o));m.push({x:g,y:x})}const f=m.at(0),a=m.at(-1);if(!f||!a)return;const l=h(u(0,o));c.save(),c.beginPath(),A(c,i),c.moveTo(f.x,l);for(const p of m)c.lineTo(p.x,p.y);if(c.lineTo(a.x,l),c.closePath(),c.fill(),e){c.beginPath(),c.moveTo(f.x,f.y);for(const p of m)c.lineTo(p.x,p.y);c.stroke()}c.restore()},[t,e,n,o,i]),null),Ve=(s,t)=>se(({clampXPosToChartArea:e,clampYPosToChartArea:n,getScale:o,valToPos:i,valFits:r})=>{const d={};for(const h in s){const u=s[h];switch(u.exceeding){case"discard":{if(r(u.value,u.scaleId)){const m=i(u.value,u.scaleId,t);d[h]=m}break}case"clamp":{const m=i(u.value,u.scaleId,t);d[h]=o(u.scaleId)?.origin==="x"?e(m,t):n(m,t);break}}}return d}),He=({layer:s="BOTTOM",scaleId:t,tickStyle:e,labelStyle:n,labelGap:o,tickSize:i,ticks:r})=>(O(s,({getCtx:d,valToPos:h,getScale:u,getFrame:m})=>{const c=d(),f=u(t);if(!f||!f.axis||f.origin!=="x")return;const a=f.axis,l=a.position==="top"?a.canvasRect.y+a.canvasRect.height:a.canvasRect.y,p=window.devicePixelRatio||1,g=l,x=(i??6)*p,S=a.position==="top"?l-x:l+x,y=(o??12)*p;c.save(),c.fontKerning="auto",A(c,{...e}),c.beginPath();const w=Array.isArray(r)?r:r({...f,axis:a},m());for(const{value:T}of w){const b=h(T,t,"canvas");c.moveTo(b,g),c.lineTo(b,S)}c.stroke(),c.restore(),c.save(),A(c,{textBaseline:a.position==="top"?"bottom":"top",textAlign:"center",...e,...n});for(const{value:T,label:b}of w){const C=h(T,t,"canvas"),P=b.split(`
|
|
2
|
-
`);for(let E=0;E<
|
|
3
|
-
`);for(let P=0;P<C.length;P++)c.fillText(` ${C[P]} `,x,b+P*S)}c.restore()},[r,t,e,n]),null),L=()=>{const s=[];return{addEventListener:(t,e)=>(s.push({syncKey:t,callback:e}),()=>{const n=s.findIndex(o=>o.callback===e);n!==-1&&s.splice(n,1)}),dispatchEvent:(t,e)=>{for(const n of s)n.syncKey===t&&n.callback(t,e)}}},M={dblclick:L(),click:L(),contextmenu:L(),move:L(),mousedown:L(),mouseup:L(),spanselect:L(),documentmouseup:L(),pressandwheel:L(),sync_move:L()},D=(s,t,e)=>{const n=v.useRef(e);n.current=e,v.useEffect(()=>M[s].addEventListener(t,(i,r)=>{n.current(r,i)}),[t,s,n])},he=v.createContext(""),ne=(s,t)=>{const e=v.useContext(he);return D(s,e,t)},We=(s,t,e,n,o)=>{if(!t)return;const i=n??e.scales.find(c=>c.origin==="x")?.id,r=o??e.scales.find(c=>c.origin==="y")?.id;if(!i||!r)return;const d=s.clientX-t.left,h={scaleId:i,value:_(e,d,i,"css")},u=s.clientY-t.top,m={scaleId:r,value:_(e,u,r,"css")};return{pointerSyncPosition:{x:h,y:m},cssX:d,cssY:u}},K=(s,t)=>{const{x:e,y:n}=s,o=e&&t.scales.some(r=>r.id===e.scaleId)?j(t,e.value,e.scaleId,"css"):null,i=n&&t.scales.some(r=>r.id===n.scaleId)?j(t,n.value,n.scaleId,"css"):null;return{cssX:o,cssY:i,scaled:Object.fromEntries(t.scales.flatMap(r=>{const d=r.origin==="y"?i:o;return d===null?[]:[[r.id,_(t,d,r.id,"css")]]}))}},ye=(s,t,e)=>{const n=j(e,t.from,t.scaleId,"css"),o=j(e,t.to,t.scaleId,"css"),i=e.scales.flatMap(r=>{if(r.origin!==s)return[];const d=_(e,n,r.id,"css"),h=_(e,o,r.id,"css");return[{scaleId:r.id,from:d,to:h}]});return{fromCSS:n,toCSS:o,scaled:i}},Ge=({id:s,onClick:t,onDblClick:e,onMouseMove:n,onMouseDown:o,onMouseUp:i,onDocumentMouseUp:r,onSpanSelect:d,onContextMenu:h,className:u,style:m,sync:c,children:f})=>{const a=v.useId(),l=s||a;return D("dblclick",l,p=>{e?.(p)}),D("click",l,p=>{t?.(p)}),D("move",l,p=>{n?.(p)}),D("mousedown",l,p=>{o?.(p)}),D("mouseup",l,p=>{i?.(p)}),D("documentmouseup",l,p=>{r?.(p)}),D("spanselect",l,p=>{d?.(p)}),D("contextmenu",l,p=>{h?.(p)}),k.jsxs(he.Provider,{value:l,children:[k.jsx(Ze,{className:u,style:m,sync:c}),f]})},Ze=({className:s,style:t,sync:e})=>{const n=v.useRef(null),o=se(),i=v.useRef(o);i.current=o;const r=v.useContext(he),d=e?.key||r,h=v.useRef(null),u=v.useRef(null),m=v.useRef(null),c=()=>{const l=n.current?.parentElement;if(l){if(l.dataset.canplotroot===void 0)throw new Error("ChartAreaInteractions must be used within a CanPlot component");return l.getBoundingClientRect()}},f=(l,p)=>{const g=We(l,c(),i.current,e?.xViaScaleId,e?.yViaScaleId);g&&p(g.pointerSyncPosition,{cssX:g.cssX,cssY:g.cssY},{ctrlKey:l.ctrlKey,altKey:l.altKey,shiftKey:l.shiftKey,metaKey:l.metaKey})},a=v.useRef(f);return a.current=f,v.useEffect(()=>{const l=y=>{const w=u.current;w&&M.spanselect.dispatchEvent(r,{...w,completed:!0}),M.documentmouseup.dispatchEvent(r,{frame:i.current,keys:{ctrlKey:y.ctrlKey,altKey:y.altKey,shiftKey:y.shiftKey,metaKey:y.metaKey}})},p=y=>{const w={ctrlKey:y.ctrlKey,altKey:y.altKey,shiftKey:y.shiftKey,metaKey:y.metaKey},T=m.current;if(T&&Object.entries(w).some(([C,P])=>T.keys[C]!==P)){const C={...T,keys:w};m.current=C,M.sync_move.dispatchEvent(d,C)}const b=u.current;if(b&&Object.entries(w).some(([C,P])=>b.keys[C]!==P)){y.stopPropagation(),y.preventDefault();const C={...b,keys:w};u.current=C,M.spanselect.dispatchEvent(r,C)}},g=y=>{a.current(y,(w,{cssX:T,cssY:b},C)=>{const P=h.current;if(!P||!w.x||!w.y)return;const E=i.current,R=P.xRangeCss.start,N=T,F=P.yRangeCss.start,ie=b,re=I(E,w.x.scaleId),ce=I(E,w.y.scaleId);h.current={xRangeCss:{start:R,end:N},yRangeCss:{start:F,end:ie}};let $="below_threshold";const ae=Math.abs(F-ie),le=Math.abs(R-N);ae<10&&le<10?$="below_threshold":ae>30&&le>30?$="box":ae>le?$="y":$="x";const me={scaleId:re.id,from:_(E,Y(i.current,R,"css"),re.id,"css"),to:_(E,Y(i.current,N,"css"),re.id,"css")},pe={scaleId:ce.id,from:_(E,z(i.current,F,"css"),ce.id,"css"),to:_(E,z(i.current,ie,"css"),ce.id,"css")},V=me&&ye("x",me,i.current),H=pe&&ye("y",pe,i.current),Ee=V?.scaled,Me=H?.scaled,xe={mode:$,frame:i.current,completed:!1,x:{css:V&&{from:V.fromCSS,to:V.toCSS},scaled:Ee??[]},y:{css:H&&{from:H.fromCSS,to:H.toCSS},scaled:Me??[]},keys:C};u.current=xe,M.spanselect.dispatchEvent(r,xe)})},x=y=>{a.current(y,(w,T,b)=>{const C=K(w,i.current);if(!C)return;if(Object.values(b).some(E=>E)){y.preventDefault();const E=Math.abs(y.deltaY)>Math.abs(y.deltaX)?y.deltaY:y.deltaX;M.pressandwheel.dispatchEvent(r,{pointer:C,frame:i.current,keys:b,deltaX:y.deltaX,deltaY:y.deltaY,deltaAbs:E})}})};document.addEventListener("mouseup",l),document.addEventListener("keydown",p),document.addEventListener("keyup",p),document.addEventListener("mousemove",g);const S=n.current;return S?.addEventListener("wheel",x,{passive:!1}),()=>{document.removeEventListener("mouseup",l),document.removeEventListener("keydown",p),document.removeEventListener("keyup",p),document.removeEventListener("mousemove",g),S?.removeEventListener("wheel",x)}},[i,r,d,a]),D("sync_move",d,l=>{const p=l.positions?K(l.positions,i.current):null;m.current=l,M.move.dispatchEvent(r,{frame:i.current,pointer:p??null,keys:l.keys})}),k.jsx("div",{ref:n,id:"interactions",className:s,style:{position:"absolute",left:o.chartAreaCSS.x,top:o.chartAreaCSS.y,width:o.chartAreaCSS.width,height:o.chartAreaCSS.height,zIndex:25,...t},onDragStart:l=>{l.preventDefault()},onClick:l=>{f(l,(p,g,x)=>{const S=K(p,i.current);S&&M.click.dispatchEvent(r,{pointer:S,frame:i.current,keys:x})})},onMouseLeave:l=>{f(l,(p,g,x)=>{M.sync_move.dispatchEvent(d,{positions:null,keys:x})})},onMouseMove:l=>{f(l,(p,g,x)=>{M.sync_move.dispatchEvent(d,{positions:p,keys:x})})},onMouseDown:l=>{f(l,(p,{cssX:g,cssY:x},S)=>{const y=K(p,i.current);y&&(M.mousedown.dispatchEvent(r,{pointer:y,frame:i.current,keys:S}),h.current={xRangeCss:{start:g,end:g},yRangeCss:{start:x,end:x}})})},onMouseUp:l=>{f(l,(p,g,x)=>{const S=K(p,i.current);if(!S)return;M.mouseup.dispatchEvent(r,{frame:i.current,pointer:S,keys:x});const y=u.current;u.current=null;const w=h.current;if(h.current=null,w&&y){const T={...y,keys:x,completed:!0};u.current=null,M.spanselect.dispatchEvent(r,T)}})},onContextMenu:l=>{l.preventDefault(),f(l,(p,g,x)=>{const S=K(p,i.current);S&&M.contextmenu.dispatchEvent(r,{frame:i.current,pointer:S,keys:x})})},onDoubleClick:l=>{f(l,(p,g,x)=>{const S=K(p,i.current);S&&M.dblclick.dispatchEvent(r,{frame:i.current,pointer:S,keys:x})})}})},Je=({data:s,renderTooltip:t,xScaleId:e})=>{const[n,o]=v.useState(null);ne("move",r=>{o(r)});const i=v.useMemo(()=>{if(!n)return null;const{frame:r,pointer:d}=n,h=d?.scaled[e];if(h===void 0)return null;const u=[];let m=h;for(const c of s){let f=null,a=1/0;for(const[p,g]of c.points.entries()){if(!Z(r,g.x,e)||!Z(r,g.y,c.yScaleId))continue;const x=Math.abs(g.x-h);x<a&&(a=x,f=p)}const l=c.points[f??-1];if(!l||te(r,a,e,"css")>30){u.push({seriesId:c.seriesId,y:null});continue}m=l.x,u.push({seriesId:c.seriesId,y:l.y})}return{frame:r,x:m,points:u}},[s,n,e]);return t(i)},Qe=({makeXStyle:s,makeXClassName:t,makeYStyle:e,makeYClassName:n})=>{const[o,i]=v.useState(null);if(ne("move",f=>{i(f)}),!o)return null;const{frame:r,pointer:d}=o,h=d?.cssX??null,u=d?.cssY??null,m=h?Y(r,h,"css"):0,c=u?z(r,u,"css"):0;return k.jsxs(k.Fragment,{children:[k.jsx("div",{"data-show":!!d,className:t?.(o),style:{position:"absolute",left:0,visibility:h===null?"hidden":"visible",top:r.chartAreaCSS.y,height:r.chartAreaCSS.height,borderColor:"red",borderLeftWidth:"1px",borderLeftStyle:"solid",pointerEvents:"none",opacity:d?1:0,transform:`translateX(${m}px)`,...s?.(o)}}),k.jsx("div",{className:n?.(o),"data-show":!!d,style:{position:"absolute",visibility:u===null?"hidden":"visible",top:0,height:0,borderTop:"solid 1px red",left:r.chartAreaCSS.x,width:r.chartAreaCSS.width,pointerEvents:"none",opacity:d?1:0,transform:`translateY(${c}px)`,...e?.(o)}})]})},et=({makeClassName:s,makeStyle:t})=>{const[e,n]=v.useState(null);ne("spanselect",i=>{n(i.mode==="below_threshold"||i.completed?null:i)});const o=v.useMemo(()=>{if(!e||e.mode==="below_threshold")return null;const i=e.mode==="y"?-1/0:e.x.css?.from??-1/0,r=e.mode==="y"?1/0:e.x.css?.to??1/0,d=e.mode==="x"?-1/0:e.y.css?.from??-1/0,h=e.mode==="x"?1/0:e.y.css?.to??1/0,u=Y(e.frame,i,"css"),m=Y(e.frame,r,"css"),c=z(e.frame,d,"css"),f=z(e.frame,h,"css"),a=Math.min(u,m),l=Math.min(c,f),p=Math.abs(m-u),g=Math.abs(f-c);return{leftPx:a,topPx:l,widthPx:p,heightPx:g}},[e]);return k.jsx("div",{className:e?s?.(e):void 0,style:{position:"absolute",visibility:e?"visible":"hidden",left:`${o?.leftPx??0}px`,top:`${o?.topPx??0}px`,width:`${o?.widthPx??0}px`,height:`${o?.heightPx??0}px`,pointerEvents:"none",...e?t?.(e):void 0}})},tt=({style:s,children:t,scaleId:e,...n})=>{const o=se(i=>i.getScale(e)?.axis);return o?k.jsx("div",{style:{position:"absolute",backgroundColor:"#0000ff11",left:o.cssRect.x,top:o.cssRect.y,height:o.cssRect.height,width:o.cssRect.width,...s},...n,children:t}):null},we=60,st=30,be="UTC",nt="en-GB",ot=({space:s,formatter:t,acceptableIncrements:e}={})=>(n,o)=>{const{min:i,max:r}=n,d=[],h=window.devicePixelRatio||1,u=(s??(n.origin==="x"?we:st))*h,m=ge(o,u,n.id,"canvas"),c=e??q,f=c.find(l=>l>m)??c.at(-1)??1;let a=i;if(Math.abs(a%f)>Number.EPSILON){const l=(f-a%f)%f;a+=l}for(;a<=r&&d.length<1e3;)d.push(a),a+=f;return(t??Te)(d)},Te=s=>{const t=Math.max(0,Math.ceil(-Math.log10(s[1]-s[0])));return s.map(e=>({value:e,label:e.toFixed(t)}))},q=[];for(let s=-12;s<=12;s++)q.push(1*10**s),q.push(2*10**s),q.push(5*10**s);const it=1,Ce=1e3*it,fe=60*Ce,Q=60*fe,oe=24*Q,rt=30*oe,ct=365*oe,at=[[1,"milliseconds"],[2,"milliseconds"],[5,"milliseconds"],[10,"milliseconds"],[20,"milliseconds"],[50,"milliseconds"],[100,"milliseconds"],[200,"milliseconds"],[500,"milliseconds"],[1,"seconds"],[5,"seconds"],[10,"seconds"],[15,"seconds"],[30,"seconds"],[1,"minutes"],[5,"minutes"],[10,"minutes"],[15,"minutes"],[30,"minutes"],[1,"hours"],[2,"hours"],[3,"hours"],[4,"hours"],[6,"hours"],[8,"hours"],[12,"hours"],[1,"days"],[3,"days"],[5,"days"],[7,"days"],[10,"days"],[15,"days"],[1,"months"],[2,"months"],[3,"months"],[4,"months"],[6,"months"],[1,"years"],[2,"years"],[5,"years"],[10,"years"],[25,"years"],[50,"years"],[100,"years"]],W=s=>{const[t,e]=s;switch(e){case"milliseconds":return t;case"seconds":return t*Ce;case"minutes":return t*fe;case"hours":return t*Q;case"days":return t*oe;case"months":return t*rt;case"years":return t*ct}},lt=(s,t)=>{const e=new Date(s);return e.setUTCMilliseconds(e.getUTCMilliseconds()+t),e.getTime()},ut=(s,t)=>{const e=new Date(s);return e.setUTCSeconds(e.getUTCSeconds()+t),e.getTime()},dt=(s,t)=>{const e=new Date(s);return e.setUTCMinutes(e.getUTCMinutes()+t),e.getTime()},ht=(s,t)=>{const e=new Date(s);return e.setUTCHours(e.getUTCHours()+t),e.getTime()},ft=(s,t)=>{const e=new Date(s);return e.setUTCDate(e.getUTCDate()+t),e.getTime()},ve=(s,t)=>{const e=new Date(s);return e.setUTCMonth(e.getUTCMonth()+t),e.getTime()},X=(s,t)=>{const[e,n]=t;switch(n){case"milliseconds":return lt(s,e);case"seconds":return ut(s,e);case"minutes":return dt(s,e);case"hours":return ht(s,e);case"days":return ft(s,e);case"months":return ve(s,e);case"years":return ve(s,e*12)}};function G(s,t){const e=new Date(s),n=new Date(e.toLocaleString("en-US",{timeZone:t})),o=new Date(e.toLocaleString("en-US",{timeZone:"UTC"}));return(n.getTime()-o.getTime())/(3600*1e3)}const mt=(s,t,e="UTC")=>{const[n,o]=t;let i=new Date(s);const r=()=>{i.setUTCHours(-G(i,e),0,0,0)};switch(o){case"milliseconds":i.setUTCMilliseconds(Math.ceil(i.getUTCMilliseconds()/n)*n);break;case"seconds":i.setUTCSeconds(Math.ceil(i.getUTCSeconds()/n)*n,0);break;case"minutes":i.setUTCMinutes(Math.ceil(i.getTime()%Q/fe/n)*n,0,0);break;case"hours":i.setUTCHours(Math.ceil(i.getTime()%oe/Q/n)*n,0,0,0);break;case"days":case"months":case"years":o==="months"?i.setUTCDate(1):o==="years"&&i.setUTCMonth(0,1),r(),i.getTime()<s&&(i=new Date(X(i,[1,o])));break}return i.getTime()},pt=({timeZone:s=be,space:t=we,formatter:e,locale:n,showTimezone:o}={})=>(i,r)=>{const{min:d,max:h}=i,u=Math.floor(r.chartAreaCanvasPX.width/t)+1,c=(h-d)/u,[f,a]=at.find(S=>W(S)>=c)??[1,"milliseconds"],l=mt(d,[f,a],s),p=G(l,s),g=[l];let x;for(;;){switch(a){case"milliseconds":case"seconds":case"minutes":case"hours":{x=X(l,[g.length*f,a]);break}case"days":{const S=X(l,[g.length*f,a]);x=X(S,[p-G(S,s),"hours"]);break}case"months":case"years":{const S=X(X(X(l,[p,"hours"]),[g.length*f,a]),[-p,"hours"]);x=X(S,[p-G(S,s),"hours"]);break}}if(x>h)break;g.push(x)}return(e??Pe({locale:n,showTimezone:o,timeZone:s}))(g)},U=(s,t,e)=>s.find(n=>n.type===e)?.value!==t.find(n=>n.type===e)?.value,Pe=({timeZone:s=be,locale:t=nt,showTimezone:e=!0})=>{const n=new Intl.DateTimeFormat(t,{year:"numeric",day:"numeric",month:"short",hour:"numeric",hourCycle:"h23",minute:"numeric",second:"numeric",fractionalSecondDigits:3,timeZoneName:"short",timeZone:s});return o=>{const i=o[1]-o[0],r=i<W([1,"days"]),d=i<W([1,"minutes"]),h=i<W([1,"seconds"]);return o.map(u=>({value:u,label:n.formatToParts(new Date(u))})).map((u,m,c)=>{const f=c[m-1],a=m===0||U(u.label,f.label,"year"),l=m===0||U(u.label,f.label,"day"),p=m===0||U(u.label,f.label,"month"),g=m===0||U(u.label,f.label,"hour"),x=m===0||U(u.label,f.label,"timeZoneName"),S=m===0||U(u.label,f.label,"minute"),y=m===0||U(u.label,f.label,"second"),w=m===0||U(u.label,f.label,"fractionalSecond"),T=[];if(r&&(g||S||x||y||w)){const b=u.label.find(R=>R.type==="hour")?.value,C=u.label.find(R=>R.type==="minute")?.value,P=u.label.find(R=>R.type==="timeZoneName")?.value;let E="";if(d){const R=u.label.find(F=>F.type==="second")?.value,N=u.label.find(F=>F.type==="fractionalSecond")?.value;E=`:${R}`+(h?`.${N}`:"")}T.push(`${b}:${C}${E}`+(e&&x?` ${P}`:""))}return(l||p)&&T.push([u.label.find(b=>b.type==="month")?.value,l&&u.label.find(b=>b.type==="day")?.value].filter(Boolean).join(" ")),a&&T.push(u.label.find(b=>b.type==="year")?.value),{value:u.value,label:T.filter(b=>b).join(`
|
|
4
|
-
`)}})}};exports.AreaPlot
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("react/jsx-runtime"),g=require("react"),ee=require("zustand"),B=(s,t,e)=>Math.min(Math.max(s,t),e),Re=s=>s.reduce((t,e)=>t+(e??0),0),Ae=(s,t)=>{let e=0;for(let n=0;n<s.length;n++)Math.abs(s[n]-t)<Math.abs(s[e]-t)&&(e=n);return e},Se=(s,t,e,n)=>{const{min:o,max:i,origin:r}=I(s,e),l=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,d=(r==="x"?l.width:l.height)/(i-o);return t/d},I=(s,t)=>{const e=s.scales.find(n=>n.id===t);if(!e)throw new Error(`Scale ${t} not found`);return e},A=(s,t)=>{const e=window.devicePixelRatio||1;s.lineCap=t?.lineCap??"butt",s.lineDashOffset=e*(t?.lineDashOffset??0),s.lineJoin=t?.lineJoin??"miter",s.lineWidth=e*(t?.lineWidth??1),s.miterLimit=e*(t?.miterLimit??10),s.strokeStyle=t?.strokeStyle??"black",s.fillStyle=t?.fillStyle??s.strokeStyle,s.font=t?.font??`${10*e}px sans-serif`,s.textAlign=t?.textAlign??"start",s.direction=t?.direction??"inherit",s.textBaseline=t?.textBaseline??"alphabetic",s.fontKerning=t?.fontKerning??"auto"},te=(s,t,e,n)=>{const o=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,{min:i,max:r,origin:l}=I(s,e),d=(l==="x"?o.width:o.height)/(r-i);return t*d},j=(s,t,e,n)=>{const{min:o,origin:i}=I(s,e),r=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,l=te(s,t-o,e,n);return i==="x"?B(r.x+l,r.x-10*r.width,r.x+11*r.width):B(r.y+r.height-l,r.y-10*r.height,r.y+11*r.height)},Le=(s,t,e)=>{const{min:n,max:o}=I(s,e);return B(t,n,o)},Y=(s,t,e)=>{const n=e==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS;return B(t,n.x,n.x+n.width)},z=(s,t,e)=>{const n=e==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS;return B(t,n.y,n.y+n.height)},Z=(s,t,e)=>{const{min:n,max:o}=I(s,e);return t>=n&&t<=o},_=(s,t,e,n)=>{const{min:o,max:i,origin:r}=I(s,e),l=n==="canvas"?s.chartAreaCanvasPX:s.chartAreaCSS,d=r==="x"?(t-l.x)/l.width:(l.height-t+l.y)/l.height;return o+d*(i-o)},De=s=>{const{ctx:t,scales:e}=s;for(const n of e){if(!n.axis)continue;t.save(),A(t,{strokeStyle:"black",fillStyle:"black",lineWidth:1,...n.axis.style});const o=n.axis.canvasRect;if(n.origin==="x"){if(n.axis.position==="bottom")t.beginPath(),t.moveTo(o.x,o.y),t.lineTo(o.x+o.width,o.y),t.stroke();else if(n.axis.position==="top"){const i=o.y+o.height;t.beginPath(),t.moveTo(o.x,i),t.lineTo(o.x+o.width,i),t.stroke()}}else if(n.axis.position==="left"){const i=o.x+o.width;t.beginPath(),t.moveTo(i,o.y),t.lineTo(i,o.y+o.height),t.stroke()}else n.axis.position==="right"&&(t.beginPath(),t.moveTo(o.x,o.y),t.lineTo(o.x,o.y+o.height),t.stroke());t.restore()}},J={TOP:400,MIDDLE:300,BOTTOM:200,BACKGROUND:100},_e=()=>ee.createStore((s,t)=>{const e=()=>{const n=t()._frame;if(!n)throw new Error("No frame set in frame store");return n};return{_frame:null,getFrame:e,getCtx:()=>e().ctx,clampXPosToChartArea:(n,o)=>Y(e(),n,o??"canvas"),clampYPosToChartArea:(n,o)=>z(e(),n,o??"canvas"),valToPos:(n,o,i)=>j(e(),n,o,i??"canvas"),valToPxDistance:(n,o,i)=>te(e(),n,o,i??"canvas"),valFits:(n,o)=>Z(e(),n,o),getScale:n=>I(e(),n),_listeners:[],_subscribe:(n,o)=>(t()._listeners.push([o,n]),t()._listeners.sort((i,r)=>i[0]-r[0]),()=>{const i=t()._listeners.findIndex(([,r])=>r===n);i!==-1&&t()._listeners.splice(i,1)}),_notifyListeners:n=>{for(const[,o]of t()._listeners)o(n)}}}),Ie=()=>ee.createStore(s=>({notify:()=>{s(t=>({version:t.version+1}))},version:0})),he=g.createContext(null),we=g.createContext(null),O=(s,t,e)=>{const n=g.useContext(he),o=g.useContext(we);if(!n||!o)throw new Error("useFrame must be used within a CanPlot component");const i=g.useRef(t);i.current=t,g.useLayoutEffect(()=>(Promise.resolve().then(()=>{i.current(n.getState())}),n.getState()._subscribe(r=>{r._frame&&i.current(r)},typeof s=="number"?s:J[s])),[n,s]),g.useEffect(()=>{o.getState().notify()},[o,...e])},se=s=>{const t=g.useContext(he);if(!t)throw new Error("useFrame must be used within a CanPlot component");return ee.useStore(t,s??(e=>e.getFrame()))};function Ue(s){return t=>{for(const e of s)de(e,t)}}function Xe(s){return t=>{const e=[];for(const n of s){const o=de(n,t),i=typeof o=="function";e.push(i?o:()=>de(n,null))}return()=>{for(const n of e)n()}}}function de(s,t){if(typeof s=="function")return s(t);s&&(s.current=t)}var Oe=parseInt(g.version.split(".")[0],10)>=19?Xe:Ue;const Fe=g.forwardRef(({configuration:s,children:t,style:e,className:n},o)=>{const i=g.useRef(null),r=g.useRef(null),l=Ye(r),d=g.useMemo(_e,[]),u=g.useMemo(Ie,[]);g.useLayoutEffect(()=>{d.setState({_frame:ze(s,l,i.current)});const a=d.getState();a._notifyListeners(a)},[s,l,i,d]),g.useLayoutEffect(()=>d.getState()._subscribe(a=>{const m=a._frame?.ctx;m&&m.clearRect(0,0,m.canvas.width,m.canvas.height)},J.BACKGROUND),[d]),g.useLayoutEffect(()=>d.getState()._subscribe(a=>{a._frame&&De(a._frame)},J.BOTTOM),[d]),g.useLayoutEffect(()=>{let a=!1;return u.subscribe(()=>{a||(a=!0,window.requestAnimationFrame(()=>{a=!1,d.setState(c=>({_frame:c._frame?{...c._frame}:null}));const m=d.getState();m._notifyListeners(m)}))})},[u,d]);const h=window.devicePixelRatio||1;return R.jsxs("div",{ref:Oe([o,r]),className:n,style:{position:"relative",overflow:"hidden",...e},"data-canplotroot":!0,children:[R.jsx("canvas",{ref:i,width:l.width*h,height:l.height*h,style:{inset:0,position:"absolute",width:`${l.width}px`,height:`${l.height}px`}}),R.jsx(Ke,{frameStore:d,updateRequestStore:u,children:t})]})}),Ke=({frameStore:s,updateRequestStore:t,children:e})=>ee.useStore(s,o=>!!o._frame)?R.jsx(we.Provider,{value:t,children:R.jsx(he.Provider,{value:s,children:e})}):null,Ye=s=>{const[t,e]=g.useState({width:0,height:0}),[n]=g.useState(()=>new ResizeObserver(o=>{for(const i of o){const r=Math.round(i.contentRect.width),l=Math.round(i.contentRect.height);e(d=>d.width!==r||d.height!==l?{...d,width:r,height:l}:d)}}));return g.useLayoutEffect(()=>{if(!s.current)return;const o=s.current.clientWidth,i=s.current.clientHeight;return e(r=>r.width!==o||r.height!==i?{...r,width:o,height:i}:r),n.observe(s.current,{box:"border-box"}),()=>n.disconnect()},[n,s]),t},ze=(s,t,e)=>{const n=e?.getContext("2d");if(!n)return null;const o=window.devicePixelRatio||1;if(t.width===0||t.height===0)return null;const i={x:s.padding.left,y:s.padding.top,width:t.width-s.padding.left-s.padding.right,height:t.height-s.padding.top-s.padding.bottom};for(const c of s.scales)c.axis&&(c.origin==="x"?(c.axis.position==="bottom"||c.axis.position==="top")&&(i.height=Math.max(0,i.height-c.axis.size),c.axis.position==="top"&&(i.y+=c.axis.size)):(c.axis.position==="left"||c.axis.position==="right")&&(i.width=Math.max(0,i.width-c.axis.size),c.axis.position==="left"&&(i.x+=c.axis.size)));const r={x:i.x*o,y:i.y*o,width:i.width*o,height:i.height*o},l=[];let d=s.padding.left*o,u=n.canvas.width-s.padding.right*o,h=n.canvas.height-s.padding.bottom*o,a=s.padding.top*o;for(const c of s.scales){if(!c.axis){l.push({...c,axis:null});continue}let x;if(c.origin==="x")switch(c.axis.position){case"bottom":h-=c.axis.size*o,x={x:i.x,y:h/o,width:i.width,height:c.axis.size};break;case"top":a+=c.axis.size*o,x={x:i.x,y:a/o-c.axis.size,width:i.width,height:c.axis.size};break;case"left":case"right":throw new Error("Invalid axis position for x origin")}else switch(c.axis.position){case"left":d+=c.axis.size*o,x={x:d/o-c.axis.size,y:i.y,width:c.axis.size,height:i.height};break;case"right":u-=c.axis.size*o,x={x:u/o,y:i.y,width:c.axis.size,height:i.height};break;case"top":case"bottom":throw new Error("Invalid axis position for y origin")}const f={x:x.x*o,y:x.y*o,width:x.width*o,height:x.height*o};l.push({...c,axis:{...c.axis,cssRect:x,canvasRect:f}})}return{ctx:n,dpr:o,padding:s.padding,scales:l,chartAreaCSS:i,chartAreaCanvasPX:r}},Be=({layer:s="MIDDLE",data:t,xScaleId:e,yScaleId:n,style:o})=>(O(s,({getCtx:i,clampXPosToChartArea:r,clampYPosToChartArea:l,valToPos:d})=>{const u=i();u.save(),u.beginPath(),A(u,o);for(const h of t){const a=r(d(h.x,e)),m=l(d(h.y,n));u.lineTo(a,m)}u.stroke(),u.restore()},[t,e,n,o]),null),je=({layer:s="MIDDLE",data:t,xScaleId:e,yScaleId:n,radius:o=5,style:i,globalAlpha:r})=>(O(s,({getCtx:l,valToPos:d,valFits:u})=>{const h=l();h.save(),h.beginPath();const a=new Path2D;A(h,i),r!==void 0&&(h.globalAlpha=r);for(const m of t){if(!u(m.x,e)||!u(m.y,n))continue;const c=d(m.x,e),x=d(m.y,n);a.moveTo(c+o,x),a.arc(c,x,o,0,Math.PI*2)}h.fill(a),h.stroke(a),h.restore()},[t,e,n,o,i,r]),null),Ne=({data:s,xScaleId:t,yScaleId:e,style:n,barWidth:o,xPositionOffset:i,radius:r,layer:l="MIDDLE"})=>(O(l,({getCtx:d,valToPxDistance:u,valToPos:h,clampXPosToChartArea:a,clampYPosToChartArea:m})=>{if(s.length===0)return;const c=d();c.save(),A(c,n);const x=u(o,t);c.beginPath();for(const f of s){const v=h(f.x,t)-x/2+i*x,S=m(h(0,e)),T=m(h(f.y,e)),y=S-T,w=a(v),b=a(v+x)-w;r?c.roundRect(w,T,b,y,r):c.rect(w,T,b,y)}c.closePath(),c.fill(),n?.strokeStyle&&c.stroke(),c.restore()},[s,t,e,n,o,i,r]),null),$e=({layer:s="MIDDLE",data:t,xScaleId:e,yScaleId:n,style:o})=>(O(s,({getCtx:i,clampXPosToChartArea:r,clampYPosToChartArea:l,valToPos:d})=>{const u=[];for(const a of t){const m=r(d(a.x,e)),c=l(d(a.y[0],n)),x=l(d(a.y[1],n));u.push({x:m,y:c}),u.unshift({x:m,y:x})}const h=i();if(u.length>0){h.save(),h.beginPath(),A(h,o),h.moveTo(u[0].x,u[0].y);for(const a of u)h.lineTo(a.x,a.y);h.closePath(),h.fill(),h.restore()}},[t,e,n,o]),null),Ve=({layer:s="MIDDLE",data:t,stroked:e,xScaleId:n,yScaleId:o,style:i})=>(O(s,({getCtx:r,clampXPosToChartArea:l,clampYPosToChartArea:d,valToPos:u})=>{const h=[],a=r();for(const f of t){const p=l(u(f.x,n)),v=d(u(f.y,o));h.push({x:p,y:v})}const m=h.at(0),c=h.at(-1);if(!m||!c)return;const x=d(u(0,o));a.save(),a.beginPath(),A(a,i),a.moveTo(m.x,x);for(const f of h)a.lineTo(f.x,f.y);if(a.lineTo(c.x,x),a.closePath(),a.fill(),e){a.beginPath(),a.moveTo(m.x,m.y);for(const f of h)a.lineTo(f.x,f.y);a.stroke()}a.restore()},[t,e,n,o,i]),null),He=(s,t)=>se(({clampXPosToChartArea:e,clampYPosToChartArea:n,getScale:o,valToPos:i,valFits:r})=>{const l={};for(const d in s){const u=s[d];switch(u.exceeding){case"discard":{if(r(u.value,u.scaleId)){const h=i(u.value,u.scaleId,t);l[d]=h}break}case"clamp":{const h=i(u.value,u.scaleId,t);l[d]=o(u.scaleId)?.origin==="x"?e(h,t):n(h,t);break}}}return l}),qe=({layer:s="BOTTOM",scaleId:t,tickStyle:e,labelStyle:n,labelGap:o,tickSize:i,ticks:r})=>(O(s,({getCtx:l,valToPos:d,getScale:u,getFrame:h})=>{const a=l(),m=u(t);if(!m||!m.axis||m.origin!=="x")return;const c=m.axis,x=c.position==="top"?c.canvasRect.y+c.canvasRect.height:c.canvasRect.y,f=window.devicePixelRatio||1,p=x,v=(i??6)*f,S=c.position==="top"?x-v:x+v,T=(o??12)*f;a.save(),a.fontKerning="auto",A(a,{...e}),a.beginPath();const y=Array.isArray(r)?r:r({...m,axis:c},h());for(const{value:w}of y){const b=d(w,t,"canvas");a.moveTo(b,p),a.lineTo(b,S)}a.stroke(),a.restore(),a.save(),A(a,{textBaseline:c.position==="top"?"bottom":"top",textAlign:"center",...e,...n});for(const{value:w,label:b}of y){const k=d(w,t,"canvas"),C=b.split(`
|
|
2
|
+
`);for(let E=0;E<C.length;E++)a.fillText(C[E],k,S+f*2+E*T)}a.restore()},[r,t,e,n]),null),We=({layer:s="BOTTOM",scaleId:t,tickStyle:e,labelStyle:n,labelGap:o,tickSize:i,ticks:r})=>(O(s,({getCtx:l,valToPos:d,getScale:u,getFrame:h})=>{const a=l(),m=u(t);if(!m||!m.axis||m.origin!=="y")return;const c=m.axis,x=c.position==="left"?c.canvasRect.x+c.canvasRect.width:c.canvasRect.x,f=x,p=i??6,v=c.position==="left"?x-p:x+p,S=o??12,T=Array.isArray(r)?r:r({...m,axis:c},h());a.save(),a.fontKerning="auto",A(a,{...e}),a.beginPath();for(const{value:y}of T){const w=d(y,t,"canvas");a.moveTo(f,w),a.lineTo(v,w)}a.stroke(),a.restore(),a.save(),A(a,{textBaseline:"middle",textAlign:c.position==="left"?"right":"left",...e,...n});for(const{value:y,label:w}of T){const b=d(y,t,"canvas"),k=w.split(`
|
|
3
|
+
`);for(let C=0;C<k.length;C++)a.fillText(` ${k[C]} `,v,b+C*S)}a.restore()},[r,t,e,n]),null),L=()=>{const s=[];return{addEventListener:(t,e)=>(s.push({syncKey:t,callback:e}),()=>{const n=s.findIndex(o=>o.callback===e);n!==-1&&s.splice(n,1)}),dispatchEvent:(t,e)=>{for(const n of s)n.syncKey===t&&n.callback(t,e)}}},M={dblclick:L(),click:L(),contextmenu:L(),move:L(),mousedown:L(),mouseup:L(),spanselect:L(),documentmouseup:L(),pressandwheel:L(),sync_move:L()},D=(s,t,e)=>{const n=g.useRef(e);n.current=e,g.useEffect(()=>M[s].addEventListener(t,(i,r)=>{n.current(r,i)}),[t,s,n])},fe=g.createContext(""),ne=(s,t)=>{const e=g.useContext(fe);return D(s,e,t)},Ge=(s,t,e,n,o)=>{if(!t)return;const i=n??e.scales.find(a=>a.origin==="x")?.id,r=o??e.scales.find(a=>a.origin==="y")?.id;if(!i||!r)return;const l=s.clientX-t.left,d={scaleId:i,value:_(e,l,i,"css")},u=s.clientY-t.top,h={scaleId:r,value:_(e,u,r,"css")};return{pointerSyncPosition:{x:d,y:h},cssX:l,cssY:u}},K=(s,t)=>{const{x:e,y:n}=s,o=e&&t.scales.some(r=>r.id===e.scaleId)?j(t,e.value,e.scaleId,"css"):null,i=n&&t.scales.some(r=>r.id===n.scaleId)?j(t,n.value,n.scaleId,"css"):null;return{cssX:o,cssY:i,scaled:Object.fromEntries(t.scales.flatMap(r=>{const l=r.origin==="y"?i:o;return l===null?[]:[[r.id,_(t,l,r.id,"css")]]}))}},ve=(s,t,e)=>{const n=j(e,t.from,t.scaleId,"css"),o=j(e,t.to,t.scaleId,"css"),i=e.scales.flatMap(r=>{if(r.origin!==s)return[];const l=_(e,n,r.id,"css"),d=_(e,o,r.id,"css");return[{scaleId:r.id,from:l,to:d}]});return{fromCSS:n,toCSS:o,scaled:i}},Ze=({id:s,onClick:t,onDblClick:e,onMouseMove:n,onMouseDown:o,onMouseUp:i,onDocumentMouseUp:r,onSpanSelect:l,onContextMenu:d,className:u,style:h,sync:a,innerChildren:m,children:c})=>{const x=g.useId(),f=s||x;return D("dblclick",f,p=>{e?.(p)}),D("click",f,p=>{t?.(p)}),D("move",f,p=>{n?.(p)}),D("mousedown",f,p=>{o?.(p)}),D("mouseup",f,p=>{i?.(p)}),D("documentmouseup",f,p=>{r?.(p)}),D("spanselect",f,p=>{l?.(p)}),D("contextmenu",f,p=>{d?.(p)}),R.jsxs(fe.Provider,{value:f,children:[R.jsx(Je,{className:u,style:h,sync:a,children:m}),c]})},Je=({className:s,style:t,sync:e,children:n})=>{const o=g.useRef(null),i=se(),r=g.useRef(i);r.current=i;const l=g.useContext(fe),d=e?.key||l,u=g.useRef(null),h=g.useRef(null),a=g.useRef(null),m=()=>{const f=o.current?.parentElement;if(f){if(f.dataset.canplotroot===void 0)throw new Error("ChartAreaInteractions must be used within a CanPlot component");return f.getBoundingClientRect()}},c=(f,p)=>{const v=Ge(f,m(),r.current,e?.xViaScaleId,e?.yViaScaleId);v&&p(v.pointerSyncPosition,{cssX:v.cssX,cssY:v.cssY},{ctrlKey:f.ctrlKey,altKey:f.altKey,shiftKey:f.shiftKey,metaKey:f.metaKey})},x=g.useRef(c);return x.current=c,g.useEffect(()=>{const f=y=>{const w=h.current;w&&M.spanselect.dispatchEvent(l,{...w,completed:!0}),M.documentmouseup.dispatchEvent(l,{frame:r.current,keys:{ctrlKey:y.ctrlKey,altKey:y.altKey,shiftKey:y.shiftKey,metaKey:y.metaKey}})},p=y=>{const w={ctrlKey:y.ctrlKey,altKey:y.altKey,shiftKey:y.shiftKey,metaKey:y.metaKey},b=a.current;if(b&&Object.entries(w).some(([C,E])=>b.keys[C]!==E)){const C={...b,keys:w};a.current=C,M.sync_move.dispatchEvent(d,C)}const k=h.current;if(k&&Object.entries(w).some(([C,E])=>k.keys[C]!==E)){y.stopPropagation(),y.preventDefault();const C={...k,keys:w};h.current=C,M.spanselect.dispatchEvent(l,C)}},v=y=>{x.current(y,(w,{cssX:b,cssY:k},C)=>{const E=u.current;if(!E||!w.x||!w.y)return;const P=r.current,N=E.xRangeCss.start,F=b,ie=E.yRangeCss.start,re=k,ce=I(P,w.x.scaleId),ae=I(P,w.y.scaleId);u.current={xRangeCss:{start:N,end:F},yRangeCss:{start:ie,end:re}};let $="below_threshold";const le=Math.abs(ie-re),ue=Math.abs(N-F);le<10&&ue<10?$="below_threshold":le>30&&ue>30?$="box":le>ue?$="y":$="x";const pe={scaleId:ce.id,from:_(P,Y(r.current,N,"css"),ce.id,"css"),to:_(P,Y(r.current,F,"css"),ce.id,"css")},xe={scaleId:ae.id,from:_(P,z(r.current,ie,"css"),ae.id,"css"),to:_(P,z(r.current,re,"css"),ae.id,"css")},V=pe&&ve("x",pe,r.current),H=xe&&ve("y",xe,r.current),Me=V?.scaled,ke=H?.scaled,ye={mode:$,frame:r.current,completed:!1,x:{css:V&&{from:V.fromCSS,to:V.toCSS},scaled:Me??[]},y:{css:H&&{from:H.fromCSS,to:H.toCSS},scaled:ke??[]},keys:C};h.current=ye,M.spanselect.dispatchEvent(l,ye)})},S=y=>{x.current(y,(w,b,k)=>{const C=K(w,r.current);if(!C)return;if(Object.values(k).some(P=>P)){y.preventDefault();const P=Math.abs(y.deltaY)>Math.abs(y.deltaX)?y.deltaY:y.deltaX;M.pressandwheel.dispatchEvent(l,{pointer:C,frame:r.current,keys:k,deltaX:y.deltaX,deltaY:y.deltaY,deltaAbs:P})}})};document.addEventListener("mouseup",f),document.addEventListener("keydown",p),document.addEventListener("keyup",p),document.addEventListener("mousemove",v);const T=o.current;return T?.addEventListener("wheel",S,{passive:!1}),()=>{document.removeEventListener("mouseup",f),document.removeEventListener("keydown",p),document.removeEventListener("keyup",p),document.removeEventListener("mousemove",v),T?.removeEventListener("wheel",S)}},[r,l,d,x]),D("sync_move",d,f=>{const p=f.positions?K(f.positions,r.current):null;a.current=f,M.move.dispatchEvent(l,{frame:r.current,pointer:p??null,keys:f.keys})}),R.jsx("div",{ref:o,id:"interactions",className:s,style:{position:"absolute",left:i.chartAreaCSS.x,top:i.chartAreaCSS.y,width:i.chartAreaCSS.width,height:i.chartAreaCSS.height,zIndex:25,...t},onDragStart:f=>{f.preventDefault()},onClick:f=>{c(f,(p,v,S)=>{const T=K(p,r.current);T&&M.click.dispatchEvent(l,{pointer:T,frame:r.current,keys:S})})},onMouseLeave:f=>{c(f,(p,v,S)=>{M.sync_move.dispatchEvent(d,{positions:null,keys:S})})},onMouseMove:f=>{c(f,(p,v,S)=>{M.sync_move.dispatchEvent(d,{positions:p,keys:S})})},onMouseDown:f=>{c(f,(p,{cssX:v,cssY:S},T)=>{const y=K(p,r.current);y&&(M.mousedown.dispatchEvent(l,{pointer:y,frame:r.current,keys:T}),u.current={xRangeCss:{start:v,end:v},yRangeCss:{start:S,end:S}})})},onMouseUp:f=>{c(f,(p,v,S)=>{const T=K(p,r.current);if(!T)return;M.mouseup.dispatchEvent(l,{frame:r.current,pointer:T,keys:S});const y=h.current;h.current=null;const w=u.current;if(u.current=null,w&&y){const b={...y,keys:S,completed:!0};h.current=null,M.spanselect.dispatchEvent(l,b)}})},onContextMenu:f=>{f.preventDefault(),c(f,(p,v,S)=>{const T=K(p,r.current);T&&M.contextmenu.dispatchEvent(l,{frame:r.current,pointer:T,keys:S})})},onDoubleClick:f=>{c(f,(p,v,S)=>{const T=K(p,r.current);T&&M.dblclick.dispatchEvent(l,{frame:r.current,pointer:T,keys:S})})},children:n})},Qe=({data:s,renderTooltip:t,xScaleId:e})=>{const[n,o]=g.useState(null);ne("move",r=>{o(r)});const i=g.useMemo(()=>{if(!n)return null;const{frame:r,pointer:l}=n,d=l?.scaled[e];if(d===void 0)return null;const u=[];let h=d;for(const a of s){let m=null,c=1/0;for(const[f,p]of a.points.entries()){if(!Z(r,p.x,e)||!Z(r,p.y,a.yScaleId))continue;const v=Math.abs(p.x-d);v<c&&(c=v,m=f)}const x=a.points[m??-1];if(!x||te(r,c,e,"css")>30){u.push({seriesId:a.seriesId,y:null});continue}h=x.x,u.push({seriesId:a.seriesId,y:x.y})}return{frame:r,x:h,points:u}},[s,n,e]);return t(i)},et=({makeXStyle:s,makeXClassName:t,makeYStyle:e,makeYClassName:n})=>{const[o,i]=g.useState(null);if(ne("move",m=>{i(m)}),!o)return null;const{frame:r,pointer:l}=o,d=l?.cssX??null,u=l?.cssY??null,h=d?Y(r,d,"css"):0,a=u?z(r,u,"css"):0;return R.jsxs(R.Fragment,{children:[R.jsx("div",{"data-show":!!l,className:t?.(o),style:{position:"absolute",left:0,visibility:d===null?"hidden":"visible",top:r.chartAreaCSS.y,height:r.chartAreaCSS.height,borderColor:"red",borderLeftWidth:"1px",borderLeftStyle:"solid",pointerEvents:"none",opacity:l?1:0,transform:`translateX(${h}px)`,...s?.(o)}}),R.jsx("div",{className:n?.(o),"data-show":!!l,style:{position:"absolute",visibility:u===null?"hidden":"visible",top:0,height:0,borderTop:"solid 1px red",left:r.chartAreaCSS.x,width:r.chartAreaCSS.width,pointerEvents:"none",opacity:l?1:0,transform:`translateY(${a}px)`,...e?.(o)}})]})},tt=({makeClassName:s,makeStyle:t})=>{const[e,n]=g.useState(null);ne("spanselect",i=>{n(i.mode==="below_threshold"||i.completed?null:i)});const o=g.useMemo(()=>{if(!e||e.mode==="below_threshold")return null;const i=e.mode==="y"?-1/0:e.x.css?.from??-1/0,r=e.mode==="y"?1/0:e.x.css?.to??1/0,l=e.mode==="x"?-1/0:e.y.css?.from??-1/0,d=e.mode==="x"?1/0:e.y.css?.to??1/0,u=Y(e.frame,i,"css"),h=Y(e.frame,r,"css"),a=z(e.frame,l,"css"),m=z(e.frame,d,"css"),c=Math.min(u,h),x=Math.min(a,m),f=Math.abs(h-u),p=Math.abs(m-a);return{leftPx:c,topPx:x,widthPx:f,heightPx:p}},[e]);return R.jsx("div",{className:e?s?.(e):void 0,style:{position:"absolute",visibility:e?"visible":"hidden",left:`${o?.leftPx??0}px`,top:`${o?.topPx??0}px`,width:`${o?.widthPx??0}px`,height:`${o?.heightPx??0}px`,pointerEvents:"none",...e?t?.(e):void 0}})},st=({style:s,children:t,scaleId:e,...n})=>{const o=se(i=>i.getScale(e)?.axis);return o?R.jsx("div",{style:{position:"absolute",backgroundColor:"#0000ff11",left:o.cssRect.x,top:o.cssRect.y,height:o.cssRect.height,width:o.cssRect.width,...s},...n,children:t}):null},be=60,nt=30,Te="UTC",ot="en-GB",it=({space:s,formatter:t,acceptableIncrements:e}={})=>(n,o)=>{const{min:i,max:r}=n,l=[],d=window.devicePixelRatio||1,u=(s??(n.origin==="x"?be:nt))*d,h=Se(o,u,n.id,"canvas"),a=e??q,m=a.find(x=>x>h)??a.at(-1)??1;let c=i;if(Math.abs(c%m)>Number.EPSILON){const x=(m-c%m)%m;c+=x}for(;c<=r&&l.length<1e3;)l.push(c),c+=m;return(t??Ce)(l)},Ce=s=>{const t=Math.max(0,Math.ceil(-Math.log10(s[1]-s[0])));return s.map(e=>({value:e,label:e.toFixed(t)}))},q=[];for(let s=-12;s<=12;s++)q.push(1*10**s),q.push(2*10**s),q.push(5*10**s);const rt=1,Pe=1e3*rt,me=60*Pe,Q=60*me,oe=24*Q,ct=30*oe,at=365*oe,lt=[[1,"milliseconds"],[2,"milliseconds"],[5,"milliseconds"],[10,"milliseconds"],[20,"milliseconds"],[50,"milliseconds"],[100,"milliseconds"],[200,"milliseconds"],[500,"milliseconds"],[1,"seconds"],[5,"seconds"],[10,"seconds"],[15,"seconds"],[30,"seconds"],[1,"minutes"],[5,"minutes"],[10,"minutes"],[15,"minutes"],[30,"minutes"],[1,"hours"],[2,"hours"],[3,"hours"],[4,"hours"],[6,"hours"],[8,"hours"],[12,"hours"],[1,"days"],[3,"days"],[5,"days"],[7,"days"],[10,"days"],[15,"days"],[1,"months"],[2,"months"],[3,"months"],[4,"months"],[6,"months"],[1,"years"],[2,"years"],[5,"years"],[10,"years"],[25,"years"],[50,"years"],[100,"years"]],W=s=>{const[t,e]=s;switch(e){case"milliseconds":return t;case"seconds":return t*Pe;case"minutes":return t*me;case"hours":return t*Q;case"days":return t*oe;case"months":return t*ct;case"years":return t*at}},ut=(s,t)=>{const e=new Date(s);return e.setUTCMilliseconds(e.getUTCMilliseconds()+t),e.getTime()},dt=(s,t)=>{const e=new Date(s);return e.setUTCSeconds(e.getUTCSeconds()+t),e.getTime()},ht=(s,t)=>{const e=new Date(s);return e.setUTCMinutes(e.getUTCMinutes()+t),e.getTime()},ft=(s,t)=>{const e=new Date(s);return e.setUTCHours(e.getUTCHours()+t),e.getTime()},mt=(s,t)=>{const e=new Date(s);return e.setUTCDate(e.getUTCDate()+t),e.getTime()},ge=(s,t)=>{const e=new Date(s);return e.setUTCMonth(e.getUTCMonth()+t),e.getTime()},X=(s,t)=>{const[e,n]=t;switch(n){case"milliseconds":return ut(s,e);case"seconds":return dt(s,e);case"minutes":return ht(s,e);case"hours":return ft(s,e);case"days":return mt(s,e);case"months":return ge(s,e);case"years":return ge(s,e*12)}};function G(s,t){const e=new Date(s),n=new Date(e.toLocaleString("en-US",{timeZone:t})),o=new Date(e.toLocaleString("en-US",{timeZone:"UTC"}));return(n.getTime()-o.getTime())/(3600*1e3)}const pt=(s,t,e="UTC")=>{const[n,o]=t;let i=new Date(s);const r=()=>{i.setUTCHours(-G(i,e),0,0,0)};switch(o){case"milliseconds":i.setUTCMilliseconds(Math.ceil(i.getUTCMilliseconds()/n)*n);break;case"seconds":i.setUTCSeconds(Math.ceil(i.getUTCSeconds()/n)*n,0);break;case"minutes":i.setUTCMinutes(Math.ceil(i.getTime()%Q/me/n)*n,0,0);break;case"hours":i.setUTCHours(Math.ceil(i.getTime()%oe/Q/n)*n,0,0,0);break;case"days":case"months":case"years":o==="months"?i.setUTCDate(1):o==="years"&&i.setUTCMonth(0,1),r(),i.getTime()<s&&(i=new Date(X(i,[1,o])));break}return i.getTime()},xt=({timeZone:s=Te,space:t=be,formatter:e,locale:n,showTimezone:o}={})=>(i,r)=>{const{min:l,max:d}=i,u=Math.floor(r.chartAreaCanvasPX.width/t)+1,a=(d-l)/u,[m,c]=lt.find(S=>W(S)>=a)??[1,"milliseconds"],x=pt(l,[m,c],s),f=G(x,s),p=[x];let v;for(;;){switch(c){case"milliseconds":case"seconds":case"minutes":case"hours":{v=X(x,[p.length*m,c]);break}case"days":{const S=X(x,[p.length*m,c]);v=X(S,[f-G(S,s),"hours"]);break}case"months":case"years":{const S=X(X(X(x,[f,"hours"]),[p.length*m,c]),[-f,"hours"]);v=X(S,[f-G(S,s),"hours"]);break}}if(v>d)break;p.push(v)}return(e??Ee({locale:n,showTimezone:o,timeZone:s}))(p)},U=(s,t,e)=>s.find(n=>n.type===e)?.value!==t.find(n=>n.type===e)?.value,Ee=({timeZone:s=Te,locale:t=ot,showTimezone:e=!0})=>{const n=new Intl.DateTimeFormat(t,{year:"numeric",day:"numeric",month:"short",hour:"numeric",hourCycle:"h23",minute:"numeric",second:"numeric",fractionalSecondDigits:3,timeZoneName:"short",timeZone:s});return o=>{const i=o[1]-o[0],r=i<W([1,"days"]),l=i<W([1,"minutes"]),d=i<W([1,"seconds"]);return o.map(u=>({value:u,label:n.formatToParts(new Date(u))})).map((u,h,a)=>{const m=a[h-1],c=h===0||U(u.label,m.label,"year"),x=h===0||U(u.label,m.label,"day"),f=h===0||U(u.label,m.label,"month"),p=h===0||U(u.label,m.label,"hour"),v=h===0||U(u.label,m.label,"timeZoneName"),S=h===0||U(u.label,m.label,"minute"),T=h===0||U(u.label,m.label,"second"),y=h===0||U(u.label,m.label,"fractionalSecond"),w=[];if(r&&(p||S||v||T||y)){const b=u.label.find(P=>P.type==="hour")?.value,k=u.label.find(P=>P.type==="minute")?.value,C=u.label.find(P=>P.type==="timeZoneName")?.value;let E="";if(l){const P=u.label.find(F=>F.type==="second")?.value,N=u.label.find(F=>F.type==="fractionalSecond")?.value;E=`:${P}`+(d?`.${N}`:"")}w.push(`${b}:${k}${E}`+(e&&v?` ${C}`:""))}return(x||f)&&w.push([u.label.find(b=>b.type==="month")?.value,x&&u.label.find(b=>b.type==="day")?.value].filter(Boolean).join(" ")),c&&w.push(u.label.find(b=>b.type==="year")?.value),{value:u.value,label:w.filter(b=>b).join(`
|
|
4
|
+
`)}})}};exports.AreaPlot=$e;exports.AxisOverlay=st;exports.BarPlot=Ne;exports.CANPLOT_LAYER=J;exports.CanPlot=Fe;exports.ChartAreaInteractions=Ze;exports.Crosshair=et;exports.LinePlot=Be;exports.ScatterPlot=je;exports.SelectBox=tt;exports.SparklinePlot=Ve;exports.TooltipsX=Qe;exports.XTicks=qe;exports.YTicks=We;exports.applyStyles=A;exports.clamp=B;exports.clampUnfit=Le;exports.clampXPosToChartArea=Y;exports.clampYPosToChartArea=z;exports.defaultNumericalTicksFormatter=Ce;exports.findClosestIndex=Ae;exports.getScale=I;exports.makeLinearTicks=it;exports.makeTimeTickFormat=Ee;exports.makeTimeTicks=xt;exports.posToVal=_;exports.pxToValDistance=Se;exports.sum=Re;exports.useDrawEffect=O;exports.useFrameState=se;exports.useInteractionsEvent=ne;exports.useXPositioned=He;exports.valFits=Z;exports.valToPos=j;exports.valToPxDistance=te;
|
|
5
5
|
//# sourceMappingURL=canplot.cjs.map
|