@graphysdk/geom-boxplot 0.1.1-beta.1791559652655 → 0.1.1-beta.1791794533416

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let s=require("@graphysdk/react"),W=require("react"),f=require("react/jsx-runtime");var x={q1:"boxplotQ1",median:"boxplotMedian",q3:"boxplotQ3",lowerWhisker:"boxplotLowerWhisker",upperWhisker:"boxplotUpperWhisker",count:"boxplotCount",mean:"boxplotMean",notchLower:"boxplotNotchLower",notchUpper:"boxplotNotchUpper",relativeWidth:"boxplotRelativeWidth",outlier:"boxplotOutlier"},_=e=>(e.start+e.end)/2,H=(e,t)=>{const r=t==="x"?(0,s.getXMin)(e):(0,s.getYMin)(e),o=t==="x"?(0,s.getXMax)(e):(0,s.getYMax)(e),a=t==="x"?(0,s.getYMin)(e):(0,s.getXMin)(e),n=t==="x"?(0,s.getYMax)(e):(0,s.getXMax)(e),l=(0,s.getScaledAesthetic)(e,"q1"),u=(0,s.getScaledAesthetic)(e,"median"),c=(0,s.getScaledAesthetic)(e,"q3");if(r===null||o===null||a===null||n===null||u===null||l===null||c===null)return null;const i=E(e,"notchLower"),d=E(e,"notchUpper"),[m,k]=ne(r,o,e);return{observation:e,start:m,end:k,lowerWhisker:a,q1:l,median:u,q3:c,upperWhisker:n,mean:E(e,"mean"),notch:i===null||d===null?null:{lower:i,upper:d}}},F=(e,t)=>{const r=t==="x"?(0,s.getXMin)(e):(0,s.getYMin)(e),o=t==="x"?(0,s.getXMax)(e):(0,s.getYMax)(e),a=(0,s.getScaledAesthetic)(e,"outlier");return r===null||o===null||a===null?null:{observation:e,band:_({start:r,end:o}),value:a}},S=(e,t)=>{const r=[],o=[];for(const a of e){const n=H(a,t);if(n!==null){r.push(n);continue}const l=F(a,t);l!==null&&o.push(l)}return{boxes:r,outliers:o}},E=(e,t)=>(0,s.getScaledAesthetic)(e,t)??null,ne=(e,t,r)=>{const o=r[x.relativeWidth];if(typeof o!="number")return[e,t];const a=(e+t)/2,n=(t-e)*o/2;return[a-n,a+n]},b=1.5,G=e=>e==="min-max"||typeof e=="number"&&Number.isFinite(e)&&e>=0,se=1.58,le=(e,t)=>{if(e.length===0)return null;const r=[...e].sort((m,k)=>m-k),o=B(r,.25),a=B(r,.5),n=B(r,.75),l=t==="min-max"?1/0:t*(n-o),u=o-l,c=n+l,i=r.filter(m=>m>=u&&m<=c),d=se*(n-o)/Math.sqrt(e.length);return{lowerWhisker:Math.min(i[0]??o,o),q1:o,median:a,q3:n,upperWhisker:Math.max(i[i.length-1]??n,n),count:e.length,mean:r.reduce((m,k)=>m+k,0)/r.length,notchLower:a-d,notchUpper:a+d}},B=(e,t)=>{const r=(e.length-1)*t,o=Math.floor(r),a=e[o]??0;return a+((e[o+1]??a)-a)*(r-o)},Y={q1:x.q1,median:x.median,q3:x.q3,lowerWhisker:x.lowerWhisker,upperWhisker:x.upperWhisker,count:x.count,outlier:x.outlier},z=new Set([...Object.keys(Y),"mean","notchLower","notchUpper"]),ie=class extends s.Stat{constructor(...e){super(...e),this.type="boxplot",this.computedVariables=z}computeStat({data:e,mapping:t,params:r}){const o=(0,s.readVariableName)(t.x),a=(0,s.readVariableName)(t.y);if(o===null||a===null||!e.hasVariable(o)||!e.hasVariable(a))return{data:e,mapping:{}};if(e.size()>0&&e.getType(a)!=="numeric"){const l=e.getType(a);throw new s.UserInputError({code:"INCOMPATIBLE_TYPE",message:`'boxplot' stat requires a numeric 'y' (got '${l}' for variable '${a}')`,context:{aesthetic:"y",variableName:a,variableType:l},suggestion:"Map the raw values to 'y', or summarised columns under stat 'identity'."})}const n=r?.extent;return{data:ue({data:e,xVariable:o,yVariable:a,groupVariables:(0,s.getGroupVariables)(e,t).filter(l=>l!==o),extent:G(n)?n:b}),mapping:Y}}},ue=({data:e,xVariable:t,yVariable:r,groupVariables:o,extent:a})=>{const n=ce(e,t,r,o),l=e.getVariableNames(),u=new Map(l.map(p=>[p,e.getValues(p)])),c=new Map(l.map(p=>[p,[]])),i={q1:[],median:[],q3:[],lowerWhisker:[],upperWhisker:[],count:[],mean:[],notchLower:[],notchUpper:[],outlier:[]},d=[];for(const p of n){const v=le(p.values,a);if(v===null)continue;for(const h of l)c.get(h)?.push(he(u.get(h)??[],p.indices));i.q1.push(v.q1),i.median.push(v.median),i.q3.push(v.q3),i.lowerWhisker.push(v.lowerWhisker),i.upperWhisker.push(v.upperWhisker),i.count.push(v.count),i.mean.push(v.mean),i.notchLower.push(v.notchLower),i.notchUpper.push(v.notchUpper),i.outlier.push(null);const w={low:v.lowerWhisker,high:v.upperWhisker};p.indices.forEach((h,y)=>{const M=p.values[y];M!==void 0&&(M<w.low||M>w.high)&&d.push({index:h,value:M})})}for(const{index:p,value:v}of d){for(const w of l)c.get(w)?.push(u.get(w)?.[p]??null);i.q1.push(null),i.median.push(null),i.q3.push(null),i.lowerWhisker.push(null),i.upperWhisker.push(null),i.count.push(null),i.mean.push(null),i.notchLower.push(null),i.notchUpper.push(null),i.outlier.push(v)}const m=e.getValueFormat(r),k=Object.fromEntries(l.map(p=>[p,{type:e.getType(p),values:c.get(p)??[],valueFormat:e.getValueFormat(p)}]));for(const[p,v]of Object.entries(i)){const w=x[p];k[w]=p==="count"?{type:"numeric",values:v}:{type:"numeric",values:v,valueFormat:m}}return new s.Dataset(k)},ce=(e,t,r,o)=>{const a=[t,...o].map(u=>e.getValues(u)),n=e.getValues(r,{type:"numeric"}),l=new Map;return n.forEach((u,c)=>{if(u===null||!Number.isFinite(u))return;const i=a.map(m=>N(m[c]??null)).join("\0"),d=l.get(i)??{indices:[],values:[]};d.indices.push(c),d.values.push(u),l.set(i,d)}),[...l.values()]},he=(e,t)=>{const r=e[t[0]??-1]??null,o=N(r);return t.every(a=>N(e[a]??null)===o)?r:null},N=e=>e===null?"\0null":e instanceof Date?`date:${e.getTime()}`:`${typeof e}:${String(e)}`,Q=.6,de=1.5,pe=.3,me=2,A=1.5,ve=6,ge=1.5,fe=7,ke=1.5,ye=["q1","median","q3"],xe={strokeWidth:"pixels"},q={stroke:"color",alpha:"unitInterval",strokeWidth:"pixels",dashArray:"dashArray"},D={fill:"color",fillAlpha:"unitInterval",stroke:"color",alpha:"unitInterval",size:"pixels",strokeWidth:"pixels"},Z=class extends s.Geom{constructor(...e){super(...e),this.type="boxplot",this.defaultParams={extent:b,outliers:!0,notch:!1,showMean:!1,varwidth:!1},this.defaultStat=new ie,this.derivedVariables=Object.values(x),this.positionRoles=[{axis:"x",role:"point",valueKind:"value"},{axis:"y",role:"min",valueKind:"value",aes:"lowerWhisker"},{axis:"y",role:"max",valueKind:"value",aes:"upperWhisker"},{axis:"y",role:"scalar",valueKind:"value",aes:"q1"},{axis:"y",role:"scalar",valueKind:"value",aes:"median"},{axis:"y",role:"scalar",valueKind:"value",aes:"q3"},{axis:"y",role:"scalar",valueKind:"value",aes:"outlier"},{axis:"y",role:"scalar",valueKind:"value",aes:"mean"},{axis:"y",role:"scalar",valueKind:"value",aes:"notchLower"},{axis:"y",role:"scalar",valueKind:"value",aes:"notchUpper"}],this.aesthetics=[{kind:"visual",name:"color"},{kind:"data",name:"y"},{kind:"data",name:"count"}],this.defaultPosition="dodge",this.supportedPositions=["identity","dodge"],this.supportedCoordTypes=["cartesian","flip"],this.scaleConstraints={discreteMainAxis:!0},this.legend={suppressWhenSingleItem:!0},this.spatialKind="rects",this.highlightStrategy="observation-rerender",this.tooltip=[{key:"Upper whisker",aes:"upperWhisker",optional:!0},{key:"Q3",aes:"q3",optional:!0},{key:"Median",aes:"median",optional:!0},{key:"Q1",aes:"q1",optional:!0},{key:"Lower whisker",aes:"lowerWhisker",optional:!0},{key:"Count",aes:"count",optional:!0},{key:"Mean",aes:"mean",optional:!0},{key:"Outlier",aes:"outlier",optional:!0}],this.styleTarget=(0,s.defineGeomStyleTarget)({observation:{vocabulary:xe,aesthetics:{fill:"color",stroke:"color"},rest:{fill:(0,s.token)("geom"),fillAlpha:pe,stroke:(0,s.token)("geom"),strokeWidth:de}},parts:{median:{vocabulary:q,aesthetics:{stroke:"color"},rest:{stroke:(0,s.token)("geom"),alpha:1,strokeWidth:me,dashArray:[]}},whisker:{vocabulary:q,aesthetics:{stroke:"color"},rest:{stroke:(0,s.token)("geom"),alpha:1,strokeWidth:A,dashArray:[]}},cap:{vocabulary:q,aesthetics:{stroke:"color"},rest:{stroke:(0,s.token)("geom"),alpha:1,strokeWidth:A,dashArray:[]}},outlier:{vocabulary:D,aesthetics:{fill:"color",stroke:"color"},rest:{fill:(0,s.token)("geom"),fillAlpha:0,stroke:(0,s.token)("geom"),alpha:1,size:ve,strokeWidth:ge}},mean:{vocabulary:D,aesthetics:{fill:"color"},rest:{fill:(0,s.token)("geom"),fillAlpha:1,stroke:(0,s.token)("graphBackground"),alpha:1,size:fe,strokeWidth:ke}}}}),this.resolveBandFraction=t=>R(t),this.resolveValueSource=t=>t.median??{variable:x.median},this.validateMapping=({mapping:t,computedVariables:r})=>r.has("y")?[{code:"CONFLICTING_STAT_MAPPING",message:"boxplot geom draws a summary, not a single 'y'; use the 'boxplot' stat or 'identity'"}]:r.has("median")?[...[...z].filter(o=>t[o]!==void 0).map(o=>({code:"CONFLICTING_STAT_MAPPING",message:`boxplot stat computes '${o}' from 'y'; remove its mapping, or set the layer's stat to 'identity'`})),...t.y===void 0?[{code:"MISSING_AESTHETIC",message:"boxplot stat summarises 'y'; map it to the raw values"}]:[]]:ye.filter(o=>t[o]===void 0).map(o=>({code:"MISSING_AESTHETIC",message:`boxplot geom requires '${o}' when its data is already summarised`,context:{aesthetic:o}})),this.resolveAnchorPosition=(t,{coordSystem:r})=>{const o=(0,s.readMainAxis)(r),a=we(t,o);return a===null?null:o==="x"?{x:a.band,y:a.value}:{x:a.value,y:a.band}}}resolveParams(e){const{extent:t,width:r,...o}=super.resolveParams(e);return{...o,extent:this.resolveExtent(t,e.diagnostics),...this.resolveWidth(r,e.diagnostics)}}compile({data:e,mapping:t,params:r}){const o=P(e,t.lowerWhisker),a=P(e,t.upperWhisker);if(o===null||a===null)return{data:e,mapping:{}};const{outliers:n,notch:l,showMean:u,varwidth:c}=r,i=P(e,t.outlier),d=n===!1&&i!==null?Se(e,i):e,m=P(d,t.count),k=R(r)/2;return{data:d.addVariable(x.relativeWidth,"numeric",c===!0&&m!==null?Me(d,m):We(d)).addVariable(s.POSITION_VARIABLES.yMin,"numeric",K(d,o,i)).addVariable(s.POSITION_VARIABLES.yMax,"numeric",K(d,a,i)).addConstantVariable(s.POSITION_VARIABLES.xMin,"numeric",-k).addConstantVariable(s.POSITION_VARIABLES.xMax,"numeric",k),mapping:{...t.y===void 0&&{y:t.median},...u===!0&&t.mean===void 0&&d.hasVariable(x.mean)&&{mean:x.mean},...l===!0&&t.notchLower===void 0&&d.hasVariable(x.notchLower)&&{notchLower:x.notchLower,notchUpper:x.notchUpper}}}}resolveExtent(e,t){return G(e)?e:(t?.addWarning({code:"INVALID_GEOM_PARAM",message:`Boxplot extent must be a non-negative number or 'min-max', got ${String(e)}; falling back to ${b}.`,context:{geom:this.type,param:"extent",expected:"extent >= 0 or 'min-max'",actual:String(e)},suggestion:"Set params.extent to a number of interquartile ranges, or 'min-max'."}),b)}resolveWidth(e,t){if(e===void 0)return{};const r=typeof e=="number"&&Number.isFinite(e)?e:null;if(r!==null&&r>0&&r<=1)return{width:r};const o=r!==null&&r>1?1:void 0;return t?.addWarning({code:"INVALID_GEOM_PARAM",message:`Boxplot width must be a fraction in (0, 1], got ${String(e)}; falling back to ${o??Q}.`,context:{geom:this.type,param:"width",expected:"0 < width <= 1",actual:r??String(e)},suggestion:"Set params.width to a fraction of the category band in (0, 1]."}),o!==void 0?{width:o}:{}}},we=(e,t)=>{const r=H(e,t);if(r!==null)return{band:_(r),value:r.median};const o=F(e,t);return o===null?null:{band:o.band,value:o.value}},R=({width:e})=>e??Q,Me=(e,t)=>{const r=e.getValues(t,{type:"numeric"}),o=Math.max(0,...r.map(a=>a??0));return r.map(a=>a===null||o===0?null:Math.sqrt(a/o))},We=e=>Array.from({length:e.size()},()=>null),K=(e,t,r)=>{const o=r===null?[]:e.getValues(r,{type:"numeric"});return e.getValues(t,{type:"numeric"}).map((a,n)=>a??o[n]??null)},Se=(e,t)=>{const r=e.getValues(t,{type:"numeric"});return e.take(r.flatMap((o,a)=>o===null?[a]:[]))},P=(e,t)=>{const r=(0,s.readVariableName)(t);return r!==null&&e.hasVariable(r)?r:null},Oe=5e-4,Ie=.5,Pe=.5,J=(e,t)=>{const r=(e.q1+e.q3)/2,o=Math.max(Math.abs(e.q3-e.q1),Oe)/2,a=g(t,e.start,r-o),n=g(t,e.end,r+o),[l,u]=U(a.x,n.x),[c,i]=U(a.y,n.y);return{x:l,y:c,width:u-l,height:i-c}},ee=(e,t)=>{if(e.notch===null)return null;const{lower:r,upper:o}=e.notch,[a,n]=re(e);return[g(t,e.start,e.q1),g(t,e.start,r),g(t,a,e.median),g(t,e.start,o),g(t,e.start,e.q3),g(t,e.end,e.q3),g(t,e.end,o),g(t,n,e.median),g(t,e.end,r),g(t,e.end,e.q1)]},be=(e,t)=>{const[r,o]=e.notch===null?[e.start,e.end]:re(e);return{from:g(t,r,e.median),to:g(t,o,e.median)}},te=(e,t)=>e.mean===null?null:g(t,_(e),e.mean),re=e=>{const t=(e.end-e.start)*(1-Pe)/2;return[e.start+t,e.end-t]},oe=(e,t)=>{const r=_(e),o=(e.end-e.start)*Ie/2;return[{side:"lower",quartile:e.q1,reach:e.lowerWhisker},{side:"upper",quartile:e.q3,reach:e.upperWhisker}].filter(({quartile:a,reach:n})=>a!==n).map(({side:a,quartile:n,reach:l})=>({side:a,stem:{from:g(t,r,n),to:g(t,r,l)},cap:{from:g(t,r-o,l),to:g(t,r+o,l)}}))},g=(e,t,r)=>{const o=(0,s.composePoint)(e,{main:t,cross:r});return{x:(0,s.toViewBoxX)(o.x),y:(0,s.toViewBoxY)(o.y)}},U=(e,t)=>e<=t?[e,t]:[t,e],I=(e,t)=>({x:e.x*t.width,y:e.y*t.height}),V=(e,t)=>`${e.map((r,o)=>{const{x:a,y:n}=I(r,t);return`${o===0?"M":"L"}${a},${n}`}).join("")}Z`,_e=(e,t)=>V([{x:e.x,y:e.y},{x:e.x+e.width,y:e.y},{x:e.x+e.width,y:e.y+e.height},{x:e.x,y:e.y+e.height}],t),Te=(e,t)=>{const r=I(e.from,t),o=I(e.to,t);return`M${r.x},${r.y}L${o.x},${o.y}`},Ee=({layer:e,coordSystem:t,panelRect:r,styleReaders:o})=>{const a=(0,s.readMainAxis)(t),{boxes:n,outliers:l}=S(e.data,a),u=n.map(h=>{const y=ee(h,a);return y!==null?{kind:"region",pathData:V(y,r)}:{kind:"region",pathData:_e(J(h,a),r)}}),c=o.part("whisker"),i=o.part("cap"),d=n.flatMap(h=>oe(h,a).flatMap(y=>[{segment:y.stem,readers:c},{segment:y.cap,readers:i}].flatMap(({segment:M,readers:T})=>{const O=T?.get("strokeWidth",h.observation)??0;return O>0?[{kind:"stroke",pathData:Te(M,r),width:O}]:[]}))),m=o.part("outlier"),k=m===void 0?[]:l.map(h=>({center:I(g(a,h.band,h.value),r),size:j(m,h.observation)})),p=o.part("mean"),v=p===void 0?[]:n.flatMap(h=>{const y=te(h,a);return y===null?[]:[{center:I(y,r),size:j(p,h.observation)}]}),w=(0,s.buildDotsEditOutlineShapes)([...k,...v]);return[...d,...u,...w]},j=(e,t)=>(e.get("size",t)??0)+(e.get("strokeWidth",t)??0),L=e=>{const t=(0,s.createStableKeyGenerator)(e.data,e.mapping,e.id),r=(0,s.readVariableName)(e.mapping.outlier),o=a=>`${t(a.observation)}-outlier-${String(r===null?a.value:a.observation[r])}`;return{readBoxKey:t,readOutlierKeys:a=>{const n=new Map;return a.map(l=>{const u=o(l),c=n.get(u)??0;return n.set(u,c+1),c===0?u:`${u}-${c}`})}}},Be=(e,t,r)=>{const o=e.data.getVariableNames(),a=l=>JSON.stringify(o.map(u=>l.observation[u]??null)),n=new Set(r.map(a));return t.filter(l=>n.has(a(l)))},ae=(e,t,r)=>{const o=new Set(t.map(a=>r(a.observation)));return e.filter(a=>o.has(r(a.observation)))},C=({boxes:e,outliers:t,mainAxis:r,panelSize:o,source:a,styleReaders:n,keys:l,isHovered:u=!1,shouldAnimateTransitions:c=!1})=>{const i=u?"hovered":void 0,d=n.part("whisker"),m=n.part("cap"),k=n.part("median"),p=n.part("mean"),v=n.part("outlier"),w=l.readOutlierKeys(t);return(0,f.jsxs)(f.Fragment,{children:[e.flatMap(h=>oe(h,r).flatMap(y=>[{piece:"stem",testId:"boxplot-whisker",readers:d},{piece:"cap",testId:"boxplot-cap",readers:m}].map(({piece:M,testId:T,readers:O})=>O===void 0?null:(0,f.jsx)($,{testId:T,segment:y[M],observation:h.observation,readers:O,state:i,shouldAnimateTransitions:c},`${l.readBoxKey(h.observation)}-${y.side}-${M}`)))),e.map(h=>(0,f.jsx)(qe,{box:h,mainAxis:r,panelSize:o,source:a,styleReaders:n,state:i,shouldAnimateTransitions:c},l.readBoxKey(h.observation))),k&&e.map(h=>(0,f.jsx)($,{testId:"boxplot-median",segment:be(h,r),observation:h.observation,readers:k,state:i,shouldAnimateTransitions:c},l.readBoxKey(h.observation))),p&&e.map(h=>{const y=te(h,r);return y===null?null:(0,f.jsx)(X,{testId:"boxplot-mean",centre:y,observation:h.observation,readers:p,state:i,shouldAnimateTransitions:c},l.readBoxKey(h.observation))}),v&&t.map((h,y)=>(0,f.jsx)(X,{testId:"boxplot-outlier",centre:g(r,h.band,h.value),observation:h.observation,readers:v,state:i,shouldAnimateTransitions:c},w[y]))]})},$=({testId:e,segment:t,observation:r,readers:o,state:a,shouldAnimateTransitions:n})=>{const l=o.get("strokeWidth",r,a)??0,u=o.get("stroke",r,a);return(0,f.jsx)(s.GeomLine,{"data-testid":e,x1:(0,s.toPercent)(t.from.x),y1:(0,s.toPercent)(t.from.y),x2:(0,s.toPercent)(t.to.x),y2:(0,s.toPercent)(t.to.y),stroke:u===void 0?void 0:(0,s.toPaintColor)(u),strokeOpacity:o.get("alpha",r,a),strokeWidth:l,strokeDasharray:(0,s.toStrokeDasharray)(o.get("dashArray",r,a),l),shouldAnimateTransitions:n})},qe=({box:e,mainAxis:t,panelSize:r,source:o,styleReaders:a,state:n,shouldAnimateTransitions:l})=>{const u=a.get("fill",e.observation,n),c=a.get("stroke",e.observation,n),i={fill:u===void 0?void 0:(0,s.toPaintColor)(u),opacity:a.get("alpha",e.observation,n),fillOpacity:a.get("fillAlpha",e.observation,n),stroke:c===void 0?void 0:(0,s.toPaintColor)(c),strokeOpacity:a.get("strokeAlpha",e.observation,n),strokeWidth:a.get("strokeWidth",e.observation,n),shouldAnimateTransitions:l},d=ee(e,t);if(d!==null)return(0,f.jsx)(s.GeomPath,{"data-testid":"boxplot-box",d:V(d,r),source:o,...i});const m=J(e,t);return(0,f.jsx)(s.GeomRect,{"data-testid":"boxplot-box",x:(0,s.toPercent)(m.x),y:(0,s.toPercent)(m.y),width:(0,s.toPercent)(m.width),height:(0,s.toPercent)(m.height),...i})},X=({testId:e,centre:t,observation:r,readers:o,state:a,shouldAnimateTransitions:n})=>{const l=o.get("fill",r,a),u=o.get("stroke",r,a);return(0,f.jsx)(s.GeomCircle,{"data-testid":e,cx:(0,s.toPercent)(t.x),cy:(0,s.toPercent)(t.y),r:(o.get("size",r,a)??0)/2,fill:l===void 0?void 0:(0,s.toPaintColor)(l),opacity:o.get("alpha",r,a),fillOpacity:o.get("fillAlpha",r,a),stroke:u===void 0?void 0:(0,s.toPaintColor)(u),strokeWidth:o.get("strokeWidth",r,a),shouldAnimateTransitions:n})},Ne=({layer:e,sourceLayer:t,mainAxis:r,panelSize:o,styleReaders:a})=>{const n=(0,W.useMemo)(()=>L(t),[t]),l=(0,W.useMemo)(()=>S(t.data,r).outliers,[t.data,r]),{boxes:u,outliers:c}=(0,W.useMemo)(()=>{const i=S(e.data,r),d=new Set(Be(t,l,i.outliers)),m=new Set(ae(l,i.boxes,n.readBoxKey)),k=l.filter(p=>m.has(p)||d.has(p));return{boxes:i.boxes,outliers:k}},[e.data,r,t,l,n]);return(0,f.jsx)(C,{boxes:u,outliers:c,mainAxis:r,panelSize:o,source:t,styleReaders:a,keys:n})},Ve=({layer:e,primary:t,mainAxis:r,panelSize:o,styleReaders:a})=>{const n=(0,W.useMemo)(()=>S(e.data,r).outliers,[e.data,r]),l=(0,W.useMemo)(()=>L(e),[e]),{boxes:u,outliers:c}=S([t.observation],r);return(0,f.jsx)("g",{"data-testid":"hovered-boxes",children:(0,f.jsx)(C,{boxes:u,outliers:u.length>0?ae(n,u,l.readBoxKey):c,mainAxis:r,panelSize:o,source:e,styleReaders:a,keys:l,isHovered:!0})})},Le=({layer:e,mainAxis:t,panelSize:r,styleReaders:o,shouldAnimateTransitions:a,intro:n})=>{const{boxes:l,outliers:u}=(0,W.useMemo)(()=>S(e.data,t),[e.data,t]),c=(0,f.jsx)(C,{boxes:l,outliers:u,mainAxis:t,panelSize:r,source:e,styleReaders:o,keys:(0,W.useMemo)(()=>L(e),[e]),shouldAnimateTransitions:a});return n==null?c:(0,f.jsx)(s.IntroFade,{intro:{type:"fade",layerId:n.layerId,durationSeconds:n.durationSeconds},children:c})},Ce=(0,s.defineGeomRenderer)(new Z,{coord:"cartesian",swatchShape:"square",guideMode:"band",render:({layer:e,coordSystem:t,panelRect:r,styleReaders:o,shouldAnimateTransitions:a,intro:n})=>(0,f.jsx)(Le,{layer:e,mainAxis:(0,s.readMainAxis)(t),panelSize:r,styleReaders:o,shouldAnimateTransitions:a,intro:n}),renderHighlight:({layer:e,sourceLayer:t,coordSystem:r,panelRect:o,styleReaders:a})=>(0,f.jsx)(Ne,{layer:e,sourceLayer:t,mainAxis:(0,s.readMainAxis)(r),panelSize:o,styleReaders:a}),renderHover:({layer:e,primary:t,coordSystem:r,panelRect:o,styleReaders:a})=>(0,f.jsx)(Ve,{layer:e,primary:t,mainAxis:(0,s.readMainAxis)(r),panelSize:o,styleReaders:a}),renderHoverCompanions:()=>null,getEditOutlineShapes:Ee});exports.BOXPLOT_COLUMNS=x;exports.BoxplotGeom=Z;exports.boxplot=Ce;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let s=require("@graphysdk/react"),W=require("react"),f=require("react/jsx-runtime");var x={q1:"boxplotQ1",median:"boxplotMedian",q3:"boxplotQ3",lowerWhisker:"boxplotLowerWhisker",upperWhisker:"boxplotUpperWhisker",count:"boxplotCount",mean:"boxplotMean",notchLower:"boxplotNotchLower",notchUpper:"boxplotNotchUpper",relativeWidth:"boxplotRelativeWidth",outlier:"boxplotOutlier"},_=e=>(e.start+e.end)/2,H=(e,t)=>{const r=t==="x"?(0,s.getXMin)(e):(0,s.getYMin)(e),o=t==="x"?(0,s.getXMax)(e):(0,s.getYMax)(e),a=t==="x"?(0,s.getYMin)(e):(0,s.getXMin)(e),n=t==="x"?(0,s.getYMax)(e):(0,s.getXMax)(e),l=(0,s.getScaledAesthetic)(e,"q1"),u=(0,s.getScaledAesthetic)(e,"median"),c=(0,s.getScaledAesthetic)(e,"q3");if(r===null||o===null||a===null||n===null||u===null||l===null||c===null)return null;const i=T(e,"notchLower"),d=T(e,"notchUpper"),[m,k]=ne(r,o,e);return{observation:e,start:m,end:k,lowerWhisker:a,q1:l,median:u,q3:c,upperWhisker:n,mean:T(e,"mean"),notch:i===null||d===null?null:{lower:i,upper:d}}},F=(e,t)=>{const r=t==="x"?(0,s.getXMin)(e):(0,s.getYMin)(e),o=t==="x"?(0,s.getXMax)(e):(0,s.getYMax)(e),a=(0,s.getScaledAesthetic)(e,"outlier");return r===null||o===null||a===null?null:{observation:e,band:_({start:r,end:o}),value:a}},S=(e,t)=>{const r=[],o=[];for(const a of e){const n=H(a,t);if(n!==null){r.push(n);continue}const l=F(a,t);l!==null&&o.push(l)}return{boxes:r,outliers:o}},T=(e,t)=>(0,s.getScaledAesthetic)(e,t)??null,ne=(e,t,r)=>{const o=r[x.relativeWidth];if(typeof o!="number")return[e,t];const a=(e+t)/2,n=(t-e)*o/2;return[a-n,a+n]},b=1.5,G=e=>e==="min-max"||typeof e=="number"&&Number.isFinite(e)&&e>=0,se=1.58,le=(e,t)=>{if(e.length===0)return null;const r=[...e].sort((m,k)=>m-k),o=B(r,.25),a=B(r,.5),n=B(r,.75),l=t==="min-max"?1/0:t*(n-o),u=o-l,c=n+l,i=r.filter(m=>m>=u&&m<=c),d=se*(n-o)/Math.sqrt(e.length);return{lowerWhisker:Math.min(i[0]??o,o),q1:o,median:a,q3:n,upperWhisker:Math.max(i[i.length-1]??n,n),count:e.length,mean:r.reduce((m,k)=>m+k,0)/r.length,notchLower:a-d,notchUpper:a+d}},B=(e,t)=>{const r=(e.length-1)*t,o=Math.floor(r),a=e[o]??0;return a+((e[o+1]??a)-a)*(r-o)},Y={q1:x.q1,median:x.median,q3:x.q3,lowerWhisker:x.lowerWhisker,upperWhisker:x.upperWhisker,count:x.count,outlier:x.outlier},z=new Set([...Object.keys(Y),"mean","notchLower","notchUpper"]),ie=class extends s.Stat{constructor(...e){super(...e),this.type="boxplot",this.computedVariables=z}computeStat({data:e,mapping:t,params:r}){const o=(0,s.readVariableName)(t.x),a=(0,s.readVariableName)(t.y);if(o===null||a===null||!e.hasVariable(o)||!e.hasVariable(a))return{data:e,mapping:{}};if(e.size()>0&&e.getType(a)!=="numeric"){const l=e.getType(a);throw new s.UserInputError({code:"INCOMPATIBLE_TYPE",message:`'boxplot' stat requires a numeric 'y' (got '${l}' for variable '${a}')`,context:{aesthetic:"y",variableName:a,variableType:l},suggestion:"Map the raw values to 'y', or summarised columns under stat 'identity'."})}const n=r?.extent;return{data:ue({data:e,xVariable:o,yVariable:a,groupVariables:(0,s.getGroupVariables)(e,t).filter(l=>l!==o),extent:G(n)?n:b}),mapping:Y}}},ue=({data:e,xVariable:t,yVariable:r,groupVariables:o,extent:a})=>{const n=ce(e,t,r,o),l=e.getVariableNames(),u=new Map(l.map(p=>[p,e.getValues(p)])),c=new Map(l.map(p=>[p,[]])),i={q1:[],median:[],q3:[],lowerWhisker:[],upperWhisker:[],count:[],mean:[],notchLower:[],notchUpper:[],outlier:[]},d=[];for(const p of n){const v=le(p.values,a);if(v===null)continue;for(const h of l)c.get(h)?.push(he(u.get(h)??[],p.indices));i.q1.push(v.q1),i.median.push(v.median),i.q3.push(v.q3),i.lowerWhisker.push(v.lowerWhisker),i.upperWhisker.push(v.upperWhisker),i.count.push(v.count),i.mean.push(v.mean),i.notchLower.push(v.notchLower),i.notchUpper.push(v.notchUpper),i.outlier.push(null);const w={low:v.lowerWhisker,high:v.upperWhisker};p.indices.forEach((h,y)=>{const M=p.values[y];M!==void 0&&(M<w.low||M>w.high)&&d.push({index:h,value:M})})}for(const{index:p,value:v}of d){for(const w of l)c.get(w)?.push(u.get(w)?.[p]??null);i.q1.push(null),i.median.push(null),i.q3.push(null),i.lowerWhisker.push(null),i.upperWhisker.push(null),i.count.push(null),i.mean.push(null),i.notchLower.push(null),i.notchUpper.push(null),i.outlier.push(v)}const m=e.getValueFormat(r),k=Object.fromEntries(l.map(p=>[p,{type:e.getType(p),values:c.get(p)??[],valueFormat:e.getValueFormat(p)}]));for(const[p,v]of Object.entries(i)){const w=x[p];k[w]=p==="count"?{type:"numeric",values:v}:{type:"numeric",values:v,valueFormat:m}}return new s.Dataset(k)},ce=(e,t,r,o)=>{const a=[t,...o].map(u=>e.getValues(u)),n=e.getValues(r,{type:"numeric"}),l=new Map;return n.forEach((u,c)=>{if(u===null||!Number.isFinite(u))return;const i=a.map(m=>V(m[c]??null)).join("\0"),d=l.get(i)??{indices:[],values:[]};d.indices.push(c),d.values.push(u),l.set(i,d)}),[...l.values()]},he=(e,t)=>{const r=e[t[0]??-1]??null,o=V(r);return t.every(a=>V(e[a]??null)===o)?r:null},V=e=>e===null?"\0null":e instanceof Date?`date:${e.getTime()}`:`${typeof e}:${String(e)}`,Q=.6,de=1.5,pe=.3,me=2,C=1.5,ve=6,ge=1.5,fe=7,ke=1.5,ye=["q1","median","q3"],xe={strokeWidth:"pixels"},q={stroke:"color",alpha:"unitInterval",strokeWidth:"pixels",dashArray:"dashArray"},R={fill:"color",fillAlpha:"unitInterval",stroke:"color",alpha:"unitInterval",size:"pixels",strokeWidth:"pixels"},Z=class extends s.Geom{constructor(...e){super(...e),this.type="boxplot",this.defaultParams={extent:b,outliers:!0,notch:!1,showMean:!1,varwidth:!1},this.defaultStat=new ie,this.derivedVariables=Object.values(x),this.positionRoles=[{axis:"x",role:"point",valueKind:"value"},{axis:"y",role:"min",valueKind:"value",aes:"lowerWhisker"},{axis:"y",role:"max",valueKind:"value",aes:"upperWhisker"},{axis:"y",role:"scalar",valueKind:"value",aes:"q1"},{axis:"y",role:"scalar",valueKind:"value",aes:"median"},{axis:"y",role:"scalar",valueKind:"value",aes:"q3"},{axis:"y",role:"scalar",valueKind:"value",aes:"outlier"},{axis:"y",role:"scalar",valueKind:"value",aes:"mean"},{axis:"y",role:"scalar",valueKind:"value",aes:"notchLower"},{axis:"y",role:"scalar",valueKind:"value",aes:"notchUpper"}],this.aesthetics=[{kind:"visual",name:"color"},{kind:"data",name:"y"},{kind:"data",name:"count"}],this.defaultPosition="dodge",this.supportedPositions=["identity","dodge"],this.supportedCoordTypes=["cartesian","flip"],this.scaleConstraints={discreteMainAxis:!0},this.legend={suppressWhenSingleItem:!0},this.spatialKind="rects",this.highlightStrategy="observation-rerender",this.tooltip=[{key:"Upper whisker",aes:"upperWhisker",optional:!0},{key:"Q3",aes:"q3",optional:!0},{key:"Median",aes:"median",optional:!0},{key:"Q1",aes:"q1",optional:!0},{key:"Lower whisker",aes:"lowerWhisker",optional:!0},{key:"Count",aes:"count",optional:!0},{key:"Mean",aes:"mean",optional:!0},{key:"Outlier",aes:"outlier",optional:!0}],this.styleTarget=(0,s.defineGeomStyleTarget)({observation:{vocabulary:xe,aesthetics:{fill:"color",stroke:"color"},rest:{fill:(0,s.token)("geom"),fillAlpha:pe,stroke:(0,s.token)("geom"),strokeWidth:de}},parts:{median:{vocabulary:q,aesthetics:{stroke:"color"},rest:{stroke:(0,s.token)("geom"),alpha:1,strokeWidth:me,dashArray:[]}},whisker:{vocabulary:q,aesthetics:{stroke:"color"},rest:{stroke:(0,s.token)("geom"),alpha:1,strokeWidth:C,dashArray:[]}},cap:{vocabulary:q,aesthetics:{stroke:"color"},rest:{stroke:(0,s.token)("geom"),alpha:1,strokeWidth:C,dashArray:[]}},outlier:{vocabulary:R,aesthetics:{fill:"color",stroke:"color"},rest:{fill:(0,s.token)("geom"),fillAlpha:0,stroke:(0,s.token)("geom"),alpha:1,size:ve,strokeWidth:ge}},mean:{vocabulary:R,aesthetics:{fill:"color"},rest:{fill:(0,s.token)("geom"),fillAlpha:1,stroke:(0,s.token)("graphBackground"),alpha:1,size:fe,strokeWidth:ke}}}}),this.resolveBandFraction=t=>D(t),this.resolveValueSource=t=>t.median??{variable:x.median},this.validateMapping=({mapping:t,computedVariables:r})=>r.has("y")?[{code:"CONFLICTING_STAT_MAPPING",message:"boxplot geom draws a summary, not a single 'y'; use the 'boxplot' stat or 'identity'"}]:r.has("median")?[...[...z].filter(o=>t[o]!==void 0).map(o=>({code:"CONFLICTING_STAT_MAPPING",message:`boxplot stat computes '${o}' from 'y'; remove its mapping, or set the layer's stat to 'identity'`})),...t.y===void 0?[{code:"MISSING_AESTHETIC",message:"boxplot stat summarises 'y'; map it to the raw values"}]:[]]:ye.filter(o=>t[o]===void 0).map(o=>({code:"MISSING_AESTHETIC",message:`boxplot geom requires '${o}' when its data is already summarised`,context:{aesthetic:o}})),this.resolveAnchorPosition=(t,{coordSystem:r})=>{const o=(0,s.readMainAxis)(r),a=we(t,o);return a===null?null:o==="x"?{x:a.band,y:a.value}:{x:a.value,y:a.band}}}resolveParams(e){const{extent:t,width:r,...o}=super.resolveParams(e);return{...o,extent:this.resolveExtent(t,e.diagnostics),...this.resolveWidth(r,e.diagnostics)}}compile({data:e,mapping:t,params:r}){const o=P(e,t.lowerWhisker),a=P(e,t.upperWhisker);if(o===null||a===null)return{data:e,mapping:{}};const{outliers:n,notch:l,showMean:u,varwidth:c}=r,i=P(e,t.outlier),d=n===!1&&i!==null?Se(e,i):e,m=P(d,t.count),k=D(r)/2;return{data:d.addVariable(x.relativeWidth,"numeric",c===!0&&m!==null?Me(d,m):We(d)).addVariable(s.POSITION_VARIABLES.yMin,"numeric",K(d,o,i)).addVariable(s.POSITION_VARIABLES.yMax,"numeric",K(d,a,i)).addConstantVariable(s.POSITION_VARIABLES.xMin,"numeric",-k).addConstantVariable(s.POSITION_VARIABLES.xMax,"numeric",k),mapping:{...t.y===void 0&&{y:t.median},...u===!0&&t.mean===void 0&&d.hasVariable(x.mean)&&{mean:x.mean},...l===!0&&t.notchLower===void 0&&d.hasVariable(x.notchLower)&&{notchLower:x.notchLower,notchUpper:x.notchUpper}}}}resolveExtent(e,t){return G(e)?e:(t?.addWarning({code:"INVALID_GEOM_PARAM",message:`Boxplot extent must be a non-negative number or 'min-max', got ${String(e)}; falling back to ${b}.`,context:{geom:this.type,param:"extent",expected:"extent >= 0 or 'min-max'",actual:String(e)},suggestion:"Set params.extent to a number of interquartile ranges, or 'min-max'."}),b)}resolveWidth(e,t){if(e===void 0)return{};const r=typeof e=="number"&&Number.isFinite(e)?e:null;if(r!==null&&r>0&&r<=1)return{width:r};const o=r!==null&&r>1?1:void 0;return t?.addWarning({code:"INVALID_GEOM_PARAM",message:`Boxplot width must be a fraction in (0, 1], got ${String(e)}; falling back to ${o??Q}.`,context:{geom:this.type,param:"width",expected:"0 < width <= 1",actual:r??String(e)},suggestion:"Set params.width to a fraction of the category band in (0, 1]."}),o!==void 0?{width:o}:{}}},we=(e,t)=>{const r=H(e,t);if(r!==null)return{band:_(r),value:r.median};const o=F(e,t);return o===null?null:{band:o.band,value:o.value}},D=({width:e})=>e??Q,Me=(e,t)=>{const r=e.getValues(t,{type:"numeric"}),o=Math.max(0,...r.map(a=>a??0));return r.map(a=>a===null||o===0?null:Math.sqrt(a/o))},We=e=>Array.from({length:e.size()},()=>null),K=(e,t,r)=>{const o=r===null?[]:e.getValues(r,{type:"numeric"});return e.getValues(t,{type:"numeric"}).map((a,n)=>a??o[n]??null)},Se=(e,t)=>{const r=e.getValues(t,{type:"numeric"});return e.take(r.flatMap((o,a)=>o===null?[a]:[]))},P=(e,t)=>{const r=(0,s.readVariableName)(t);return r!==null&&e.hasVariable(r)?r:null},Ie=5e-4,Oe=.5,Pe=.5,J=(e,t)=>{const r=(e.q1+e.q3)/2,o=Math.max(Math.abs(e.q3-e.q1),Ie)/2,a=g(t,e.start,r-o),n=g(t,e.end,r+o),[l,u]=j(a.x,n.x),[c,i]=j(a.y,n.y);return{x:l,y:c,width:u-l,height:i-c}},ee=(e,t)=>{if(e.notch===null)return null;const{lower:r,upper:o}=e.notch,[a,n]=re(e);return[g(t,e.start,e.q1),g(t,e.start,r),g(t,a,e.median),g(t,e.start,o),g(t,e.start,e.q3),g(t,e.end,e.q3),g(t,e.end,o),g(t,n,e.median),g(t,e.end,r),g(t,e.end,e.q1)]},be=(e,t)=>{const[r,o]=e.notch===null?[e.start,e.end]:re(e);return{from:g(t,r,e.median),to:g(t,o,e.median)}},te=(e,t)=>e.mean===null?null:g(t,_(e),e.mean),re=e=>{const t=(e.end-e.start)*(1-Pe)/2;return[e.start+t,e.end-t]},oe=(e,t)=>{const r=_(e),o=(e.end-e.start)*Oe/2;return[{side:"lower",quartile:e.q1,reach:e.lowerWhisker},{side:"upper",quartile:e.q3,reach:e.upperWhisker}].filter(({quartile:a,reach:n})=>a!==n).map(({side:a,quartile:n,reach:l})=>({side:a,stem:{from:g(t,r,n),to:g(t,r,l)},cap:{from:g(t,r-o,l),to:g(t,r+o,l)}}))},g=(e,t,r)=>{const o=(0,s.composePoint)(e,{main:t,cross:r});return{x:(0,s.toViewBoxX)(o.x),y:(0,s.toViewBoxY)(o.y)}},j=(e,t)=>e<=t?[e,t]:[t,e],O=(e,t)=>({x:e.x*t.width,y:e.y*t.height}),N=(e,t)=>`${e.map((r,o)=>{const{x:a,y:n}=O(r,t);return`${o===0?"M":"L"}${a},${n}`}).join("")}Z`,_e=(e,t)=>N([{x:e.x,y:e.y},{x:e.x+e.width,y:e.y},{x:e.x+e.width,y:e.y+e.height},{x:e.x,y:e.y+e.height}],t),Ee=(e,t)=>{const r=O(e.from,t),o=O(e.to,t);return`M${r.x},${r.y}L${o.x},${o.y}`},Te=({layer:e,coordSystem:t,panelRect:r,styleReaders:o})=>{const a=(0,s.readMainAxis)(t),{boxes:n,outliers:l}=S(e.data,a),u=n.map(h=>{const y=ee(h,a);return y!==null?{kind:"region",pathData:N(y,r)}:{kind:"region",pathData:_e(J(h,a),r)}}),c=o.part("whisker"),i=o.part("cap"),d=n.flatMap(h=>oe(h,a).flatMap(y=>[{segment:y.stem,readers:c},{segment:y.cap,readers:i}].flatMap(({segment:M,readers:E})=>{const I=E?.get("strokeWidth",h.observation)??0;return I>0?[{kind:"stroke",pathData:Ee(M,r),width:I}]:[]}))),m=o.part("outlier"),k=m===void 0?[]:l.map(h=>({center:O(g(a,h.band,h.value),r),size:U(m,h.observation)})),p=o.part("mean"),v=p===void 0?[]:n.flatMap(h=>{const y=te(h,a);return y===null?[]:[{center:O(y,r),size:U(p,h.observation)}]}),w=(0,s.buildDotsEditOutlineShapes)([...k,...v]);return[...d,...u,...w]},U=(e,t)=>(e.get("size",t)??0)+(e.get("strokeWidth",t)??0),A=e=>{const t=(0,s.createStableKeyGenerator)(e.data,e.mapping,e.id),r=(0,s.readVariableName)(e.mapping.outlier),o=a=>`${t(a.observation)}-outlier-${String(r===null?a.value:a.observation[r])}`;return{readBoxKey:t,readOutlierKeys:a=>{const n=new Map;return a.map(l=>{const u=o(l),c=n.get(u)??0;return n.set(u,c+1),c===0?u:`${u}-${c}`})}}},Be=(e,t,r)=>{const o=e.data.getVariableNames(),a=l=>JSON.stringify(o.map(u=>l.observation[u]??null)),n=new Set(r.map(a));return t.filter(l=>n.has(a(l)))},ae=(e,t,r)=>{const o=new Set(t.map(a=>r(a.observation)));return e.filter(a=>o.has(r(a.observation)))},L=({boxes:e,outliers:t,mainAxis:r,panelSize:o,source:a,styleReaders:n,keys:l,isHovered:u=!1,shouldAnimateTransitions:c=!1})=>{const i=u?"hovered":void 0,d=n.part("whisker"),m=n.part("cap"),k=n.part("median"),p=n.part("mean"),v=n.part("outlier"),w=l.readOutlierKeys(t);return(0,f.jsxs)(f.Fragment,{children:[e.flatMap(h=>oe(h,r).flatMap(y=>[{piece:"stem",testId:"boxplot-whisker",readers:d},{piece:"cap",testId:"boxplot-cap",readers:m}].map(({piece:M,testId:E,readers:I})=>I===void 0?null:(0,f.jsx)($,{testId:E,segment:y[M],observation:h.observation,readers:I,state:i,shouldAnimateTransitions:c},`${l.readBoxKey(h.observation)}-${y.side}-${M}`)))),e.map(h=>(0,f.jsx)(qe,{box:h,mainAxis:r,panelSize:o,source:a,styleReaders:n,state:i,shouldAnimateTransitions:c},l.readBoxKey(h.observation))),k&&e.map(h=>(0,f.jsx)($,{testId:"boxplot-median",segment:be(h,r),observation:h.observation,readers:k,state:i,shouldAnimateTransitions:c},l.readBoxKey(h.observation))),p&&e.map(h=>{const y=te(h,r);return y===null?null:(0,f.jsx)(X,{testId:"boxplot-mean",centre:y,observation:h.observation,readers:p,state:i,shouldAnimateTransitions:c},l.readBoxKey(h.observation))}),v&&t.map((h,y)=>(0,f.jsx)(X,{testId:"boxplot-outlier",centre:g(r,h.band,h.value),observation:h.observation,readers:v,state:i,shouldAnimateTransitions:c},w[y]))]})},$=({testId:e,segment:t,observation:r,readers:o,state:a,shouldAnimateTransitions:n})=>{const l=o.get("strokeWidth",r,a)??0,u=o.get("stroke",r,a);return(0,f.jsx)(s.GeomLine,{"data-testid":e,x1:(0,s.toPercent)(t.from.x),y1:(0,s.toPercent)(t.from.y),x2:(0,s.toPercent)(t.to.x),y2:(0,s.toPercent)(t.to.y),stroke:u===void 0?void 0:(0,s.toPaintColor)(u),strokeOpacity:o.get("alpha",r,a),strokeWidth:l,strokeDasharray:(0,s.toStrokeDasharray)(o.get("dashArray",r,a),l),shouldAnimateTransitions:n})},qe=({box:e,mainAxis:t,panelSize:r,source:o,styleReaders:a,state:n,shouldAnimateTransitions:l})=>{const u=a.get("fill",e.observation,n),c=a.get("stroke",e.observation,n),i={fill:u===void 0?void 0:(0,s.toPaintColor)(u),opacity:a.get("alpha",e.observation,n),fillOpacity:a.get("fillAlpha",e.observation,n),stroke:c===void 0?void 0:(0,s.toPaintColor)(c),strokeOpacity:a.get("strokeAlpha",e.observation,n),strokeWidth:a.get("strokeWidth",e.observation,n),shouldAnimateTransitions:l},d=ee(e,t);if(d!==null)return(0,f.jsx)(s.GeomPath,{"data-testid":"boxplot-box",d:N(d,r),source:o,...i});const m=J(e,t);return(0,f.jsx)(s.GeomRect,{"data-testid":"boxplot-box",x:(0,s.toPercent)(m.x),y:(0,s.toPercent)(m.y),width:(0,s.toPercent)(m.width),height:(0,s.toPercent)(m.height),...i})},X=({testId:e,centre:t,observation:r,readers:o,state:a,shouldAnimateTransitions:n})=>{const l=o.get("fill",r,a),u=o.get("stroke",r,a);return(0,f.jsx)(s.GeomCircle,{"data-testid":e,cx:(0,s.toPercent)(t.x),cy:(0,s.toPercent)(t.y),r:(o.get("size",r,a)??0)/2,fill:l===void 0?void 0:(0,s.toPaintColor)(l),opacity:o.get("alpha",r,a),fillOpacity:o.get("fillAlpha",r,a),stroke:u===void 0?void 0:(0,s.toPaintColor)(u),strokeWidth:o.get("strokeWidth",r,a),shouldAnimateTransitions:n})},Ve=({layer:e,sourceLayer:t,mainAxis:r,panelSize:o,styleReaders:a})=>{const n=(0,W.useMemo)(()=>A(t),[t]),l=(0,W.useMemo)(()=>S(t.data,r).outliers,[t.data,r]),{boxes:u,outliers:c}=(0,W.useMemo)(()=>{const i=S(e.data,r),d=new Set(Be(t,l,i.outliers)),m=new Set(ae(l,i.boxes,n.readBoxKey)),k=l.filter(p=>m.has(p)||d.has(p));return{boxes:i.boxes,outliers:k}},[e.data,r,t,l,n]);return(0,f.jsx)(L,{boxes:u,outliers:c,mainAxis:r,panelSize:o,source:t,styleReaders:a,keys:n})},Ne=({layer:e,primary:t,mainAxis:r,panelSize:o,styleReaders:a})=>{const n=(0,W.useMemo)(()=>S(e.data,r).outliers,[e.data,r]),l=(0,W.useMemo)(()=>A(e),[e]),{boxes:u,outliers:c}=S([t.observation],r);return(0,f.jsx)("g",{"data-testid":"hovered-boxes",children:(0,f.jsx)(L,{boxes:u,outliers:u.length>0?ae(n,u,l.readBoxKey):c,mainAxis:r,panelSize:o,source:e,styleReaders:a,keys:l,isHovered:!0})})},Ae=({layer:e,mainAxis:t,panelSize:r,styleReaders:o,shouldAnimateTransitions:a,intro:n})=>{const{boxes:l,outliers:u}=(0,W.useMemo)(()=>S(e.data,t),[e.data,t]),c=(0,f.jsx)(L,{boxes:l,outliers:u,mainAxis:t,panelSize:r,source:e,styleReaders:o,keys:(0,W.useMemo)(()=>A(e),[e]),shouldAnimateTransitions:a});return n==null?c:(0,f.jsx)(s.IntroFade,{intro:{type:"fade",layerId:n.layerId,durationSeconds:n.durationSeconds},children:c})},Le=(0,s.defineGeomRenderer)(new Z,{coord:"cartesian",swatchShape:"square",guideMode:"band",render:({layer:e,coordSystem:t,panelRect:r,styleReaders:o,shouldAnimateTransitions:a,intro:n})=>(0,f.jsx)(Ae,{layer:e,mainAxis:(0,s.readMainAxis)(t),panelSize:r,styleReaders:o,shouldAnimateTransitions:a,intro:n}),renderHighlight:({layer:e,sourceLayer:t,coordSystem:r,panelRect:o,styleReaders:a})=>(0,f.jsx)(Ve,{layer:e,sourceLayer:t,mainAxis:(0,s.readMainAxis)(r),panelSize:o,styleReaders:a}),renderHover:({layer:e,primary:t,coordSystem:r,panelRect:o,styleReaders:a})=>(0,f.jsx)(Ne,{layer:e,primary:t,mainAxis:(0,s.readMainAxis)(r),panelSize:o,styleReaders:a}),renderHoverCompanions:()=>null,getEditOutlineShapes:Te});exports.BOXPLOT_VARIABLES=x;exports.BoxplotGeom=Z;exports.boxplot=Le;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { BoxplotGeom } from './engine/boxplot.geom';
2
2
  export { boxplot } from './renderer/boxplot';
