@jacshuo/onyx 2.2.0 → 2.4.0

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.
Files changed (135) hide show
  1. package/dist/Chart/BarChart.cjs +1 -1
  2. package/dist/Chart/BarChart.js +1 -1
  3. package/dist/Chart/PieChart.cjs +1 -1
  4. package/dist/Chart/PieChart.js +1 -1
  5. package/dist/Chart/ScatterChart.cjs +1 -1
  6. package/dist/Chart/ScatterChart.js +1 -1
  7. package/dist/Chart/index.cjs +1 -1
  8. package/dist/Chart/index.js +1 -1
  9. package/dist/DataDisplay/VirtualList.cjs +2 -0
  10. package/dist/DataDisplay/VirtualList.css +1 -0
  11. package/dist/DataDisplay/VirtualList.d.cts +5 -0
  12. package/dist/DataDisplay/VirtualList.d.ts +5 -0
  13. package/dist/DataDisplay/VirtualList.js +2 -0
  14. package/dist/DataDisplay/index.cjs +3 -2
  15. package/dist/DataDisplay/index.css +1 -0
  16. package/dist/DataDisplay/index.d.cts +5 -0
  17. package/dist/DataDisplay/index.d.ts +5 -0
  18. package/dist/DataDisplay/index.js +3 -2
  19. package/dist/Extras/FileExplorer.cjs +2 -2
  20. package/dist/Extras/FileExplorer.js +2 -2
  21. package/dist/Extras/LoginPanel.cjs +1 -0
  22. package/dist/Extras/LoginPanel.css +1 -0
  23. package/dist/Extras/LoginPanel.d.cts +3 -0
  24. package/dist/Extras/LoginPanel.d.ts +3 -0
  25. package/dist/Extras/LoginPanel.js +1 -0
  26. package/dist/Extras/RichTextEditor.cjs +45 -0
  27. package/dist/Extras/RichTextEditor.css +1 -0
  28. package/dist/Extras/RichTextEditor.d.cts +10 -0
  29. package/dist/Extras/RichTextEditor.d.ts +10 -0
  30. package/dist/Extras/RichTextEditor.js +45 -0
  31. package/dist/Extras/TypewriterText.cjs +2 -1
  32. package/dist/Extras/TypewriterText.css +1 -1
  33. package/dist/Extras/TypewriterText.js +2 -1
  34. package/dist/Extras/index.cjs +48 -4
  35. package/dist/Extras/index.css +1 -1
  36. package/dist/Extras/index.d.cts +7 -0
  37. package/dist/Extras/index.d.ts +7 -0
  38. package/dist/Extras/index.js +48 -4
  39. package/dist/Forms/Form.cjs +1 -1
  40. package/dist/Forms/Form.js +1 -1
  41. package/dist/Forms/OTPInput.cjs +1 -0
  42. package/dist/Forms/OTPInput.css +1 -0
  43. package/dist/Forms/OTPInput.d.cts +4 -0
  44. package/dist/Forms/OTPInput.d.ts +4 -0
  45. package/dist/Forms/OTPInput.js +1 -0
  46. package/dist/Forms/Select.cjs +1 -1
  47. package/dist/Forms/Select.js +1 -1
  48. package/dist/Forms/index.cjs +1 -1
  49. package/dist/Forms/index.css +1 -0
  50. package/dist/Forms/index.d.cts +4 -0
  51. package/dist/Forms/index.d.ts +4 -0
  52. package/dist/Forms/index.js +1 -1
  53. package/dist/Layout/SplitPanel.cjs +1 -0
  54. package/dist/Layout/SplitPanel.d.cts +3 -0
  55. package/dist/Layout/SplitPanel.d.ts +3 -0
  56. package/dist/Layout/SplitPanel.js +1 -0
  57. package/dist/Layout/index.cjs +1 -1
  58. package/dist/Layout/index.d.cts +3 -0
  59. package/dist/Layout/index.d.ts +3 -0
  60. package/dist/Layout/index.js +1 -1
  61. package/dist/Navigation/Header.cjs +1 -1
  62. package/dist/Navigation/Header.js +1 -1
  63. package/dist/Navigation/RibbonBar.d.cts +3 -3
  64. package/dist/Navigation/RibbonBar.d.ts +3 -3
  65. package/dist/Navigation/index.cjs +2 -2
  66. package/dist/Navigation/index.d.cts +3 -3
  67. package/dist/Navigation/index.d.ts +3 -3
  68. package/dist/Navigation/index.js +2 -2
  69. package/dist/Primitives/Avatar.cjs +1 -1
  70. package/dist/Primitives/Avatar.js +1 -1
  71. package/dist/Primitives/Badge.cjs +1 -1
  72. package/dist/Primitives/Badge.js +1 -1
  73. package/dist/Primitives/Button.cjs +1 -1
  74. package/dist/Primitives/Button.js +1 -1
  75. package/dist/Primitives/Checkbox.cjs +1 -1
  76. package/dist/Primitives/Checkbox.js +1 -1
  77. package/dist/Primitives/Dropdown.cjs +1 -1
  78. package/dist/Primitives/Dropdown.js +1 -1
  79. package/dist/Primitives/DropdownButton.cjs +1 -1
  80. package/dist/Primitives/DropdownButton.js +1 -1
  81. package/dist/Primitives/Indicator.cjs +1 -1
  82. package/dist/Primitives/Indicator.js +1 -1
  83. package/dist/Primitives/Input.cjs +1 -1
  84. package/dist/Primitives/Input.js +1 -1
  85. package/dist/Primitives/Kbd.cjs +1 -0
  86. package/dist/Primitives/Kbd.css +0 -0
  87. package/dist/Primitives/Kbd.d.cts +6 -0
  88. package/dist/Primitives/Kbd.d.ts +6 -0
  89. package/dist/Primitives/Kbd.js +1 -0
  90. package/dist/Primitives/Label.cjs +1 -1
  91. package/dist/Primitives/Label.js +1 -1
  92. package/dist/Primitives/Radio.cjs +1 -1
  93. package/dist/Primitives/Radio.js +1 -1
  94. package/dist/Primitives/Rating.cjs +2 -0
  95. package/dist/Primitives/Rating.css +0 -0
  96. package/dist/Primitives/Rating.d.cts +4 -0
  97. package/dist/Primitives/Rating.d.ts +4 -0
  98. package/dist/Primitives/Rating.js +2 -0
  99. package/dist/Primitives/Slider.cjs +1 -1
  100. package/dist/Primitives/Slider.js +1 -1
  101. package/dist/Primitives/Switch.cjs +1 -1
  102. package/dist/Primitives/Switch.js +1 -1
  103. package/dist/Primitives/Tag.cjs +1 -1
  104. package/dist/Primitives/Tag.js +1 -1
  105. package/dist/Primitives/index.cjs +2 -1
  106. package/dist/Primitives/index.d.cts +10 -0
  107. package/dist/Primitives/index.d.ts +10 -0
  108. package/dist/Primitives/index.js +2 -1
  109. package/dist/_tsup-dts-rollup.d.cts +843 -1
  110. package/dist/_tsup-dts-rollup.d.ts +843 -1
  111. package/dist/index.cjs +49 -5
  112. package/dist/index.css +1 -1
  113. package/dist/index.d.cts +35 -0
  114. package/dist/index.d.ts +35 -0
  115. package/dist/index.js +49 -5
  116. package/dist/styles/DataDisplay/VirtualList.css +35 -0
  117. package/dist/styles/Extras/CinePlayer.css +63 -1
  118. package/dist/styles/Extras/FileExplorer.css +39 -1
  119. package/dist/styles/Extras/FilmReel.css +24 -1
  120. package/dist/styles/Extras/LoginPanel.css +16 -0
  121. package/dist/styles/Extras/MiniPlayer.css +84 -1
  122. package/dist/styles/Extras/RichTextEditor.css +908 -0
  123. package/dist/styles/Extras/TypewriterText.css +254 -0
  124. package/dist/styles/Forms/OTPInput.css +11 -0
  125. package/dist/styles/Layout/SplitPanel.css +15 -0
  126. package/dist/styles/Primitives/DropdownButton.css +26 -1
  127. package/dist/styles/Primitives/Kbd.css +1 -0
  128. package/dist/styles/Primitives/Rating.css +1 -0
  129. package/dist/styles/base.css +281 -0
  130. package/dist/styles.css +281 -0
  131. package/dist/theme.cjs +1 -1
  132. package/dist/theme.d.cts +3 -0
  133. package/dist/theme.d.ts +3 -0
  134. package/dist/theme.js +1 -1
  135. package/package.json +22 -1
