@canplot/react 0.1.3 → 0.1.4

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/axes.d.ts CHANGED
@@ -1,18 +1,3 @@
1
1
  import type { PlotDrawFrame } from "./types";
2
2
  export declare const drawAxes: (plotDrawFrame: PlotDrawFrame) => void;
3
- export type PlotAxisGenTicksOpts = {
4
- frame: PlotDrawFrame;
5
- scaleId: string;
6
- };
7
- export type PlotAxisTickFormatOpts = {
8
- frame: PlotDrawFrame;
9
- scaleId: string;
10
- ticks: number[];
11
- };
12
- export type PlotAxisGenTicks = (opts: PlotAxisGenTicksOpts) => number[];
13
- export type PlotAxisTickFormat = (opts: PlotAxisTickFormatOpts) => string[];
14
- export declare const makeGenTicksDefault: ({ space, }?: {
15
- space?: number;
16
- }) => PlotAxisGenTicks;
17
- export declare const formatTicksDefault: PlotAxisTickFormat;
18
3
  //# sourceMappingURL=axes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"axes.d.ts","sourceRoot":"","sources":["../src/lib/axes.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAS7C,eAAO,MAAM,QAAQ,GAAI,eAAe,aAAa,SA8DpD,CAAC;AA8GF,MAAM,MAAM,oBAAoB,GAAG;IACjC,KAAK,EAAE,aAAa,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,KAAK,EAAE,aAAa,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,oBAAoB,KAAK,MAAM,EAAE,CAAC;AACxE,MAAM,MAAM,kBAAkB,GAAG,CAAC,IAAI,EAAE,sBAAsB,KAAK,MAAM,EAAE,CAAC;AAE5E,eAAO,MAAM,mBAAmB,GAAI,aAEjC;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAO,KAAG,gBA4B5B,CAAC;AAEF,eAAO,MAAM,kBAAkB,EAAE,kBAGhC,CAAC"}