3
- export { BOXPLOT_COLUMNS } from './utils/boxplot-columns';
3
+ export { BOXPLOT_VARIABLES } from './utils/boxplot-variables';
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { Dataset as me, Geom as ve, GeomCircle as ge, GeomLine as fe, GeomPath as ye, GeomRect as ke, IntroFade as we, POSITION_VARIABLES as B, Stat as xe, UserInputError as We, buildDotsEditOutlineShapes as Me, composePoint as Se, createStableKeyGenerator as Ie, defineGeomRenderer as Oe, defineGeomStyleTarget as be, getGroupVariables as Ee, getScaledAesthetic as b, getXMax as R, getXMin as U, getYMax as $, getYMin as A, readMainAxis as E, readVariableName as N, toPaintColor as T, toPercent as x, toStrokeDasharray as Te, toViewBoxX as _e, toViewBoxY as Be, token as M } from "@graphysdk/react";
1
+ import { Dataset as me, Geom as ve, GeomCircle as ge, GeomLine as fe, GeomPath as ye, GeomRect as ke, IntroFade as we, POSITION_VARIABLES as B, Stat as xe, UserInputError as We, buildDotsEditOutlineShapes as Me, composePoint as Se, createStableKeyGenerator as Ie, defineGeomRenderer as Oe, defineGeomStyleTarget as be, getGroupVariables as Ee, getScaledAesthetic as b, getXMax as K, getXMin as A, getYMax as U, getYMin as $, readMainAxis as E, readVariableName as V, toPaintColor as T, toPercent as x, toStrokeDasharray as Te, toViewBoxX as _e, toViewBoxY as Be, token as M } from "@graphysdk/react";
2
2
  import { useMemo as S } from "react";