@@ -1 +1 @@
1
- 'use strict';var react=require('react'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),classVarianceAuthority=require('class-variance-authority'),jsxRuntime=require('react/jsx-runtime');function G(...t){return tailwindMerge.twMerge(clsx.clsx(t))}var O=classVarianceAuthority.cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),P=classVarianceAuthority.cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),K=classVarianceAuthority.cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function et(t){let[o,r]=react.useState(()=>new Set(t)),i=react.useCallback(s=>{r(h=>{let f=new Set(h);return f.has(s)?f.delete(s):f.add(s),f});},[]),a=react.useCallback(s=>o.has(s),[o]),u=react.useCallback(()=>{r(new Set(t));},[t]);return {visible:o,toggle:i,isVisible:a,showAll:u}}function nt(t){let o=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(r,i)=>o[i%o.length])}function q(t,o,r=.1){if(t===o)return [t-1,o+1];let a=(o-t)*r;return [Math.floor(t-a),Math.ceil(o+a)]}function ot(t,o,r){if(r<=1)return [t];let i=(o-t)/(r-1);return Array.from({length:r},(a,u)=>+(t+u*i).toFixed(10))}function Y(t,o){return o?o(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,o,r){return {top:20,right:r?65:20,bottom:t?52:36,left:o?65:52}}function Q(t,o,r){let i=Math.max(t,120);return {width:i,height:o,margins:r,plotLeft:r.left,plotTop:r.top,plotRight:i-r.right,plotBottom:o-r.bottom,plotWidth:Math.max(i-r.left-r.right,10),plotHeight:Math.max(o-r.top-r.bottom,10)}}function at(t,o){let r=new Set,i=[];for(let a of t)if(o.has(a.id))for(let u of a.data){let s=String(u.x);r.has(s)||(r.add(s),i.push(s));}return i}function it(t,o,r,i){if(r){let a=0;for(let s of t)if(o.has(s.id))for(let h of s.data)h.y>a&&(a=h.y);a===0&&(a=1);let[,u]=q(0,a,.05);return [i?.min??0,i?.max??u]}else {let a=new Map;for(let h of t)if(o.has(h.id))for(let f of h.data){let C=String(f.x);a.set(C,(a.get(C)??0)+f.y);}let u=Math.max(0,...a.values()),[,s]=q(0,u===0?1:u,.05);return [i?.min??0,i?.max??s]}}function st(t,o){let[r,i]=t,[a,u]=o;return i===r?()=>(a+u)/2:s=>a+(s-r)/(i-r)*(u-a)}function ct(t,o=5){return ot(t[0],t[1],o)}function lt(t,o,r,i,a,u,s,h,f,C,S){let v=t.filter(y=>o.has(y.id)),d=r.length,b=v.length;if(d===0||b===0)return [];let m=u/d,p=m*(1-C),k=b>0?p*(1-S)/b:0,x=[];for(let y=0;y<r.length;y++){let V=r[y],M=a+y*m+(m-p)/2;for(let B=0;B<v.length;B++){let g=v[B],F=g.data.find(H=>String(H.x)===V);if(!F)continue;let W=M+B*(k+p*S/b),$=f(F.y);x.push({seriesId:g.id,category:V,x:W,y:$,width:Math.max(k,1),height:Math.max(s-$,0),stackBase:s,value:F.y,color:i.get(g.id)??"var(--color-chart-1)"});}}return x}function mt(t,o,r,i,a,u,s,h,f,C){let S=t.filter(x=>o.has(x.id)),v=r.length,d=S.length;if(v===0||d===0)return [];let b=s/v,m=b*(1-f),p=d>0?m*(1-C)/d:0,k=[];for(let x=0;x<r.length;x++){let y=r[x],V=u+x*b+(b-m)/2;for(let M=0;M<S.length;M++){let B=S[M],g=B.data.find($=>String($.x)===y);if(!g)continue;let F=V+M*(p+m*C/Math.max(d,1)),W=h(g.y);k.push({seriesId:B.id,category:y,x:a,y:F,width:Math.max(W-a,0),height:Math.max(p,1),stackBase:a,value:g.y,color:i.get(B.id)??"var(--color-chart-1)"});}}return k}function ut(t,o,r,i,a,u,s,h,f){let C=t.filter(m=>o.has(m.id)),S=r.length;if(S===0)return [];let v=s/S*(1-f),d=[],b=new Map;r.forEach(m=>b.set(m,a));for(let m of C)for(let p of r){let k=m.data.find(B=>String(B.x)===p);if(!k)continue;let x=b.get(p)??a,y=Math.max(h(k.y)-a,0),V=r.indexOf(p),M=u+(V+.5)*(s/S);d.push({seriesId:m.id,category:p,x,y:M-v/2,width:y,height:Math.max(v,1),stackBase:x,value:k.y,color:i.get(m.id)??"var(--color-chart-1)"}),b.set(p,x+y);}return d}function pt(t,o,r,i,a,u,s,h,f){let C=t.filter(m=>o.has(m.id)),S=r.length;if(S===0)return [];let v=u/S*(1-f),d=[],b=new Map;r.forEach(m=>b.set(m,s));for(let m of C)for(let p of r){let k=m.data.find(g=>String(g.x)===p);if(!k)continue;let x=b.get(p)??s,y=Math.max(s-h(k.y),0),V=x-y,M=r.indexOf(p),B=a+(M+.5)*(u/S);d.push({seriesId:m.id,category:p,x:B-v/2,y:V,width:Math.max(v,1),height:y,stackBase:x,value:k.y,color:i.get(m.id)??"var(--color-chart-1)"}),b.set(p,V);}return d}function j({items:t,position:o,onToggle:r}){return jsxRuntime.jsx("div",{className:P({position:o}),children:t.map(i=>jsxRuntime.jsxs("button",{onClick:()=>r(i.id),className:G("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!i.visible&&"opacity-40"),"aria-pressed":i.visible,children:[jsxRuntime.jsx("span",{className:"h-2.5 w-5 rounded-sm shrink-0",style:{background:i.color}}),i.label]},i.id))})}function Jt({series:t,xAxis:o,yAxis:r,orientation:i="vertical",grouped:a=true,barRadius:u=4,barPadding:s=.2,groupPadding:h=.3,title:f,height:C=300,className:S,legend:v=true,legendPosition:d="bottom",animateOnMount:b=true,emptyText:m="No data to display"}){let p=react.useRef(null),k=react.useRef(null),[x,y]=react.useState(600),[V,M]=react.useState(null);react.useEffect(()=>{let e=k.current;if(!e)return;let c=e.getBoundingClientRect().width;c>0&&y(c);let T=new ResizeObserver(R=>{let D=R[0]?.contentRect.width;D&&D>0&&y(D);});return T.observe(e),()=>T.disconnect()},[]);let B=react.useMemo(()=>t.map(e=>e.id),[t]),{visible:g,toggle:F,isVisible:W}=et(B),$=react.useMemo(()=>nt(t.length),[t.length]),H=react.useMemo(()=>new Map(t.map((e,c)=>[e.id,e.color??$[c]])),[t,$]),w=i==="horizontal",U=react.useMemo(()=>J(!!o?.label,!!r?.label,false),[o?.label,r?.label]),n=react.useMemo(()=>Q(x,C,U),[x,C,U]),N=react.useMemo(()=>at(t,g),[t,g]),_=react.useMemo(()=>it(t,g,a,w?o:r),[t,g,a,w,o,r]),I=react.useMemo(()=>st(_,w?[n.plotLeft,n.plotRight]:[n.plotBottom,n.plotTop]),[_,n,w]),A=react.useMemo(()=>ct(_,(w?o:r)?.tickCount??5),[_,w,o,r]),ft=react.useMemo(()=>w?a?mt(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h,s):ut(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h):a?lt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,n.plotTop,I,h,s):pt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,I,h),[t,g,N,H,n,I,a,w,h,s]),bt=N.length>0?n.plotWidth/N.length:1,xt=N.length>0?n.plotHeight/N.length:1,tt=react.useCallback((e,c)=>{let T=p.current?.getBoundingClientRect();if(!T)return;let R=t.filter(D=>W(D.id)).map(D=>{let rt=D.data.find(St=>String(St.x)===c);return {name:D.name,value:rt?Y(rt.y,r?.format):"\u2014",color:H.get(D.id)??"var(--color-chart-1)"}});M({x:e.clientX-T.left+12,y:e.clientY-T.top-10,category:c,items:R});},[t,W,H,r?.format]),E=react.useMemo(()=>t.map((e,c)=>({id:e.id,label:e.name,color:e.color??$[c],visible:W(e.id)})),[t,$,W]),gt=t.some(e=>e.data.length>0),yt=d==="left"||d==="right",vt=jsxRuntime.jsxs("svg",{role:"img","aria-label":f??"Bar chart",width:n.width,height:n.height,viewBox:`0 0 ${n.width} ${n.height}`,shapeRendering:"geometricPrecision",children:[A.map(e=>{let c=I(e);return w?jsxRuntime.jsx("line",{x1:c,x2:c,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e):jsxRuntime.jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:c,y2:c,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e)}),ft.map(e=>{let c=Math.min(u,e.width/2,e.height/2),T=`${e.seriesId}-${e.category}`;return w?jsxRuntime.jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-h",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T):jsxRuntime.jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-v",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T)}),jsxRuntime.jsx("line",{x1:n.plotLeft,x2:n.plotLeft,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:n.plotBottom,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),!w&&A.map(e=>{let c=I(e);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:n.plotLeft-4,x2:n.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:n.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,r?.format)})]},e)}),w&&A.map(e=>{let c=I(e);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:c,x2:c,y1:n.plotBottom,y2:n.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:c,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,o?.format)})]},e)}),N.map((e,c)=>{if(w){let R=n.plotTop+(c+.5)*xt;return jsxRuntime.jsx("text",{x:n.plotLeft-6,y:R,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}let T=n.plotLeft+(c+.5)*bt;return jsxRuntime.jsx("text",{x:T,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}),r?.label&&!w&&jsxRuntime.jsx("text",{x:-(n.plotTop+n.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:r.label}),o?.label&&jsxRuntime.jsx("text",{x:n.plotLeft+n.plotWidth/2,y:n.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:o.label})]});return jsxRuntime.jsxs("div",{ref:p,className:G(O(),S),children:[f&&jsxRuntime.jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:f}),v&&d==="top"&&jsxRuntime.jsx(j,{items:E,position:"top",onToggle:F}),jsxRuntime.jsxs("div",{className:G("flex items-start",yt&&"gap-0"),children:[v&&d==="left"&&jsxRuntime.jsx(j,{items:E,position:"left",onToggle:F}),jsxRuntime.jsx("div",{ref:k,className:"flex-1 min-w-0 overflow-hidden",children:gt?vt:jsxRuntime.jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:C},children:m})}),v&&d==="right"&&jsxRuntime.jsx(j,{items:E,position:"right",onToggle:F})]}),v&&d==="bottom"&&jsxRuntime.jsx(j,{items:E,position:"bottom",onToggle:F}),V&&jsxRuntime.jsxs("div",{className:K(),style:{left:V.x,top:V.y},role:"tooltip",children:[jsxRuntime.jsx("div",{className:"mb-1 font-medium text-primary-200",children:V.category}),V.items.map((e,c)=>jsxRuntime.jsxs("div",{className:"flex items-center gap-1.5",children:[jsxRuntime.jsx("span",{className:"h-2 w-2 rounded-full shrink-0",style:{background:e.color}}),jsxRuntime.jsxs("span",{className:"text-primary-300",children:[e.name,":"]}),jsxRuntime.jsx("span",{className:"font-medium",children:e.value})]},c))]})]})}exports.BarChart=Jt;
1
+ 'use strict';var react=require('react'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),classVarianceAuthority=require('class-variance-authority'),jsxRuntime=require('react/jsx-runtime');function G(...t){return tailwindMerge.twMerge(clsx.clsx(t))}var O=classVarianceAuthority.cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),P=classVarianceAuthority.cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),K=classVarianceAuthority.cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function et(t){let[o,r]=react.useState(()=>new Set(t)),i=react.useCallback(s=>{r(h=>{let f=new Set(h);return f.has(s)?f.delete(s):f.add(s),f});},[]),a=react.useCallback(s=>o.has(s),[o]),m=react.useCallback(()=>{r(new Set(t));},[t]);return {visible:o,toggle:i,isVisible:a,showAll:m}}function nt(t){let o=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(r,i)=>o[i%o.length])}function q(t,o,r=.1){if(t===o)return [t-1,o+1];let a=(o-t)*r;return [Math.floor(t-a),Math.ceil(o+a)]}function ot(t,o,r){if(r<=1)return [t];let i=(o-t)/(r-1);return Array.from({length:r},(a,m)=>+(t+m*i).toFixed(10))}function Y(t,o){return o?o(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,o,r){return {top:20,right:r?65:20,bottom:t?52:36,left:o?65:52}}function Q(t,o,r){let i=Math.max(t,120);return {width:i,height:o,margins:r,plotLeft:r.left,plotTop:r.top,plotRight:i-r.right,plotBottom:o-r.bottom,plotWidth:Math.max(i-r.left-r.right,10),plotHeight:Math.max(o-r.top-r.bottom,10)}}function at(t,o){let r=new Set,i=[];for(let a of t)if(o.has(a.id))for(let m of a.data){let s=String(m.x);r.has(s)||(r.add(s),i.push(s));}return i}function it(t,o,r,i){if(r){let a=0;for(let s of t)if(o.has(s.id))for(let h of s.data)h.y>a&&(a=h.y);a===0&&(a=1);let[,m]=q(0,a,.05);return [i?.min??0,i?.max??m]}else {let a=new Map;for(let h of t)if(o.has(h.id))for(let f of h.data){let C=String(f.x);a.set(C,(a.get(C)??0)+f.y);}let m=Math.max(0,...a.values()),[,s]=q(0,m===0?1:m,.05);return [i?.min??0,i?.max??s]}}function st(t,o){let[r,i]=t,[a,m]=o;return i===r?()=>(a+m)/2:s=>a+(s-r)/(i-r)*(m-a)}function ct(t,o=5){return ot(t[0],t[1],o)}function lt(t,o,r,i,a,m,s,h,f,C,S){let v=t.filter(y=>o.has(y.id)),d=r.length,b=v.length;if(d===0||b===0)return [];let u=m/d,p=u*(1-C),k=b>0?p*(1-S)/b:0,x=[];for(let y=0;y<r.length;y++){let V=r[y],M=a+y*u+(u-p)/2;for(let B=0;B<v.length;B++){let g=v[B],F=g.data.find(H=>String(H.x)===V);if(!F)continue;let W=M+B*(k+p*S/b),$=f(F.y);x.push({seriesId:g.id,category:V,x:W,y:$,width:Math.max(k,1),height:Math.max(s-$,0),stackBase:s,value:F.y,color:i.get(g.id)??"var(--color-chart-1)"});}}return x}function ut(t,o,r,i,a,m,s,h,f,C){let S=t.filter(x=>o.has(x.id)),v=r.length,d=S.length;if(v===0||d===0)return [];let b=s/v,u=b*(1-f),p=d>0?u*(1-C)/d:0,k=[];for(let x=0;x<r.length;x++){let y=r[x],V=m+x*b+(b-u)/2;for(let M=0;M<S.length;M++){let B=S[M],g=B.data.find($=>String($.x)===y);if(!g)continue;let F=V+M*(p+u*C/Math.max(d,1)),W=h(g.y);k.push({seriesId:B.id,category:y,x:a,y:F,width:Math.max(W-a,0),height:Math.max(p,1),stackBase:a,value:g.y,color:i.get(B.id)??"var(--color-chart-1)"});}}return k}function mt(t,o,r,i,a,m,s,h,f){let C=t.filter(u=>o.has(u.id)),S=r.length;if(S===0)return [];let v=s/S*(1-f),d=[],b=new Map;r.forEach(u=>b.set(u,a));for(let u of C)for(let p of r){let k=u.data.find(B=>String(B.x)===p);if(!k)continue;let x=b.get(p)??a,y=Math.max(h(k.y)-a,0),V=r.indexOf(p),M=m+(V+.5)*(s/S);d.push({seriesId:u.id,category:p,x,y:M-v/2,width:y,height:Math.max(v,1),stackBase:x,value:k.y,color:i.get(u.id)??"var(--color-chart-1)"}),b.set(p,x+y);}return d}function pt(t,o,r,i,a,m,s,h,f){let C=t.filter(u=>o.has(u.id)),S=r.length;if(S===0)return [];let v=m/S*(1-f),d=[],b=new Map;r.forEach(u=>b.set(u,s));for(let u of C)for(let p of r){let k=u.data.find(g=>String(g.x)===p);if(!k)continue;let x=b.get(p)??s,y=Math.max(s-h(k.y),0),V=x-y,M=r.indexOf(p),B=a+(M+.5)*(m/S);d.push({seriesId:u.id,category:p,x:B-v/2,y:V,width:Math.max(v,1),height:y,stackBase:x,value:k.y,color:i.get(u.id)??"var(--color-chart-1)"}),b.set(p,V);}return d}function j({items:t,position:o,onToggle:r}){return jsxRuntime.jsx("div",{className:P({position:o}),children:t.map(i=>jsxRuntime.jsxs("button",{onClick:()=>r(i.id),className:G("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!i.visible&&"opacity-40"),"aria-pressed":i.visible,children:[jsxRuntime.jsx("span",{className:"h-2.5 w-5 rounded-sm shrink-0",style:{background:i.color}}),i.label]},i.id))})}function Jt({series:t,xAxis:o,yAxis:r,orientation:i="vertical",grouped:a=true,barRadius:m=4,barPadding:s=.2,groupPadding:h=.3,title:f,height:C=300,className:S,legend:v=true,legendPosition:d="bottom",animateOnMount:b=true,emptyText:u="No data to display"}){let p=react.useRef(null),k=react.useRef(null),[x,y]=react.useState(600),[V,M]=react.useState(null);react.useEffect(()=>{let e=k.current;if(!e)return;let c=e.getBoundingClientRect().width;c>0&&y(c);let T=new ResizeObserver(R=>{let D=R[0]?.contentRect.width;D&&D>0&&y(D);});return T.observe(e),()=>T.disconnect()},[]);let B=react.useMemo(()=>t.map(e=>e.id),[t]),{visible:g,toggle:F,isVisible:W}=et(B),$=react.useMemo(()=>nt(t.length),[t.length]),H=react.useMemo(()=>new Map(t.map((e,c)=>[e.id,e.color??$[c]])),[t,$]),w=i==="horizontal",U=react.useMemo(()=>J(!!o?.label,!!r?.label,false),[o?.label,r?.label]),n=react.useMemo(()=>Q(x,C,U),[x,C,U]),N=react.useMemo(()=>at(t,g),[t,g]),_=react.useMemo(()=>it(t,g,a,w?o:r),[t,g,a,w,o,r]),I=react.useMemo(()=>st(_,w?[n.plotLeft,n.plotRight]:[n.plotBottom,n.plotTop]),[_,n,w]),A=react.useMemo(()=>ct(_,(w?o:r)?.tickCount??5),[_,w,o,r]),ft=react.useMemo(()=>w?a?ut(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h,s):mt(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h):a?lt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,n.plotTop,I,h,s):pt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,I,h),[t,g,N,H,n,I,a,w,h,s]),bt=N.length>0?n.plotWidth/N.length:1,xt=N.length>0?n.plotHeight/N.length:1,tt=react.useCallback((e,c)=>{let T=p.current?.getBoundingClientRect();if(!T)return;let R=t.filter(D=>W(D.id)).map(D=>{let rt=D.data.find(St=>String(St.x)===c);return {name:D.name,value:rt?Y(rt.y,r?.format):"\u2014",color:H.get(D.id)??"var(--color-chart-1)"}});M({x:e.clientX-T.left+12,y:e.clientY-T.top-10,category:c,items:R});},[t,W,H,r?.format]),E=react.useMemo(()=>t.map((e,c)=>({id:e.id,label:e.name,color:e.color??$[c],visible:W(e.id)})),[t,$,W]),gt=t.some(e=>e.data.length>0),yt=d==="left"||d==="right",vt=jsxRuntime.jsxs("svg",{role:"img","aria-label":f??"Bar chart",width:n.width,height:n.height,viewBox:`0 0 ${n.width} ${n.height}`,shapeRendering:"geometricPrecision",children:[A.map(e=>{let c=I(e);return w?jsxRuntime.jsx("line",{x1:c,x2:c,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e):jsxRuntime.jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:c,y2:c,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e)}),ft.map(e=>{let c=Math.min(m,e.width/2,e.height/2),T=`${e.seriesId}-${e.category}`;return w?jsxRuntime.jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-h",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T):jsxRuntime.jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-v",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T)}),jsxRuntime.jsx("line",{x1:n.plotLeft,x2:n.plotLeft,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:n.plotBottom,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),!w&&A.map(e=>{let c=I(e);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:n.plotLeft-4,x2:n.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:n.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,r?.format)})]},e)}),w&&A.map(e=>{let c=I(e);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:c,x2:c,y1:n.plotBottom,y2:n.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:c,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,o?.format)})]},e)}),N.map((e,c)=>{if(w){let R=n.plotTop+(c+.5)*xt;return jsxRuntime.jsx("text",{x:n.plotLeft-6,y:R,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}let T=n.plotLeft+(c+.5)*bt;return jsxRuntime.jsx("text",{x:T,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}),r?.label&&!w&&jsxRuntime.jsx("text",{x:-(n.plotTop+n.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:r.label}),o?.label&&jsxRuntime.jsx("text",{x:n.plotLeft+n.plotWidth/2,y:n.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:o.label})]});return jsxRuntime.jsxs("div",{ref:p,className:G(O(),S),children:[f&&jsxRuntime.jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:f}),v&&d==="top"&&jsxRuntime.jsx(j,{items:E,position:"top",onToggle:F}),jsxRuntime.jsxs("div",{className:G("flex items-start",yt&&"gap-0"),children:[v&&d==="left"&&jsxRuntime.jsx(j,{items:E,position:"left",onToggle:F}),jsxRuntime.jsx("div",{ref:k,className:"flex-1 min-w-0 overflow-hidden",children:gt?vt:jsxRuntime.jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:C},children:u})}),v&&d==="right"&&jsxRuntime.jsx(j,{items:E,position:"right",onToggle:F})]}),v&&d==="bottom"&&jsxRuntime.jsx(j,{items:E,position:"bottom",onToggle:F}),V&&jsxRuntime.jsxs("div",{className:K(),style:{left:V.x,top:V.y},role:"tooltip",children:[jsxRuntime.jsx("div",{className:"mb-1 font-medium text-primary-200",children:V.category}),V.items.map((e,c)=>jsxRuntime.jsxs("div",{className:"flex items-center gap-1.5",children:[jsxRuntime.jsx("span",{className:"h-2 w-2 rounded-full shrink-0",style:{background:e.color}}),jsxRuntime.jsxs("span",{className:"text-primary-300",children:[e.name,":"]}),jsxRuntime.jsx("span",{className:"font-medium",children:e.value})]},c))]})]})}exports.BarChart=Jt;
@@ -1 +1 @@
1
- import {useRef,useState,useEffect,useMemo,useCallback}from'react';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {cva}from'class-variance-authority';import {jsxs,jsx}from'react/jsx-runtime';function G(...t){return twMerge(clsx(t))}var O=cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),P=cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),K=cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function et(t){let[o,r]=useState(()=>new Set(t)),i=useCallback(s=>{r(h=>{let f=new Set(h);return f.has(s)?f.delete(s):f.add(s),f});},[]),a=useCallback(s=>o.has(s),[o]),u=useCallback(()=>{r(new Set(t));},[t]);return {visible:o,toggle:i,isVisible:a,showAll:u}}function nt(t){let o=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(r,i)=>o[i%o.length])}function q(t,o,r=.1){if(t===o)return [t-1,o+1];let a=(o-t)*r;return [Math.floor(t-a),Math.ceil(o+a)]}function ot(t,o,r){if(r<=1)return [t];let i=(o-t)/(r-1);return Array.from({length:r},(a,u)=>+(t+u*i).toFixed(10))}function Y(t,o){return o?o(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,o,r){return {top:20,right:r?65:20,bottom:t?52:36,left:o?65:52}}function Q(t,o,r){let i=Math.max(t,120);return {width:i,height:o,margins:r,plotLeft:r.left,plotTop:r.top,plotRight:i-r.right,plotBottom:o-r.bottom,plotWidth:Math.max(i-r.left-r.right,10),plotHeight:Math.max(o-r.top-r.bottom,10)}}function at(t,o){let r=new Set,i=[];for(let a of t)if(o.has(a.id))for(let u of a.data){let s=String(u.x);r.has(s)||(r.add(s),i.push(s));}return i}function it(t,o,r,i){if(r){let a=0;for(let s of t)if(o.has(s.id))for(let h of s.data)h.y>a&&(a=h.y);a===0&&(a=1);let[,u]=q(0,a,.05);return [i?.min??0,i?.max??u]}else {let a=new Map;for(let h of t)if(o.has(h.id))for(let f of h.data){let C=String(f.x);a.set(C,(a.get(C)??0)+f.y);}let u=Math.max(0,...a.values()),[,s]=q(0,u===0?1:u,.05);return [i?.min??0,i?.max??s]}}function st(t,o){let[r,i]=t,[a,u]=o;return i===r?()=>(a+u)/2:s=>a+(s-r)/(i-r)*(u-a)}function ct(t,o=5){return ot(t[0],t[1],o)}function lt(t,o,r,i,a,u,s,h,f,C,S){let v=t.filter(y=>o.has(y.id)),d=r.length,b=v.length;if(d===0||b===0)return [];let m=u/d,p=m*(1-C),k=b>0?p*(1-S)/b:0,x=[];for(let y=0;y<r.length;y++){let V=r[y],M=a+y*m+(m-p)/2;for(let B=0;B<v.length;B++){let g=v[B],F=g.data.find(H=>String(H.x)===V);if(!F)continue;let W=M+B*(k+p*S/b),$=f(F.y);x.push({seriesId:g.id,category:V,x:W,y:$,width:Math.max(k,1),height:Math.max(s-$,0),stackBase:s,value:F.y,color:i.get(g.id)??"var(--color-chart-1)"});}}return x}function mt(t,o,r,i,a,u,s,h,f,C){let S=t.filter(x=>o.has(x.id)),v=r.length,d=S.length;if(v===0||d===0)return [];let b=s/v,m=b*(1-f),p=d>0?m*(1-C)/d:0,k=[];for(let x=0;x<r.length;x++){let y=r[x],V=u+x*b+(b-m)/2;for(let M=0;M<S.length;M++){let B=S[M],g=B.data.find($=>String($.x)===y);if(!g)continue;let F=V+M*(p+m*C/Math.max(d,1)),W=h(g.y);k.push({seriesId:B.id,category:y,x:a,y:F,width:Math.max(W-a,0),height:Math.max(p,1),stackBase:a,value:g.y,color:i.get(B.id)??"var(--color-chart-1)"});}}return k}function ut(t,o,r,i,a,u,s,h,f){let C=t.filter(m=>o.has(m.id)),S=r.length;if(S===0)return [];let v=s/S*(1-f),d=[],b=new Map;r.forEach(m=>b.set(m,a));for(let m of C)for(let p of r){let k=m.data.find(B=>String(B.x)===p);if(!k)continue;let x=b.get(p)??a,y=Math.max(h(k.y)-a,0),V=r.indexOf(p),M=u+(V+.5)*(s/S);d.push({seriesId:m.id,category:p,x,y:M-v/2,width:y,height:Math.max(v,1),stackBase:x,value:k.y,color:i.get(m.id)??"var(--color-chart-1)"}),b.set(p,x+y);}return d}function pt(t,o,r,i,a,u,s,h,f){let C=t.filter(m=>o.has(m.id)),S=r.length;if(S===0)return [];let v=u/S*(1-f),d=[],b=new Map;r.forEach(m=>b.set(m,s));for(let m of C)for(let p of r){let k=m.data.find(g=>String(g.x)===p);if(!k)continue;let x=b.get(p)??s,y=Math.max(s-h(k.y),0),V=x-y,M=r.indexOf(p),B=a+(M+.5)*(u/S);d.push({seriesId:m.id,category:p,x:B-v/2,y:V,width:Math.max(v,1),height:y,stackBase:x,value:k.y,color:i.get(m.id)??"var(--color-chart-1)"}),b.set(p,V);}return d}function j({items:t,position:o,onToggle:r}){return jsx("div",{className:P({position:o}),children:t.map(i=>jsxs("button",{onClick:()=>r(i.id),className:G("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!i.visible&&"opacity-40"),"aria-pressed":i.visible,children:[jsx("span",{className:"h-2.5 w-5 rounded-sm shrink-0",style:{background:i.color}}),i.label]},i.id))})}function Jt({series:t,xAxis:o,yAxis:r,orientation:i="vertical",grouped:a=true,barRadius:u=4,barPadding:s=.2,groupPadding:h=.3,title:f,height:C=300,className:S,legend:v=true,legendPosition:d="bottom",animateOnMount:b=true,emptyText:m="No data to display"}){let p=useRef(null),k=useRef(null),[x,y]=useState(600),[V,M]=useState(null);useEffect(()=>{let e=k.current;if(!e)return;let c=e.getBoundingClientRect().width;c>0&&y(c);let T=new ResizeObserver(R=>{let D=R[0]?.contentRect.width;D&&D>0&&y(D);});return T.observe(e),()=>T.disconnect()},[]);let B=useMemo(()=>t.map(e=>e.id),[t]),{visible:g,toggle:F,isVisible:W}=et(B),$=useMemo(()=>nt(t.length),[t.length]),H=useMemo(()=>new Map(t.map((e,c)=>[e.id,e.color??$[c]])),[t,$]),w=i==="horizontal",U=useMemo(()=>J(!!o?.label,!!r?.label,false),[o?.label,r?.label]),n=useMemo(()=>Q(x,C,U),[x,C,U]),N=useMemo(()=>at(t,g),[t,g]),_=useMemo(()=>it(t,g,a,w?o:r),[t,g,a,w,o,r]),I=useMemo(()=>st(_,w?[n.plotLeft,n.plotRight]:[n.plotBottom,n.plotTop]),[_,n,w]),A=useMemo(()=>ct(_,(w?o:r)?.tickCount??5),[_,w,o,r]),ft=useMemo(()=>w?a?mt(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h,s):ut(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h):a?lt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,n.plotTop,I,h,s):pt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,I,h),[t,g,N,H,n,I,a,w,h,s]),bt=N.length>0?n.plotWidth/N.length:1,xt=N.length>0?n.plotHeight/N.length:1,tt=useCallback((e,c)=>{let T=p.current?.getBoundingClientRect();if(!T)return;let R=t.filter(D=>W(D.id)).map(D=>{let rt=D.data.find(St=>String(St.x)===c);return {name:D.name,value:rt?Y(rt.y,r?.format):"\u2014",color:H.get(D.id)??"var(--color-chart-1)"}});M({x:e.clientX-T.left+12,y:e.clientY-T.top-10,category:c,items:R});},[t,W,H,r?.format]),E=useMemo(()=>t.map((e,c)=>({id:e.id,label:e.name,color:e.color??$[c],visible:W(e.id)})),[t,$,W]),gt=t.some(e=>e.data.length>0),yt=d==="left"||d==="right",vt=jsxs("svg",{role:"img","aria-label":f??"Bar chart",width:n.width,height:n.height,viewBox:`0 0 ${n.width} ${n.height}`,shapeRendering:"geometricPrecision",children:[A.map(e=>{let c=I(e);return w?jsx("line",{x1:c,x2:c,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e):jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:c,y2:c,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e)}),ft.map(e=>{let c=Math.min(u,e.width/2,e.height/2),T=`${e.seriesId}-${e.category}`;return w?jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-h",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T):jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-v",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T)}),jsx("line",{x1:n.plotLeft,x2:n.plotLeft,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:n.plotBottom,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),!w&&A.map(e=>{let c=I(e);return jsxs("g",{children:[jsx("line",{x1:n.plotLeft-4,x2:n.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:n.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,r?.format)})]},e)}),w&&A.map(e=>{let c=I(e);return jsxs("g",{children:[jsx("line",{x1:c,x2:c,y1:n.plotBottom,y2:n.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:c,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,o?.format)})]},e)}),N.map((e,c)=>{if(w){let R=n.plotTop+(c+.5)*xt;return jsx("text",{x:n.plotLeft-6,y:R,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}let T=n.plotLeft+(c+.5)*bt;return jsx("text",{x:T,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}),r?.label&&!w&&jsx("text",{x:-(n.plotTop+n.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:r.label}),o?.label&&jsx("text",{x:n.plotLeft+n.plotWidth/2,y:n.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:o.label})]});return jsxs("div",{ref:p,className:G(O(),S),children:[f&&jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:f}),v&&d==="top"&&jsx(j,{items:E,position:"top",onToggle:F}),jsxs("div",{className:G("flex items-start",yt&&"gap-0"),children:[v&&d==="left"&&jsx(j,{items:E,position:"left",onToggle:F}),jsx("div",{ref:k,className:"flex-1 min-w-0 overflow-hidden",children:gt?vt:jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:C},children:m})}),v&&d==="right"&&jsx(j,{items:E,position:"right",onToggle:F})]}),v&&d==="bottom"&&jsx(j,{items:E,position:"bottom",onToggle:F}),V&&jsxs("div",{className:K(),style:{left:V.x,top:V.y},role:"tooltip",children:[jsx("div",{className:"mb-1 font-medium text-primary-200",children:V.category}),V.items.map((e,c)=>jsxs("div",{className:"flex items-center gap-1.5",children:[jsx("span",{className:"h-2 w-2 rounded-full shrink-0",style:{background:e.color}}),jsxs("span",{className:"text-primary-300",children:[e.name,":"]}),jsx("span",{className:"font-medium",children:e.value})]},c))]})]})}export{Jt as BarChart};
1
+ import {useRef,useState,useEffect,useMemo,useCallback}from'react';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {cva}from'class-variance-authority';import {jsxs,jsx}from'react/jsx-runtime';function G(...t){return twMerge(clsx(t))}var O=cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),P=cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),K=cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function et(t){let[o,r]=useState(()=>new Set(t)),i=useCallback(s=>{r(h=>{let f=new Set(h);return f.has(s)?f.delete(s):f.add(s),f});},[]),a=useCallback(s=>o.has(s),[o]),m=useCallback(()=>{r(new Set(t));},[t]);return {visible:o,toggle:i,isVisible:a,showAll:m}}function nt(t){let o=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(r,i)=>o[i%o.length])}function q(t,o,r=.1){if(t===o)return [t-1,o+1];let a=(o-t)*r;return [Math.floor(t-a),Math.ceil(o+a)]}function ot(t,o,r){if(r<=1)return [t];let i=(o-t)/(r-1);return Array.from({length:r},(a,m)=>+(t+m*i).toFixed(10))}function Y(t,o){return o?o(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,o,r){return {top:20,right:r?65:20,bottom:t?52:36,left:o?65:52}}function Q(t,o,r){let i=Math.max(t,120);return {width:i,height:o,margins:r,plotLeft:r.left,plotTop:r.top,plotRight:i-r.right,plotBottom:o-r.bottom,plotWidth:Math.max(i-r.left-r.right,10),plotHeight:Math.max(o-r.top-r.bottom,10)}}function at(t,o){let r=new Set,i=[];for(let a of t)if(o.has(a.id))for(let m of a.data){let s=String(m.x);r.has(s)||(r.add(s),i.push(s));}return i}function it(t,o,r,i){if(r){let a=0;for(let s of t)if(o.has(s.id))for(let h of s.data)h.y>a&&(a=h.y);a===0&&(a=1);let[,m]=q(0,a,.05);return [i?.min??0,i?.max??m]}else {let a=new Map;for(let h of t)if(o.has(h.id))for(let f of h.data){let C=String(f.x);a.set(C,(a.get(C)??0)+f.y);}let m=Math.max(0,...a.values()),[,s]=q(0,m===0?1:m,.05);return [i?.min??0,i?.max??s]}}function st(t,o){let[r,i]=t,[a,m]=o;return i===r?()=>(a+m)/2:s=>a+(s-r)/(i-r)*(m-a)}function ct(t,o=5){return ot(t[0],t[1],o)}function lt(t,o,r,i,a,m,s,h,f,C,S){let v=t.filter(y=>o.has(y.id)),d=r.length,b=v.length;if(d===0||b===0)return [];let u=m/d,p=u*(1-C),k=b>0?p*(1-S)/b:0,x=[];for(let y=0;y<r.length;y++){let V=r[y],M=a+y*u+(u-p)/2;for(let B=0;B<v.length;B++){let g=v[B],F=g.data.find(H=>String(H.x)===V);if(!F)continue;let W=M+B*(k+p*S/b),$=f(F.y);x.push({seriesId:g.id,category:V,x:W,y:$,width:Math.max(k,1),height:Math.max(s-$,0),stackBase:s,value:F.y,color:i.get(g.id)??"var(--color-chart-1)"});}}return x}function ut(t,o,r,i,a,m,s,h,f,C){let S=t.filter(x=>o.has(x.id)),v=r.length,d=S.length;if(v===0||d===0)return [];let b=s/v,u=b*(1-f),p=d>0?u*(1-C)/d:0,k=[];for(let x=0;x<r.length;x++){let y=r[x],V=m+x*b+(b-u)/2;for(let M=0;M<S.length;M++){let B=S[M],g=B.data.find($=>String($.x)===y);if(!g)continue;let F=V+M*(p+u*C/Math.max(d,1)),W=h(g.y);k.push({seriesId:B.id,category:y,x:a,y:F,width:Math.max(W-a,0),height:Math.max(p,1),stackBase:a,value:g.y,color:i.get(B.id)??"var(--color-chart-1)"});}}return k}function mt(t,o,r,i,a,m,s,h,f){let C=t.filter(u=>o.has(u.id)),S=r.length;if(S===0)return [];let v=s/S*(1-f),d=[],b=new Map;r.forEach(u=>b.set(u,a));for(let u of C)for(let p of r){let k=u.data.find(B=>String(B.x)===p);if(!k)continue;let x=b.get(p)??a,y=Math.max(h(k.y)-a,0),V=r.indexOf(p),M=m+(V+.5)*(s/S);d.push({seriesId:u.id,category:p,x,y:M-v/2,width:y,height:Math.max(v,1),stackBase:x,value:k.y,color:i.get(u.id)??"var(--color-chart-1)"}),b.set(p,x+y);}return d}function pt(t,o,r,i,a,m,s,h,f){let C=t.filter(u=>o.has(u.id)),S=r.length;if(S===0)return [];let v=m/S*(1-f),d=[],b=new Map;r.forEach(u=>b.set(u,s));for(let u of C)for(let p of r){let k=u.data.find(g=>String(g.x)===p);if(!k)continue;let x=b.get(p)??s,y=Math.max(s-h(k.y),0),V=x-y,M=r.indexOf(p),B=a+(M+.5)*(m/S);d.push({seriesId:u.id,category:p,x:B-v/2,y:V,width:Math.max(v,1),height:y,stackBase:x,value:k.y,color:i.get(u.id)??"var(--color-chart-1)"}),b.set(p,V);}return d}function j({items:t,position:o,onToggle:r}){return jsx("div",{className:P({position:o}),children:t.map(i=>jsxs("button",{onClick:()=>r(i.id),className:G("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!i.visible&&"opacity-40"),"aria-pressed":i.visible,children:[jsx("span",{className:"h-2.5 w-5 rounded-sm shrink-0",style:{background:i.color}}),i.label]},i.id))})}function Jt({series:t,xAxis:o,yAxis:r,orientation:i="vertical",grouped:a=true,barRadius:m=4,barPadding:s=.2,groupPadding:h=.3,title:f,height:C=300,className:S,legend:v=true,legendPosition:d="bottom",animateOnMount:b=true,emptyText:u="No data to display"}){let p=useRef(null),k=useRef(null),[x,y]=useState(600),[V,M]=useState(null);useEffect(()=>{let e=k.current;if(!e)return;let c=e.getBoundingClientRect().width;c>0&&y(c);let T=new ResizeObserver(R=>{let D=R[0]?.contentRect.width;D&&D>0&&y(D);});return T.observe(e),()=>T.disconnect()},[]);let B=useMemo(()=>t.map(e=>e.id),[t]),{visible:g,toggle:F,isVisible:W}=et(B),$=useMemo(()=>nt(t.length),[t.length]),H=useMemo(()=>new Map(t.map((e,c)=>[e.id,e.color??$[c]])),[t,$]),w=i==="horizontal",U=useMemo(()=>J(!!o?.label,!!r?.label,false),[o?.label,r?.label]),n=useMemo(()=>Q(x,C,U),[x,C,U]),N=useMemo(()=>at(t,g),[t,g]),_=useMemo(()=>it(t,g,a,w?o:r),[t,g,a,w,o,r]),I=useMemo(()=>st(_,w?[n.plotLeft,n.plotRight]:[n.plotBottom,n.plotTop]),[_,n,w]),A=useMemo(()=>ct(_,(w?o:r)?.tickCount??5),[_,w,o,r]),ft=useMemo(()=>w?a?ut(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h,s):mt(t,g,N,H,n.plotLeft,n.plotTop,n.plotHeight,I,h):a?lt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,n.plotTop,I,h,s):pt(t,g,N,H,n.plotLeft,n.plotWidth,n.plotBottom,I,h),[t,g,N,H,n,I,a,w,h,s]),bt=N.length>0?n.plotWidth/N.length:1,xt=N.length>0?n.plotHeight/N.length:1,tt=useCallback((e,c)=>{let T=p.current?.getBoundingClientRect();if(!T)return;let R=t.filter(D=>W(D.id)).map(D=>{let rt=D.data.find(St=>String(St.x)===c);return {name:D.name,value:rt?Y(rt.y,r?.format):"\u2014",color:H.get(D.id)??"var(--color-chart-1)"}});M({x:e.clientX-T.left+12,y:e.clientY-T.top-10,category:c,items:R});},[t,W,H,r?.format]),E=useMemo(()=>t.map((e,c)=>({id:e.id,label:e.name,color:e.color??$[c],visible:W(e.id)})),[t,$,W]),gt=t.some(e=>e.data.length>0),yt=d==="left"||d==="right",vt=jsxs("svg",{role:"img","aria-label":f??"Bar chart",width:n.width,height:n.height,viewBox:`0 0 ${n.width} ${n.height}`,shapeRendering:"geometricPrecision",children:[A.map(e=>{let c=I(e);return w?jsx("line",{x1:c,x2:c,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e):jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:c,y2:c,stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},e)}),ft.map(e=>{let c=Math.min(m,e.width/2,e.height/2),T=`${e.seriesId}-${e.category}`;return w?jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-h",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T):jsx("rect",{x:e.x,y:e.y,width:e.width,height:e.height,rx:c,fill:e.color,className:G("chart-bar-v",b&&"chart-bar-animate"),onMouseMove:R=>tt(R,e.category),onMouseLeave:()=>M(null)},T)}),jsx("line",{x1:n.plotLeft,x2:n.plotLeft,y1:n.plotTop,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("line",{x1:n.plotLeft,x2:n.plotRight,y1:n.plotBottom,y2:n.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),!w&&A.map(e=>{let c=I(e);return jsxs("g",{children:[jsx("line",{x1:n.plotLeft-4,x2:n.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:n.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,r?.format)})]},e)}),w&&A.map(e=>{let c=I(e);return jsxs("g",{children:[jsx("line",{x1:c,x2:c,y1:n.plotBottom,y2:n.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:c,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:Y(e,o?.format)})]},e)}),N.map((e,c)=>{if(w){let R=n.plotTop+(c+.5)*xt;return jsx("text",{x:n.plotLeft-6,y:R,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}let T=n.plotLeft+(c+.5)*bt;return jsx("text",{x:T,y:n.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:e},e)}),r?.label&&!w&&jsx("text",{x:-(n.plotTop+n.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:r.label}),o?.label&&jsx("text",{x:n.plotLeft+n.plotWidth/2,y:n.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:o.label})]});return jsxs("div",{ref:p,className:G(O(),S),children:[f&&jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:f}),v&&d==="top"&&jsx(j,{items:E,position:"top",onToggle:F}),jsxs("div",{className:G("flex items-start",yt&&"gap-0"),children:[v&&d==="left"&&jsx(j,{items:E,position:"left",onToggle:F}),jsx("div",{ref:k,className:"flex-1 min-w-0 overflow-hidden",children:gt?vt:jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:C},children:u})}),v&&d==="right"&&jsx(j,{items:E,position:"right",onToggle:F})]}),v&&d==="bottom"&&jsx(j,{items:E,position:"bottom",onToggle:F}),V&&jsxs("div",{className:K(),style:{left:V.x,top:V.y},role:"tooltip",children:[jsx("div",{className:"mb-1 font-medium text-primary-200",children:V.category}),V.items.map((e,c)=>jsxs("div",{className:"flex items-center gap-1.5",children:[jsx("span",{className:"h-2 w-2 rounded-full shrink-0",style:{background:e.color}}),jsxs("span",{className:"text-primary-300",children:[e.name,":"]}),jsx("span",{className:"font-medium",children:e.value})]},c))]})]})}export{Jt as BarChart};
@@ -1 +1 @@
1
- 'use strict';var react=require('react'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),classVarianceAuthority=require('class-variance-authority'),jsxRuntime=require('react/jsx-runtime');function B(...n){return tailwindMerge.twMerge(clsx.clsx(n))}var lt=classVarianceAuthority.cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),dt=classVarianceAuthority.cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),mt=classVarianceAuthority.cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function $t(n){let[l,h]=react.useState(()=>new Set(n)),a=react.useCallback(u=>{h(C=>{let A=new Set(C);return A.has(u)?A.delete(u):A.add(u),A});},[]),g=react.useCallback(u=>l.has(u),[l]),p=react.useCallback(()=>{h(new Set(n));},[n]);return {visible:l,toggle:a,isVisible:g,showAll:p}}function wt(n){let l=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:n},(h,a)=>l[a%l.length])}function tt(n,l){return l?l(n):typeof n=="number"?Math.abs(n)>=1e6?`${(n/1e6).toFixed(1)}M`:Math.abs(n)>=1e3?`${(n/1e3).toFixed(1)}K`:Number.isInteger(n)?n.toString():n.toFixed(2):String(n)}function k(n,l,h,a){let g=(a-90)*Math.PI/180;return {x:n+h*Math.cos(g),y:l+h*Math.sin(g)}}function kt(n,l,h,a,g,p,u,C,A){let z=n.filter(m=>l.has(m.id)),L=z.reduce((m,F)=>m+F.value,0);if(L===0)return [];let H=new Map(n.map((m,F)=>[m.id,m.color??h[F%h.length]])),j=[],E=C;for(let m of z){let F=m.value/L*360,G=A/2,b=E+G,M=E+F-G,ot=(b+M)/2,T;if(Math.abs(M-b)>=359.9){let f=k(p,u,a,b),V=k(p,u,a,b+180);T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 1 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} A ${a} ${a} 0 1 1 ${f.x.toFixed(3)} ${f.y.toFixed(3)} Z`;}else if(g>0){let f=k(p,u,a,b),V=k(p,u,a,M),S=k(p,u,g,M),R=k(p,u,g,b),P=M-b>180?1:0;T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${P} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} L ${S.x.toFixed(3)} ${S.y.toFixed(3)} A ${g} ${g} 0 ${P} 0 ${R.x.toFixed(3)} ${R.y.toFixed(3)} Z`;}else {let f=k(p,u,a,b),V=k(p,u,a,M),S=M-b>180?1:0;T=`M ${p.toFixed(3)} ${u.toFixed(3)} L ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${S} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} Z`;}j.push({id:m.id,label:m.label,value:m.value,percent:Math.round(m.value/L*1e3)/10,color:H.get(m.id)??"var(--color-chart-1)",startAngle:b,endAngle:M,midAngle:ot,path:T,outerR:a,innerR:g,cx:p,cy:u}),E+=F;}return j}function Mt(n,l){let h=(n-90)*Math.PI/180;return {dx:Math.cos(h)*l,dy:Math.sin(h)*l}}function rt({items:n,position:l,onToggle:h}){return jsxRuntime.jsx("div",{className:dt({position:l}),children:n.map(a=>jsxRuntime.jsxs("button",{onClick:()=>h(a.id),className:B("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!a.visible&&"opacity-40"),"aria-pressed":a.visible,children:[jsxRuntime.jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:a.color}}),jsxRuntime.jsx("span",{children:a.label}),jsxRuntime.jsxs("span",{className:"text-secondary-400",children:["(",a.percent,"%)"]})]},a.id))})}function ce({data:n,donut:l=false,donutThickness:h=.5,startAngle:a=-90,padAngle:g=0,labelType:p="percent",centerLabel:u,centerSubLabel:C,explodeOnHover:A=true,explodeOffset:z=8,title:L,height:H=300,className:j,legend:E=true,legendPosition:m="bottom",animateOnMount:F=true,emptyText:G="No data to display"}){let b=react.useRef(null),M=react.useRef(null),[ot,T]=react.useState(400),[f,V]=react.useState(null),[S,R]=react.useState(null),[P,ft]=react.useState({}),O=react.useRef(null);react.useEffect(()=>{let t=M.current;if(!t)return;let o=t.getBoundingClientRect().width;o>0&&T(o);let r=new ResizeObserver(i=>{let c=i[0]?.contentRect.width;c&&c>0&&T(c);});return r.observe(t),()=>r.disconnect()},[]),react.useEffect(()=>{function t(r){let i=O.current;if(!i)return;"touches"in r&&r.cancelable&&r.preventDefault();let c="touches"in r?r.touches[0]?.clientX??i.startX:r.clientX,w="touches"in r?r.touches[0]?.clientY??i.startY:r.clientY;ft(X=>({...X,[i.id]:{dx:i.origDx+c-i.startX,dy:i.origDy+w-i.startY}}));}function o(){O.current=null;}return window.addEventListener("mousemove",t),window.addEventListener("mouseup",o),window.addEventListener("touchmove",t,{passive:false}),window.addEventListener("touchend",o),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("mouseup",o),window.removeEventListener("touchmove",t),window.removeEventListener("touchend",o);}},[]);let St=react.useMemo(()=>n.map(t=>t.id),[n]),{visible:xt,toggle:Z,isVisible:gt}=$t(St),K=react.useMemo(()=>wt(n.length),[n.length]),I=10,At=6.4,bt=18,it=14,U=6,at=ot,_=H,N=at/2,D=_/2,Et=p!=="none"?bt+U+4:U+4,st=Math.max(Math.min(N,D)-Et,20),yt=l?st*h:0,$=react.useMemo(()=>kt(n,xt,K,st,yt,N,D,a,g),[n,xt,K,st,yt,N,D,a,g]),Ct=react.useMemo(()=>$.map(t=>t.id).join(","),[$]);react.useEffect(()=>{ft({});},[Ct]);let Lt=react.useCallback((t,o)=>{if(O.current)return;let r=b.current?.getBoundingClientRect();if(!r)return;V(o);let i=$.find(c=>c.id===o);i&&R({x:t.clientX-r.left+12,y:t.clientY-r.top-10,items:[{label:i.label,value:tt(i.value),percent:`${i.percent}%`,color:i.color}]});},[$]),vt=react.useCallback(()=>{V(null),R(null);},[]),Vt=react.useCallback((t,o,r)=>{let i=P[t]??{dx:0,dy:0};O.current={id:t,startX:o,startY:r,origDx:i.dx,origDy:i.dy};},[P]),Tt=react.useCallback((t,o)=>{t.preventDefault(),V(o);let r=b.current?.getBoundingClientRect();if(!r)return;let i=t.touches[0];if(!i)return;let c=$.find(w=>w.id===o);c&&R({x:i.clientX-r.left+12,y:i.clientY-r.top-10,items:[{label:c.label,value:tt(c.value),percent:`${c.percent}%`,color:c.color}]});},[$]),q=react.useMemo(()=>{let t=n.reduce((o,r)=>o+r.value,0);return n.map((o,r)=>({id:o.id,label:o.label,color:o.color??K[r],visible:gt(o.id),percent:t>0?Math.round(o.value/t*1e3)/10:0}))},[n,K,gt]),Nt=n.length>0&&n.some(t=>t.value>0),Dt=m==="left"||m==="right",Xt=react.useMemo(()=>{if(p==="none")return [];let t=I+3,o=U+I*.5,r=_-U-I*.5,i=$.filter(e=>e.percent>=3).map(e=>{let x="";switch(p){case "percent":x=`${e.percent}%`;break;case "value":x=tt(e.value);break;case "label":x=e.label;break;case "label+percent":x=`${e.label} ${e.percent}%`;break}let Y=k(e.cx,e.cy,e.outerR+2,e.midAngle),y=k(e.cx,e.cy,e.outerR+bt,e.midAngle),s=(e.midAngle-90)*Math.PI/180,J=Math.cos(s)>=0,Q=Math.ceil(x.length*At);return {id:e.id,color:e.color,text:x,textW:Q,p1:Y,elbowX:y.x,y:y.y,isRight:J}}),c=i.filter(e=>e.isRight).sort((e,x)=>e.y-x.y),w=i.filter(e=>!e.isRight).sort((e,x)=>e.y-x.y);function X(e){if(e.length!==0)for(let x=0;x<20;x++){let Y=false;for(let s=1;s<e.length;s++){let J=e[s].y-e[s-1].y;if(J<t){let Q=(t-J)/2;e[s-1].y-=Q,e[s].y+=Q,Y=true;}}e[0].y<o&&(e[0].y=o);for(let s=1;s<e.length;s++)e[s].y<e[s-1].y+t&&(e[s].y=e[s-1].y+t);let y=e.length-1;e[y].y>r&&(e[y].y=r);for(let s=y-1;s>=0;s--)e[s].y>e[s+1].y-t&&(e[s].y=e[s+1].y-t);for(let s of e)s.y=Math.max(o,Math.min(r,s.y));if(!Y)break}}return X(c),X(w),[...w,...c]},[$,p,_]),Rt=jsxRuntime.jsxs("svg",{role:"img","aria-label":L??"Pie chart",width:at,height:_,viewBox:`0 0 ${at} ${_}`,shapeRendering:"geometricPrecision",children:[jsxRuntime.jsxs("g",{className:F?"chart-pie-animate":"",children:[$.map((t,o)=>{let r=f===t.id,i=A&&r,c=Mt(t.midAngle,i?z:0);return jsxRuntime.jsx("path",{d:t.path,fill:t.color,transform:i?`translate(${c.dx}, ${c.dy})`:void 0,className:B("chart-slice",F&&`chart-slice-animate-${Math.min(o,7)}`,r&&"chart-slice--exploded"),stroke:t.color,strokeWidth:.6,onMouseMove:w=>Lt(w,t.id),onMouseLeave:vt,onTouchStart:w=>Tt(w,t.id),onTouchEnd:vt,"aria-label":`${t.label}: ${t.percent}%`},t.id)}),Xt.map(t=>{let o=P[t.id]??{dx:0,dy:0},r=t.elbowX+o.dx,i=t.y+o.dy,c=r+(t.isRight?it:-it),w=t.isRight?c+3:c-3,X=it+3+t.textW+6,e=t.isRight?r:r-X,x=i-I-2,Y=I*2+4;return jsxRuntime.jsxs("g",{style:{userSelect:"none"},children:[jsxRuntime.jsx("polyline",{points:[`${t.p1.x.toFixed(2)},${t.p1.y.toFixed(2)}`,`${r.toFixed(2)},${i.toFixed(2)}`,`${c.toFixed(2)},${i.toFixed(2)}`].join(" "),fill:"none",stroke:t.color,strokeWidth:1.2,opacity:.85,strokeLinejoin:"round",strokeLinecap:"round",style:{pointerEvents:"none"}}),jsxRuntime.jsx("text",{x:w.toFixed(2),y:i.toFixed(2),textAnchor:t.isRight?"start":"end",dominantBaseline:"middle",fontSize:I,fill:"currentColor",className:"text-primary-700 dark:text-primary-300",style:{pointerEvents:"none"},children:t.text}),jsxRuntime.jsx("rect",{x:e,y:x,width:X,height:Y,fill:"transparent",style:{cursor:"grab",touchAction:"none"},onMouseDown:y=>{y.stopPropagation(),Vt(t.id,y.clientX,y.clientY);},onTouchStart:y=>{y.stopPropagation();let s=y.touches[0];s&&Vt(t.id,s.clientX,s.clientY);}})]},`label-${t.id}`)})]}),l&&jsxRuntime.jsx("g",{style:{pointerEvents:"none"},children:f&&$.find(t=>t.id===f)?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsxs("text",{x:N,y:D-8,textAnchor:"middle",dominantBaseline:"middle",fontSize:18,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:[$.find(t=>t.id===f)?.percent,"%"]}),jsxRuntime.jsx("text",{x:N,y:D+12,textAnchor:"middle",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:$.find(t=>t.id===f)?.label})]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[u&&jsxRuntime.jsx("text",{x:N,y:D-(C?8:0),textAnchor:"middle",dominantBaseline:"middle",fontSize:16,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:u}),C&&jsxRuntime.jsx("text",{x:N,y:D+14,textAnchor:"middle",dominantBaseline:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C})]})})]});return jsxRuntime.jsxs("div",{ref:b,className:B(lt(),j),children:[L&&jsxRuntime.jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:L}),E&&m==="top"&&jsxRuntime.jsx(rt,{items:q,position:"top",onToggle:Z}),jsxRuntime.jsxs("div",{className:B("flex items-start justify-center",Dt&&"gap-0"),children:[E&&m==="left"&&jsxRuntime.jsx(rt,{items:q,position:"left",onToggle:Z}),jsxRuntime.jsx("div",{ref:M,className:"flex-1 min-w-0 overflow-hidden",children:Nt?Rt:jsxRuntime.jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:H},children:G})}),E&&m==="right"&&jsxRuntime.jsx(rt,{items:q,position:"right",onToggle:Z})]}),E&&m==="bottom"&&jsxRuntime.jsx(rt,{items:q,position:"bottom",onToggle:Z}),S&&jsxRuntime.jsx("div",{className:mt(),style:{left:S.x,top:S.y},role:"tooltip",children:S.items.map((t,o)=>jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:t.color}}),jsxRuntime.jsxs("span",{className:"font-medium",children:[t.label,":"]}),jsxRuntime.jsx("span",{children:t.value}),jsxRuntime.jsxs("span",{className:"text-primary-300",children:["(",t.percent,")"]})]},o))})]})}exports.PieChart=ce;
1
+ 'use strict';var react=require('react'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),classVarianceAuthority=require('class-variance-authority'),jsxRuntime=require('react/jsx-runtime');function B(...n){return tailwindMerge.twMerge(clsx.clsx(n))}var lt=classVarianceAuthority.cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),dt=classVarianceAuthority.cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),mt=classVarianceAuthority.cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function $t(n){let[l,h]=react.useState(()=>new Set(n)),a=react.useCallback(u=>{h(C=>{let A=new Set(C);return A.has(u)?A.delete(u):A.add(u),A});},[]),g=react.useCallback(u=>l.has(u),[l]),p=react.useCallback(()=>{h(new Set(n));},[n]);return {visible:l,toggle:a,isVisible:g,showAll:p}}function wt(n){let l=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:n},(h,a)=>l[a%l.length])}function tt(n,l){return l?l(n):typeof n=="number"?Math.abs(n)>=1e6?`${(n/1e6).toFixed(1)}M`:Math.abs(n)>=1e3?`${(n/1e3).toFixed(1)}K`:Number.isInteger(n)?n.toString():n.toFixed(2):String(n)}function k(n,l,h,a){let g=(a-90)*Math.PI/180;return {x:n+h*Math.cos(g),y:l+h*Math.sin(g)}}function kt(n,l,h,a,g,p,u,C,A){let z=n.filter(m=>l.has(m.id)),L=z.reduce((m,F)=>m+F.value,0);if(L===0)return [];let H=new Map(n.map((m,F)=>[m.id,m.color??h[F%h.length]])),j=[],E=C;for(let m of z){let F=m.value/L*360,G=A/2,b=E+G,M=E+F-G,ot=(b+M)/2,T;if(Math.abs(M-b)>=359.9){let f=k(p,u,a,b),V=k(p,u,a,b+180);T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 1 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} A ${a} ${a} 0 1 1 ${f.x.toFixed(3)} ${f.y.toFixed(3)} Z`;}else if(g>0){let f=k(p,u,a,b),V=k(p,u,a,M),S=k(p,u,g,M),R=k(p,u,g,b),I=M-b>180?1:0;T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${I} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} L ${S.x.toFixed(3)} ${S.y.toFixed(3)} A ${g} ${g} 0 ${I} 0 ${R.x.toFixed(3)} ${R.y.toFixed(3)} Z`;}else {let f=k(p,u,a,b),V=k(p,u,a,M),S=M-b>180?1:0;T=`M ${p.toFixed(3)} ${u.toFixed(3)} L ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${S} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} Z`;}j.push({id:m.id,label:m.label,value:m.value,percent:Math.round(m.value/L*1e3)/10,color:H.get(m.id)??"var(--color-chart-1)",startAngle:b,endAngle:M,midAngle:ot,path:T,outerR:a,innerR:g,cx:p,cy:u}),E+=F;}return j}function Mt(n,l){let h=(n-90)*Math.PI/180;return {dx:Math.cos(h)*l,dy:Math.sin(h)*l}}function rt({items:n,position:l,onToggle:h}){return jsxRuntime.jsx("div",{className:dt({position:l}),children:n.map(a=>jsxRuntime.jsxs("button",{onClick:()=>h(a.id),className:B("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!a.visible&&"opacity-40"),"aria-pressed":a.visible,children:[jsxRuntime.jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:a.color}}),jsxRuntime.jsx("span",{children:a.label}),jsxRuntime.jsxs("span",{className:"text-secondary-400",children:["(",a.percent,"%)"]})]},a.id))})}function ce({data:n,donut:l=false,donutThickness:h=.5,startAngle:a=-90,padAngle:g=0,labelType:p="percent",centerLabel:u,centerSubLabel:C,explodeOnHover:A=true,explodeOffset:z=8,title:L,height:H=300,className:j,legend:E=true,legendPosition:m="bottom",animateOnMount:F=true,emptyText:G="No data to display"}){let b=react.useRef(null),M=react.useRef(null),[ot,T]=react.useState(400),[f,V]=react.useState(null),[S,R]=react.useState(null),[I,ft]=react.useState({}),O=react.useRef(null);react.useEffect(()=>{let t=M.current;if(!t)return;let o=t.getBoundingClientRect().width;o>0&&T(o);let r=new ResizeObserver(i=>{let c=i[0]?.contentRect.width;c&&c>0&&T(c);});return r.observe(t),()=>r.disconnect()},[]),react.useEffect(()=>{function t(r){let i=O.current;if(!i)return;"touches"in r&&r.cancelable&&r.preventDefault();let c="touches"in r?r.touches[0]?.clientX??i.startX:r.clientX,w="touches"in r?r.touches[0]?.clientY??i.startY:r.clientY;ft(X=>({...X,[i.id]:{dx:i.origDx+c-i.startX,dy:i.origDy+w-i.startY}}));}function o(){O.current=null;}return window.addEventListener("mousemove",t),window.addEventListener("mouseup",o),window.addEventListener("touchmove",t,{passive:false}),window.addEventListener("touchend",o),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("mouseup",o),window.removeEventListener("touchmove",t),window.removeEventListener("touchend",o);}},[]);let St=react.useMemo(()=>n.map(t=>t.id),[n]),{visible:xt,toggle:Z,isVisible:gt}=$t(St),K=react.useMemo(()=>wt(n.length),[n.length]),P=10,At=6.4,bt=18,it=14,U=6,at=ot,_=H,N=at/2,D=_/2,Et=p!=="none"?bt+U+4:U+4,st=Math.max(Math.min(N,D)-Et,20),yt=l?st*h:0,$=react.useMemo(()=>kt(n,xt,K,st,yt,N,D,a,g),[n,xt,K,st,yt,N,D,a,g]),Ct=react.useMemo(()=>$.map(t=>t.id).join(","),[$]);react.useEffect(()=>{ft({});},[Ct]);let Lt=react.useCallback((t,o)=>{if(O.current)return;let r=b.current?.getBoundingClientRect();if(!r)return;V(o);let i=$.find(c=>c.id===o);i&&R({x:t.clientX-r.left+12,y:t.clientY-r.top-10,items:[{label:i.label,value:tt(i.value),percent:`${i.percent}%`,color:i.color}]});},[$]),vt=react.useCallback(()=>{V(null),R(null);},[]),Vt=react.useCallback((t,o,r)=>{let i=I[t]??{dx:0,dy:0};O.current={id:t,startX:o,startY:r,origDx:i.dx,origDy:i.dy};},[I]),Tt=react.useCallback((t,o)=>{t.preventDefault(),V(o);let r=b.current?.getBoundingClientRect();if(!r)return;let i=t.touches[0];if(!i)return;let c=$.find(w=>w.id===o);c&&R({x:i.clientX-r.left+12,y:i.clientY-r.top-10,items:[{label:c.label,value:tt(c.value),percent:`${c.percent}%`,color:c.color}]});},[$]),q=react.useMemo(()=>{let t=n.reduce((o,r)=>o+r.value,0);return n.map((o,r)=>({id:o.id,label:o.label,color:o.color??K[r],visible:gt(o.id),percent:t>0?Math.round(o.value/t*1e3)/10:0}))},[n,K,gt]),Nt=n.length>0&&n.some(t=>t.value>0),Dt=m==="left"||m==="right",Xt=react.useMemo(()=>{if(p==="none")return [];let t=P+3,o=U+P*.5,r=_-U-P*.5,i=$.filter(e=>e.percent>=3).map(e=>{let x="";switch(p){case "percent":x=`${e.percent}%`;break;case "value":x=tt(e.value);break;case "label":x=e.label;break;case "label+percent":x=`${e.label} ${e.percent}%`;break}let Y=k(e.cx,e.cy,e.outerR+2,e.midAngle),y=k(e.cx,e.cy,e.outerR+bt,e.midAngle),s=(e.midAngle-90)*Math.PI/180,J=Math.cos(s)>=0,Q=Math.ceil(x.length*At);return {id:e.id,color:e.color,text:x,textW:Q,p1:Y,elbowX:y.x,y:y.y,isRight:J}}),c=i.filter(e=>e.isRight).sort((e,x)=>e.y-x.y),w=i.filter(e=>!e.isRight).sort((e,x)=>e.y-x.y);function X(e){if(e.length!==0)for(let x=0;x<20;x++){let Y=false;for(let s=1;s<e.length;s++){let J=e[s].y-e[s-1].y;if(J<t){let Q=(t-J)/2;e[s-1].y-=Q,e[s].y+=Q,Y=true;}}e[0].y<o&&(e[0].y=o);for(let s=1;s<e.length;s++)e[s].y<e[s-1].y+t&&(e[s].y=e[s-1].y+t);let y=e.length-1;e[y].y>r&&(e[y].y=r);for(let s=y-1;s>=0;s--)e[s].y>e[s+1].y-t&&(e[s].y=e[s+1].y-t);for(let s of e)s.y=Math.max(o,Math.min(r,s.y));if(!Y)break}}return X(c),X(w),[...w,...c]},[$,p,_]),Rt=jsxRuntime.jsxs("svg",{role:"img","aria-label":L??"Pie chart",width:at,height:_,viewBox:`0 0 ${at} ${_}`,shapeRendering:"geometricPrecision",children:[jsxRuntime.jsxs("g",{className:F?"chart-pie-animate":"",children:[$.map((t,o)=>{let r=f===t.id,i=A&&r,c=Mt(t.midAngle,i?z:0);return jsxRuntime.jsx("path",{d:t.path,fill:t.color,transform:i?`translate(${c.dx}, ${c.dy})`:void 0,className:B("chart-slice",F&&`chart-slice-animate-${Math.min(o,7)}`,r&&"chart-slice--exploded"),stroke:t.color,strokeWidth:.6,onMouseMove:w=>Lt(w,t.id),onMouseLeave:vt,onTouchStart:w=>Tt(w,t.id),onTouchEnd:vt,"aria-label":`${t.label}: ${t.percent}%`},t.id)}),Xt.map(t=>{let o=I[t.id]??{dx:0,dy:0},r=t.elbowX+o.dx,i=t.y+o.dy,c=r+(t.isRight?it:-it),w=t.isRight?c+3:c-3,X=it+3+t.textW+6,e=t.isRight?r:r-X,x=i-P-2,Y=P*2+4;return jsxRuntime.jsxs("g",{style:{userSelect:"none"},children:[jsxRuntime.jsx("polyline",{points:[`${t.p1.x.toFixed(2)},${t.p1.y.toFixed(2)}`,`${r.toFixed(2)},${i.toFixed(2)}`,`${c.toFixed(2)},${i.toFixed(2)}`].join(" "),fill:"none",stroke:t.color,strokeWidth:1.2,opacity:.85,strokeLinejoin:"round",strokeLinecap:"round",style:{pointerEvents:"none"}}),jsxRuntime.jsx("text",{x:w.toFixed(2),y:i.toFixed(2),textAnchor:t.isRight?"start":"end",dominantBaseline:"middle",fontSize:P,fill:"currentColor",className:"text-primary-700 dark:text-primary-300",style:{pointerEvents:"none"},children:t.text}),jsxRuntime.jsx("rect",{x:e,y:x,width:X,height:Y,fill:"transparent",style:{cursor:"grab",touchAction:"none"},onMouseDown:y=>{y.stopPropagation(),Vt(t.id,y.clientX,y.clientY);},onTouchStart:y=>{y.stopPropagation();let s=y.touches[0];s&&Vt(t.id,s.clientX,s.clientY);}})]},`label-${t.id}`)})]}),l&&jsxRuntime.jsx("g",{style:{pointerEvents:"none"},children:f&&$.find(t=>t.id===f)?jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsxs("text",{x:N,y:D-8,textAnchor:"middle",dominantBaseline:"middle",fontSize:18,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:[$.find(t=>t.id===f)?.percent,"%"]}),jsxRuntime.jsx("text",{x:N,y:D+12,textAnchor:"middle",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:$.find(t=>t.id===f)?.label})]}):jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[u&&jsxRuntime.jsx("text",{x:N,y:D-(C?8:0),textAnchor:"middle",dominantBaseline:"middle",fontSize:16,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:u}),C&&jsxRuntime.jsx("text",{x:N,y:D+14,textAnchor:"middle",dominantBaseline:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C})]})})]});return jsxRuntime.jsxs("div",{ref:b,className:B(lt(),j),children:[L&&jsxRuntime.jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:L}),E&&m==="top"&&jsxRuntime.jsx(rt,{items:q,position:"top",onToggle:Z}),jsxRuntime.jsxs("div",{className:B("flex items-start justify-center",Dt&&"gap-0"),children:[E&&m==="left"&&jsxRuntime.jsx(rt,{items:q,position:"left",onToggle:Z}),jsxRuntime.jsx("div",{ref:M,className:"flex-1 min-w-0 overflow-hidden",children:Nt?Rt:jsxRuntime.jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:H},children:G})}),E&&m==="right"&&jsxRuntime.jsx(rt,{items:q,position:"right",onToggle:Z})]}),E&&m==="bottom"&&jsxRuntime.jsx(rt,{items:q,position:"bottom",onToggle:Z}),S&&jsxRuntime.jsx("div",{className:mt(),style:{left:S.x,top:S.y},role:"tooltip",children:S.items.map((t,o)=>jsxRuntime.jsxs("div",{className:"flex items-center gap-2",children:[jsxRuntime.jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:t.color}}),jsxRuntime.jsxs("span",{className:"font-medium",children:[t.label,":"]}),jsxRuntime.jsx("span",{children:t.value}),jsxRuntime.jsxs("span",{className:"text-primary-300",children:["(",t.percent,")"]})]},o))})]})}exports.PieChart=ce;
@@ -1 +1 @@
1
- import {useRef,useState,useEffect,useMemo,useCallback}from'react';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {cva}from'class-variance-authority';import {jsxs,jsx,Fragment}from'react/jsx-runtime';function B(...n){return twMerge(clsx(n))}var lt=cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),dt=cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),mt=cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function $t(n){let[l,h]=useState(()=>new Set(n)),a=useCallback(u=>{h(C=>{let A=new Set(C);return A.has(u)?A.delete(u):A.add(u),A});},[]),g=useCallback(u=>l.has(u),[l]),p=useCallback(()=>{h(new Set(n));},[n]);return {visible:l,toggle:a,isVisible:g,showAll:p}}function wt(n){let l=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:n},(h,a)=>l[a%l.length])}function tt(n,l){return l?l(n):typeof n=="number"?Math.abs(n)>=1e6?`${(n/1e6).toFixed(1)}M`:Math.abs(n)>=1e3?`${(n/1e3).toFixed(1)}K`:Number.isInteger(n)?n.toString():n.toFixed(2):String(n)}function k(n,l,h,a){let g=(a-90)*Math.PI/180;return {x:n+h*Math.cos(g),y:l+h*Math.sin(g)}}function kt(n,l,h,a,g,p,u,C,A){let z=n.filter(m=>l.has(m.id)),L=z.reduce((m,F)=>m+F.value,0);if(L===0)return [];let H=new Map(n.map((m,F)=>[m.id,m.color??h[F%h.length]])),j=[],E=C;for(let m of z){let F=m.value/L*360,G=A/2,b=E+G,M=E+F-G,ot=(b+M)/2,T;if(Math.abs(M-b)>=359.9){let f=k(p,u,a,b),V=k(p,u,a,b+180);T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 1 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} A ${a} ${a} 0 1 1 ${f.x.toFixed(3)} ${f.y.toFixed(3)} Z`;}else if(g>0){let f=k(p,u,a,b),V=k(p,u,a,M),S=k(p,u,g,M),R=k(p,u,g,b),P=M-b>180?1:0;T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${P} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} L ${S.x.toFixed(3)} ${S.y.toFixed(3)} A ${g} ${g} 0 ${P} 0 ${R.x.toFixed(3)} ${R.y.toFixed(3)} Z`;}else {let f=k(p,u,a,b),V=k(p,u,a,M),S=M-b>180?1:0;T=`M ${p.toFixed(3)} ${u.toFixed(3)} L ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${S} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} Z`;}j.push({id:m.id,label:m.label,value:m.value,percent:Math.round(m.value/L*1e3)/10,color:H.get(m.id)??"var(--color-chart-1)",startAngle:b,endAngle:M,midAngle:ot,path:T,outerR:a,innerR:g,cx:p,cy:u}),E+=F;}return j}function Mt(n,l){let h=(n-90)*Math.PI/180;return {dx:Math.cos(h)*l,dy:Math.sin(h)*l}}function rt({items:n,position:l,onToggle:h}){return jsx("div",{className:dt({position:l}),children:n.map(a=>jsxs("button",{onClick:()=>h(a.id),className:B("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!a.visible&&"opacity-40"),"aria-pressed":a.visible,children:[jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:a.color}}),jsx("span",{children:a.label}),jsxs("span",{className:"text-secondary-400",children:["(",a.percent,"%)"]})]},a.id))})}function ce({data:n,donut:l=false,donutThickness:h=.5,startAngle:a=-90,padAngle:g=0,labelType:p="percent",centerLabel:u,centerSubLabel:C,explodeOnHover:A=true,explodeOffset:z=8,title:L,height:H=300,className:j,legend:E=true,legendPosition:m="bottom",animateOnMount:F=true,emptyText:G="No data to display"}){let b=useRef(null),M=useRef(null),[ot,T]=useState(400),[f,V]=useState(null),[S,R]=useState(null),[P,ft]=useState({}),O=useRef(null);useEffect(()=>{let t=M.current;if(!t)return;let o=t.getBoundingClientRect().width;o>0&&T(o);let r=new ResizeObserver(i=>{let c=i[0]?.contentRect.width;c&&c>0&&T(c);});return r.observe(t),()=>r.disconnect()},[]),useEffect(()=>{function t(r){let i=O.current;if(!i)return;"touches"in r&&r.cancelable&&r.preventDefault();let c="touches"in r?r.touches[0]?.clientX??i.startX:r.clientX,w="touches"in r?r.touches[0]?.clientY??i.startY:r.clientY;ft(X=>({...X,[i.id]:{dx:i.origDx+c-i.startX,dy:i.origDy+w-i.startY}}));}function o(){O.current=null;}return window.addEventListener("mousemove",t),window.addEventListener("mouseup",o),window.addEventListener("touchmove",t,{passive:false}),window.addEventListener("touchend",o),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("mouseup",o),window.removeEventListener("touchmove",t),window.removeEventListener("touchend",o);}},[]);let St=useMemo(()=>n.map(t=>t.id),[n]),{visible:xt,toggle:Z,isVisible:gt}=$t(St),K=useMemo(()=>wt(n.length),[n.length]),I=10,At=6.4,bt=18,it=14,U=6,at=ot,_=H,N=at/2,D=_/2,Et=p!=="none"?bt+U+4:U+4,st=Math.max(Math.min(N,D)-Et,20),yt=l?st*h:0,$=useMemo(()=>kt(n,xt,K,st,yt,N,D,a,g),[n,xt,K,st,yt,N,D,a,g]),Ct=useMemo(()=>$.map(t=>t.id).join(","),[$]);useEffect(()=>{ft({});},[Ct]);let Lt=useCallback((t,o)=>{if(O.current)return;let r=b.current?.getBoundingClientRect();if(!r)return;V(o);let i=$.find(c=>c.id===o);i&&R({x:t.clientX-r.left+12,y:t.clientY-r.top-10,items:[{label:i.label,value:tt(i.value),percent:`${i.percent}%`,color:i.color}]});},[$]),vt=useCallback(()=>{V(null),R(null);},[]),Vt=useCallback((t,o,r)=>{let i=P[t]??{dx:0,dy:0};O.current={id:t,startX:o,startY:r,origDx:i.dx,origDy:i.dy};},[P]),Tt=useCallback((t,o)=>{t.preventDefault(),V(o);let r=b.current?.getBoundingClientRect();if(!r)return;let i=t.touches[0];if(!i)return;let c=$.find(w=>w.id===o);c&&R({x:i.clientX-r.left+12,y:i.clientY-r.top-10,items:[{label:c.label,value:tt(c.value),percent:`${c.percent}%`,color:c.color}]});},[$]),q=useMemo(()=>{let t=n.reduce((o,r)=>o+r.value,0);return n.map((o,r)=>({id:o.id,label:o.label,color:o.color??K[r],visible:gt(o.id),percent:t>0?Math.round(o.value/t*1e3)/10:0}))},[n,K,gt]),Nt=n.length>0&&n.some(t=>t.value>0),Dt=m==="left"||m==="right",Xt=useMemo(()=>{if(p==="none")return [];let t=I+3,o=U+I*.5,r=_-U-I*.5,i=$.filter(e=>e.percent>=3).map(e=>{let x="";switch(p){case "percent":x=`${e.percent}%`;break;case "value":x=tt(e.value);break;case "label":x=e.label;break;case "label+percent":x=`${e.label} ${e.percent}%`;break}let Y=k(e.cx,e.cy,e.outerR+2,e.midAngle),y=k(e.cx,e.cy,e.outerR+bt,e.midAngle),s=(e.midAngle-90)*Math.PI/180,J=Math.cos(s)>=0,Q=Math.ceil(x.length*At);return {id:e.id,color:e.color,text:x,textW:Q,p1:Y,elbowX:y.x,y:y.y,isRight:J}}),c=i.filter(e=>e.isRight).sort((e,x)=>e.y-x.y),w=i.filter(e=>!e.isRight).sort((e,x)=>e.y-x.y);function X(e){if(e.length!==0)for(let x=0;x<20;x++){let Y=false;for(let s=1;s<e.length;s++){let J=e[s].y-e[s-1].y;if(J<t){let Q=(t-J)/2;e[s-1].y-=Q,e[s].y+=Q,Y=true;}}e[0].y<o&&(e[0].y=o);for(let s=1;s<e.length;s++)e[s].y<e[s-1].y+t&&(e[s].y=e[s-1].y+t);let y=e.length-1;e[y].y>r&&(e[y].y=r);for(let s=y-1;s>=0;s--)e[s].y>e[s+1].y-t&&(e[s].y=e[s+1].y-t);for(let s of e)s.y=Math.max(o,Math.min(r,s.y));if(!Y)break}}return X(c),X(w),[...w,...c]},[$,p,_]),Rt=jsxs("svg",{role:"img","aria-label":L??"Pie chart",width:at,height:_,viewBox:`0 0 ${at} ${_}`,shapeRendering:"geometricPrecision",children:[jsxs("g",{className:F?"chart-pie-animate":"",children:[$.map((t,o)=>{let r=f===t.id,i=A&&r,c=Mt(t.midAngle,i?z:0);return jsx("path",{d:t.path,fill:t.color,transform:i?`translate(${c.dx}, ${c.dy})`:void 0,className:B("chart-slice",F&&`chart-slice-animate-${Math.min(o,7)}`,r&&"chart-slice--exploded"),stroke:t.color,strokeWidth:.6,onMouseMove:w=>Lt(w,t.id),onMouseLeave:vt,onTouchStart:w=>Tt(w,t.id),onTouchEnd:vt,"aria-label":`${t.label}: ${t.percent}%`},t.id)}),Xt.map(t=>{let o=P[t.id]??{dx:0,dy:0},r=t.elbowX+o.dx,i=t.y+o.dy,c=r+(t.isRight?it:-it),w=t.isRight?c+3:c-3,X=it+3+t.textW+6,e=t.isRight?r:r-X,x=i-I-2,Y=I*2+4;return jsxs("g",{style:{userSelect:"none"},children:[jsx("polyline",{points:[`${t.p1.x.toFixed(2)},${t.p1.y.toFixed(2)}`,`${r.toFixed(2)},${i.toFixed(2)}`,`${c.toFixed(2)},${i.toFixed(2)}`].join(" "),fill:"none",stroke:t.color,strokeWidth:1.2,opacity:.85,strokeLinejoin:"round",strokeLinecap:"round",style:{pointerEvents:"none"}}),jsx("text",{x:w.toFixed(2),y:i.toFixed(2),textAnchor:t.isRight?"start":"end",dominantBaseline:"middle",fontSize:I,fill:"currentColor",className:"text-primary-700 dark:text-primary-300",style:{pointerEvents:"none"},children:t.text}),jsx("rect",{x:e,y:x,width:X,height:Y,fill:"transparent",style:{cursor:"grab",touchAction:"none"},onMouseDown:y=>{y.stopPropagation(),Vt(t.id,y.clientX,y.clientY);},onTouchStart:y=>{y.stopPropagation();let s=y.touches[0];s&&Vt(t.id,s.clientX,s.clientY);}})]},`label-${t.id}`)})]}),l&&jsx("g",{style:{pointerEvents:"none"},children:f&&$.find(t=>t.id===f)?jsxs(Fragment,{children:[jsxs("text",{x:N,y:D-8,textAnchor:"middle",dominantBaseline:"middle",fontSize:18,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:[$.find(t=>t.id===f)?.percent,"%"]}),jsx("text",{x:N,y:D+12,textAnchor:"middle",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:$.find(t=>t.id===f)?.label})]}):jsxs(Fragment,{children:[u&&jsx("text",{x:N,y:D-(C?8:0),textAnchor:"middle",dominantBaseline:"middle",fontSize:16,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:u}),C&&jsx("text",{x:N,y:D+14,textAnchor:"middle",dominantBaseline:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C})]})})]});return jsxs("div",{ref:b,className:B(lt(),j),children:[L&&jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:L}),E&&m==="top"&&jsx(rt,{items:q,position:"top",onToggle:Z}),jsxs("div",{className:B("flex items-start justify-center",Dt&&"gap-0"),children:[E&&m==="left"&&jsx(rt,{items:q,position:"left",onToggle:Z}),jsx("div",{ref:M,className:"flex-1 min-w-0 overflow-hidden",children:Nt?Rt:jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:H},children:G})}),E&&m==="right"&&jsx(rt,{items:q,position:"right",onToggle:Z})]}),E&&m==="bottom"&&jsx(rt,{items:q,position:"bottom",onToggle:Z}),S&&jsx("div",{className:mt(),style:{left:S.x,top:S.y},role:"tooltip",children:S.items.map((t,o)=>jsxs("div",{className:"flex items-center gap-2",children:[jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:t.color}}),jsxs("span",{className:"font-medium",children:[t.label,":"]}),jsx("span",{children:t.value}),jsxs("span",{className:"text-primary-300",children:["(",t.percent,")"]})]},o))})]})}export{ce as PieChart};
1
+ import {useRef,useState,useEffect,useMemo,useCallback}from'react';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {cva}from'class-variance-authority';import {jsxs,jsx,Fragment}from'react/jsx-runtime';function B(...n){return twMerge(clsx(n))}var lt=cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),dt=cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),mt=cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function $t(n){let[l,h]=useState(()=>new Set(n)),a=useCallback(u=>{h(C=>{let A=new Set(C);return A.has(u)?A.delete(u):A.add(u),A});},[]),g=useCallback(u=>l.has(u),[l]),p=useCallback(()=>{h(new Set(n));},[n]);return {visible:l,toggle:a,isVisible:g,showAll:p}}function wt(n){let l=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:n},(h,a)=>l[a%l.length])}function tt(n,l){return l?l(n):typeof n=="number"?Math.abs(n)>=1e6?`${(n/1e6).toFixed(1)}M`:Math.abs(n)>=1e3?`${(n/1e3).toFixed(1)}K`:Number.isInteger(n)?n.toString():n.toFixed(2):String(n)}function k(n,l,h,a){let g=(a-90)*Math.PI/180;return {x:n+h*Math.cos(g),y:l+h*Math.sin(g)}}function kt(n,l,h,a,g,p,u,C,A){let z=n.filter(m=>l.has(m.id)),L=z.reduce((m,F)=>m+F.value,0);if(L===0)return [];let H=new Map(n.map((m,F)=>[m.id,m.color??h[F%h.length]])),j=[],E=C;for(let m of z){let F=m.value/L*360,G=A/2,b=E+G,M=E+F-G,ot=(b+M)/2,T;if(Math.abs(M-b)>=359.9){let f=k(p,u,a,b),V=k(p,u,a,b+180);T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 1 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} A ${a} ${a} 0 1 1 ${f.x.toFixed(3)} ${f.y.toFixed(3)} Z`;}else if(g>0){let f=k(p,u,a,b),V=k(p,u,a,M),S=k(p,u,g,M),R=k(p,u,g,b),I=M-b>180?1:0;T=`M ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${I} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} L ${S.x.toFixed(3)} ${S.y.toFixed(3)} A ${g} ${g} 0 ${I} 0 ${R.x.toFixed(3)} ${R.y.toFixed(3)} Z`;}else {let f=k(p,u,a,b),V=k(p,u,a,M),S=M-b>180?1:0;T=`M ${p.toFixed(3)} ${u.toFixed(3)} L ${f.x.toFixed(3)} ${f.y.toFixed(3)} A ${a} ${a} 0 ${S} 1 ${V.x.toFixed(3)} ${V.y.toFixed(3)} Z`;}j.push({id:m.id,label:m.label,value:m.value,percent:Math.round(m.value/L*1e3)/10,color:H.get(m.id)??"var(--color-chart-1)",startAngle:b,endAngle:M,midAngle:ot,path:T,outerR:a,innerR:g,cx:p,cy:u}),E+=F;}return j}function Mt(n,l){let h=(n-90)*Math.PI/180;return {dx:Math.cos(h)*l,dy:Math.sin(h)*l}}function rt({items:n,position:l,onToggle:h}){return jsx("div",{className:dt({position:l}),children:n.map(a=>jsxs("button",{onClick:()=>h(a.id),className:B("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!a.visible&&"opacity-40"),"aria-pressed":a.visible,children:[jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:a.color}}),jsx("span",{children:a.label}),jsxs("span",{className:"text-secondary-400",children:["(",a.percent,"%)"]})]},a.id))})}function ce({data:n,donut:l=false,donutThickness:h=.5,startAngle:a=-90,padAngle:g=0,labelType:p="percent",centerLabel:u,centerSubLabel:C,explodeOnHover:A=true,explodeOffset:z=8,title:L,height:H=300,className:j,legend:E=true,legendPosition:m="bottom",animateOnMount:F=true,emptyText:G="No data to display"}){let b=useRef(null),M=useRef(null),[ot,T]=useState(400),[f,V]=useState(null),[S,R]=useState(null),[I,ft]=useState({}),O=useRef(null);useEffect(()=>{let t=M.current;if(!t)return;let o=t.getBoundingClientRect().width;o>0&&T(o);let r=new ResizeObserver(i=>{let c=i[0]?.contentRect.width;c&&c>0&&T(c);});return r.observe(t),()=>r.disconnect()},[]),useEffect(()=>{function t(r){let i=O.current;if(!i)return;"touches"in r&&r.cancelable&&r.preventDefault();let c="touches"in r?r.touches[0]?.clientX??i.startX:r.clientX,w="touches"in r?r.touches[0]?.clientY??i.startY:r.clientY;ft(X=>({...X,[i.id]:{dx:i.origDx+c-i.startX,dy:i.origDy+w-i.startY}}));}function o(){O.current=null;}return window.addEventListener("mousemove",t),window.addEventListener("mouseup",o),window.addEventListener("touchmove",t,{passive:false}),window.addEventListener("touchend",o),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("mouseup",o),window.removeEventListener("touchmove",t),window.removeEventListener("touchend",o);}},[]);let St=useMemo(()=>n.map(t=>t.id),[n]),{visible:xt,toggle:Z,isVisible:gt}=$t(St),K=useMemo(()=>wt(n.length),[n.length]),P=10,At=6.4,bt=18,it=14,U=6,at=ot,_=H,N=at/2,D=_/2,Et=p!=="none"?bt+U+4:U+4,st=Math.max(Math.min(N,D)-Et,20),yt=l?st*h:0,$=useMemo(()=>kt(n,xt,K,st,yt,N,D,a,g),[n,xt,K,st,yt,N,D,a,g]),Ct=useMemo(()=>$.map(t=>t.id).join(","),[$]);useEffect(()=>{ft({});},[Ct]);let Lt=useCallback((t,o)=>{if(O.current)return;let r=b.current?.getBoundingClientRect();if(!r)return;V(o);let i=$.find(c=>c.id===o);i&&R({x:t.clientX-r.left+12,y:t.clientY-r.top-10,items:[{label:i.label,value:tt(i.value),percent:`${i.percent}%`,color:i.color}]});},[$]),vt=useCallback(()=>{V(null),R(null);},[]),Vt=useCallback((t,o,r)=>{let i=I[t]??{dx:0,dy:0};O.current={id:t,startX:o,startY:r,origDx:i.dx,origDy:i.dy};},[I]),Tt=useCallback((t,o)=>{t.preventDefault(),V(o);let r=b.current?.getBoundingClientRect();if(!r)return;let i=t.touches[0];if(!i)return;let c=$.find(w=>w.id===o);c&&R({x:i.clientX-r.left+12,y:i.clientY-r.top-10,items:[{label:c.label,value:tt(c.value),percent:`${c.percent}%`,color:c.color}]});},[$]),q=useMemo(()=>{let t=n.reduce((o,r)=>o+r.value,0);return n.map((o,r)=>({id:o.id,label:o.label,color:o.color??K[r],visible:gt(o.id),percent:t>0?Math.round(o.value/t*1e3)/10:0}))},[n,K,gt]),Nt=n.length>0&&n.some(t=>t.value>0),Dt=m==="left"||m==="right",Xt=useMemo(()=>{if(p==="none")return [];let t=P+3,o=U+P*.5,r=_-U-P*.5,i=$.filter(e=>e.percent>=3).map(e=>{let x="";switch(p){case "percent":x=`${e.percent}%`;break;case "value":x=tt(e.value);break;case "label":x=e.label;break;case "label+percent":x=`${e.label} ${e.percent}%`;break}let Y=k(e.cx,e.cy,e.outerR+2,e.midAngle),y=k(e.cx,e.cy,e.outerR+bt,e.midAngle),s=(e.midAngle-90)*Math.PI/180,J=Math.cos(s)>=0,Q=Math.ceil(x.length*At);return {id:e.id,color:e.color,text:x,textW:Q,p1:Y,elbowX:y.x,y:y.y,isRight:J}}),c=i.filter(e=>e.isRight).sort((e,x)=>e.y-x.y),w=i.filter(e=>!e.isRight).sort((e,x)=>e.y-x.y);function X(e){if(e.length!==0)for(let x=0;x<20;x++){let Y=false;for(let s=1;s<e.length;s++){let J=e[s].y-e[s-1].y;if(J<t){let Q=(t-J)/2;e[s-1].y-=Q,e[s].y+=Q,Y=true;}}e[0].y<o&&(e[0].y=o);for(let s=1;s<e.length;s++)e[s].y<e[s-1].y+t&&(e[s].y=e[s-1].y+t);let y=e.length-1;e[y].y>r&&(e[y].y=r);for(let s=y-1;s>=0;s--)e[s].y>e[s+1].y-t&&(e[s].y=e[s+1].y-t);for(let s of e)s.y=Math.max(o,Math.min(r,s.y));if(!Y)break}}return X(c),X(w),[...w,...c]},[$,p,_]),Rt=jsxs("svg",{role:"img","aria-label":L??"Pie chart",width:at,height:_,viewBox:`0 0 ${at} ${_}`,shapeRendering:"geometricPrecision",children:[jsxs("g",{className:F?"chart-pie-animate":"",children:[$.map((t,o)=>{let r=f===t.id,i=A&&r,c=Mt(t.midAngle,i?z:0);return jsx("path",{d:t.path,fill:t.color,transform:i?`translate(${c.dx}, ${c.dy})`:void 0,className:B("chart-slice",F&&`chart-slice-animate-${Math.min(o,7)}`,r&&"chart-slice--exploded"),stroke:t.color,strokeWidth:.6,onMouseMove:w=>Lt(w,t.id),onMouseLeave:vt,onTouchStart:w=>Tt(w,t.id),onTouchEnd:vt,"aria-label":`${t.label}: ${t.percent}%`},t.id)}),Xt.map(t=>{let o=I[t.id]??{dx:0,dy:0},r=t.elbowX+o.dx,i=t.y+o.dy,c=r+(t.isRight?it:-it),w=t.isRight?c+3:c-3,X=it+3+t.textW+6,e=t.isRight?r:r-X,x=i-P-2,Y=P*2+4;return jsxs("g",{style:{userSelect:"none"},children:[jsx("polyline",{points:[`${t.p1.x.toFixed(2)},${t.p1.y.toFixed(2)}`,`${r.toFixed(2)},${i.toFixed(2)}`,`${c.toFixed(2)},${i.toFixed(2)}`].join(" "),fill:"none",stroke:t.color,strokeWidth:1.2,opacity:.85,strokeLinejoin:"round",strokeLinecap:"round",style:{pointerEvents:"none"}}),jsx("text",{x:w.toFixed(2),y:i.toFixed(2),textAnchor:t.isRight?"start":"end",dominantBaseline:"middle",fontSize:P,fill:"currentColor",className:"text-primary-700 dark:text-primary-300",style:{pointerEvents:"none"},children:t.text}),jsx("rect",{x:e,y:x,width:X,height:Y,fill:"transparent",style:{cursor:"grab",touchAction:"none"},onMouseDown:y=>{y.stopPropagation(),Vt(t.id,y.clientX,y.clientY);},onTouchStart:y=>{y.stopPropagation();let s=y.touches[0];s&&Vt(t.id,s.clientX,s.clientY);}})]},`label-${t.id}`)})]}),l&&jsx("g",{style:{pointerEvents:"none"},children:f&&$.find(t=>t.id===f)?jsxs(Fragment,{children:[jsxs("text",{x:N,y:D-8,textAnchor:"middle",dominantBaseline:"middle",fontSize:18,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:[$.find(t=>t.id===f)?.percent,"%"]}),jsx("text",{x:N,y:D+12,textAnchor:"middle",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:$.find(t=>t.id===f)?.label})]}):jsxs(Fragment,{children:[u&&jsx("text",{x:N,y:D-(C?8:0),textAnchor:"middle",dominantBaseline:"middle",fontSize:16,fontWeight:600,fill:"currentColor",className:"text-primary-800 dark:text-primary-100",children:u}),C&&jsx("text",{x:N,y:D+14,textAnchor:"middle",dominantBaseline:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C})]})})]});return jsxs("div",{ref:b,className:B(lt(),j),children:[L&&jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:L}),E&&m==="top"&&jsx(rt,{items:q,position:"top",onToggle:Z}),jsxs("div",{className:B("flex items-start justify-center",Dt&&"gap-0"),children:[E&&m==="left"&&jsx(rt,{items:q,position:"left",onToggle:Z}),jsx("div",{ref:M,className:"flex-1 min-w-0 overflow-hidden",children:Nt?Rt:jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:H},children:G})}),E&&m==="right"&&jsx(rt,{items:q,position:"right",onToggle:Z})]}),E&&m==="bottom"&&jsx(rt,{items:q,position:"bottom",onToggle:Z}),S&&jsx("div",{className:mt(),style:{left:S.x,top:S.y},role:"tooltip",children:S.items.map((t,o)=>jsxs("div",{className:"flex items-center gap-2",children:[jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:t.color}}),jsxs("span",{className:"font-medium",children:[t.label,":"]}),jsx("span",{children:t.value}),jsxs("span",{className:"text-primary-300",children:["(",t.percent,")"]})]},o))})]})}export{ce as PieChart};
@@ -1 +1 @@
1
- 'use strict';var react=require('react'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),classVarianceAuthority=require('class-variance-authority'),jsxRuntime=require('react/jsx-runtime');function M(...t){return tailwindMerge.twMerge(clsx.clsx(t))}var _=classVarianceAuthority.cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),j=classVarianceAuthority.cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),H=classVarianceAuthority.cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function nt(t){let[n,e]=react.useState(()=>new Set(t)),o=react.useCallback(l=>{e(b=>{let h=new Set(b);return h.has(l)?h.delete(l):h.add(l),h});},[]),s=react.useCallback(l=>n.has(l),[n]),m=react.useCallback(()=>{e(new Set(t));},[t]);return {visible:n,toggle:o,isVisible:s,showAll:m}}function ot(t){let n=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(e,o)=>n[o%n.length])}function G(t,n,e=.1){if(t===n)return [t-1,n+1];let s=(n-t)*e;return [Math.floor(t-s),Math.ceil(n+s)]}function at(t,n,e){if(e<=1)return [t];let o=(n-t)/(e-1);return Array.from({length:e},(s,m)=>+(t+m*o).toFixed(10))}function v(t,n){return n?n(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,n,e){return {top:20,right:e?65:20,bottom:t?52:36,left:n?65:52}}function K(t,n,e){let o=Math.max(t,120);return {width:o,height:n,margins:e,plotLeft:e.left,plotTop:e.top,plotRight:o-e.right,plotBottom:n-e.bottom,plotWidth:Math.max(o-e.left-e.right,10),plotHeight:Math.max(n-e.top-e.bottom,10)}}function it(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data){let k=Number(h.x);k<o&&(o=k),k>s&&(s=k);}if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function st(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data)h.y<o&&(o=h.y),h.y>s&&(s=h.y);if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function Z(t,n){let[e,o]=t,[s,m]=n;return o===e?()=>(s+m)/2:l=>s+(l-e)/(o-e)*(m-s)}function q(t,n=5){return at(t[0],t[1],n)}function ct(t,n,e){let o=1.618033988749895,s=t*137+n,m=s*o%1*2-1,l=s*o*o%1*2-1;return [m*e,l*e]}function lt(t,n,e=12){if(t.length<2)return null;let o=Math.min(...t),s=Math.max(...t),m=Math.min(...n),l=Math.max(...n);return {cx:(o+s)/2,cy:(m+l)/2,rx:(s-o)/2+e,ry:(l-m)/2+e}}function W({items:t,position:n,onToggle:e}){return jsxRuntime.jsx("div",{className:j({position:n}),children:t.map(o=>jsxRuntime.jsxs("button",{onClick:()=>e(o.id),className:M("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!o.visible&&"opacity-40"),"aria-pressed":o.visible,children:[jsxRuntime.jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:o.color}}),o.label]},o.id))})}function Ut({series:t,xAxis:n,yAxis:e,dotRadius:o=5,dotOpacity:s=.75,clusters:m=false,jitter:l=false,jitterAmount:b=12,linkedLines:h=false,title:k,height:E=300,className:dt,legend:F=true,legendPosition:V="bottom",animateOnMount:Q=true,emptyText:ft="No data to display"}){let P=react.useRef(null),U=react.useRef(null),[A,tt]=react.useState(600),[C,T]=react.useState(null);react.useEffect(()=>{let r=U.current;if(!r)return;let c=r.getBoundingClientRect().width;c>0&&tt(c);let d=new ResizeObserver(f=>{let u=f[0]?.contentRect.width;u&&u>0&&tt(u);});return d.observe(r),()=>d.disconnect()},[]);let ht=react.useMemo(()=>t.map(r=>r.id),[t]),{visible:w,toggle:L,isVisible:N}=nt(ht),$=react.useMemo(()=>ot(t.length),[t.length]),X=react.useMemo(()=>new Map(t.map((r,c)=>[r.id,r.color??$[c]])),[t,$]),et=react.useMemo(()=>J(!!n?.label,!!e?.label,false),[n?.label,e?.label]),a=react.useMemo(()=>K(A,E,et),[A,E,et]),D=react.useMemo(()=>it(t,w,n),[t,w,n]),R=react.useMemo(()=>st(t,w,e),[t,w,e]),B=react.useMemo(()=>Z(D,[a.plotLeft,a.plotRight]),[D,a]),S=react.useMemo(()=>Z(R,[a.plotBottom,a.plotTop]),[R,a]),xt=react.useMemo(()=>q(D,n?.tickCount??6),[D,n?.tickCount]),rt=react.useMemo(()=>q(R,e?.tickCount??5),[R,e?.tickCount]),bt=react.useCallback((r,c,d,f,u)=>{let p=P.current?.getBoundingClientRect();p&&T({x:r.clientX-p.left+12,y:r.clientY-p.top-10,seriesName:c,xVal:v(d,n?.format),yVal:v(f,e?.format),color:u});},[n?.format,e?.format]),gt=react.useCallback((r,c,d,f,u)=>{r.preventDefault();let p=P.current?.getBoundingClientRect();if(!p)return;let g=r.touches[0];g&&T({x:g.clientX-p.left+12,y:g.clientY-p.top-10,seriesName:c,xVal:v(d,n?.format),yVal:v(f,e?.format),color:u});},[n?.format,e?.format]),I=react.useMemo(()=>t.map((r,c)=>({id:r.id,label:r.name,color:r.color??$[c],visible:N(r.id)})),[t,$,N]),yt=t.some(r=>r.data.length>0),kt=V==="left"||V==="right",Ct=jsxRuntime.jsxs("svg",{role:"img","aria-label":k??"Scatter chart",width:a.width,height:a.height,viewBox:`0 0 ${a.width} ${a.height}`,shapeRendering:"geometricPrecision",children:[rt.map(r=>jsxRuntime.jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:S(r),y2:S(r),stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},r)),m&&t.map((r,c)=>{if(!N(r.id)||r.data.length<2)return null;let d=X.get(r.id)??"var(--color-chart-1)",f=r.data.map(g=>B(Number(g.x))),u=r.data.map(g=>S(g.y)),p=lt(f,u,16);return p?jsxRuntime.jsx("ellipse",{cx:p.cx,cy:p.cy,rx:p.rx,ry:p.ry,fill:d,fillOpacity:.06,stroke:d,strokeOpacity:.3,strokeWidth:1.5,strokeDasharray:"5 3",className:Q?"chart-cluster-ellipse":"",style:{animationDelay:`${c*.1}s`}},`cluster-${r.id}`):null}),h&&t.map(r=>{if(!N(r.id)||r.data.length<2)return null;let c=X.get(r.id)??"var(--color-chart-1)",f=[...r.data].sort((u,p)=>Number(u.x)-Number(p.x)).map(u=>`${B(Number(u.x)).toFixed(2)},${S(u.y).toFixed(2)}`).join(" ");return jsxRuntime.jsx("polyline",{points:f,fill:"none",stroke:c,strokeWidth:1.5,className:"chart-scatter-link"},`link-${r.id}`)}),t.map((r,c)=>{if(!N(r.id))return null;let d=X.get(r.id)??"var(--color-chart-1)";return jsxRuntime.jsx("g",{className:"chart-scatter-group",children:r.data.map((f,u)=>{let[p,g]=l?ct(c,u,b):[0,0],vt=B(Number(f.x))+p,Vt=S(f.y)+g;return jsxRuntime.jsx("circle",{cx:vt,cy:Vt,r:o,fill:d,fillOpacity:s,stroke:d,strokeWidth:1,strokeOpacity:.5,className:M("chart-scatter-dot",Q&&"chart-scatter-dot--animate"),style:{color:d},onMouseMove:Y=>bt(Y,r.name,f.x,f.y,d),onMouseLeave:()=>T(null),onTouchStart:Y=>gt(Y,r.name,f.x,f.y,d),onTouchEnd:()=>T(null)},u)})},r.id)}),jsxRuntime.jsx("line",{x1:a.plotLeft,x2:a.plotLeft,y1:a.plotTop,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:a.plotBottom,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),rt.map(r=>{let c=S(r);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:a.plotLeft-4,x2:a.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:a.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:v(r,e?.format)})]},r)}),xt.map(r=>{let c=B(r);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:c,x2:c,y1:a.plotBottom,y2:a.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:c,y:a.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:v(r,n?.format)})]},r)}),e?.label&&jsxRuntime.jsx("text",{x:-(a.plotTop+a.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:e.label}),n?.label&&jsxRuntime.jsx("text",{x:a.plotLeft+a.plotWidth/2,y:a.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:n.label})]});return jsxRuntime.jsxs("div",{ref:P,className:M(_(),dt),children:[k&&jsxRuntime.jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:k}),F&&V==="top"&&jsxRuntime.jsx(W,{items:I,position:"top",onToggle:L}),jsxRuntime.jsxs("div",{className:M("flex items-start",kt&&"gap-0"),children:[F&&V==="left"&&jsxRuntime.jsx(W,{items:I,position:"left",onToggle:L}),jsxRuntime.jsx("div",{ref:U,className:"flex-1 min-w-0 overflow-hidden",children:yt?Ct:jsxRuntime.jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:E},children:ft})}),F&&V==="right"&&jsxRuntime.jsx(W,{items:I,position:"right",onToggle:L})]}),F&&V==="bottom"&&jsxRuntime.jsx(W,{items:I,position:"bottom",onToggle:L}),C&&jsxRuntime.jsxs("div",{className:H(),style:{left:C.x,top:C.y},role:"tooltip",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-1.5 mb-1",children:[jsxRuntime.jsx("span",{className:"h-2 w-2 rounded-full",style:{background:C.color}}),jsxRuntime.jsx("span",{className:"font-medium",children:C.seriesName})]}),jsxRuntime.jsxs("div",{className:"text-primary-300",children:["X: ",jsxRuntime.jsx("span",{className:"font-medium text-primary-50",children:C.xVal})," ","Y: ",jsxRuntime.jsx("span",{className:"font-medium text-primary-50",children:C.yVal})]})]})]})}exports.ScatterChart=Ut;
1
+ 'use strict';var react=require('react'),clsx=require('clsx'),tailwindMerge=require('tailwind-merge'),classVarianceAuthority=require('class-variance-authority'),jsxRuntime=require('react/jsx-runtime');function M(...t){return tailwindMerge.twMerge(clsx.clsx(t))}var _=classVarianceAuthority.cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),j=classVarianceAuthority.cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),H=classVarianceAuthority.cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function nt(t){let[n,e]=react.useState(()=>new Set(t)),o=react.useCallback(l=>{e(b=>{let h=new Set(b);return h.has(l)?h.delete(l):h.add(l),h});},[]),s=react.useCallback(l=>n.has(l),[n]),m=react.useCallback(()=>{e(new Set(t));},[t]);return {visible:n,toggle:o,isVisible:s,showAll:m}}function ot(t){let n=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(e,o)=>n[o%n.length])}function G(t,n,e=.1){if(t===n)return [t-1,n+1];let s=(n-t)*e;return [Math.floor(t-s),Math.ceil(n+s)]}function at(t,n,e){if(e<=1)return [t];let o=(n-t)/(e-1);return Array.from({length:e},(s,m)=>+(t+m*o).toFixed(10))}function C(t,n){return n?n(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,n,e){return {top:20,right:e?65:20,bottom:t?52:36,left:n?65:52}}function K(t,n,e){let o=Math.max(t,120);return {width:o,height:n,margins:e,plotLeft:e.left,plotTop:e.top,plotRight:o-e.right,plotBottom:n-e.bottom,plotWidth:Math.max(o-e.left-e.right,10),plotHeight:Math.max(n-e.top-e.bottom,10)}}function it(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data){let V=Number(h.x);V<o&&(o=V),V>s&&(s=V);}if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function st(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data)h.y<o&&(o=h.y),h.y>s&&(s=h.y);if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function Z(t,n){let[e,o]=t,[s,m]=n;return o===e?()=>(s+m)/2:l=>s+(l-e)/(o-e)*(m-s)}function q(t,n=5){return at(t[0],t[1],n)}function ct(t,n,e){let o=1.618033988749895,s=t*137+n,m=s*o%1*2-1,l=s*o*o%1*2-1;return [m*e,l*e]}function lt(t,n,e=12){if(t.length<2)return null;let o=Math.min(...t),s=Math.max(...t),m=Math.min(...n),l=Math.max(...n);return {cx:(o+s)/2,cy:(m+l)/2,rx:(s-o)/2+e,ry:(l-m)/2+e}}function W({items:t,position:n,onToggle:e}){return jsxRuntime.jsx("div",{className:j({position:n}),children:t.map(o=>jsxRuntime.jsxs("button",{onClick:()=>e(o.id),className:M("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!o.visible&&"opacity-40"),"aria-pressed":o.visible,children:[jsxRuntime.jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:o.color}}),o.label]},o.id))})}function Ut({series:t,xAxis:n,yAxis:e,dotRadius:o=5,dotOpacity:s=.75,clusters:m=false,jitter:l=false,jitterAmount:b=12,linkedLines:h=false,title:V,height:E=300,className:dt,legend:F=true,legendPosition:v="bottom",animateOnMount:Q=true,emptyText:ft="No data to display"}){let P=react.useRef(null),U=react.useRef(null),[A,tt]=react.useState(600),[k,T]=react.useState(null);react.useEffect(()=>{let r=U.current;if(!r)return;let c=r.getBoundingClientRect().width;c>0&&tt(c);let d=new ResizeObserver(f=>{let u=f[0]?.contentRect.width;u&&u>0&&tt(u);});return d.observe(r),()=>d.disconnect()},[]);let ht=react.useMemo(()=>t.map(r=>r.id),[t]),{visible:w,toggle:L,isVisible:N}=nt(ht),$=react.useMemo(()=>ot(t.length),[t.length]),X=react.useMemo(()=>new Map(t.map((r,c)=>[r.id,r.color??$[c]])),[t,$]),et=react.useMemo(()=>J(!!n?.label,!!e?.label,false),[n?.label,e?.label]),a=react.useMemo(()=>K(A,E,et),[A,E,et]),D=react.useMemo(()=>it(t,w,n),[t,w,n]),R=react.useMemo(()=>st(t,w,e),[t,w,e]),B=react.useMemo(()=>Z(D,[a.plotLeft,a.plotRight]),[D,a]),S=react.useMemo(()=>Z(R,[a.plotBottom,a.plotTop]),[R,a]),xt=react.useMemo(()=>q(D,n?.tickCount??6),[D,n?.tickCount]),rt=react.useMemo(()=>q(R,e?.tickCount??5),[R,e?.tickCount]),bt=react.useCallback((r,c,d,f,u)=>{let p=P.current?.getBoundingClientRect();p&&T({x:r.clientX-p.left+12,y:r.clientY-p.top-10,seriesName:c,xVal:C(d,n?.format),yVal:C(f,e?.format),color:u});},[n?.format,e?.format]),gt=react.useCallback((r,c,d,f,u)=>{r.preventDefault();let p=P.current?.getBoundingClientRect();if(!p)return;let g=r.touches[0];g&&T({x:g.clientX-p.left+12,y:g.clientY-p.top-10,seriesName:c,xVal:C(d,n?.format),yVal:C(f,e?.format),color:u});},[n?.format,e?.format]),I=react.useMemo(()=>t.map((r,c)=>({id:r.id,label:r.name,color:r.color??$[c],visible:N(r.id)})),[t,$,N]),yt=t.some(r=>r.data.length>0),Vt=v==="left"||v==="right",kt=jsxRuntime.jsxs("svg",{role:"img","aria-label":V??"Scatter chart",width:a.width,height:a.height,viewBox:`0 0 ${a.width} ${a.height}`,shapeRendering:"geometricPrecision",children:[rt.map(r=>jsxRuntime.jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:S(r),y2:S(r),stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},r)),m&&t.map((r,c)=>{if(!N(r.id)||r.data.length<2)return null;let d=X.get(r.id)??"var(--color-chart-1)",f=r.data.map(g=>B(Number(g.x))),u=r.data.map(g=>S(g.y)),p=lt(f,u,16);return p?jsxRuntime.jsx("ellipse",{cx:p.cx,cy:p.cy,rx:p.rx,ry:p.ry,fill:d,fillOpacity:.06,stroke:d,strokeOpacity:.3,strokeWidth:1.5,strokeDasharray:"5 3",className:Q?"chart-cluster-ellipse":"",style:{animationDelay:`${c*.1}s`}},`cluster-${r.id}`):null}),h&&t.map(r=>{if(!N(r.id)||r.data.length<2)return null;let c=X.get(r.id)??"var(--color-chart-1)",f=[...r.data].sort((u,p)=>Number(u.x)-Number(p.x)).map(u=>`${B(Number(u.x)).toFixed(2)},${S(u.y).toFixed(2)}`).join(" ");return jsxRuntime.jsx("polyline",{points:f,fill:"none",stroke:c,strokeWidth:1.5,className:"chart-scatter-link"},`link-${r.id}`)}),t.map((r,c)=>{if(!N(r.id))return null;let d=X.get(r.id)??"var(--color-chart-1)";return jsxRuntime.jsx("g",{className:"chart-scatter-group",children:r.data.map((f,u)=>{let[p,g]=l?ct(c,u,b):[0,0],Ct=B(Number(f.x))+p,vt=S(f.y)+g;return jsxRuntime.jsx("circle",{cx:Ct,cy:vt,r:o,fill:d,fillOpacity:s,stroke:d,strokeWidth:1,strokeOpacity:.5,className:M("chart-scatter-dot",Q&&"chart-scatter-dot--animate"),style:{color:d},onMouseMove:Y=>bt(Y,r.name,f.x,f.y,d),onMouseLeave:()=>T(null),onTouchStart:Y=>gt(Y,r.name,f.x,f.y,d),onTouchEnd:()=>T(null)},u)})},r.id)}),jsxRuntime.jsx("line",{x1:a.plotLeft,x2:a.plotLeft,y1:a.plotTop,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:a.plotBottom,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),rt.map(r=>{let c=S(r);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:a.plotLeft-4,x2:a.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:a.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C(r,e?.format)})]},r)}),xt.map(r=>{let c=B(r);return jsxRuntime.jsxs("g",{children:[jsxRuntime.jsx("line",{x1:c,x2:c,y1:a.plotBottom,y2:a.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsxRuntime.jsx("text",{x:c,y:a.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C(r,n?.format)})]},r)}),e?.label&&jsxRuntime.jsx("text",{x:-(a.plotTop+a.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:e.label}),n?.label&&jsxRuntime.jsx("text",{x:a.plotLeft+a.plotWidth/2,y:a.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:n.label})]});return jsxRuntime.jsxs("div",{ref:P,className:M(_(),dt),children:[V&&jsxRuntime.jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:V}),F&&v==="top"&&jsxRuntime.jsx(W,{items:I,position:"top",onToggle:L}),jsxRuntime.jsxs("div",{className:M("flex items-start",Vt&&"gap-0"),children:[F&&v==="left"&&jsxRuntime.jsx(W,{items:I,position:"left",onToggle:L}),jsxRuntime.jsx("div",{ref:U,className:"flex-1 min-w-0 overflow-hidden",children:yt?kt:jsxRuntime.jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:E},children:ft})}),F&&v==="right"&&jsxRuntime.jsx(W,{items:I,position:"right",onToggle:L})]}),F&&v==="bottom"&&jsxRuntime.jsx(W,{items:I,position:"bottom",onToggle:L}),k&&jsxRuntime.jsxs("div",{className:H(),style:{left:k.x,top:k.y},role:"tooltip",children:[jsxRuntime.jsxs("div",{className:"flex items-center gap-1.5 mb-1",children:[jsxRuntime.jsx("span",{className:"h-2 w-2 rounded-full",style:{background:k.color}}),jsxRuntime.jsx("span",{className:"font-medium",children:k.seriesName})]}),jsxRuntime.jsxs("div",{className:"text-primary-300",children:["X: ",jsxRuntime.jsx("span",{className:"font-medium text-primary-50",children:k.xVal})," ","Y: ",jsxRuntime.jsx("span",{className:"font-medium text-primary-50",children:k.yVal})]})]})]})}exports.ScatterChart=Ut;
@@ -1 +1 @@
1
- import {useRef,useState,useEffect,useMemo,useCallback}from'react';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {cva}from'class-variance-authority';import {jsxs,jsx}from'react/jsx-runtime';function M(...t){return twMerge(clsx(t))}var _=cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),j=cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),H=cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function nt(t){let[n,e]=useState(()=>new Set(t)),o=useCallback(l=>{e(b=>{let h=new Set(b);return h.has(l)?h.delete(l):h.add(l),h});},[]),s=useCallback(l=>n.has(l),[n]),m=useCallback(()=>{e(new Set(t));},[t]);return {visible:n,toggle:o,isVisible:s,showAll:m}}function ot(t){let n=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(e,o)=>n[o%n.length])}function G(t,n,e=.1){if(t===n)return [t-1,n+1];let s=(n-t)*e;return [Math.floor(t-s),Math.ceil(n+s)]}function at(t,n,e){if(e<=1)return [t];let o=(n-t)/(e-1);return Array.from({length:e},(s,m)=>+(t+m*o).toFixed(10))}function v(t,n){return n?n(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,n,e){return {top:20,right:e?65:20,bottom:t?52:36,left:n?65:52}}function K(t,n,e){let o=Math.max(t,120);return {width:o,height:n,margins:e,plotLeft:e.left,plotTop:e.top,plotRight:o-e.right,plotBottom:n-e.bottom,plotWidth:Math.max(o-e.left-e.right,10),plotHeight:Math.max(n-e.top-e.bottom,10)}}function it(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data){let k=Number(h.x);k<o&&(o=k),k>s&&(s=k);}if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function st(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data)h.y<o&&(o=h.y),h.y>s&&(s=h.y);if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function Z(t,n){let[e,o]=t,[s,m]=n;return o===e?()=>(s+m)/2:l=>s+(l-e)/(o-e)*(m-s)}function q(t,n=5){return at(t[0],t[1],n)}function ct(t,n,e){let o=1.618033988749895,s=t*137+n,m=s*o%1*2-1,l=s*o*o%1*2-1;return [m*e,l*e]}function lt(t,n,e=12){if(t.length<2)return null;let o=Math.min(...t),s=Math.max(...t),m=Math.min(...n),l=Math.max(...n);return {cx:(o+s)/2,cy:(m+l)/2,rx:(s-o)/2+e,ry:(l-m)/2+e}}function W({items:t,position:n,onToggle:e}){return jsx("div",{className:j({position:n}),children:t.map(o=>jsxs("button",{onClick:()=>e(o.id),className:M("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!o.visible&&"opacity-40"),"aria-pressed":o.visible,children:[jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:o.color}}),o.label]},o.id))})}function Ut({series:t,xAxis:n,yAxis:e,dotRadius:o=5,dotOpacity:s=.75,clusters:m=false,jitter:l=false,jitterAmount:b=12,linkedLines:h=false,title:k,height:E=300,className:dt,legend:F=true,legendPosition:V="bottom",animateOnMount:Q=true,emptyText:ft="No data to display"}){let P=useRef(null),U=useRef(null),[A,tt]=useState(600),[C,T]=useState(null);useEffect(()=>{let r=U.current;if(!r)return;let c=r.getBoundingClientRect().width;c>0&&tt(c);let d=new ResizeObserver(f=>{let u=f[0]?.contentRect.width;u&&u>0&&tt(u);});return d.observe(r),()=>d.disconnect()},[]);let ht=useMemo(()=>t.map(r=>r.id),[t]),{visible:w,toggle:L,isVisible:N}=nt(ht),$=useMemo(()=>ot(t.length),[t.length]),X=useMemo(()=>new Map(t.map((r,c)=>[r.id,r.color??$[c]])),[t,$]),et=useMemo(()=>J(!!n?.label,!!e?.label,false),[n?.label,e?.label]),a=useMemo(()=>K(A,E,et),[A,E,et]),D=useMemo(()=>it(t,w,n),[t,w,n]),R=useMemo(()=>st(t,w,e),[t,w,e]),B=useMemo(()=>Z(D,[a.plotLeft,a.plotRight]),[D,a]),S=useMemo(()=>Z(R,[a.plotBottom,a.plotTop]),[R,a]),xt=useMemo(()=>q(D,n?.tickCount??6),[D,n?.tickCount]),rt=useMemo(()=>q(R,e?.tickCount??5),[R,e?.tickCount]),bt=useCallback((r,c,d,f,u)=>{let p=P.current?.getBoundingClientRect();p&&T({x:r.clientX-p.left+12,y:r.clientY-p.top-10,seriesName:c,xVal:v(d,n?.format),yVal:v(f,e?.format),color:u});},[n?.format,e?.format]),gt=useCallback((r,c,d,f,u)=>{r.preventDefault();let p=P.current?.getBoundingClientRect();if(!p)return;let g=r.touches[0];g&&T({x:g.clientX-p.left+12,y:g.clientY-p.top-10,seriesName:c,xVal:v(d,n?.format),yVal:v(f,e?.format),color:u});},[n?.format,e?.format]),I=useMemo(()=>t.map((r,c)=>({id:r.id,label:r.name,color:r.color??$[c],visible:N(r.id)})),[t,$,N]),yt=t.some(r=>r.data.length>0),kt=V==="left"||V==="right",Ct=jsxs("svg",{role:"img","aria-label":k??"Scatter chart",width:a.width,height:a.height,viewBox:`0 0 ${a.width} ${a.height}`,shapeRendering:"geometricPrecision",children:[rt.map(r=>jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:S(r),y2:S(r),stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},r)),m&&t.map((r,c)=>{if(!N(r.id)||r.data.length<2)return null;let d=X.get(r.id)??"var(--color-chart-1)",f=r.data.map(g=>B(Number(g.x))),u=r.data.map(g=>S(g.y)),p=lt(f,u,16);return p?jsx("ellipse",{cx:p.cx,cy:p.cy,rx:p.rx,ry:p.ry,fill:d,fillOpacity:.06,stroke:d,strokeOpacity:.3,strokeWidth:1.5,strokeDasharray:"5 3",className:Q?"chart-cluster-ellipse":"",style:{animationDelay:`${c*.1}s`}},`cluster-${r.id}`):null}),h&&t.map(r=>{if(!N(r.id)||r.data.length<2)return null;let c=X.get(r.id)??"var(--color-chart-1)",f=[...r.data].sort((u,p)=>Number(u.x)-Number(p.x)).map(u=>`${B(Number(u.x)).toFixed(2)},${S(u.y).toFixed(2)}`).join(" ");return jsx("polyline",{points:f,fill:"none",stroke:c,strokeWidth:1.5,className:"chart-scatter-link"},`link-${r.id}`)}),t.map((r,c)=>{if(!N(r.id))return null;let d=X.get(r.id)??"var(--color-chart-1)";return jsx("g",{className:"chart-scatter-group",children:r.data.map((f,u)=>{let[p,g]=l?ct(c,u,b):[0,0],vt=B(Number(f.x))+p,Vt=S(f.y)+g;return jsx("circle",{cx:vt,cy:Vt,r:o,fill:d,fillOpacity:s,stroke:d,strokeWidth:1,strokeOpacity:.5,className:M("chart-scatter-dot",Q&&"chart-scatter-dot--animate"),style:{color:d},onMouseMove:Y=>bt(Y,r.name,f.x,f.y,d),onMouseLeave:()=>T(null),onTouchStart:Y=>gt(Y,r.name,f.x,f.y,d),onTouchEnd:()=>T(null)},u)})},r.id)}),jsx("line",{x1:a.plotLeft,x2:a.plotLeft,y1:a.plotTop,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:a.plotBottom,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),rt.map(r=>{let c=S(r);return jsxs("g",{children:[jsx("line",{x1:a.plotLeft-4,x2:a.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:a.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:v(r,e?.format)})]},r)}),xt.map(r=>{let c=B(r);return jsxs("g",{children:[jsx("line",{x1:c,x2:c,y1:a.plotBottom,y2:a.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:c,y:a.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:v(r,n?.format)})]},r)}),e?.label&&jsx("text",{x:-(a.plotTop+a.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:e.label}),n?.label&&jsx("text",{x:a.plotLeft+a.plotWidth/2,y:a.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:n.label})]});return jsxs("div",{ref:P,className:M(_(),dt),children:[k&&jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:k}),F&&V==="top"&&jsx(W,{items:I,position:"top",onToggle:L}),jsxs("div",{className:M("flex items-start",kt&&"gap-0"),children:[F&&V==="left"&&jsx(W,{items:I,position:"left",onToggle:L}),jsx("div",{ref:U,className:"flex-1 min-w-0 overflow-hidden",children:yt?Ct:jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:E},children:ft})}),F&&V==="right"&&jsx(W,{items:I,position:"right",onToggle:L})]}),F&&V==="bottom"&&jsx(W,{items:I,position:"bottom",onToggle:L}),C&&jsxs("div",{className:H(),style:{left:C.x,top:C.y},role:"tooltip",children:[jsxs("div",{className:"flex items-center gap-1.5 mb-1",children:[jsx("span",{className:"h-2 w-2 rounded-full",style:{background:C.color}}),jsx("span",{className:"font-medium",children:C.seriesName})]}),jsxs("div",{className:"text-primary-300",children:["X: ",jsx("span",{className:"font-medium text-primary-50",children:C.xVal})," ","Y: ",jsx("span",{className:"font-medium text-primary-50",children:C.yVal})]})]})]})}export{Ut as ScatterChart};
1
+ import {useRef,useState,useEffect,useMemo,useCallback}from'react';import {clsx}from'clsx';import {twMerge}from'tailwind-merge';import {cva}from'class-variance-authority';import {jsxs,jsx}from'react/jsx-runtime';function M(...t){return twMerge(clsx(t))}var _=cva("relative w-full font-sans select-none",{variants:{size:{sm:"text-xs",md:"text-sm",lg:"text-base"}},defaultVariants:{size:"md"}}),j=cva("flex flex-wrap gap-3 items-center justify-center",{variants:{position:{top:"mb-3",bottom:"mt-3",left:"flex-col mr-4",right:"flex-col ml-4"}},defaultVariants:{position:"bottom"}}),H=cva("absolute z-50 pointer-events-none px-3 py-2 rounded-lg text-xs shadow-lg border transition-opacity duration-150 bg-primary-900 text-primary-50 border-primary-700 dark:bg-primary-950 dark:border-primary-800");function nt(t){let[n,e]=useState(()=>new Set(t)),o=useCallback(l=>{e(b=>{let h=new Set(b);return h.has(l)?h.delete(l):h.add(l),h});},[]),s=useCallback(l=>n.has(l),[n]),m=useCallback(()=>{e(new Set(t));},[t]);return {visible:n,toggle:o,isVisible:s,showAll:m}}function ot(t){let n=["var(--color-chart-1)","var(--color-chart-2)","var(--color-chart-3)","var(--color-chart-4)","var(--color-chart-5)","var(--color-chart-6)","var(--color-chart-7)","var(--color-chart-8)"];return Array.from({length:t},(e,o)=>n[o%n.length])}function G(t,n,e=.1){if(t===n)return [t-1,n+1];let s=(n-t)*e;return [Math.floor(t-s),Math.ceil(n+s)]}function at(t,n,e){if(e<=1)return [t];let o=(n-t)/(e-1);return Array.from({length:e},(s,m)=>+(t+m*o).toFixed(10))}function C(t,n){return n?n(t):typeof t=="number"?Math.abs(t)>=1e6?`${(t/1e6).toFixed(1)}M`:Math.abs(t)>=1e3?`${(t/1e3).toFixed(1)}K`:Number.isInteger(t)?t.toString():t.toFixed(2):String(t)}function J(t,n,e){return {top:20,right:e?65:20,bottom:t?52:36,left:n?65:52}}function K(t,n,e){let o=Math.max(t,120);return {width:o,height:n,margins:e,plotLeft:e.left,plotTop:e.top,plotRight:o-e.right,plotBottom:n-e.bottom,plotWidth:Math.max(o-e.left-e.right,10),plotHeight:Math.max(n-e.top-e.bottom,10)}}function it(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data){let V=Number(h.x);V<o&&(o=V),V>s&&(s=V);}if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function st(t,n,e){let o=1/0,s=-1/0;for(let b of t)if(n.has(b.id))for(let h of b.data)h.y<o&&(o=h.y),h.y>s&&(s=h.y);if(!isFinite(o))return [0,1];let[m,l]=G(o,s);return [e?.min??m,e?.max??l]}function Z(t,n){let[e,o]=t,[s,m]=n;return o===e?()=>(s+m)/2:l=>s+(l-e)/(o-e)*(m-s)}function q(t,n=5){return at(t[0],t[1],n)}function ct(t,n,e){let o=1.618033988749895,s=t*137+n,m=s*o%1*2-1,l=s*o*o%1*2-1;return [m*e,l*e]}function lt(t,n,e=12){if(t.length<2)return null;let o=Math.min(...t),s=Math.max(...t),m=Math.min(...n),l=Math.max(...n);return {cx:(o+s)/2,cy:(m+l)/2,rx:(s-o)/2+e,ry:(l-m)/2+e}}function W({items:t,position:n,onToggle:e}){return jsx("div",{className:j({position:n}),children:t.map(o=>jsxs("button",{onClick:()=>e(o.id),className:M("flex items-center gap-1.5 cursor-pointer rounded px-1.5 py-1 text-xs text-primary-700 transition-opacity hover:bg-primary-100 dark:text-primary-300 dark:hover:bg-primary-800",!o.visible&&"opacity-40"),"aria-pressed":o.visible,children:[jsx("span",{className:"h-2.5 w-2.5 rounded-full shrink-0",style:{background:o.color}}),o.label]},o.id))})}function Ut({series:t,xAxis:n,yAxis:e,dotRadius:o=5,dotOpacity:s=.75,clusters:m=false,jitter:l=false,jitterAmount:b=12,linkedLines:h=false,title:V,height:E=300,className:dt,legend:F=true,legendPosition:v="bottom",animateOnMount:Q=true,emptyText:ft="No data to display"}){let P=useRef(null),U=useRef(null),[A,tt]=useState(600),[k,T]=useState(null);useEffect(()=>{let r=U.current;if(!r)return;let c=r.getBoundingClientRect().width;c>0&&tt(c);let d=new ResizeObserver(f=>{let u=f[0]?.contentRect.width;u&&u>0&&tt(u);});return d.observe(r),()=>d.disconnect()},[]);let ht=useMemo(()=>t.map(r=>r.id),[t]),{visible:w,toggle:L,isVisible:N}=nt(ht),$=useMemo(()=>ot(t.length),[t.length]),X=useMemo(()=>new Map(t.map((r,c)=>[r.id,r.color??$[c]])),[t,$]),et=useMemo(()=>J(!!n?.label,!!e?.label,false),[n?.label,e?.label]),a=useMemo(()=>K(A,E,et),[A,E,et]),D=useMemo(()=>it(t,w,n),[t,w,n]),R=useMemo(()=>st(t,w,e),[t,w,e]),B=useMemo(()=>Z(D,[a.plotLeft,a.plotRight]),[D,a]),S=useMemo(()=>Z(R,[a.plotBottom,a.plotTop]),[R,a]),xt=useMemo(()=>q(D,n?.tickCount??6),[D,n?.tickCount]),rt=useMemo(()=>q(R,e?.tickCount??5),[R,e?.tickCount]),bt=useCallback((r,c,d,f,u)=>{let p=P.current?.getBoundingClientRect();p&&T({x:r.clientX-p.left+12,y:r.clientY-p.top-10,seriesName:c,xVal:C(d,n?.format),yVal:C(f,e?.format),color:u});},[n?.format,e?.format]),gt=useCallback((r,c,d,f,u)=>{r.preventDefault();let p=P.current?.getBoundingClientRect();if(!p)return;let g=r.touches[0];g&&T({x:g.clientX-p.left+12,y:g.clientY-p.top-10,seriesName:c,xVal:C(d,n?.format),yVal:C(f,e?.format),color:u});},[n?.format,e?.format]),I=useMemo(()=>t.map((r,c)=>({id:r.id,label:r.name,color:r.color??$[c],visible:N(r.id)})),[t,$,N]),yt=t.some(r=>r.data.length>0),Vt=v==="left"||v==="right",kt=jsxs("svg",{role:"img","aria-label":V??"Scatter chart",width:a.width,height:a.height,viewBox:`0 0 ${a.width} ${a.height}`,shapeRendering:"geometricPrecision",children:[rt.map(r=>jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:S(r),y2:S(r),stroke:"currentColor",strokeWidth:.5,strokeDasharray:"4 4",className:"text-secondary-200 dark:text-secondary-700"},r)),m&&t.map((r,c)=>{if(!N(r.id)||r.data.length<2)return null;let d=X.get(r.id)??"var(--color-chart-1)",f=r.data.map(g=>B(Number(g.x))),u=r.data.map(g=>S(g.y)),p=lt(f,u,16);return p?jsx("ellipse",{cx:p.cx,cy:p.cy,rx:p.rx,ry:p.ry,fill:d,fillOpacity:.06,stroke:d,strokeOpacity:.3,strokeWidth:1.5,strokeDasharray:"5 3",className:Q?"chart-cluster-ellipse":"",style:{animationDelay:`${c*.1}s`}},`cluster-${r.id}`):null}),h&&t.map(r=>{if(!N(r.id)||r.data.length<2)return null;let c=X.get(r.id)??"var(--color-chart-1)",f=[...r.data].sort((u,p)=>Number(u.x)-Number(p.x)).map(u=>`${B(Number(u.x)).toFixed(2)},${S(u.y).toFixed(2)}`).join(" ");return jsx("polyline",{points:f,fill:"none",stroke:c,strokeWidth:1.5,className:"chart-scatter-link"},`link-${r.id}`)}),t.map((r,c)=>{if(!N(r.id))return null;let d=X.get(r.id)??"var(--color-chart-1)";return jsx("g",{className:"chart-scatter-group",children:r.data.map((f,u)=>{let[p,g]=l?ct(c,u,b):[0,0],Ct=B(Number(f.x))+p,vt=S(f.y)+g;return jsx("circle",{cx:Ct,cy:vt,r:o,fill:d,fillOpacity:s,stroke:d,strokeWidth:1,strokeOpacity:.5,className:M("chart-scatter-dot",Q&&"chart-scatter-dot--animate"),style:{color:d},onMouseMove:Y=>bt(Y,r.name,f.x,f.y,d),onMouseLeave:()=>T(null),onTouchStart:Y=>gt(Y,r.name,f.x,f.y,d),onTouchEnd:()=>T(null)},u)})},r.id)}),jsx("line",{x1:a.plotLeft,x2:a.plotLeft,y1:a.plotTop,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("line",{x1:a.plotLeft,x2:a.plotRight,y1:a.plotBottom,y2:a.plotBottom,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),rt.map(r=>{let c=S(r);return jsxs("g",{children:[jsx("line",{x1:a.plotLeft-4,x2:a.plotLeft,y1:c,y2:c,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:a.plotLeft-6,y:c,textAnchor:"end",dominantBaseline:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C(r,e?.format)})]},r)}),xt.map(r=>{let c=B(r);return jsxs("g",{children:[jsx("line",{x1:c,x2:c,y1:a.plotBottom,y2:a.plotBottom+4,stroke:"currentColor",strokeWidth:1,className:"text-secondary-300 dark:text-secondary-600"}),jsx("text",{x:c,y:a.plotBottom+14,textAnchor:"middle",fontSize:10,fill:"currentColor",className:"text-secondary-500 dark:text-secondary-400",children:C(r,n?.format)})]},r)}),e?.label&&jsx("text",{x:-(a.plotTop+a.plotHeight/2),y:14,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",transform:"rotate(-90)",children:e.label}),n?.label&&jsx("text",{x:a.plotLeft+a.plotWidth/2,y:a.height-4,textAnchor:"middle",fontSize:11,fill:"currentColor",className:"text-secondary-600 dark:text-secondary-400",children:n.label})]});return jsxs("div",{ref:P,className:M(_(),dt),children:[V&&jsx("h3",{className:"mb-2 text-sm font-semibold text-primary-700 dark:text-primary-200",children:V}),F&&v==="top"&&jsx(W,{items:I,position:"top",onToggle:L}),jsxs("div",{className:M("flex items-start",Vt&&"gap-0"),children:[F&&v==="left"&&jsx(W,{items:I,position:"left",onToggle:L}),jsx("div",{ref:U,className:"flex-1 min-w-0 overflow-hidden",children:yt?kt:jsx("div",{className:"flex items-center justify-center text-secondary-400 dark:text-secondary-500",style:{height:E},children:ft})}),F&&v==="right"&&jsx(W,{items:I,position:"right",onToggle:L})]}),F&&v==="bottom"&&jsx(W,{items:I,position:"bottom",onToggle:L}),k&&jsxs("div",{className:H(),style:{left:k.x,top:k.y},role:"tooltip",children:[jsxs("div",{className:"flex items-center gap-1.5 mb-1",children:[jsx("span",{className:"h-2 w-2 rounded-full",style:{background:k.color}}),jsx("span",{className:"font-medium",children:k.seriesName})]}),jsxs("div",{className:"text-primary-300",children:["X: ",jsx("span",{className:"font-medium text-primary-50",children:k.xVal})," ","Y: ",jsx("span",{className:"font-medium text-primary-50",children:k.yVal})]})]})]})}export{Ut as ScatterChart};