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