3
3
  import { Fragment as qe, jsx as k, jsxs as Pe } from "react/jsx-runtime";
4
4
  var y = {
@@ -13,10 +13,10 @@ var y = {
13
13
  notchUpper: "boxplotNotchUpper",
14
14
  relativeWidth: "boxplotRelativeWidth",
15
15
  outlier: "boxplotOutlier"
16
- }, V = (e) => (e.start + e.end) / 2, re = (e, t) => {
17
- const r = t === "x" ? U(e) : A(e), o = t === "x" ? R(e) : $(e), n = t === "x" ? A(e) : U(e), a = t === "x" ? $(e) : R(e), s = b(e, "q1"), i = b(e, "median"), u = b(e, "q3");
16
+ }, N = (e) => (e.start + e.end) / 2, re = (e, t) => {
17
+ const r = t === "x" ? A(e) : $(e), o = t === "x" ? K(e) : U(e), n = t === "x" ? $(e) : A(e), a = t === "x" ? U(e) : K(e), s = b(e, "q1"), i = b(e, "median"), u = b(e, "q3");
18
18
  if (r === null || o === null || n === null || a === null || i === null || s === null || u === null) return null;
19
- const l = C(e, "notchLower"), h = C(e, "notchUpper"), [p, g] = Ne(r, o, e);
19
+ const l = C(e, "notchLower"), h = C(e, "notchUpper"), [p, g] = Ve(r, o, e);
20
20
  return {
21
21
  observation: e,
22
22
  start: p,
@@ -33,10 +33,10 @@ var y = {
33
33
  }
34
34
  };
35
35
  }, oe = (e, t) => {
36
- const r = t === "x" ? U(e) : A(e), o = t === "x" ? R(e) : $(e), n = b(e, "outlier");
36
+ const r = t === "x" ? A(e) : $(e), o = t === "x" ? K(e) : U(e), n = b(e, "outlier");
37
37
  return r === null || o === null || n === null ? null : {
38
38
  observation: e,
39
- band: V({
39
+ band: N({
40
40
  start: r,
41
41
  end: o
42
42
  }),
@@ -57,14 +57,14 @@ var y = {
57
57
  boxes: r,
58
58
  outliers: o
59
59
  };
60
- }, C = (e, t) => b(e, t) ?? null, Ne = (e, t, r) => {
60
+ }, C = (e, t) => b(e, t) ?? null, Ve = (e, t, r) => {
61
61
  const o = r[y.relativeWidth];
62
62
  if (typeof o != "number") return [e, t];
63
63
  const n = (e + t) / 2, a = (t - e) * o / 2;
64
64
  return [n - a, n + a];
65
- }, P = 1.5, ne = (e) => e === "min-max" || typeof e == "number" && Number.isFinite(e) && e >= 0, Ve = 1.58, Le = (e, t) => {
65
+ }, P = 1.5, ne = (e) => e === "min-max" || typeof e == "number" && Number.isFinite(e) && e >= 0, Ne = 1.58, Le = (e, t) => {
66
66
  if (e.length === 0) return null;
67
- const r = [...e].sort((p, g) => p - g), o = D(r, 0.25), n = D(r, 0.5), a = D(r, 0.75), s = t === "min-max" ? 1 / 0 : t * (a - o), i = o - s, u = a + s, l = r.filter((p) => p >= i && p <= u), h = Ve * (a - o) / Math.sqrt(e.length);
67
+ const r = [...e].sort((p, g) => p - g), o = D(r, 0.25), n = D(r, 0.5), a = D(r, 0.75), s = t === "min-max" ? 1 / 0 : t * (a - o), i = o - s, u = a + s, l = r.filter((p) => p >= i && p <= u), h = Ne * (a - o) / Math.sqrt(e.length);
68
68
  return {
69
69
  lowerWhisker: Math.min(l[0] ?? o, o),
70
70
  q1: o,
@@ -97,7 +97,7 @@ var y = {
97
97
  super(...e), this.type = "boxplot", this.computedVariables = se;
98
98
  }
99
99
  computeStat({ data: e, mapping: t, params: r }) {
100
- const o = N(t.x), n = N(t.y);
100
+ const o = V(t.x), n = V(t.y);
101
101
  if (o === null || n === null || !e.hasVariable(o) || !e.hasVariable(n)) return {
102
102
  data: e,
103
103
  mapping: {}
@@ -128,7 +128,7 @@ var y = {
128
128
  };
129
129
  }
130
130
  }, De = ({ data: e, xVariable: t, yVariable: r, groupVariables: o, extent: n }) => {
131
- const a = Ke(e, t, r, o), s = e.getVariableNames(), i = new Map(s.map((d) => [d, e.getValues(d)])), u = new Map(s.map((d) => [d, []])), l = {
131
+ const a = Re(e, t, r, o), s = e.getVariableNames(), i = new Map(s.map((d) => [d, e.getValues(d)])), u = new Map(s.map((d) => [d, []])), l = {
132
132
  q1: [],
133
133
  median: [],
134
134
  q3: [],
@@ -143,7 +143,7 @@ var y = {
143
143
  for (const d of a) {
144
144
  const m = Le(d.values, n);
145
145
  if (m === null) continue;
146
- for (const c of s) u.get(c)?.push(Re(i.get(c) ?? [], d.indices));
146
+ for (const c of s) u.get(c)?.push(Ke(i.get(c) ?? [], d.indices));
147
147
  l.q1.push(m.q1), l.median.push(m.median), l.q3.push(m.q3), l.lowerWhisker.push(m.lowerWhisker), l.upperWhisker.push(m.upperWhisker), l.count.push(m.count), l.mean.push(m.mean), l.notchLower.push(m.notchLower), l.notchUpper.push(m.notchUpper), l.outlier.push(null);
148
148
  const w = {
149
149
  low: m.lowerWhisker,
@@ -178,7 +178,7 @@ var y = {
178
178
  };
179
179
  }
180
180
  return new me(g);
181
- }, Ke = (e, t, r, o) => {
181
+ }, Re = (e, t, r, o) => {
182
182
  const n = [t, ...o].map((i) => e.getValues(i)), a = e.getValues(r, { type: "numeric" }), s = /* @__PURE__ */ new Map();
183
183
  return a.forEach((i, u) => {
184
184
  if (i === null || !Number.isFinite(i)) return;
@@ -188,14 +188,14 @@ var y = {
188
188
  };
189
189
  h.indices.push(u), h.values.push(i), s.set(l, h);
190
190
  }), [...s.values()];
191
- }, Re = (e, t) => {
191
+ }, Ke = (e, t) => {
192
192
  const r = e[t[0] ?? -1] ?? null, o = H(r);
193
193
  return t.every((n) => H(e[n] ?? null) === o) ? r : null;
194
- }, H = (e) => e === null ? "\0null" : e instanceof Date ? `date:${e.getTime()}` : `${typeof e}:${String(e)}`, le = 0.6, Ue = 1.5, $e = 0.3, Ae = 2, z = 1.5, He = 6, Fe = 1.5, Ge = 7, Xe = 1.5, ze = [
194
+ }, H = (e) => e === null ? "\0null" : e instanceof Date ? `date:${e.getTime()}` : `${typeof e}:${String(e)}`, le = 0.6, Ae = 1.5, Ue = 0.3, $e = 2, z = 1.5, He = 6, Fe = 1.5, Ge = 7, Xe = 1.5, ze = [
195
195
  "q1",
196
196
  "median",
197
197
  "q3"
198
- ], Ye = { strokeWidth: "pixels" }, K = {
198
+ ], Ye = { strokeWidth: "pixels" }, R = {
199
199
  stroke: "color",
200
200
  alpha: "unitInterval",
201
201
  strokeWidth: "pixels",
@@ -338,24 +338,24 @@ var y = {
338
338
  },
339
339
  rest: {
340
340
  fill: M("geom"),
341
- fillAlpha: $e,
341
+ fillAlpha: Ue,
342
342
  stroke: M("geom"),
343
- strokeWidth: Ue
343
+ strokeWidth: Ae
344
344
  }
345
345
  },
346
346
  parts: {
347
347
  median: {
348
- vocabulary: K,
348
+ vocabulary: R,
349
349
  aesthetics: { stroke: "color" },
350
350
  rest: {
351
351
  stroke: M("geom"),
352
352
  alpha: 1,
353
- strokeWidth: Ae,
353
+ strokeWidth: $e,
354
354
  dashArray: []
355
355
  }
356
356
  },
357
357
  whisker: {
358
- vocabulary: K,
358
+ vocabulary: R,
359
359
  aesthetics: { stroke: "color" },
360
360
  rest: {
361
361
  stroke: M("geom"),
@@ -365,7 +365,7 @@ var y = {
365
365
  }
366
366
  },
367
367
  cap: {
368
- vocabulary: K,
368
+ vocabulary: R,
369
369
  aesthetics: { stroke: "color" },
370
370
  rest: {
371
371
  stroke: M("geom"),
@@ -486,7 +486,7 @@ var y = {
486
486
  }, Qe = (e, t) => {
487
487
  const r = re(e, t);
488
488
  if (r !== null) return {
489
- band: V(r),
489
+ band: N(r),
490
490
  value: r.median
491
491
  };
492
492
  const o = oe(e, t);
@@ -504,7 +504,7 @@ var y = {
504
504
  const r = e.getValues(t, { type: "numeric" });
505
505
  return e.take(r.flatMap((o, n) => o === null ? [n] : []));
506
506
  }, q = (e, t) => {
507
- const r = N(t);
507
+ const r = V(t);
508
508
  return r !== null && e.hasVariable(r) ? r : null;
509
509
  }, tt = 5e-4, rt = 0.5, ot = 0.5, ie = (e, t) => {
510
510
  const r = (e.q1 + e.q3) / 2, o = Math.max(Math.abs(e.q3 - e.q1), tt) / 2, n = v(t, e.start, r - o), a = v(t, e.end, r + o), [s, i] = Z(n.x, a.x), [u, l] = Z(n.y, a.y);
@@ -535,11 +535,11 @@ var y = {
535
535
  from: v(t, r, e.median),
536
536
  to: v(t, o, e.median)
537
537
  };
538
- }, ce = (e, t) => e.mean === null ? null : v(t, V(e), e.mean), he = (e) => {
538
+ }, ce = (e, t) => e.mean === null ? null : v(t, N(e), e.mean), he = (e) => {
539
539
  const t = (e.end - e.start) * (1 - ot) / 2;
540
540
  return [e.start + t, e.end - t];
541
541
  }, de = (e, t) => {
542
- const r = V(e), o = (e.end - e.start) * rt / 2;
542
+ const r = N(e), o = (e.end - e.start) * rt / 2;
543
543
  return [{
544
544
  side: "lower",
545
545
  quartile: e.q1,
@@ -633,7 +633,7 @@ var y = {
633
633
  ...w
634
634
  ];
635
635
  }, J = (e, t) => (e.get("size", t) ?? 0) + (e.get("strokeWidth", t) ?? 0), G = (e) => {
636
- const t = Ie(e.data, e.mapping, e.id), r = N(e.mapping.outlier), o = (n) => `${t(n.observation)}-outlier-${String(r === null ? n.value : n.observation[r])}`;
636
+ const t = Ie(e.data, e.mapping, e.id), r = V(e.mapping.outlier), o = (n) => `${t(n.observation)}-outlier-${String(r === null ? n.value : n.observation[r])}`;
637
637
  return {
638
638
  readBoxKey: t,
639
639
  readOutlierKeys: (n) => {
@@ -846,7 +846,7 @@ var y = {
846
846
  getEditOutlineShapes: lt
847
847
  });
848
848
  export {
849
- y as BOXPLOT_COLUMNS,
849
+ y as BOXPLOT_VARIABLES,
850
850
  je as BoxplotGeom,
851
851
  gt as boxplot
852
852
  };
@@ -1,9 +1,9 @@
1
1
  /**
2
- * The columns `compile()` writes and the renderer reads, beside the position columns. The values sit in the
2
+ * The variables `compile()` writes and the renderer reads, beside the position variables. The values sit in the
3
3
  * units of the variable `y` maps, and carry its format, so the tooltip reads them like the value axis. Authors name them
4
4
  * in a `highlight` or an entry's `where`, so they are part of the geom's API.
5
5
  */
6
- export declare const BOXPLOT_COLUMNS: {
6
+ export declare const BOXPLOT_VARIABLES: {
7
7
  /** The first quartile, where the box starts. */
8
8
  readonly q1: "boxplotQ1";
9
9
  /** The median, where the line across the box sits. */
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "boxplot",
14
14
  "box-plot"
15
15
  ],
16
- "version": "0.1.1-beta.1791559652655",
16
+ "version": "0.1.1-beta.1791794533416",
17
17
  "type": "module",
18
18
  "sideEffects": false,
19
19
  "types": "./dist/index.d.ts",
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "peerDependencies": {
34
34
  "react": "^19.0.0",
35
- "@graphysdk/react": "^1.9.0-beta.1791559652655"
35
+ "@graphysdk/react": "^1.9.0-beta.1791794533416"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@testing-library/react": "^16.3.1",
@@ -44,11 +44,11 @@
44
44
  "vite": "^8.1.5",
45
45
  "vite-plugin-dts": "^4.5.4",
46
46
  "vitest": "^4.1.11",
47
+ "@graphysdk/react": "1.9.0-beta.1791794533416",
47
48
  "@graphytools/typescript-config": "0.0.1",
48
- "@graphytools/vitest-config": "1.0.0",
49
+ "@graphysdk/viz-engine": "1.9.0-beta.1791794533416",
49
50
  "@graphytools/eslint-config": "0.0.1",
50
- "@graphysdk/viz-engine": "1.9.0-beta.1791559652655",
51
- "@graphysdk/react": "1.9.0-beta.1791559652655"
51
+ "@graphytools/vitest-config": "1.0.0"
52
52
  },
53
53
  "publishConfig": {
54
54
  "access": "public"