1
+ {"version":3,"file":"axes.d.ts","sourceRoot":"","sources":["../src/lib/axes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,QAAQ,GAAI,eAAe,aAAa,SA0CpD,CAAC"}
package/dist/canplot.cjs CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A=require("react/jsx-runtime"),S=require("react"),ct=require("zustand"),xt=5,St=10,gt=60,kt=30,vt="UTC",Et="en-GB",W=(t,s,e)=>Math.min(Math.max(t,s),e),wt=(t,s,e,o)=>{const{min:i,max:n}=R(t,e),r=o==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,a=(B(t,e)?r.width:r.height)/(n-i);return s/a},R=(t,s)=>{const e=t.scales.find(o=>o.id===s);if(!e)throw new Error(`Scale ${s} not found`);return e},B=(t,s)=>t.scales.find(e=>e.id===s)?.origin==="x",Pt=(t,s)=>t.scales.find(e=>e.id===s)?.origin==="y",U=(t,s)=>{const e=window.devicePixelRatio||1;t.lineCap=s?.lineCap??"butt",t.lineDashOffset=e*(s?.lineDashOffset??0),t.lineJoin=s?.lineJoin??"miter",t.lineWidth=e*(s?.lineWidth??1),t.miterLimit=e*(s?.miterLimit??10),t.strokeStyle=s?.strokeStyle??"black",t.fillStyle=s?.fillStyle??t.strokeStyle,t.font=s?.font??`${10*e}px sans-serif`,t.textAlign=s?.textAlign??"start",t.direction=s?.direction??"inherit",t.textBaseline=s?.textBaseline??"alphabetic",t.fontKerning=s?.fontKerning??"auto"},J=(t,s,e,o)=>{const i=o==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,{min:n,max:r}=R(t,e),a=(B(t,e)?i.width:i.height)/(r-n);return s*a},D=(t,s,e,o)=>{const{min:i}=R(t,e),n=o==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,r=J(t,s-i,e,o);return B(t,e)?W(n.x+r,n.x-10*n.width,n.x+11*n.width):W(n.y+n.height-r,n.y-10*n.height,n.y+11*n.height)},Y=(t,s,e)=>{const o=e==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS;return W(s,o.x,o.x+o.width)},j=(t,s,e)=>{const o=e==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS;return W(s,o.y,o.y+o.height)},G=(t,s,e)=>{const{min:o,max:i}=R(t,e);return s>=o&&s<=i},L=(t,s,e,o)=>{const{min:i,max:n}=R(t,e),r=o==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,a=B(t,e)?(s-r.x)/r.width:(r.height-s+r.y)/r.height;return i+a*(n-i)},Mt=1,Ct=1e3*Mt,rt=60*Ct,q=60*rt,Q=24*q,At=30*Q,Rt=365*Q,_t=[[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"]],V=t=>{const[s,e]=t;switch(e){case"milliseconds":return s;case"seconds":return s*Ct;case"minutes":return s*rt;case"hours":return s*q;case"days":return s*Q;case"months":return s*At;case"years":return s*Rt}},Lt=(t,s)=>{const e=new Date(t);return e.setUTCMilliseconds(e.getUTCMilliseconds()+s),e.getTime()},Ut=(t,s)=>{const e=new Date(t);return e.setUTCSeconds(e.getUTCSeconds()+s),e.getTime()},Dt=(t,s)=>{const e=new Date(t);return e.setUTCMinutes(e.getUTCMinutes()+s),e.getTime()},Xt=(t,s)=>{const e=new Date(t);return e.setUTCHours(e.getUTCHours()+s),e.getTime()},Kt=(t,s)=>{const e=new Date(t);return e.setUTCDate(e.getUTCDate()+s),e.getTime()},ft=(t,s)=>{const e=new Date(t);return e.setUTCMonth(e.getUTCMonth()+s),e.getTime()},z=(t,s)=>{const[e,o]=s;switch(o){case"milliseconds":return Lt(t,e);case"seconds":return Ut(t,e);case"minutes":return Dt(t,e);case"hours":return Xt(t,e);case"days":return Kt(t,e);case"months":return ft(t,e);case"years":return ft(t,e*12)}};function H(t,s){const e=new Date(t),o=new Date(e.toLocaleString("en-US",{timeZone:s})),i=new Date(e.toLocaleString("en-US",{timeZone:"UTC"}));return(o.getTime()-i.getTime())/(3600*1e3)}const It=(t,s,e="UTC")=>{const[o,i]=s;let n=new Date(t);const r=()=>{n.setUTCHours(-H(n,e),0,0,0)};switch(i){case"milliseconds":n.setUTCMilliseconds(Math.ceil(n.getUTCMilliseconds()/o)*o);break;case"seconds":n.setUTCSeconds(Math.ceil(n.getUTCSeconds()/o)*o,0);break;case"minutes":n.setUTCMinutes(Math.ceil(n.getTime()%q/rt/o)*o,0,0);break;case"hours":n.setUTCHours(Math.ceil(n.getTime()%Q/q/o)*o,0,0,0);break;case"days":case"months":case"years":i==="months"?n.setUTCDate(1):i==="years"&&n.setUTCMonth(0,1),r(),n.getTime()<t&&(n=new Date(z(n,[1,i])));break}return n.getTime()},zt=({timeZone:t=vt,space:s=gt})=>({frame:e,scaleId:o})=>{const{min:i,max:n}=R(e,o),r=Math.floor(e.chartAreaCanvasPX.width/s)+1,u=(n-i)/r,[l,d]=_t.find(f=>V(f)>=u)??[1,"milliseconds"],m=It(i,[l,d],t),p=H(m,t),h=[m];let c;for(;;){switch(d){case"milliseconds":case"seconds":case"minutes":case"hours":{c=z(m,[h.length*l,d]);break}case"days":{const f=z(m,[h.length*l,d]);c=z(f,[p-H(f,t),"hours"]);break}case"months":case"years":{const f=z(z(z(m,[p,"hours"]),[h.length*l,d]),[-p,"hours"]);c=z(f,[p-H(f,t),"hours"]);break}}if(c>n)break;h.push(c)}return h},I=(t,s,e)=>t.find(o=>o.type===e)?.value!==s.find(o=>o.type===e)?.value,Ft=({timeZone:t=vt,locale:s=Et,showTimezone:e=!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:t});return({ticks:i})=>{const n=i[1]-i[0],r=n<V([1,"days"]),a=n<V([1,"minutes"]),u=n<V([1,"seconds"]);return i.map(l=>o.formatToParts(new Date(l))).map((l,d,m)=>{const p=m[d-1],h=d===0||I(l,p,"year"),c=d===0||I(l,p,"day"),f=d===0||I(l,p,"month"),y=d===0||I(l,p,"hour"),x=d===0||I(l,p,"timeZoneName"),T=d===0||I(l,p,"minute"),g=d===0||I(l,p,"second"),v=d===0||I(l,p,"fractionalSecond"),E=[];if(r&&(y||T||x||g||v)){const C=l.find(_=>_.type==="hour")?.value,P=l.find(_=>_.type==="minute")?.value,M=l.find(_=>_.type==="timeZoneName")?.value;let X="";if(a){const _=l.find(F=>F.type==="second")?.value,$=l.find(F=>F.type==="fractionalSecond")?.value;X=`:${_}`+(u?`.${$}`:"")}E.push(`${C}:${P}${X}`+(e&&x?` ${M}`:""))}return(c||f)&&E.push([l.find(C=>C.type==="month")?.value,c&&l.find(C=>C.type==="day")?.value].filter(Boolean).join(" ")),h&&E.push(l.find(C=>C.type==="year")?.value),E.filter(C=>C).join(`
2
- `)})}},Z=[];for(let t=-12;t<=12;t++)Z.push(1*10**t),Z.push(2*10**t),Z.push(5*10**t);const Yt=t=>{const{ctx:s,scales:e}=t;s.save(),s.strokeStyle="black",s.fillStyle="black",s.lineWidth=1,s.font="12px Arial",s.textAlign="center",s.textBaseline="middle";for(const o of e){if(!o.axis)continue;const i=o.type==="time"?zt({space:o.axis.tickSpace,timeZone:o.timeZone}):jt({space:o.axis.tickSpace}),n=o.type==="time"?Ft({timeZone:o.timeZone,showTimezone:o.axis.showTimezone,locale:o.locale}):Ot,r=o.axis.canvasRect;if(o.origin==="x"){if(o.axis.position==="bottom")s.beginPath(),s.moveTo(r.x,r.y),s.lineTo(r.x+r.width,r.y),s.stroke(),mt(t,o.id,r.y,i,n);else if(o.axis.position==="top"){const a=r.y+r.height;s.beginPath(),s.moveTo(r.x,a),s.lineTo(r.x+r.width,a),s.stroke(),mt(t,o.id,a,i,n)}}else if(o.axis.position==="left"){const a=r.x+r.width;s.beginPath(),s.moveTo(a,r.y),s.lineTo(a,r.y+r.height),s.stroke(),pt(t,o.id,a,i,n)}else o.axis.position==="right"&&(s.beginPath(),s.moveTo(r.x,r.y),s.lineTo(r.x,r.y+r.height),s.stroke(),pt(t,o.id,r.x,i,n))}s.restore()},pt=(t,s,e,o,i)=>{const{ctx:n}=t,r=t.scales.find(c=>c.id===s)?.axis;if(!r)return;const a=window.devicePixelRatio||1,u=xt,l=e,d=r.position==="left"?e-u:e+u,m=St*a,p=o({frame:t,scaleId:s})??[],h=i({frame:t,scaleId:s,ticks:p});n.save(),n.fontKerning="auto",U(n,{}),n.beginPath();for(let c=0;c<p.length;c++){const f=D(t,p[c],s,"canvas");n.moveTo(l,f),n.lineTo(d,f)}n.stroke(),n.restore(),n.save(),U(n,{textBaseline:"middle",textAlign:r.position==="left"?"right":"left"});for(let c=0;c<p.length;c++){const f=D(t,p[c],s,"canvas"),y=h[c].split(`
3
- `);for(let x=0;x<y.length;x++)n.fillText(` ${y[x]} `,d,f+x*m)}n.restore()},mt=(t,s,e,o,i)=>{const{ctx:n}=t,r=t.scales.find(c=>c.id===s)?.axis;if(!r)return;const a=window.devicePixelRatio||1,u=xt,l=e,d=r.position==="top"?e-u:e+u,m=St*a,p=o({frame:t,scaleId:s})??[],h=i({frame:t,scaleId:s,ticks:p});n.save(),n.fontKerning="auto",U(n,{}),n.beginPath();for(let c=0;c<p.length;c++){const f=D(t,p[c],s,"canvas");n.moveTo(f,l),n.lineTo(f,d)}n.stroke(),n.restore(),n.save(),U(n,{textBaseline:r.position==="top"?"bottom":"top",textAlign:"center"});for(let c=0;c<p.length;c++){const f=D(t,p[c],s,"canvas"),y=h[c].split(`
4
- `);for(let x=0;x<y.length;x++)n.fillText(y[x],f,d+a*2+x*m)}n.restore()},jt=({space:t}={})=>({frame:s,scaleId:e})=>{const{min:o,max:i}=R(s,e),n=[],r=window.devicePixelRatio||1,a=(t??(B(s,e)?gt:kt))*r,u=wt(s,a,e,"canvas"),l=Z.find(m=>m>u)??1;let d=o%l<Number.EPSILON?o:o+l-o%l;for(;d<=i;)n.push(d),d+=l;return n},Ot=({ticks:t})=>{const s=Math.max(0,Math.ceil(-Math.log10(t[1]-t[0])));return t.map(e=>e.toFixed(s))},Bt=()=>ct.createStore((t,s)=>{const e=()=>{const o=s()._frame;if(!o)throw new Error("No frame set in frame store");return o};return{_frame:null,getFrame:e,getCtx:()=>e().ctx,clampXPosToChartArea:(o,i)=>Y(e(),o,i??"canvas"),clampYPosToChartArea:(o,i)=>j(e(),o,i??"canvas"),valToPos:(o,i,n)=>D(e(),o,i,n??"canvas"),valToPxDistance:(o,i,n)=>J(e(),o,i,n??"canvas"),valFits:(o,i)=>G(e(),o,i),getScale:o=>R(e(),o)}}),at=S.createContext(null),N=(t,s)=>{const e=S.useContext(at);if(!e)throw new Error("useFrame must be used within a CanPlot component");const o=S.useRef(t);o.current=t,S.useLayoutEffect(()=>{o.current(e.getState()),e.subscribe(i=>{i._frame&&o.current(i)})},[e,...s])},lt=t=>{const s=S.useContext(at);if(!s)throw new Error("useFrame must be used within a CanPlot component");return ct.useStore(s,t??(e=>e.getFrame()))};function $t(t){return s=>{for(const e of t)it(e,s)}}function Nt(t){return s=>{const e=[];for(const o of t){const i=it(o,s),n=typeof i=="function";e.push(n?i:()=>it(o,null))}return()=>{for(const o of e)o()}}}function it(t,s){if(typeof t=="function")return t(s);t&&(t.current=s)}var Vt=parseInt(S.version.split(".")[0],10)>=19?Nt:$t;const Ht=S.forwardRef(({configuration:t,children:s,style:e,className:o},i)=>{const n=S.useRef(null),r=S.useRef(null),a=Wt(r),u=S.useMemo(Bt,[]);S.useLayoutEffect(()=>{u.setState({_frame:Gt(t,a,n.current)})},[t,a,n,u]),S.useLayoutEffect(()=>u.subscribe(d=>{d._frame&&qt(d._frame)}),[u]);const l=window.devicePixelRatio||1;return A.jsxs("div",{ref:Vt([i,r]),className:o,style:{position:"relative",overflow:"hidden",...e},"data-canplotroot":!0,children:[A.jsx("canvas",{ref:n,width:a.width*l,height:a.height*l,style:{inset:0,position:"absolute",width:`${a.width}px`,height:`${a.height}px`}}),A.jsx(Zt,{frameStore:u,children:s})]})}),Zt=({frameStore:t,children:s})=>ct.useStore(t,o=>!!o._frame)?A.jsx(at.Provider,{value:t,children:s}):null,Wt=t=>{const[s,e]=S.useState({width:0,height:0}),[o]=S.useState(()=>new ResizeObserver(i=>{for(const n of i){const r=Math.round(n.contentRect.width),a=Math.round(n.contentRect.height);e(u=>u.width!==r||u.height!==a?{...u,width:r,height:a}:u)}}));return S.useLayoutEffect(()=>{if(!t.current)return;const i=t.current.clientWidth,n=t.current.clientHeight;return e(r=>r.width!==i||r.height!==n?{...r,width:i,height:n}:r),o.observe(t.current,{box:"border-box"}),()=>o.disconnect()},[o,t]),s},Gt=(t,s,e)=>{const o=e?.getContext("2d");if(!o)return null;const i=window.devicePixelRatio||1;if(s.width===0||s.height===0)return null;const n={x:t.padding.left,y:t.padding.top,width:s.width-t.padding.left-t.padding.right,height:s.height-t.padding.top-t.padding.bottom};for(const h of t.scales)h.axis&&(h.origin==="x"?(h.axis.position==="bottom"||h.axis.position==="top")&&(n.height=Math.max(0,n.height-h.axis.size),h.axis.position==="top"&&(n.y+=h.axis.size)):(h.axis.position==="left"||h.axis.position==="right")&&(n.width=Math.max(0,n.width-h.axis.size),h.axis.position==="left"&&(n.x+=h.axis.size)));const r={x:n.x*i,y:n.y*i,width:n.width*i,height:n.height*i},a=[];let u=t.padding.left*i,l=o.canvas.width-t.padding.right*i,d=o.canvas.height-t.padding.bottom*i,m=t.padding.top*i;for(const h of t.scales){if(!h.axis){a.push({...h,axis:null});continue}let c;if(h.origin==="x")switch(h.axis.position){case"bottom":d-=h.axis.size*i,c={x:n.x,y:d/i,width:n.width,height:h.axis.size};break;case"top":m+=h.axis.size*i,c={x:n.x,y:m/i-h.axis.size,width:n.width,height:h.axis.size};break;case"left":case"right":throw new Error("Invalid axis position for x origin")}else switch(h.axis.position){case"left":u+=h.axis.size*i,c={x:u/i-h.axis.size,y:n.y,width:h.axis.size,height:n.height};break;case"right":l-=h.axis.size*i,c={x:l/i,y:n.y,width:h.axis.size,height:n.height};break;case"top":case"bottom":throw new Error("Invalid axis position for y origin")}const f={x:c.x*i,y:c.y*i,width:c.width*i,height:c.height*i};a.push({...h,axis:{...h.axis,cssRect:c,canvasRect:f}})}return{ctx:o,dpr:i,padding:t.padding,scales:a,chartAreaCSS:n,chartAreaCanvasPX:r}},qt=t=>{t.ctx.clearRect(0,0,t.ctx.canvas.width,t.ctx.canvas.height),Yt(t)},Jt=({data:t,xScaleId:s,yScaleId:e,style:o})=>(N(({getCtx:i,clampXPosToChartArea:n,clampYPosToChartArea:r,valToPos:a})=>{const u=i();u.save(),u.beginPath(),U(u,o);for(const l of t){const d=n(a(l.x,s)),m=r(a(l.y,e));u.lineTo(d,m)}u.stroke(),u.restore()},[t,s,e,o]),null),Qt=({data:t,xScaleId:s,yScaleId:e,radius:o=5,style:i})=>(N(({getCtx:n,valToPos:r,valFits:a})=>{const u=n();u.save(),u.beginPath(),U(u,i);for(const l of t){if(!a(l.x,s)||!a(l.y,e))continue;const d=r(l.x,s),m=r(l.y,e);u.moveTo(d+o,m),u.arc(d,m,o,0,Math.PI*2)}u.stroke(),u.fill(),u.restore()},[t,s,e,o,i]),null),te=({data:t,xScaleId:s,yScaleId:e,style:o,barWidth:i,xPositionOffset:n,radius:r})=>(N(({getCtx:a,valToPxDistance:u,valToPos:l,clampXPosToChartArea:d,clampYPosToChartArea:m})=>{if(t.length===0)return;const p=a();p.save(),U(p,o);const h=u(i,s);p.beginPath();for(const c of t){const y=l(c.x,s)-h/2+n*h,x=m(l(0,e)),T=m(l(c.y,e)),g=x-T,v=d(y),E=d(y+h)-v;r?p.roundRect(v,T,E,g,r):p.rect(v,T,E,g)}p.closePath(),p.fill(),o?.strokeStyle&&p.stroke(),p.restore()},[t,s,e,o,i,n,r]),null),ee=({data:t,xScaleId:s,yScaleId:e,style:o})=>(N(({getCtx:i,clampXPosToChartArea:n,clampYPosToChartArea:r,valToPos:a})=>{const u=[];for(const d of t){const m=n(a(d.x,s)),p=r(a(d.y[0],e)),h=r(a(d.y[1],e));u.push({x:m,y:p}),u.unshift({x:m,y:h})}const l=i();if(u.length>0){l.save(),l.beginPath(),U(l,o),l.moveTo(u[0].x,u[0].y);for(const d of u)l.lineTo(d.x,d.y);l.closePath(),l.fill(),l.restore()}},[t,s,e,o]),null),se=({data:t,stroked:s,xScaleId:e,yScaleId:o,style:i})=>(N(({getCtx:n,clampXPosToChartArea:r,clampYPosToChartArea:a,valToPos:u})=>{const l=[],d=n();for(const c of t){const f=r(u(c.x,e)),y=a(u(c.y,o));l.push({x:f,y})}const m=l.at(0),p=l.at(-1);if(!m||!p)return;const h=a(u(0,o));d.save(),d.beginPath(),U(d,i),d.moveTo(m.x,h);for(const c of l)d.lineTo(c.x,c.y);if(d.lineTo(p.x,h),d.closePath(),d.fill(),s){d.beginPath(),d.moveTo(m.x,m.y);for(const c of l)d.lineTo(c.x,c.y);d.stroke()}d.restore()},[t,s,e,o,i]),null),ne=(t,s)=>lt(({clampXPosToChartArea:e,clampYPosToChartArea:o,getScale:i,valToPos:n,valFits:r})=>{const a={};for(const u in t){const l=t[u];switch(l.exceeding){case"discard":{if(r(l.value,l.scaleId)){const d=n(l.value,l.scaleId,s);a[u]=d}break}case"clamp":{const d=n(l.value,l.scaleId,s);a[u]=i(l.scaleId)?.origin==="x"?e(d,s):o(d,s);break}}}return a}),b=()=>{const t=[];return{addEventListener:(s,e)=>(t.push({syncKey:s,callback:e}),()=>{const o=t.findIndex(i=>i.callback===e);o!==-1&&t.splice(o,1)}),dispatchEvent:(s,e)=>{for(const o of t)o.syncKey===s&&o.callback(s,e)}}},w={dblclick:b(),click:b(),move:b(),mousedown:b(),mouseup:b(),spanselect:b(),documentmouseup:b(),pressandwheel:b(),sync_dblclick:b(),sync_click:b(),sync_move:b(),sync_mousedown:b(),sync_mouseup:b(),sync_spanselect:b(),sync_pressandwheel:b()},k=(t,s,e)=>{const o=S.useRef(e);o.current=e,S.useEffect(()=>w[t].addEventListener(s,(n,r)=>{o.current(r,n)}),[s,t,o])},ut=S.createContext(""),dt=(t,s)=>{const e=S.useContext(ut);return k(t,e,s)},oe=(t,s,e,o,i)=>{if(!s)return;const n=o??e.scales.find(m=>m.origin==="x")?.id,r=i??e.scales.find(m=>m.origin==="y")?.id;if(!n||!r)return;const a=t.clientX-s.left,u={scaleId:n,value:L(e,a,n,"css")},l=t.clientY-s.top,d={scaleId:r,value:L(e,l,r,"css")};return{pointerSyncPosition:{x:u,y:d},cssX:a,cssY:l}},O=(t,s)=>{const e=t.x?D(s,t.x.value,t.x.scaleId,"css"):0,o=t.y?D(s,t.y.value,t.y.scaleId,"css"):0;return{cssX:e,cssY:o,scaled:Object.fromEntries(s.scales.map(i=>{const n=i.origin==="y"?o:e;return[i.id,L(s,n,i.id,"css")]}))}},yt=(t,s,e)=>{if(!s){const r=t==="x"?e.chartAreaCSS.x:e.chartAreaCSS.y,a=t==="x"?e.chartAreaCSS.x+e.chartAreaCSS.width:e.chartAreaCSS.y+e.chartAreaCSS.height;return{fromCSS:r,toCSS:a,scaled:e.scales.flatMap(u=>{if(u.origin!==t)return[];const{min:l,max:d}=R(e,u.id);return[{scaleId:u.id,from:l,to:d}]})}}const o=D(e,s.from,s.scaleId,"css"),i=D(e,s.to,s.scaleId,"css"),n=e.scales.flatMap(r=>{if(r.origin!==t)return[];const a=L(e,o,r.id,"css"),u=L(e,i,r.id,"css");return[{scaleId:r.id,from:a,to:u}]});return{fromCSS:o,toCSS:i,scaled:n}},ie=({id:t,onClick:s,onDblClick:e,onMouseMove:o,onMouseDown:i,onMouseUp:n,onDocumentMouseUp:r,onSpanSelect:a,className:u,style:l,sync:d,children:m})=>{const p=S.useId(),h=t||p;return k("dblclick",h,c=>{e?.(c)}),k("click",h,c=>{s?.(c)}),k("move",h,c=>{o?.(c)}),k("mousedown",h,c=>{i?.(c)}),k("mouseup",h,c=>{n?.(c)}),k("documentmouseup",h,c=>{r?.(c)}),k("spanselect",h,c=>{a?.(c)}),A.jsxs(ut.Provider,{value:h,children:[A.jsx(ce,{className:u,style:l,sync:d}),m]})},ce=({className:t,style:s,sync:e})=>{const o=S.useRef(null),i=lt(),n=S.useRef(i);n.current=i;const r=S.useContext(ut),a=e?.key||r,u=S.useRef(null),l=S.useRef(null),d=S.useRef(null),m=()=>{const c=o.current?.parentElement;if(c){if(c.dataset.canplotroot===void 0)throw new Error("ChartAreaInteractions must be used within a CanPlot component");return c.getBoundingClientRect()}},p=(c,f)=>{const y=oe(c,m(),n.current,e?.xViaScaleId,e?.yViaScaleId);y&&f(y.pointerSyncPosition,{cssX:y.cssX,cssY:y.cssY},{ctrlKey:c.ctrlKey,altKey:c.altKey,shiftKey:c.shiftKey,metaKey:c.metaKey})},h=S.useRef(p);return h.current=p,S.useEffect(()=>{const c=g=>{const v=l.current;v&&w.sync_spanselect.dispatchEvent(a,{...v,completed:!0}),w.documentmouseup.dispatchEvent(a,{frame:n.current,keys:{ctrlKey:g.ctrlKey,altKey:g.altKey,shiftKey:g.shiftKey,metaKey:g.metaKey}})},f=g=>{const v={ctrlKey:g.ctrlKey,altKey:g.altKey,shiftKey:g.shiftKey,metaKey:g.metaKey},E=d.current;if(E&&Object.entries(v).some(([P,M])=>E.keys[P]!==M)){const P={...E,keys:v};d.current=P,w.sync_move.dispatchEvent(a,P)}const C=l.current;if(C&&Object.entries(v).some(([P,M])=>C.keys[P]!==M)){g.stopPropagation(),g.preventDefault();const P={...C,keys:v};l.current=P,w.sync_spanselect.dispatchEvent(a,P)}},y=g=>{h.current(g,(v,{cssX:E,cssY:C},P)=>{const M=u.current;if(!M||!v.x||!v.y)return;const X=n.current,_=M.xRangeCss.start,$=E,F=M.yRangeCss.start,tt=C,et=R(X,v.x.scaleId),st=R(X,v.y.scaleId);u.current={xRangeCss:{start:_,end:$},yRangeCss:{start:F,end:tt}};let K="none";const nt=Math.abs(F-tt),ot=Math.abs(_-$);nt<10&&ot<10?K="none":nt>30&&ot>30?K="box":nt>ot?K="y":K="x";const Tt=K==="x"||K==="box"?{scaleId:et.id,from:L(X,Y(n.current,_,"css"),et.id,"css"),to:L(X,Y(n.current,$,"css"),et.id,"css")}:void 0,bt=K==="y"||K==="box"?{scaleId:st.id,from:L(X,j(n.current,F,"css"),st.id,"css"),to:L(X,j(n.current,tt,"css"),st.id,"css")}:void 0,ht={mode:K,xRange:Tt,yRange:bt,completed:!1,keys:P};l.current=ht,w.sync_spanselect.dispatchEvent(a,ht)})},x=g=>{h.current(g,(v,E,C)=>{if(Object.values(C).some(M=>M)){g.preventDefault();const M=Math.abs(g.deltaY)>Math.abs(g.deltaX)?g.deltaY:g.deltaX;w.sync_pressandwheel.dispatchEvent(a,{positions:v,keys:C,deltaX:g.deltaX,deltaY:g.deltaY,deltaAbs:M})}})};document.addEventListener("mouseup",c),document.addEventListener("keydown",f),document.addEventListener("keyup",f),document.addEventListener("mousemove",y);const T=o.current;return T?.addEventListener("wheel",x,{passive:!1}),()=>{document.removeEventListener("mouseup",c),document.removeEventListener("keydown",f),document.removeEventListener("keyup",f),document.removeEventListener("mousemove",y),T?.removeEventListener("wheel",x)}},[n,a,h]),k("sync_dblclick",a,c=>{const f=O(c.positions,n.current);f&&w.dblclick.dispatchEvent(r,{frame:n.current,pointer:f,keys:c.keys})}),k("sync_click",a,c=>{const f=O(c.positions,n.current);f&&w.click.dispatchEvent(r,{frame:n.current,pointer:f,keys:c.keys})}),k("sync_move",a,c=>{const f=c.positions?O(c.positions,n.current):null;d.current=c,w.move.dispatchEvent(r,{frame:n.current,pointer:f??null,keys:c.keys})}),k("sync_mousedown",a,c=>{const f=O(c.positions,n.current);f&&w.mousedown.dispatchEvent(r,{frame:n.current,pointer:f,keys:c.keys})}),k("sync_mouseup",a,c=>{const f=O(c.positions,n.current);f&&w.mouseup.dispatchEvent(r,{frame:n.current,pointer:f,keys:c.keys})}),k("sync_pressandwheel",a,c=>{const f=O(c.positions,n.current);f&&w.pressandwheel.dispatchEvent(r,{frame:n.current,pointer:f,keys:c.keys,deltaX:c.deltaX,deltaY:c.deltaY,deltaAbs:c.deltaAbs})}),k("sync_spanselect",a,c=>{const f=yt("x",c.xRange,n.current),y=yt("y",c.yRange,n.current),x=f.scaled,T=y.scaled;c.completed&&(u.current=null),w.spanselect.dispatchEvent(r,{mode:c.mode,frame:n.current,xRanges:x,yRanges:T,completed:c.completed,x:{fromCSS:f.fromCSS,toCSS:f.toCSS},y:{fromCSS:y.fromCSS,toCSS:y.toCSS},keys:c.keys})}),A.jsx("div",{ref:o,id:"interactions",className:t,style:{position:"absolute",left:i.chartAreaCSS.x,top:i.chartAreaCSS.y,width:i.chartAreaCSS.width,height:i.chartAreaCSS.height,zIndex:25,...s},onClick:c=>{p(c,(f,y,x)=>{w.sync_click.dispatchEvent(a,{positions:f,keys:x})})},onMouseLeave:c=>{p(c,(f,y,x)=>{w.sync_move.dispatchEvent(a,{positions:null,keys:x})})},onMouseMove:c=>{p(c,(f,y,x)=>{w.sync_move.dispatchEvent(a,{positions:f,keys:x})})},onMouseDown:c=>{p(c,(f,{cssX:y,cssY:x},T)=>{w.sync_mousedown.dispatchEvent(a,{positions:f,keys:T}),l.current=null,u.current={xRangeCss:{start:y,end:y},yRangeCss:{start:x,end:x}}})},onMouseUp:c=>{p(c,(f,y,x)=>{w.sync_mouseup.dispatchEvent(a,{positions:f,keys:x});const T=l.current;l.current=null;const g=u.current;if(u.current=null,g&&T){const v={...T,keys:x,completed:!0};w.sync_spanselect.dispatchEvent(a,v)}})},onDoubleClick:c=>{p(c,(f,y,x)=>{w.sync_dblclick.dispatchEvent(a,{positions:f,keys:x})})}})},re=({data:t,renderTooltip:s,xScaleId:e})=>{const[o,i]=S.useState(null);dt("move",r=>{i(r)});const n=S.useMemo(()=>{if(!o)return null;const{frame:r,pointer:a}=o,u=a?.scaled[e];if(u===void 0)return null;const l=[];let d=u;for(const m of t){let p=null,h=1/0;for(const[f,y]of m.points.entries()){if(!G(r,y.x,e)||!G(r,y.y,m.yScaleId))continue;const x=Math.abs(y.x-u);x<h&&(h=x,p=f)}const c=m.points[p??-1];if(!c||J(r,h,e,"css")>30){l.push({seriesId:m.seriesId,y:null});continue}d=c.x,l.push({seriesId:m.seriesId,y:c.y})}return{frame:r,x:d,points:l}},[t,o,e]);return s(n)},ae=({makeXStyle:t,makeXClassName:s,makeYStyle:e,makeYClassName:o})=>{const[i,n]=S.useState(null);if(dt("move",d=>{n(d)}),!i)return null;const{frame:r,pointer:a}=i,u=Y(r,a?.cssX??0,"css"),l=j(r,a?.cssY??0,"css");return A.jsxs(A.Fragment,{children:[A.jsx("div",{"data-show":!!a,className:s?.(i),style:{position:"absolute",left:0,top:r.chartAreaCSS.y,height:r.chartAreaCSS.height,borderColor:"red",borderLeftWidth:"1px",borderLeftStyle:"solid",pointerEvents:"none",opacity:a?1:0,transform:`translateX(${u}px)`,...t?.(i)}}),A.jsx("div",{className:o?.(i),"data-show":!!a,style:{position:"absolute",top:0,height:0,borderTop:"solid 1px red",left:r.chartAreaCSS.x,width:r.chartAreaCSS.width,pointerEvents:"none",opacity:a?1:0,transform:`translateY(${l}px)`,...e?.(i)}})]})},le=({makeClassName:t,makeStyle:s})=>{const[e,o]=S.useState(null);if(dt("spanselect",p=>{o(p.mode==="none"||p.completed?null:p)}),!e)return null;const i=Y(e.frame,e.x.fromCSS,"css"),n=Y(e.frame,e.x.toCSS,"css"),r=j(e.frame,e.y.fromCSS,"css"),a=j(e.frame,e.y.toCSS,"css"),u=Math.min(i,n),l=Math.min(r,a),d=Math.abs(n-i),m=Math.abs(a-r);return A.jsx("div",{className:t?.(e),style:{position:"absolute",backgroundColor:"#0000ff22",left:`${u}px`,top:`${l}px`,width:`${d}px`,height:`${m}px`,pointerEvents:"none",...s?.(e)}})},ue=({style:t,children:s,scaleId:e,...o})=>{const i=lt(n=>n.getScale(e)?.axis);return i?A.jsx("div",{style:{position:"absolute",backgroundColor:"#0000ff11",left:i.cssRect.x,top:i.cssRect.y,height:i.cssRect.height,width:i.cssRect.width,...t},...o,children:s}):null};exports.AreaPlot=ee;exports.AxisOverlay=ue;exports.BarPlot=te;exports.CanPlot=Ht;exports.ChartAreaInteractions=ie;exports.Crosshair=ae;exports.LinePlot=Jt;exports.ScatterPlot=Qt;exports.SelectBox=le;exports.SparklinePlot=se;exports.TooltipsX=re;exports.applyStyles=U;exports.clampXPosToChartArea=Y;exports.clampYPosToChartArea=j;exports.getScale=R;exports.isXScale=B;exports.isYScale=Pt;exports.posToVal=L;exports.pxToValDistance=wt;exports.usePositioned=ne;exports.valFits=G;exports.valToPos=D;exports.valToPxDistance=J;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A=require("react/jsx-runtime"),S=require("react"),it=require("zustand"),q=(t,s,e)=>Math.min(Math.max(t,s),e),pt=(t,s,e,c)=>{const{min:n,max:o}=L(t,e),i=c==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,a=($(t,e)?i.width:i.height)/(o-n);return s/a},L=(t,s)=>{const e=t.scales.find(c=>c.id===s);if(!e)throw new Error(`Scale ${s} not found`);return e},$=(t,s)=>t.scales.find(e=>e.id===s)?.origin==="x",bt=(t,s)=>t.scales.find(e=>e.id===s)?.origin==="y",z=(t,s)=>{const e=window.devicePixelRatio||1;t.lineCap=s?.lineCap??"butt",t.lineDashOffset=e*(s?.lineDashOffset??0),t.lineJoin=s?.lineJoin??"miter",t.lineWidth=e*(s?.lineWidth??1),t.miterLimit=e*(s?.miterLimit??10),t.strokeStyle=s?.strokeStyle??"black",t.fillStyle=s?.fillStyle??t.strokeStyle,t.font=s?.font??`${10*e}px sans-serif`,t.textAlign=s?.textAlign??"start",t.direction=s?.direction??"inherit",t.textBaseline=s?.textBaseline??"alphabetic",t.fontKerning=s?.fontKerning??"auto"},J=(t,s,e,c)=>{const n=c==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,{min:o,max:i}=L(t,e),a=($(t,e)?n.width:n.height)/(i-o);return s*a},j=(t,s,e,c)=>{const{min:n}=L(t,e),o=c==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,i=J(t,s-n,e,c);return $(t,e)?q(o.x+i,o.x-10*o.width,o.x+11*o.width):q(o.y+o.height-i,o.y-10*o.height,o.y+11*o.height)},K=(t,s,e)=>{const c=e==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS;return q(s,c.x,c.x+c.width)},Y=(t,s,e)=>{const c=e==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS;return q(s,c.y,c.y+c.height)},Z=(t,s,e)=>{const{min:c,max:n}=L(t,e);return s>=c&&s<=n},_=(t,s,e,c)=>{const{min:n,max:o}=L(t,e),i=c==="canvas"?t.chartAreaCanvasPX:t.chartAreaCSS,a=$(t,e)?(s-i.x)/i.width:(i.height-s+i.y)/i.height;return n+a*(o-n)},Tt=t=>{const{ctx:s,scales:e}=t;for(const c of e){if(!c.axis)continue;s.save(),z(s,{strokeStyle:"black",fillStyle:"black",lineWidth:1,...c.axis.style});const n=c.axis.canvasRect;if(c.origin==="x"){if(c.axis.position==="bottom")s.beginPath(),s.moveTo(n.x,n.y),s.lineTo(n.x+n.width,n.y),s.stroke();else if(c.axis.position==="top"){const o=n.y+n.height;s.beginPath(),s.moveTo(n.x,o),s.lineTo(n.x+n.width,o),s.stroke()}}else if(c.axis.position==="left"){const o=n.x+n.width;s.beginPath(),s.moveTo(o,n.y),s.lineTo(o,n.y+n.height),s.stroke()}else c.axis.position==="right"&&(s.beginPath(),s.moveTo(n.x,n.y),s.lineTo(n.x,n.y+n.height),s.stroke());s.restore()}},kt=()=>it.createStore((t,s)=>{const e=()=>{const c=s()._frame;if(!c)throw new Error("No frame set in frame store");return c};return{_frame:null,getFrame:e,getCtx:()=>e().ctx,clampXPosToChartArea:(c,n)=>K(e(),c,n??"canvas"),clampYPosToChartArea:(c,n)=>Y(e(),c,n??"canvas"),valToPos:(c,n,o)=>j(e(),c,n,o??"canvas"),valToPxDistance:(c,n,o)=>J(e(),c,n,o??"canvas"),valFits:(c,n)=>Z(e(),c,n),getScale:c=>L(e(),c)}}),at=S.createContext(null),B=(t,s)=>{const e=S.useContext(at);if(!e)throw new Error("useFrame must be used within a CanPlot component");const c=S.useRef(t);c.current=t,S.useLayoutEffect(()=>{c.current(e.getState()),e.subscribe(n=>{n._frame&&c.current(n)})},[e,...s])},Q=t=>{const s=S.useContext(at);if(!s)throw new Error("useFrame must be used within a CanPlot component");return it.useStore(s,t??(e=>e.getFrame()))};function Et(t){return s=>{for(const e of t)rt(e,s)}}function Pt(t){return s=>{const e=[];for(const c of t){const n=rt(c,s),o=typeof n=="function";e.push(o?n:()=>rt(c,null))}return()=>{for(const c of e)c()}}}function rt(t,s){if(typeof t=="function")return t(s);t&&(t.current=s)}var Mt=parseInt(S.version.split(".")[0],10)>=19?Pt:Et;const At=S.forwardRef(({configuration:t,children:s,style:e,className:c},n)=>{const o=S.useRef(null),i=S.useRef(null),a=_t(i),u=S.useMemo(kt,[]);S.useLayoutEffect(()=>{u.setState({_frame:Lt(t,a,o.current)})},[t,a,o,u]),S.useLayoutEffect(()=>u.subscribe(d=>{d._frame&&Ut(d._frame)}),[u]);const l=window.devicePixelRatio||1;return A.jsxs("div",{ref:Mt([n,i]),className:c,style:{position:"relative",overflow:"hidden",...e},"data-canplotroot":!0,children:[A.jsx("canvas",{ref:o,width:a.width*l,height:a.height*l,style:{inset:0,position:"absolute",width:`${a.width}px`,height:`${a.height}px`}}),A.jsx(Rt,{frameStore:u,children:s})]})}),Rt=({frameStore:t,children:s})=>it.useStore(t,c=>!!c._frame)?A.jsx(at.Provider,{value:t,children:s}):null,_t=t=>{const[s,e]=S.useState({width:0,height:0}),[c]=S.useState(()=>new ResizeObserver(n=>{for(const o of n){const i=Math.round(o.contentRect.width),a=Math.round(o.contentRect.height);e(u=>u.width!==i||u.height!==a?{...u,width:i,height:a}:u)}}));return S.useLayoutEffect(()=>{if(!t.current)return;const n=t.current.clientWidth,o=t.current.clientHeight;return e(i=>i.width!==n||i.height!==o?{...i,width:n,height:o}:i),c.observe(t.current,{box:"border-box"}),()=>c.disconnect()},[c,t]),s},Lt=(t,s,e)=>{const c=e?.getContext("2d");if(!c)return null;const n=window.devicePixelRatio||1;if(s.width===0||s.height===0)return null;const o={x:t.padding.left,y:t.padding.top,width:s.width-t.padding.left-t.padding.right,height:s.height-t.padding.top-t.padding.bottom};for(const h of t.scales)h.axis&&(h.origin==="x"?(h.axis.position==="bottom"||h.axis.position==="top")&&(o.height=Math.max(0,o.height-h.axis.size),h.axis.position==="top"&&(o.y+=h.axis.size)):(h.axis.position==="left"||h.axis.position==="right")&&(o.width=Math.max(0,o.width-h.axis.size),h.axis.position==="left"&&(o.x+=h.axis.size)));const i={x:o.x*n,y:o.y*n,width:o.width*n,height:o.height*n},a=[];let u=t.padding.left*n,l=c.canvas.width-t.padding.right*n,d=c.canvas.height-t.padding.bottom*n,p=t.padding.top*n;for(const h of t.scales){if(!h.axis){a.push({...h,axis:null});continue}let r;if(h.origin==="x")switch(h.axis.position){case"bottom":d-=h.axis.size*n,r={x:o.x,y:d/n,width:o.width,height:h.axis.size};break;case"top":p+=h.axis.size*n,r={x:o.x,y:p/n-h.axis.size,width:o.width,height:h.axis.size};break;case"left":case"right":throw new Error("Invalid axis position for x origin")}else switch(h.axis.position){case"left":u+=h.axis.size*n,r={x:u/n-h.axis.size,y:o.y,width:h.axis.size,height:o.height};break;case"right":l-=h.axis.size*n,r={x:l/n,y:o.y,width:h.axis.size,height:o.height};break;case"top":case"bottom":throw new Error("Invalid axis position for y origin")}const m={x:r.x*n,y:r.y*n,width:r.width*n,height:r.height*n};a.push({...h,axis:{...h.axis,cssRect:r,canvasRect:m}})}return{ctx:c,dpr:n,padding:t.padding,scales:a,chartAreaCSS:o,chartAreaCanvasPX:i}},Ut=t=>{t.ctx.clearRect(0,0,t.ctx.canvas.width,t.ctx.canvas.height),Tt(t)},Dt=({data:t,xScaleId:s,yScaleId:e,style:c})=>(B(({getCtx:n,clampXPosToChartArea:o,clampYPosToChartArea:i,valToPos:a})=>{const u=n();u.save(),u.beginPath(),z(u,c);for(const l of t){const d=o(a(l.x,s)),p=i(a(l.y,e));u.lineTo(d,p)}u.stroke(),u.restore()},[t,s,e,c]),null),Xt=({data:t,xScaleId:s,yScaleId:e,radius:c=5,style:n})=>(B(({getCtx:o,valToPos:i,valFits:a})=>{const u=o();u.save(),u.beginPath(),z(u,n);for(const l of t){if(!a(l.x,s)||!a(l.y,e))continue;const d=i(l.x,s),p=i(l.y,e);u.moveTo(d+c,p),u.arc(d,p,c,0,Math.PI*2)}u.stroke(),u.fill(),u.restore()},[t,s,e,c,n]),null),It=({data:t,xScaleId:s,yScaleId:e,style:c,barWidth:n,xPositionOffset:o,radius:i})=>(B(({getCtx:a,valToPxDistance:u,valToPos:l,clampXPosToChartArea:d,clampYPosToChartArea:p})=>{if(t.length===0)return;const f=a();f.save(),z(f,c);const h=u(n,s);f.beginPath();for(const r of t){const y=l(r.x,s)-h/2+o*h,x=p(l(0,e)),w=p(l(r.y,e)),g=x-w,v=d(y),E=d(y+h)-v;i?f.roundRect(v,w,E,g,i):f.rect(v,w,E,g)}f.closePath(),f.fill(),c?.strokeStyle&&f.stroke(),f.restore()},[t,s,e,c,n,o,i]),null),Ft=({data:t,xScaleId:s,yScaleId:e,style:c})=>(B(({getCtx:n,clampXPosToChartArea:o,clampYPosToChartArea:i,valToPos:a})=>{const u=[];for(const d of t){const p=o(a(d.x,s)),f=i(a(d.y[0],e)),h=i(a(d.y[1],e));u.push({x:p,y:f}),u.unshift({x:p,y:h})}const l=n();if(u.length>0){l.save(),l.beginPath(),z(l,c),l.moveTo(u[0].x,u[0].y);for(const d of u)l.lineTo(d.x,d.y);l.closePath(),l.fill(),l.restore()}},[t,s,e,c]),null),Kt=({data:t,stroked:s,xScaleId:e,yScaleId:c,style:n})=>(B(({getCtx:o,clampXPosToChartArea:i,clampYPosToChartArea:a,valToPos:u})=>{const l=[],d=o();for(const r of t){const m=i(u(r.x,e)),y=a(u(r.y,c));l.push({x:m,y})}const p=l.at(0),f=l.at(-1);if(!p||!f)return;const h=a(u(0,c));d.save(),d.beginPath(),z(d,n),d.moveTo(p.x,h);for(const r of l)d.lineTo(r.x,r.y);if(d.lineTo(f.x,h),d.closePath(),d.fill(),s){d.beginPath(),d.moveTo(p.x,p.y);for(const r of l)d.lineTo(r.x,r.y);d.stroke()}d.restore()},[t,s,e,c,n]),null),Yt=(t,s)=>Q(({clampXPosToChartArea:e,clampYPosToChartArea:c,getScale:n,valToPos:o,valFits:i})=>{const a={};for(const u in t){const l=t[u];switch(l.exceeding){case"discard":{if(i(l.value,l.scaleId)){const d=o(l.value,l.scaleId,s);a[u]=d}break}case"clamp":{const d=o(l.value,l.scaleId,s);a[u]=n(l.scaleId)?.origin==="x"?e(d,s):c(d,s);break}}}return a}),T=()=>{const t=[];return{addEventListener:(s,e)=>(t.push({syncKey:s,callback:e}),()=>{const c=t.findIndex(n=>n.callback===e);c!==-1&&t.splice(c,1)}),dispatchEvent:(s,e)=>{for(const c of t)c.syncKey===s&&c.callback(s,e)}}},C={dblclick:T(),click:T(),move:T(),mousedown:T(),mouseup:T(),spanselect:T(),documentmouseup:T(),pressandwheel:T(),sync_dblclick:T(),sync_click:T(),sync_move:T(),sync_mousedown:T(),sync_mouseup:T(),sync_spanselect:T(),sync_pressandwheel:T()},k=(t,s,e)=>{const c=S.useRef(e);c.current=e,S.useEffect(()=>C[t].addEventListener(s,(o,i)=>{c.current(i,o)}),[s,t,c])},lt=S.createContext(""),ut=(t,s)=>{const e=S.useContext(lt);return k(t,e,s)},zt=(t,s,e,c,n)=>{if(!s)return;const o=c??e.scales.find(p=>p.origin==="x")?.id,i=n??e.scales.find(p=>p.origin==="y")?.id;if(!o||!i)return;const a=t.clientX-s.left,u={scaleId:o,value:_(e,a,o,"css")},l=t.clientY-s.top,d={scaleId:i,value:_(e,l,i,"css")};return{pointerSyncPosition:{x:u,y:d},cssX:a,cssY:l}},O=(t,s)=>{const e=t.x?j(s,t.x.value,t.x.scaleId,"css"):0,c=t.y?j(s,t.y.value,t.y.scaleId,"css"):0;return{cssX:e,cssY:c,scaled:Object.fromEntries(s.scales.map(n=>{const o=n.origin==="y"?c:e;return[n.id,_(s,o,n.id,"css")]}))}},mt=(t,s,e)=>{if(!s){const i=t==="x"?e.chartAreaCSS.x:e.chartAreaCSS.y,a=t==="x"?e.chartAreaCSS.x+e.chartAreaCSS.width:e.chartAreaCSS.y+e.chartAreaCSS.height;return{fromCSS:i,toCSS:a,scaled:e.scales.flatMap(u=>{if(u.origin!==t)return[];const{min:l,max:d}=L(e,u.id);return[{scaleId:u.id,from:l,to:d}]})}}const c=j(e,s.from,s.scaleId,"css"),n=j(e,s.to,s.scaleId,"css"),o=e.scales.flatMap(i=>{if(i.origin!==t)return[];const a=_(e,c,i.id,"css"),u=_(e,n,i.id,"css");return[{scaleId:i.id,from:a,to:u}]});return{fromCSS:c,toCSS:n,scaled:o}},Ot=({id:t,onClick:s,onDblClick:e,onMouseMove:c,onMouseDown:n,onMouseUp:o,onDocumentMouseUp:i,onSpanSelect:a,className:u,style:l,sync:d,children:p})=>{const f=S.useId(),h=t||f;return k("dblclick",h,r=>{e?.(r)}),k("click",h,r=>{s?.(r)}),k("move",h,r=>{c?.(r)}),k("mousedown",h,r=>{n?.(r)}),k("mouseup",h,r=>{o?.(r)}),k("documentmouseup",h,r=>{i?.(r)}),k("spanselect",h,r=>{a?.(r)}),A.jsxs(lt.Provider,{value:h,children:[A.jsx(jt,{className:u,style:l,sync:d}),p]})},jt=({className:t,style:s,sync:e})=>{const c=S.useRef(null),n=Q(),o=S.useRef(n);o.current=n;const i=S.useContext(lt),a=e?.key||i,u=S.useRef(null),l=S.useRef(null),d=S.useRef(null),p=()=>{const r=c.current?.parentElement;if(r){if(r.dataset.canplotroot===void 0)throw new Error("ChartAreaInteractions must be used within a CanPlot component");return r.getBoundingClientRect()}},f=(r,m)=>{const y=zt(r,p(),o.current,e?.xViaScaleId,e?.yViaScaleId);y&&m(y.pointerSyncPosition,{cssX:y.cssX,cssY:y.cssY},{ctrlKey:r.ctrlKey,altKey:r.altKey,shiftKey:r.shiftKey,metaKey:r.metaKey})},h=S.useRef(f);return h.current=f,S.useEffect(()=>{const r=g=>{const v=l.current;v&&C.sync_spanselect.dispatchEvent(a,{...v,completed:!0}),C.documentmouseup.dispatchEvent(a,{frame:o.current,keys:{ctrlKey:g.ctrlKey,altKey:g.altKey,shiftKey:g.shiftKey,metaKey:g.metaKey}})},m=g=>{const v={ctrlKey:g.ctrlKey,altKey:g.altKey,shiftKey:g.shiftKey,metaKey:g.metaKey},E=d.current;if(E&&Object.entries(v).some(([P,M])=>E.keys[P]!==M)){const P={...E,keys:v};d.current=P,C.sync_move.dispatchEvent(a,P)}const b=l.current;if(b&&Object.entries(v).some(([P,M])=>b.keys[P]!==M)){g.stopPropagation(),g.preventDefault();const P={...b,keys:v};l.current=P,C.sync_spanselect.dispatchEvent(a,P)}},y=g=>{h.current(g,(v,{cssX:E,cssY:b},P)=>{const M=u.current;if(!M||!v.x||!v.y)return;const U=o.current,R=M.xRangeCss.start,N=E,F=M.yRangeCss.start,et=b,st=L(U,v.x.scaleId),nt=L(U,v.y.scaleId);u.current={xRangeCss:{start:R,end:N},yRangeCss:{start:F,end:et}};let D="none";const ot=Math.abs(F-et),ct=Math.abs(R-N);ot<10&&ct<10?D="none":ot>30&&ct>30?D="box":ot>ct?D="y":D="x";const vt=D==="x"||D==="box"?{scaleId:st.id,from:_(U,K(o.current,R,"css"),st.id,"css"),to:_(U,K(o.current,N,"css"),st.id,"css")}:void 0,Ct=D==="y"||D==="box"?{scaleId:nt.id,from:_(U,Y(o.current,F,"css"),nt.id,"css"),to:_(U,Y(o.current,et,"css"),nt.id,"css")}:void 0,ht={mode:D,xRange:vt,yRange:Ct,completed:!1,keys:P};l.current=ht,C.sync_spanselect.dispatchEvent(a,ht)})},x=g=>{h.current(g,(v,E,b)=>{if(Object.values(b).some(M=>M)){g.preventDefault();const M=Math.abs(g.deltaY)>Math.abs(g.deltaX)?g.deltaY:g.deltaX;C.sync_pressandwheel.dispatchEvent(a,{positions:v,keys:b,deltaX:g.deltaX,deltaY:g.deltaY,deltaAbs:M})}})};document.addEventListener("mouseup",r),document.addEventListener("keydown",m),document.addEventListener("keyup",m),document.addEventListener("mousemove",y);const w=c.current;return w?.addEventListener("wheel",x,{passive:!1}),()=>{document.removeEventListener("mouseup",r),document.removeEventListener("keydown",m),document.removeEventListener("keyup",m),document.removeEventListener("mousemove",y),w?.removeEventListener("wheel",x)}},[o,a,h]),k("sync_dblclick",a,r=>{const m=O(r.positions,o.current);m&&C.dblclick.dispatchEvent(i,{frame:o.current,pointer:m,keys:r.keys})}),k("sync_click",a,r=>{const m=O(r.positions,o.current);m&&C.click.dispatchEvent(i,{frame:o.current,pointer:m,keys:r.keys})}),k("sync_move",a,r=>{const m=r.positions?O(r.positions,o.current):null;d.current=r,C.move.dispatchEvent(i,{frame:o.current,pointer:m??null,keys:r.keys})}),k("sync_mousedown",a,r=>{const m=O(r.positions,o.current);m&&C.mousedown.dispatchEvent(i,{frame:o.current,pointer:m,keys:r.keys})}),k("sync_mouseup",a,r=>{const m=O(r.positions,o.current);m&&C.mouseup.dispatchEvent(i,{frame:o.current,pointer:m,keys:r.keys})}),k("sync_pressandwheel",a,r=>{const m=O(r.positions,o.current);m&&C.pressandwheel.dispatchEvent(i,{frame:o.current,pointer:m,keys:r.keys,deltaX:r.deltaX,deltaY:r.deltaY,deltaAbs:r.deltaAbs})}),k("sync_spanselect",a,r=>{const m=mt("x",r.xRange,o.current),y=mt("y",r.yRange,o.current),x=m.scaled,w=y.scaled;r.completed&&(u.current=null),C.spanselect.dispatchEvent(i,{mode:r.mode,frame:o.current,xRanges:x,yRanges:w,completed:r.completed,x:{fromCSS:m.fromCSS,toCSS:m.toCSS},y:{fromCSS:y.fromCSS,toCSS:y.toCSS},keys:r.keys})}),A.jsx("div",{ref:c,id:"interactions",className:t,style:{position:"absolute",left:n.chartAreaCSS.x,top:n.chartAreaCSS.y,width:n.chartAreaCSS.width,height:n.chartAreaCSS.height,zIndex:25,...s},onClick:r=>{f(r,(m,y,x)=>{C.sync_click.dispatchEvent(a,{positions:m,keys:x})})},onMouseLeave:r=>{f(r,(m,y,x)=>{C.sync_move.dispatchEvent(a,{positions:null,keys:x})})},onMouseMove:r=>{f(r,(m,y,x)=>{C.sync_move.dispatchEvent(a,{positions:m,keys:x})})},onMouseDown:r=>{f(r,(m,{cssX:y,cssY:x},w)=>{C.sync_mousedown.dispatchEvent(a,{positions:m,keys:w}),l.current=null,u.current={xRangeCss:{start:y,end:y},yRangeCss:{start:x,end:x}}})},onMouseUp:r=>{f(r,(m,y,x)=>{C.sync_mouseup.dispatchEvent(a,{positions:m,keys:x});const w=l.current;l.current=null;const g=u.current;if(u.current=null,g&&w){const v={...w,keys:x,completed:!0};C.sync_spanselect.dispatchEvent(a,v)}})},onDoubleClick:r=>{f(r,(m,y,x)=>{C.sync_dblclick.dispatchEvent(a,{positions:m,keys:x})})}})},Bt=({data:t,renderTooltip:s,xScaleId:e})=>{const[c,n]=S.useState(null);ut("move",i=>{n(i)});const o=S.useMemo(()=>{if(!c)return null;const{frame:i,pointer:a}=c,u=a?.scaled[e];if(u===void 0)return null;const l=[];let d=u;for(const p of t){let f=null,h=1/0;for(const[m,y]of p.points.entries()){if(!Z(i,y.x,e)||!Z(i,y.y,p.yScaleId))continue;const x=Math.abs(y.x-u);x<h&&(h=x,f=m)}const r=p.points[f??-1];if(!r||J(i,h,e,"css")>30){l.push({seriesId:p.seriesId,y:null});continue}d=r.x,l.push({seriesId:p.seriesId,y:r.y})}return{frame:i,x:d,points:l}},[t,c,e]);return s(o)},Nt=({makeXStyle:t,makeXClassName:s,makeYStyle:e,makeYClassName:c})=>{const[n,o]=S.useState(null);if(ut("move",d=>{o(d)}),!n)return null;const{frame:i,pointer:a}=n,u=K(i,a?.cssX??0,"css"),l=Y(i,a?.cssY??0,"css");return A.jsxs(A.Fragment,{children:[A.jsx("div",{"data-show":!!a,className:s?.(n),style:{position:"absolute",left:0,top:i.chartAreaCSS.y,height:i.chartAreaCSS.height,borderColor:"red",borderLeftWidth:"1px",borderLeftStyle:"solid",pointerEvents:"none",opacity:a?1:0,transform:`translateX(${u}px)`,...t?.(n)}}),A.jsx("div",{className:c?.(n),"data-show":!!a,style:{position:"absolute",top:0,height:0,borderTop:"solid 1px red",left:i.chartAreaCSS.x,width:i.chartAreaCSS.width,pointerEvents:"none",opacity:a?1:0,transform:`translateY(${l}px)`,...e?.(n)}})]})},$t=({makeClassName:t,makeStyle:s})=>{const[e,c]=S.useState(null);if(ut("spanselect",f=>{c(f.mode==="none"||f.completed?null:f)}),!e)return null;const n=K(e.frame,e.x.fromCSS,"css"),o=K(e.frame,e.x.toCSS,"css"),i=Y(e.frame,e.y.fromCSS,"css"),a=Y(e.frame,e.y.toCSS,"css"),u=Math.min(n,o),l=Math.min(i,a),d=Math.abs(o-n),p=Math.abs(a-i);return A.jsx("div",{className:t?.(e),style:{position:"absolute",backgroundColor:"#0000ff22",left:`${u}px`,top:`${l}px`,width:`${d}px`,height:`${p}px`,pointerEvents:"none",...s?.(e)}})},Vt=({style:t,children:s,scaleId:e,...c})=>{const n=Q(o=>o.getScale(e)?.axis);return n?A.jsx("div",{style:{position:"absolute",backgroundColor:"#0000ff11",left:n.cssRect.x,top:n.cssRect.y,height:n.cssRect.height,width:n.cssRect.width,...t},...c,children:s}):null},yt=60,Ht=30,xt="UTC",Wt="en-GB",qt=({space:t,formatter:s}={})=>(e,c)=>{const{min:n,max:o}=e,i=[],a=window.devicePixelRatio||1,u=(t??(e.origin==="x"?yt:Ht))*a,l=pt(c,u,e.id,"canvas"),d=V.find(f=>f>l)??1;let p=n%d<Number.EPSILON?n:n+d-n%d;for(;p<=o;)i.push(p),p+=d;return(s??St)(i)},St=t=>{const s=Math.max(0,Math.ceil(-Math.log10(t[1]-t[0])));return t.map(e=>({value:e,label:e.toFixed(s)}))},V=[];for(let t=-12;t<=12;t++)V.push(1*10**t),V.push(2*10**t),V.push(5*10**t);const Zt=1,gt=1e3*Zt,dt=60*gt,G=60*dt,tt=24*G,Gt=30*tt,Jt=365*tt,Qt=[[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=t=>{const[s,e]=t;switch(e){case"milliseconds":return s;case"seconds":return s*gt;case"minutes":return s*dt;case"hours":return s*G;case"days":return s*tt;case"months":return s*Gt;case"years":return s*Jt}},te=(t,s)=>{const e=new Date(t);return e.setUTCMilliseconds(e.getUTCMilliseconds()+s),e.getTime()},ee=(t,s)=>{const e=new Date(t);return e.setUTCSeconds(e.getUTCSeconds()+s),e.getTime()},se=(t,s)=>{const e=new Date(t);return e.setUTCMinutes(e.getUTCMinutes()+s),e.getTime()},ne=(t,s)=>{const e=new Date(t);return e.setUTCHours(e.getUTCHours()+s),e.getTime()},oe=(t,s)=>{const e=new Date(t);return e.setUTCDate(e.getUTCDate()+s),e.getTime()},ft=(t,s)=>{const e=new Date(t);return e.setUTCMonth(e.getUTCMonth()+s),e.getTime()},I=(t,s)=>{const[e,c]=s;switch(c){case"milliseconds":return te(t,e);case"seconds":return ee(t,e);case"minutes":return se(t,e);case"hours":return ne(t,e);case"days":return oe(t,e);case"months":return ft(t,e);case"years":return ft(t,e*12)}};function W(t,s){const e=new Date(t),c=new Date(e.toLocaleString("en-US",{timeZone:s})),n=new Date(e.toLocaleString("en-US",{timeZone:"UTC"}));return(c.getTime()-n.getTime())/(3600*1e3)}const ce=(t,s,e="UTC")=>{const[c,n]=s;let o=new Date(t);const i=()=>{o.setUTCHours(-W(o,e),0,0,0)};switch(n){case"milliseconds":o.setUTCMilliseconds(Math.ceil(o.getUTCMilliseconds()/c)*c);break;case"seconds":o.setUTCSeconds(Math.ceil(o.getUTCSeconds()/c)*c,0);break;case"minutes":o.setUTCMinutes(Math.ceil(o.getTime()%G/dt/c)*c,0,0);break;case"hours":o.setUTCHours(Math.ceil(o.getTime()%tt/G/c)*c,0,0,0);break;case"days":case"months":case"years":n==="months"?o.setUTCDate(1):n==="years"&&o.setUTCMonth(0,1),i(),o.getTime()<t&&(o=new Date(I(o,[1,n])));break}return o.getTime()},re=({timeZone:t=xt,space:s=yt,formatter:e,locale:c,showTimezone:n}={})=>(o,i)=>{const{min:a,max:u}=o,l=Math.floor(i.chartAreaCanvasPX.width/s)+1,p=(u-a)/l,[f,h]=Qt.find(w=>H(w)>=p)??[1,"milliseconds"],r=ce(a,[f,h],t),m=W(r,t),y=[r];let x;for(;;){switch(h){case"milliseconds":case"seconds":case"minutes":case"hours":{x=I(r,[y.length*f,h]);break}case"days":{const w=I(r,[y.length*f,h]);x=I(w,[m-W(w,t),"hours"]);break}case"months":case"years":{const w=I(I(I(r,[m,"hours"]),[y.length*f,h]),[-m,"hours"]);x=I(w,[m-W(w,t),"hours"]);break}}if(x>u)break;y.push(x)}return(e??wt({locale:c,showTimezone:n,timeZone:t}))(y)},X=(t,s,e)=>t.find(c=>c.type===e)?.value!==s.find(c=>c.type===e)?.value,wt=({timeZone:t=xt,locale:s=Wt,showTimezone:e=!0})=>{const c=new Intl.DateTimeFormat(s,{year:"numeric",day:"numeric",month:"short",hour:"numeric",hourCycle:"h23",minute:"numeric",second:"numeric",fractionalSecondDigits:3,timeZoneName:"short",timeZone:t});return n=>{const o=n[1]-n[0],i=o<H([1,"days"]),a=o<H([1,"minutes"]),u=o<H([1,"seconds"]);return n.map(l=>({value:l,label:c.formatToParts(new Date(l))})).map((l,d,p)=>{const f=p[d-1],h=d===0||X(l.label,f.label,"year"),r=d===0||X(l.label,f.label,"day"),m=d===0||X(l.label,f.label,"month"),y=d===0||X(l.label,f.label,"hour"),x=d===0||X(l.label,f.label,"timeZoneName"),w=d===0||X(l.label,f.label,"minute"),g=d===0||X(l.label,f.label,"second"),v=d===0||X(l.label,f.label,"fractionalSecond"),E=[];if(i&&(y||w||x||g||v)){const b=l.label.find(R=>R.type==="hour")?.value,P=l.label.find(R=>R.type==="minute")?.value,M=l.label.find(R=>R.type==="timeZoneName")?.value;let U="";if(a){const R=l.label.find(F=>F.type==="second")?.value,N=l.label.find(F=>F.type==="fractionalSecond")?.value;U=`:${R}`+(u?`.${N}`:"")}E.push(`${b}:${P}${U}`+(e&&x?` ${M}`:""))}return(r||m)&&E.push([l.label.find(b=>b.type==="month")?.value,r&&l.label.find(b=>b.type==="day")?.value].filter(Boolean).join(" ")),h&&E.push(l.label.find(b=>b.type==="year")?.value),{value:l.value,label:E.filter(b=>b).join(`
2
+ `)}})}};exports.AreaPlot=Ft;exports.AxisOverlay=Vt;exports.BarPlot=It;exports.CanPlot=At;exports.ChartAreaInteractions=Ot;exports.Crosshair=Nt;exports.LinePlot=Dt;exports.ScatterPlot=Xt;exports.SelectBox=$t;exports.SparklinePlot=Kt;exports.TooltipsX=Bt;exports.applyStyles=z;exports.clampXPosToChartArea=K;exports.clampYPosToChartArea=Y;exports.defaultNumericalTicksFormatter=St;exports.getScale=L;exports.isXScale=$;exports.isYScale=bt;exports.makeLinearTicks=qt;exports.makeTimeTickFormat=wt;exports.makeTimeTicks=re;exports.posToVal=_;exports.pxToValDistance=pt;exports.useDrawEffect=B;exports.useFrameState=Q;exports.usePositioned=Yt;exports.valFits=Z;exports.valToPos=j;exports.valToPxDistance=J;
5
3
  //# sourceMappingURL=canplot.cjs.map