UNPKG

ranui

Version:

A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.

40 lines (39 loc) 6.71 kB
const u=require("./chunk-Y2CYZVJY-DPaYLFFP.cjs"),B=require("./src-D8aDujwE.cjs"),g=require("./chunk-I66GZJ75-BzqGKy7F.cjs"),ie=require("./ordinal-B4H1mW8J.cjs"),C=require("./path-DmfWsuKi.cjs"),R=require("./dist-4UCZR9Jy.cjs"),U=require("./arc-bOapWCJu.cjs"),le=require("./array-C3cbN3GY.cjs"),X=require("./chunk-NSK5VX7P-DcRXFc-R.cjs"),oe=require("./chunk-3NCLNEKW-CHPh0zzA.cjs"),se=require("./chunk-JWPE2WC7-D8EnWK1B.cjs"),ce=require("./mermaid-parser.core-DQClw8Il.cjs");function ue(e,n){return n<e?-1:n>e?1:n>=e?0:NaN}function de(e){return e}function ge(){var e=de,n=ue,m=null,s=C.constant_default(0),c=C.constant_default(R.tau),D=C.constant_default(0);function a(t){var l,_=(t=le.array_default(t)).length,v,$,A=0,f=new Array(_),i=new Array(_),w=+s.apply(this,arguments),k=Math.min(R.tau,Math.max(-R.tau,c.apply(this,arguments)-w)),T,M=Math.min(Math.abs(k)/_,D.apply(this,arguments)),h=M*(k<0?-1:1),S;for(l=0;l<_;++l)(S=i[f[l]=l]=+e(t[l],l,t))>0&&(A+=S);for(n!=null?f.sort(function(b,p){return n(i[b],i[p])}):m!=null&&f.sort(function(b,p){return m(t[b],t[p])}),l=0,$=A?(k-_*h)/A:0;l<_;++l,w=T)v=f[l],S=i[v],T=w+(S>0?S*$:0)+h,i[v]={data:t[v],index:l,value:S,startAngle:w,endAngle:T,padAngle:M};return i}return a.value=function(t){return arguments.length?(e=typeof t=="function"?t:C.constant_default(+t),a):e},a.sortValues=function(t){return arguments.length?(n=t,m=null,a):n},a.sort=function(t){return arguments.length?(m=t,n=null,a):m},a.startAngle=function(t){return arguments.length?(s=typeof t=="function"?t:C.constant_default(+t),a):s},a.endAngle=function(t){return arguments.length?(c=typeof t=="function"?t:C.constant_default(+t),a):c},a.padAngle=function(t){return arguments.length?(D=typeof t=="function"?t:C.constant_default(+t),a):D},a}var j=g.defaultConfig_default.pie,V={sections:new Map,showData:!1,config:j},L=V.sections,I=V.showData,fe=structuredClone(j),Q={getConfig:u.__name(()=>structuredClone(fe),"getConfig"),clear:u.__name(()=>{L=new Map,I=V.showData,g.clear()},"clear"),setDiagramTitle:g.setDiagramTitle,getDiagramTitle:g.getDiagramTitle,setAccTitle:g.setAccTitle,getAccTitle:g.getAccTitle,setAccDescription:g.setAccDescription,getAccDescription:g.getAccDescription,addSection:u.__name(({label:e,value:n})=>{if(n<0)throw new Error(`"${e}" has invalid value: ${n}. Negative values are not allowed in pie charts. All slice values must be >= 0.`);L.has(e)||(L.set(e,n),B.log.debug(`added new section: ${e}, with value: ${n}`))},"addSection"),getSections:u.__name(()=>L,"getSections"),setShowData:u.__name(e=>{I=e},"setShowData"),getShowData:u.__name(()=>I,"getShowData")},he=u.__name((e,n)=>{se.populateCommonDb(e,n),n.setShowData(e.showData),e.sections.map(n.addSection)},"populateDb"),pe={parse:u.__name(async e=>{const n=await ce.parse("pie",e);B.log.debug(n),he(n,Q)},"parse")},me=u.__name(e=>` .pieCircle{ stroke: ${e.pieStrokeColor}; stroke-width : ${e.pieStrokeWidth}; opacity : ${e.pieOpacity}; } .pieCircle.highlighted{ scale: 1.05; opacity: 1; } .pieCircle.highlightedOnHover:hover{ transition-duration: 250ms; scale: 1.05; opacity: 1; } .pieOuterCircle{ stroke: ${e.pieOuterStrokeColor}; stroke-width: ${e.pieOuterStrokeWidth}; fill: none; } .pieTitleText { text-anchor: middle; font-size: ${e.pieTitleTextSize}; fill: ${e.pieTitleTextColor}; font-family: ${e.fontFamily}; } .slice { font-family: ${e.fontFamily}; fill: ${e.pieSectionTextColor}; font-size:${e.pieSectionTextSize}; // fill: white; } .legend text { fill: ${e.pieLegendTextColor}; font-family: ${e.fontFamily}; font-size: ${e.pieLegendTextSize}; } `,"getStyles"),_e=u.__name(e=>{const n=[...e.values()].reduce((s,c)=>s+c,0),m=[...e.entries()].map(([s,c])=>({label:s,value:c})).filter(s=>s.value/n*100>=1);return ge().value(s=>s.value).sort(null)(m)},"createPieArcs"),ve={parser:pe,db:Q,renderer:{draw:u.__name((e,n,m,s)=>{B.log.debug(`rendering pie chart `+e);const c=s.db,D=g.getConfig2(),a=X.cleanAndMerge(c.getConfig(),D.pie),t=40,l=18,_=4,v=450,$=v,A=oe.selectSvgElement(n),f=A.append("g");f.attr("transform","translate(225,225)");const{themeVariables:i}=D;let[w]=X.parseFontSize(i.pieOuterStrokeWidth);w??=2;const k=a.legendPosition,T=a.textPosition,M=a.donutHole>0&&a.donutHole<=.9?a.donutHole:0,h=Math.min($,v)/2-t,S=U.arc_default().innerRadius(M*h).outerRadius(h),b=U.arc_default().innerRadius(h*T).outerRadius(h*T),p=f.append("g");p.append("circle").attr("cx",0).attr("cy",0).attr("r",h+w/2).attr("class","pieOuterCircle");const W=c.getSections(),ee=_e(W),te=[i.pie1,i.pie2,i.pie3,i.pie4,i.pie5,i.pie6,i.pie7,i.pie8,i.pie9,i.pie10,i.pie11,i.pie12];let P=0;W.forEach(r=>{P+=r});const J=ee.filter(r=>(r.data.value/P*100).toFixed(0)!=="0"),z=ie.ordinal(te).domain([...W.keys()]);p.selectAll("mySlices").data(J).enter().append("path").attr("d",S).attr("fill",r=>z(r.data.label)).attr("class",r=>{let o="pieCircle";return a.highlightSlice==="hover"?o+=" highlightedOnHover":a.highlightSlice===r.data.label&&(o+=" highlighted"),o}),p.selectAll("mySlices").data(J).enter().append("text").text(r=>(r.data.value/P*100).toFixed(0)+"%").attr("transform",r=>"translate("+b.centroid(r)+")").style("text-anchor","middle").attr("class","slice");const re=f.append("text").text(c.getDiagramTitle()).attr("x",0).attr("y",-200).attr("class","pieTitleText"),E=[...W.entries()].map(([r,o])=>({label:r,value:o})),y=f.selectAll(".legend").data(E).enter().append("g").attr("class","legend");y.append("rect").attr("width",l).attr("height",l).style("fill",r=>z(r.label)).style("stroke",r=>z(r.label)),y.append("text").attr("x",22).attr("y",14).text(r=>c.getShowData()?`${r.label} [${r.value}]`:r.label);const q=Math.max(...y.selectAll("text").nodes().map(r=>r?.getBoundingClientRect().width??0));let N=v,F=490;const d=22,G=E.length*d;switch(k){case"center":y.attr("transform",(r,o)=>{const x=d*E.length/2,H=-q/2-22,O=o*d-x;return"translate("+H+","+O+")"});break;case"top":N+=G,y.attr("transform",(r,o)=>{const x=h;return`translate(${-q/2-22}, ${o*d-x})`}),p.attr("transform",()=>`translate(0, ${G+d})`);break;case"bottom":N+=G,y.attr("transform",(r,o)=>{const H=-q/2-22,O=o*d- -207;return"translate("+H+","+O+")"});break;case"left":F+=22+q,y.attr("transform",(r,o)=>{const x=d*E.length/2;return"translate(-207,"+(o*d-x)+")"}),p.attr("transform",()=>`translate(${q+l+_}, 0)`);break;default:F+=22+q,y.attr("transform",(r,o)=>{const x=d*E.length/2;return"translate(216,"+(o*d-x)+")"})}const Y=re.node()?.getBoundingClientRect().width??0,ne=$/2-Y/2,ae=$/2+Y/2,Z=Math.min(0,ne),K=Math.max(F,ae)-Z;A.attr("viewBox",`${Z} 0 ${K} ${N}`),g.configureSvgSize(A,N,K,a.useMaxWidth)},"draw")},styles:me};exports.diagram=ve;