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.72 kB
const ie=require("./mermaid-parser.core-DYumW1vq.cjs"),d=require("./chunk-Y2CYZVJY-DPaYLFFP.cjs"),B=require("./src-C9EhKhbk.cjs"),g=require("./chunk-WYO6CB5R-u4mJNOcO.cjs"),le=require("./ordinal-BplLHIij.cjs"),C=require("./path-DmfWsuKi.cjs"),G=require("./dist-C20XxSsP.cjs"),Z=require("./arc-D25LhgTs.cjs"),oe=require("./array-C3cbN3GY.cjs"),j=require("./chunk-ICXQ74PX-BbT9JaEW.cjs"),se=require("./chunk-VAUOI2AC-DfQf5L2_.cjs"),ce=require("./chunk-JWPE2WC7-D8EnWK1B.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,v=null,s=C.constant_default(0),c=C.constant_default(G.tau),D=C.constant_default(0);function a(t){var l,f=(t=oe.array_default(t)).length,A,$,x=0,h=new Array(f),i=new Array(f),w=+s.apply(this,arguments),k=Math.min(G.tau,Math.max(-G.tau,c.apply(this,arguments)-w)),T,M=Math.min(Math.abs(k)/f,D.apply(this,arguments)),p=M*(k<0?-1:1),y;for(l=0;l<f;++l)(y=i[h[l]=l]=+e(t[l],l,t))>0&&(x+=y);for(n!=null?h.sort(function(b,m){return n(i[b],i[m])}):v!=null&&h.sort(function(b,m){return v(t[b],t[m])}),l=0,$=x?(k-f*p)/x:0;l<f;++l,w=T)A=h[l],y=i[A],T=w+(y>0?y*$:0)+p,i[A]={data:t[A],index:l,value:y,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,v=null,a):n},a.sort=function(t){return arguments.length?(v=t,n=null,a):v},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 Q=g.defaultConfig_default.pie,V={sections:new Map,showData:!1,config:Q},P=V.sections,I=V.showData,fe=structuredClone(Q),K={getConfig:d.__name(()=>structuredClone(fe),"getConfig"),clear:d.__name(()=>{P=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:d.__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.`);P.has(e)||(P.set(e,n),B.log.debug(`added new section: ${e}, with value: ${n}`))},"addSection"),getSections:d.__name(()=>P,"getSections"),setShowData:d.__name(e=>{I=e},"setShowData"),getShowData:d.__name(()=>I,"getShowData")},he=d.__name((e,n)=>{ce.populateCommonDb(e,n),n.setShowData(e.showData),e.sections.map(n.addSection)},"populateDb"),pe={parse:d.__name(async e=>{const n=await ie.parse("pie",e);B.log.debug(n),he(n,K)},"parse")},me=d.__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=d.__name(e=>{const n=[...e.values()].reduce((s,c)=>s+c,0),v=[...e.entries()].map(([s,c])=>({label:s,value:c})).filter(s=>s.value/n*100>=1);return ge().value(s=>s.value).sort(null)(v)},"createPieArcs"),ve={parser:pe,db:K,renderer:{draw:d.__name((e,n,v,s)=>{B.log.debug(`rendering pie chart `+e);const c=s.db,D=g.getConfig2(),a=j.cleanAndMerge(c.getConfig(),D.pie),t=40,l=18,f=4,A=450,$=A,x=se.selectSvgElement(n),h=x.append("g");h.attr("transform","translate(225,225)");const{themeVariables:i}=D;let[w]=j.parseFontSize(i.pieOuterStrokeWidth);w??=2;const k=a.legendPosition,T=a.textPosition,M=a.donutHole>0&&a.donutHole<=.9?a.donutHole:0,p=Math.min($,A)/2-t,y=Z.arc_default().innerRadius(M*p).outerRadius(p),b=Z.arc_default().innerRadius(p*T).outerRadius(p*T),m=h.append("g");m.append("circle").attr("cx",0).attr("cy",0).attr("r",p+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 z=0;W.forEach(r=>{z+=r});const Y=ee.filter(r=>(r.data.value/z*100).toFixed(0)!=="0"),L=le.ordinal(te).domain([...W.keys()]);m.selectAll("mySlices").data(Y).enter().append("path").attr("d",y).attr("fill",r=>L(r.data.label)).attr("class",r=>{let o="pieCircle";return a.highlightSlice==="hover"?o+=" highlightedOnHover":a.highlightSlice===r.data.label&&(o+=" highlighted"),o}),m.selectAll("mySlices").data(Y).enter().append("text").text(r=>(r.data.value/z*100).toFixed(0)+"%").attr("transform",r=>"translate("+b.centroid(r)+")").style("text-anchor","middle").attr("class","slice");const re=h.append("text").text(c.getDiagramTitle()).attr("x",0).attr("y",-400/2).attr("class","pieTitleText"),E=[...W.entries()].map(([r,o])=>({label:r,value:o})),S=h.selectAll(".legend").data(E).enter().append("g").attr("class","legend");S.append("rect").attr("width",l).attr("height",l).style("fill",r=>L(r.label)).style("stroke",r=>L(r.label)),S.append("text").attr("x",22).attr("y",l-f).text(r=>c.getShowData()?`${r.label} [${r.value}]`:r.label);const q=Math.max(...S.selectAll("text").nodes().map(r=>r?.getBoundingClientRect().width??0));let O=A,R=490;const u=22,F=E.length*u;switch(k){case"center":S.attr("transform",(r,o)=>{const _=u*E.length/2,H=-q/2-22,N=o*u-_;return"translate("+H+","+N+")"});break;case"top":O+=F,S.attr("transform",(r,o)=>{const _=p;return`translate(${-q/2-22}, ${o*u-_})`}),m.attr("transform",()=>`translate(0, ${F+u})`);break;case"bottom":O+=F,S.attr("transform",(r,o)=>{const _=-185-u,H=-q/2-22,N=o*u-_;return"translate("+H+","+N+")"});break;case"left":R+=22+q,S.attr("transform",(r,o)=>{const _=u*E.length/2;return"translate(-207,"+(o*u-_)+")"}),m.attr("transform",()=>`translate(${q+l+f}, 0)`);break;default:R+=22+q,S.attr("transform",(r,o)=>{const _=u*E.length/2;return"translate(216,"+(o*u-_)+")"});break}const U=re.node()?.getBoundingClientRect().width??0,ne=$/2-U/2,ae=$/2+U/2,X=Math.min(0,ne),J=Math.max(R,ae)-X;x.attr("viewBox",`${X} 0 ${J} ${O}`),g.configureSvgSize(x,O,J,a.useMaxWidth)},"draw")},styles:me};exports.diagram=ve;