@wafertools/wafermap 0.39.0 → 0.40.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.
- package/CHANGELOG.md +24 -3
- package/README.md +2 -2
- package/dist/packages/canvas-adapter/charts/commonality.js +4 -1
- package/dist/packages/canvas-adapter/charts/plotChart.d.ts +5 -0
- package/dist/packages/canvas-adapter/charts/plotChart.js +1 -1
- package/dist/packages/canvas-adapter/charts/regionYieldDiagram.js +1 -1
- package/dist/packages/canvas-adapter/charts/sweep.js +1 -1
- package/dist/packages/canvas-adapter/insightsTab.js +1 -1
- package/dist/packages/canvas-adapter/renderWaferGallery.js +1 -1
- package/dist/packages/canvas-adapter/renderWaferMap.js +1 -1
- package/dist/packages/canvas-adapter/summaryPanel.js +4 -4
- package/dist/packages/canvas-adapter/sweepEditor.js +1 -1
- package/dist/packages/canvas-adapter/toCanvas.d.ts +1 -0
- package/dist/packages/canvas-adapter/toCanvas.js +1 -1
- package/dist/packages/canvas-adapter/userGuideHtml.d.ts +1 -1
- package/dist/packages/canvas-adapter/userGuideHtml.js +44 -28
- package/dist/packages/canvas-adapter/version.d.ts +2 -2
- package/dist/packages/canvas-adapter/version.js +1 -1
- package/dist/packages/core/compact.js +2 -2
- package/dist/packages/core/dieTable.js +1 -1
- package/dist/packages/stats/commonality.d.ts +37 -4
- package/dist/packages/stats/commonality.js +1 -1
- package/dist/packages/stats/dieRegions.d.ts +2 -0
- package/dist/packages/stats/dieRegions.js +1 -1
- package/dist/packages/stats/examplePicks.d.ts +6 -0
- package/dist/packages/stats/examplePicks.js +1 -1
- package/dist/packages/stats/groupEffects.d.ts +66 -0
- package/dist/packages/stats/groupEffects.js +1 -0
- package/dist/packages/stats/plotData.js +1 -1
- package/dist/packages/stats/renderFindingsReport.js +1 -1
- package/dist/packages/stats/renderSummaryReport.js +7 -7
- package/dist/packages/stats/reportHtml.js +18 -13
- package/dist/packages/stats/sweep.d.ts +3 -3
- package/dist/packages/stats/sweepXFromName.js +1 -1
- package/dist/packages/stats/synthesis.d.ts +13 -0
- package/dist/packages/stats/synthesis.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -24,6 +24,27 @@ under `### Breaking`.
|
|
|
24
24
|
|
|
25
25
|
## [Unreleased]
|
|
26
26
|
|
|
27
|
+
## [0.40.0] — 2026-11-06
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
|
|
31
|
+
- **What stands out compares groups of wafers on yield and on the weakest ring, as well as on tests.** A probe card, a tester or a process recipe that costs yield is listed with its size in yield points and the fail bin most of it is in, and a pattern that is heavier in one group shows as a lower pass rate in its ring on that group's wafers. Each set of groups is read after allowing for the strongest other one (a weak card on wafers that also differ by recipe), and says so. Fields that group the wafers identically (a tester and the handler docked to it) are one item that names them together. A yield item has no map view, so its target is `{ kind: 'group', field }` and its chart is a bar of yield by that field.
|
|
32
|
+
- **What divides yield reads each field after allowing for the strongest other one.** A probe card hidden by a process recipe (wafers that differ by both) is ranked by what is left once the recipe's group means are taken out, with the degrees of freedom those means used charged to the p. The row says "allowing for …", and its Worst and Best groups stay the measure's own means. What stands out, the example plots and this card now share one reading of groups (`stats/groupEffects`).
|
|
33
|
+
- **Worth a look draws a group item once.** An item about groups of wafers (yield by a field) has no Show on map, since it has no place on the map, and its card is left out when Insights is already grouped by that field or one that gives the same groups, whose yield card shows the same bars.
|
|
34
|
+
- **Example plots offer a bar of yield for each lot field that divides the wafers' yield.** The fields are read as What stands out reads them (one set of groups for fields that group the wafers identically, each set read after allowing for the strongest other one), so a probe card hidden by a process recipe is found, and a tester and the handler docked to it each get a bar. The split the test plots use is chosen the same way.
|
|
35
|
+
- **Fields that nearly match are said so.** Where two sets of groups put most wafers in matching groups without being identical (a tester and an operator who mostly go together), an item about one says how many wafers the other agrees on, and What divides yield names the pair. Neither is allowed for in reading the other, since that would remove most of its effect.
|
|
36
|
+
- **A "Possible, not confirmed" list under What stands out.** Groups of wafers that differ on two or more measures with no overlap between the groups, and that account for a real share of a material loss, are listed apart from what stands out when the sample is too small to confirm them once the number of comparisons is allowed for (four wafers against four cannot reach an exact p under 0.014). Each needs a raw p under 0.05, complete separation, agreement in direction between yield and the ring's pass rate, and a set of groups with no confirmed item of its own. `Synthesis.possible` carries them; the Summary panel, the Insights tab and the report show them, and `synthesisText` prints them under their own heading.
|
|
37
|
+
- **The showcase lots carry the equipment and fab fields of their probe sessions.** Each demo CSV has tester, handler, probe card, operator, test program, test date and, where the lot has one, a process split. The parametric lot has twelve wafers, four of each corner, and the cluster lot eight.
|
|
38
|
+
|
|
39
|
+
### Changed
|
|
40
|
+
|
|
41
|
+
- **A click and a right-click agree on every Insights chart.** A right-click on a bar, a histogram bar, a heatmap cell, a ring, quadrant or reticle cell, a sweep level or a point opens the same menu on the dies it counts that a click opens, and on a wafer's own bar or point the menu for that wafer. The heatmaps (yield by ring or quadrant and wafer, re-probed dies, tests by wafer), Yield by site and the charts on Worth a look open it too, and their tooltips say so.
|
|
42
|
+
- **What divides yield shows fields that group the wafers identically as one row.** The row names every field of the set, and its Group by button groups by the first.
|
|
43
|
+
- **What divides yield has one row per field.** Each row shows the field's yield (or a fail bin, when that differs more clearly) as two bars, the worst and the best group's mean, and a sentence giving the gap in points. **Confidence** reads Clear, Possible or Not clear from q (under 0.05, under 0.2), with q, Welch's p and the rank p in its tooltip, and the other measures open under a button on the field. Fields are ordered clear first, then by the size of the difference. How to read the statistics is under **How to read this**.
|
|
44
|
+
- **Axes and legends keep clear of each other.** With axes shown, the tick labels sit beside the map, clear of a left or bottom legend, and stop at a right-hand legend or colorbar. A side legend leaves a 12 px gap to the wafer area. Tick labels name only die positions: they run across the dies and no further, whichever side the legend is on.
|
|
45
|
+
- **The Insights demo lot carries a tester and a probe card.** Its wafers are probed on two testers and three probe cards, one of them weak, so **What divides yield** ranks equipment beside the process split, and a field that moves with another is named as such. `docs/examples/data.js` takes `equipmentForWafer(index)` and a `yieldShift` option for it.
|
|
46
|
+
- **The Chart gallery shows Sweep and Histogram of the dies picked.** Each has a thumbnail and a tile that opens its own page. The sweep, derived-test and name-pattern examples use neutral test names (`LEVEL_12K`, `Cum_share= …`) in the guide, the API reference and the tests.
|
|
47
|
+
|
|
27
48
|
## [0.39.0] — 2026-10-31
|
|
28
49
|
|
|
29
50
|
### Added
|
|
@@ -1510,10 +1531,10 @@ under `### Breaking`.
|
|
|
1510
1531
|
|
|
1511
1532
|
- **Sweep x values from test names, an x unit, and a log x axis.** For a
|
|
1512
1533
|
program that records the swept quantity only in the test text — a
|
|
1513
|
-
resistance CDF named `
|
|
1534
|
+
resistance CDF named `Cum_share= LEVEL_12K / …`, one test per
|
|
1514
1535
|
threshold:
|
|
1515
1536
|
- `SweepSeriesSpec.xFromName` reads each test's x from its name with a
|
|
1516
|
-
placeholder pattern, `"
|
|
1537
|
+
placeholder pattern, `"LEVEL_{x}"`: `{x}` reads a number with its SI
|
|
1517
1538
|
prefix (`12K` → 12,000; case-sensitive, `K` accepted as kilo; a letter is a
|
|
1518
1539
|
prefix only alone or before a unit, so `12Kangaroos` reads 12; in an all-capitals
|
|
1519
1540
|
name the unit's case is gone, so `5NS` is 5 ns and `2MV` — milli or mega? — is
|
|
@@ -1748,7 +1769,7 @@ under `### Breaking`.
|
|
|
1748
1769
|
- **Docs and examples brought up to date with this release.** The developer
|
|
1749
1770
|
guide's sweep section no longer says `tests` has no range syntax or that a
|
|
1750
1771
|
sweep has no log option; it covers ranges, `xFromName`, `xUnit`, `xScale` and
|
|
1751
|
-
drilldown. The sweeps example adds a fifth sweep (
|
|
1772
|
+
drilldown. The sweeps example adds a fifth sweep (a resistance
|
|
1752
1773
|
distribution read from test names on a log axis) and gives the output-driver
|
|
1753
1774
|
sweep an x unit; the derived-tests example's sweep uses ranges and a real swept
|
|
1754
1775
|
quantity (pulse amplitude in volts) instead of ordinal steps, and places the †
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/wafertools/wafermap/actions/workflows/deploy.yml)
|
|
6
6
|
[](https://www.npmjs.com/package/@wafertools/wafermap)
|
|
7
7
|

|
|
8
|
-

|
|
9
9
|
[](./LICENSE)
|
|
10
10
|
[](https://github.com/sponsors/wafertools)
|
|
11
11
|
[](https://ko-fi.com/wafertools)
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
Browser-first wafer map visualisation for semiconductor test data.
|
|
16
16
|
|
|
17
17
|
**Zero runtime dependencies.** Pure ES modules with TypeScript types — works in React,
|
|
18
|
-
Svelte, Vue, plain HTML, or a Web Worker. The DOM-free data-and-stats layer is ~
|
|
18
|
+
Svelte, Vue, plain HTML, or a Web Worker. The DOM-free data-and-stats layer is ~85 kB
|
|
19
19
|
min+gz; the interactive renderer is larger, and its chart suite and in-app guide are
|
|
20
20
|
loaded on demand rather than shipped up front — [measured sizes](docs/performance.md).
|
|
21
21
|
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
import{rankCommonality as
|
|
1
|
+
import{rankCommonality as se,measureLabel as V,groupCommonalityRows as ae,commonalityConfidence as X,CONFIDENCE_LABEL as ie}from"../../stats/commonality.js";import{formatP as I}from"../../stats/groupCompare.js";import{CLR as m,FONT as N,RADIUS as re,SPACE as c,controlStyle as Y,wireControlHover as z,wireTooltip as g}from"../toolbar.js";import{shortfallShade as le}from"../cellShade.js";import{cardShell as de,firstRowsToggle as ce,guideCite as pe,renderEmptyState as me}from"./chartShell.js";import{GUIDE_SECTIONS as fe}from"../../stats/guideSections.js";import{cardAbout as he}from"./cardAbout.js";const J=8,ue=5,we=110;export function renderCommonalityPanel(h){const s=h.ownerDocument??document,{card:$,body:E,setAbout:K}=de("What divides yield",void 0,s);K(he("commonality")),$.dataset.wmapCommonality="1",$.style.gridColumn="1 / -1",$.style.flex="0 0 auto";const p=se(h.wafers,h.fields,h.curation);if(h.fields.length===0||p.rows.length===0)return me(E,h.fields.length===0?"No wafer field divides these wafers, so there is nothing to compare.":"No wafer field has two groups of two or more wafers, so there is nothing to compare."),{card:$,destroy:()=>{}};const B=ae(p.rows,p.tied),v=e=>`${e.slice(0,-1).map(t=>t.label).join(", ")}${e.length>1?" and ":""}${e[e.length-1].label}`,T=s.createElement("div"),R=[`Each wafer field, compared for yield and for the ${p.bins.length} most common fail bin${p.bins.length===1?"":"s"}, with each wafer as one value. The clearest fields come first. A clear difference is more than wafer-to-wafer noise; it does not say why.`];p.overlapping.length&&R.push(`These fields nearly match, so the wafers cannot fully tell them apart: ${p.overlapping.map(e=>`${v(e.fields)} and ${v(e.with)} put ${e.agree} of ${e.of} wafers in the same groups`).join("; ")}.`),p.untestable.length&&R.push(`Not compared, with no two groups of two or more wafers: ${p.untestable.map(e=>e.label).join(", ")}.`),T.textContent=R.join(" "),Object.assign(T.style,{color:m.label,fontSize:N.body,marginBottom:c.xs}),E.appendChild(T);const W=s.createElement("details"),j=s.createElement("summary");j.textContent="How to read this",Object.assign(j.style,{cursor:"pointer"}),z(j,"bare");const L=s.createElement("div");L.textContent=`Clear means q is under 0.05 and possible under 0.2, where q is Welch\u2019s p adjusted for the ${p.rows.length} comparisons in the table (Benjamini\u2013Hochberg). Hover a word for the p-values. Each field is read after allowing for the strongest other field that explains the same measure and does not contain it, and its row says so. The bars show the worst (in bold) and the best group\u2019s mean, filled from zero on a scale of 0 to 100%; hover a bar for its group\u2019s median, spread and wafers. `,L.append(pe(s,fe.dividesYield),"."),W.append(j,L),Object.assign(W.style,{color:m.label,fontSize:N.body,marginBottom:c.sm}),E.appendChild(W);const O=s.createElement("table");Object.assign(O.style,{borderCollapse:"collapse",width:"100%",fontSize:N.body,fontVariantNumeric:"tabular-nums"});const D=s.createElement("tr"),S=(e,t)=>{const n=s.createElement("th");n.scope="col",n.textContent=e,t&&g(n,t),Object.assign(n.style,{textAlign:"left",padding:`${c.xs} ${c.sm}`,borderBottom:`1px solid ${m.menuBorder}`,color:m.label,fontWeight:"600",whiteSpace:"nowrap"}),D.appendChild(n)};S("Field"),S("Effect","The worst and the best group\u2019s mean over their wafers, and how far apart they are"),S("Confidence","Whether the groups differ by more than wafer-to-wafer noise: clear (q under 0.05), possible (under 0.2), or not clear"),S("");const H=s.createElement("thead");H.appendChild(D),O.appendChild(H);const G=s.createElement("tbody");O.appendChild(G);const y=(e,t,n={})=>{const o=s.createElement("td");return typeof t=="string"?o.textContent=t:o.appendChild(t),n.title&&g(o,n.title),n.shade&&(o.style.background=n.shade),Object.assign(o.style,{padding:`${c.xs} ${c.sm}`,textAlign:"left",borderBottom:`1px solid ${m.menuBorder}`,verticalAlign:"top",fontWeight:n.strong?"700":"400"}),e.appendChild(o),o},A=e=>`${e.toFixed(1)}%`,M=e=>`${e.toFixed(1)} pts`,_=(e,t)=>t.comparison.groups.find(n=>n.key===e)?.n??0,Q=e=>e.comparison.groups.filter(t=>!e.comparison.tooSmall.includes(t.key)&&Number.isFinite(t.mean)),Z=e=>{if(e.measure.kind!=="yield")return;const t=e.comparison.groups.filter(o=>Number.isFinite(o.mean)&&o.n>0),n=t.reduce((o,r)=>o+r.n,0);return n>0?le(e.worst.mean-t.reduce((o,r)=>o+r.mean*r.n,0)/n):void 0},ee=e=>{const t=e.measure.kind==="yield"?`${e.worst.key} is ${M(e.spread)} below ${e.best.key}`:`${V(e.measure)} is ${M(e.spread)} higher in ${e.worst.key} than in ${e.best.key}`,[n,o]=[_(e.worst.key,e),_(e.best.key,e)];return Math.min(n,o)<ue?`${t} (${n} vs ${o} wafers)`:t},te=e=>{const t=Q(e),n=100,o=s.createElement("div");Object.assign(o.style,{display:"flex",flexDirection:"column",gap:"2px",flex:"0 0 auto"});const r=(i,l)=>{const b=s.createElement("div");b.dataset.wmapBar=l,Object.assign(b.style,{display:"flex",alignItems:"center",gap:c.sm});const a=s.createElement("span");a.textContent=i.key,Object.assign(a.style,{width:"72px",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",color:m.label,fontWeight:l==="worst"?"700":"400"});const d=s.createElement("div");Object.assign(d.style,{width:`${we}px`,height:"10px",border:`1px solid ${m.label}`,boxSizing:"border-box",flex:"0 0 auto"});const C=s.createElement("div");Object.assign(C.style,{width:`${Math.max(0,Math.min(100,i.mean/n*100)).toFixed(1)}%`,height:"100%",background:m.label}),d.appendChild(C);const k=s.createElement("span");return k.textContent=A(i.mean),Object.assign(k.style,{width:"52px",textAlign:"right"}),b.append(a,d,k),g(b,[`${i.key}${l==="worst"?" (worst group)":" (best group)"}`,`mean ${A(i.mean)} \xB7 median ${A(i.median)}`,Number.isFinite(i.sd)?`standard deviation ${i.sd.toFixed(1)} pts between wafers`:"",`${i.n} wafer${i.n===1?"":"s"}`].filter(Boolean).join(`
|
|
2
|
+
`)),b},u=t.find(i=>i.key===e.worst.key),w=t.find(i=>i.key===e.best.key);u&&o.appendChild(r(u,"worst")),w&&o.appendChild(r(w,"best"));const f=t.filter(i=>i!==u&&i!==w);if(f.length){const i=s.createElement("div");i.textContent=`+ ${f.length} other group${f.length===1?"":"s"}`,Object.assign(i.style,{color:m.label}),g(i,f.map(l=>`${l.key}: ${A(l.mean)} (${l.n} wafer${l.n===1?"":"s"})`).join(`
|
|
3
|
+
`)),o.appendChild(i)}return o},ne=e=>{const t=e.adjusted??e.comparison,n=t.kruskal;return[`q ${I(e.pAdjusted)} \xB7 Welch p ${I(t.welch?.p??NaN)} \xB7 rank p ${I(n?.p??NaN)}${n?.approximate?"* (approximate: fewer than five wafers in a group)":""}`,t.issue].filter(Boolean).join(`
|
|
4
|
+
`)},x=new Set;let q=!1;const P=(e,t,n)=>{const o=s.createElement("tr");o.dataset.wmapCommonalityRow=n?"field":"measure";const r=s.createElement("div");if(n){const a=s.createElement("div");a.textContent=v(t.fields),a.style.fontWeight="600",t.fields.length>1&&g(a,"These fields group the wafers identically, so the wafers cannot tell them apart: one row stands for all of them."),r.appendChild(a)}const u=s.createElement("div");if(u.textContent=V(e.measure)+(e.allowedFor?`, allowing for ${e.allowedFor.map(a=>a.label).join(", ")}`:""),Object.assign(u.style,{color:m.label,paddingLeft:n?"0":c.md}),e.allowedFor&&g(u,`Read after allowing for ${e.allowedFor.map(a=>a.label).join(", ")}: each wafer's value less its ${e.allowedFor.length===1?"group\u2019s":"groups\u2019"} mean on ${e.allowedFor.length===1?"that field":"those fields"}, so a difference the other field explains does not hide this one. The bars and the points are the measure's own means.`),r.appendChild(u),n&&t.others.length){const a=t.others.filter(k=>X(k.pAdjusted)!=="none").length,d=s.createElement("button");d.type="button",d.dataset.wmapCommonalityMore=t.field.key;const C=x.has(t.field.key);d.setAttribute("aria-expanded",String(C)),d.textContent=C?"Hide other measures":a?`+${a} other measure${a===1?"":"s"} ${a===1?"differs":"differ"}`:`${t.others.length} other measure${t.others.length===1?"":"s"}`,Object.assign(d.style,{...Y("outlined"),marginTop:c.xs,fontSize:N.body}),z(d),d.addEventListener("click",()=>{x.has(t.field.key)?x.delete(t.field.key):x.add(t.field.key),U(q)}),r.appendChild(d)}y(o,r);const w=s.createElement("div");Object.assign(w.style,{display:"flex",alignItems:"flex-start",gap:c.md});const f=s.createElement("span");f.textContent=ee(e);const i=Z(e);i&&Object.assign(f.style,{background:i,padding:`0 ${c.xs}`,borderRadius:re.control}),f.dataset.wmapSentence="1",w.append(te(e),f),y(o,w);const l=X(e.pAdjusted),b=y(o,ie[l],{strong:l==="clear",title:ne(e)});if(b.dataset.wmapConfidence=l,n)if(t.fields.some(a=>a.key===h.groupedBy))y(o,"Grouped by this");else{const a=s.createElement("button");a.type="button",a.textContent="Group by",a.dataset.wmapCommonalityGroup=t.field.key,g(a,`Group Insights by ${t.field.label}, so every chart compares its groups${t.fields.length>1?`; ${v(t.fields.slice(1))} give the same groups`:""}`),Object.assign(a.style,Y("outlined")),z(a),a.addEventListener("click",()=>h.onGroupBy(t.field.key)),y(o,a)}else y(o,"");G.appendChild(o)};function U(e){q=e,G.replaceChildren();for(const t of e?B:B.slice(0,J))if(P(t.primary,t,!0),x.has(t.field.key))for(const n of t.others)P(n,t,!1)}const F=s.createElement("div");Object.assign(F.style,{overflowX:"auto"}),F.appendChild(O);const oe=ce(s,{total:B.length,first:J,noun:"rows",scroller:F,fill:U});return E.append(F,oe),{card:$,destroy:()=>{}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{escHtml as D,maxOf as Te,minOf as jt}from"../../core/utils.js";import{fiveNumberSummary as Ut,normalQuantile as qt,sampleVariance as Rn}from"../../stats/math.js";import{CONTROL_RULE_TEXT as On}from"../../stats/control.js";import{PAT_VERDICT_LABEL as Yt}from"../../stats/pat.js";import{fmt as J}from"../../renderer/fmt.js";import{fitTicks as pt}from"../../renderer/axisTicks.js";import{binOfLabel as In,plotFootnote as Ht,plotGuideSections as Kt,plotTestNumber as Ve,plotTitle as Nn,probabilityPosition as ct}from"../../stats/plotData.js";import{SPACE as me,RADIUS as Fn,fontPx as Q,FONT as gt,CLR as nt,markNoPrint as Vt}from"../toolbar.js";import{exportCsv as zn}from"../tableExport.js";import{tableToTsv as _n}from"../../core/tableCsv.js";import{plotTable as Gn}from"../../stats/plotTable.js";import{cardShell as Xn,appendGuideCites as Dn,observeResize as jn,makeTooltip as Un,attachChartTip as qn,chartFillHeight as Yn,applyCanvasFlow as Xe,prepareCanvas as De,positionChartTooltip as yt,resolveChartCanvasColors as je,makeAxisFormat as Hn,horizontalTickSpacing as Zt,VERTICAL_TICK_SPACING_PX as Kn,makeSeriesLegendItem as Vn,renderEmptyState as Jt,isExpandedCard as Qt,makeLabeledSelect as Zn,fillTextOnHalo as Ct,limitLines as Jn,limitExtent as Qn,stackLabelRows as en,placeClearOf as tn,strokeLimitLine as nn,drawOffAxisLimits as eo,limitLabelPlacement as to,shouldIncludeLimitsByDefault as no,WAFER_MENU_HINT as Et}from"./chartShell.js";import{guideCitesText as oo}from"../../stats/guideSections.js";import{categorical as lo}from"./palette.js";import{NO_DATA_FILL as on}from"../../renderer/colorMap.js";import{darkTextOnColor as so,plotGradient as bt}from"./divergingScale.js";import{wirePointInteractions as Bt}from"./pointInteractions.js";import{plotAbout as io}from"./cardAbout.js";const Wt=64,xt=16,Rt=12,Ue=52,ao=16,ro=92,co=16,vt=5e3,ln=1500,ft=100,fo=[1e-4,.001,.01,.05,.1,.25,.5,.75,.9,.95,.99,.999,.9999],sn=m=>`${+(m*100).toPrecision(4)}%`;export function categoryLayout(m){const W=.8/Math.max(1,...m.map(R=>R.length)),U=R=>(1-m[R].length*W)/2;return{width:W,left:(R,q)=>{const V=m[R].indexOf(q);return V<0?void 0:U(R)+V*W},groupAt:(R,q)=>{const V=Math.floor((q-U(R))/W);return V>=0&&V<m[R].length?m[R][V]:void 0}}}function uo(m){if(m<=ln)return Array.from({length:m},(R,q)=>q);const z=[];for(let R=0;R<ft;R++)z.push(R);const W=ln-2*ft,U=m-2*ft;for(let R=0;R<W;R++)z.push(ft+Math.floor(R*U/W));for(let R=m-ft;R<m;R++)z.push(R);return z}function Ot(m,z,W){return W?Math.round(m).toLocaleString("en-GB"):z?J(m,z):`${m<=-.005?"\u2212":""}${Math.abs(m).toFixed(2)}`}const ho=260;function mo(m){const z=[...m.querySelectorAll("[data-wmap-plot-panel]")],W=z[0]?.querySelector("canvas");if(!W)return null;const U=W.width/Math.max(1,W.getBoundingClientRect().width)||1,R=m.getBoundingClientRect(),q=m.ownerDocument.createElement("canvas");q.width=Math.ceil(R.width*U),q.height=Math.ceil(R.height*U);const V=q.getContext("2d");if(!V)return null;const we=getComputedStyle(m);V.fillStyle=we.backgroundColor&&we.backgroundColor!=="rgba(0, 0, 0, 0)"?we.backgroundColor:"#fff",V.fillRect(0,0,q.width,q.height);for(const Me of z){const Se=Me.querySelector("canvas"),ce=Me.dataset.wmapChartTitle??"",_=(Se??Me).getBoundingClientRect(),s=Me.getBoundingClientRect();V.fillStyle=getComputedStyle(Me).color||"#333",V.font=`600 ${Math.round(13*U)}px system-ui, sans-serif`,V.textBaseline="top",V.fillText(ce,(s.left-R.left+8)*U,(s.top-R.top+6)*U),Se&&V.drawImage(Se,(_.left-R.left)*U,(_.top-R.top)*U,_.width*U,_.height*U)}return q}const Re=(m,z)=>z.extent?[...m,...z.extent]:m;function ue(m,z,W={}){const U=z.scale==="log";let R=m.length?jt(m):0,q=m.length?Te(m):1;U&&(R=Math.log10(Math.max(R,Number.MIN_VALUE)),q=Math.log10(Math.max(q,Number.MIN_VALUE)));const V=(q-R)*.05||(U?.5:1);R=W.floorAtZero&&!U&&R>=0?0:R-V,q+=V;const Me=$e=>$e===void 0||!Number.isFinite($e)?void 0:U?$e>0?Math.log10($e):void 0:$e,Se=Me(z.min),ce=Me(z.max);Se!==void 0&&(R=Se),ce!==void 0&&(q=ce),q>R||(q=R+1);const _=q-R,s=!!z.reverse,Oe=$e=>{const C=((U?Math.log10(Math.max($e,Number.MIN_VALUE)):$e)-R)/_;return s?1-C:C};return{lo:U?10**R:R,hi:U?10**q:q,log:U,reverse:s,frac:Oe}}function It(m,z,W,U){if(z/m<10)return pt(m,z,W,U);const R=Math.floor(Math.log10(m)),q=Math.ceil(Math.log10(z)),V=Se=>{const ce=[];for(let _=R;_<=q;_++)for(const s of Se){const Oe=s*10**_;Oe>=m&&Oe<=z&&ce.push(Oe)}return ce},we=V([1]);return{ticks:we.length>=4?we:V([1,2,5]),step:0}}export function renderPlotChart(m){const z=m.ownerDocument??document,{card:W,heading:U,body:R,controlsRow:q,setPngDecor:V,setPngCanvas:we,setAbout:Me,setMenuRows:Se}=Xn(m.panel?.title??"Plot",m.onSaveImage,z,{png:!m.panel});m.panel&&(W.dataset.wmapPlotPanel="1");let ce=[];W.dataset.wmapPlotCard="1";const _=Un(W);let s,Oe=()=>[];const $e=()=>{const e=s&&(s.marks||s.facets?.length)?Gn(s,m.waferLabel):void 0;return e?[{label:"Save data as CSV",onClick:()=>{zn(e.columns,e.rows,`${e.name}.csv`,m.onSaveText)}},{label:"Copy data",onClick:()=>{z.defaultView?.navigator.clipboard?.writeText(_n(e.columns,e.rows))}}]:[]};Se(()=>[...$e(),...Oe()]);let C,pe,te,Ze,Ie,ut,ht=!1;W.addEventListener("pointerenter",()=>{ht=!0,W.dataset.wmapPlotPanelsOwner&&(W.dataset.wmapPanelsHover="1")}),W.addEventListener("pointerleave",()=>{ht=!1,delete W.dataset.wmapPanelsHover});let ot=[];const O=new Set,he=new Set;let ge=null,ye=-1,le=[],Z,Ee,ne,se=null,lt=0,Pe,dt;const an=()=>!!s?.facets;let de=[],ke=-1,Mt=new Map,qe=[],Ye=Ue,w=Wt,v=Rt;const rn={[Yt.outlier]:"#e69f00",[Yt.fails]:"#c0392b"},ee=e=>{const l=s?.groups[e]??"";if(s?.colorBins&&m.binColors){const a=In(l);return a===void 0?on:(s.colorBins==="hard"?m.binColors.hard:m.binColors.soft).get(a)??on}return rn[l]??lo(e)},He=e=>s?.groups[e]||"";function Ne(){const e=R.clientWidth,l=(m.panel?Math.max(170,Math.min(280,e*.72)):Math.max(220,Math.min(420,e*.62)))+(Ye-Ue),a=Yn(W,R,C,l);return{w:e,h:a,plotW:Math.max(10,e-w-xt),plotH:Math.max(10,a-v-Ye)}}const Be=e=>{const l=(e?.references??[]).flatMap(a=>a.to===void 0?[{value:a.at,kind:"ref",end:"lo",label:a.label??""}]:[{value:a.at,kind:"ref",end:"lo",label:a.label??"",bandTo:a.to},{value:a.to,kind:"ref",end:"hi",label:a.label??""}]);return[...Jn(e?.limits,s?.spec.limits??"both"),...l].filter(a=>e?.scale!=="log"||a.value>0)},Fe=(e,l,a)=>{const f=a?.clip,r=f?[...e.filter(d=>d>=f.lo&&d<=f.hi),f.lo,f.hi]:e,h=r.filter(Number.isFinite);if(!l.length||h.length===0)return r;const{lo:n,hi:t}=Qn(l);return(a?s?.spec.axes?.[a===s?.x?"x":"y"]?.includeLimits:void 0)??no(jt(h),Te(h),n,t)?[...r,...l.map(d=>d.value)]:r};let kt;function Je(e,l,a,f=!1){return!f&&!s?.x?.clip&&!s?.y?.clip?()=>{}:(e.save(),e.beginPath(),e.rect(w,v,l,a),e.clip(),kt=e,Nt)}function Nt(){kt?.restore(),kt=void 0}function ze(e,l,a,f,r,h,n,t,o){const d=[];if(h.length===0)return()=>{};const k=h.filter(p=>p.value>=r.lo&&p.value<=r.hi),x=h.filter(p=>!k.includes(p)).map(p=>({value:p.value,label:p.label,side:p.value<r.lo?"lo":"hi"}));e.save(),e.font=`${Q(-1)}px system-ui, sans-serif`;const g=p=>`${p.label} ${J(p.value,n.unit)}`.trim(),c=Q(-1)+2;for(const p of h){if(p.bandTo===void 0)continue;const A=r.frac(Math.min(p.value,p.bandTo)),S=r.frac(Math.max(p.value,p.bandTo)),y=Math.max(0,Math.min(A,S)),P=Math.min(1,Math.max(A,S));P<=y||(e.save(),e.globalAlpha=.1,e.fillStyle=l,t==="y"?e.fillRect(w,v+(1-P)*f,a,(P-y)*f):e.fillRect(w+y*a,v,(P-y)*a,f),e.restore())}if(t==="y"){const p=k.map(y=>{const P=v+(1-r.frac(y.value))*f;return{l:y,y:P,start:P-6-c/2,end:P-6+c/2}}),A=en(p,1),S=p.length?Te(p.map(y=>e.measureText(g(y.l)).width))+8:0;p.forEach(({l:y,y:P},u)=>{e.globalAlpha=.7,nn(e,y,l,w,P,w+a,P),e.globalAlpha=1,e.fillStyle=l,e.textAlign="left",e.textBaseline="alphabetic";const i=w+3+A[u]*S,M=tn(qe,{x0:i,x1:i+e.measureText(g(y)).width,y0:P-4-c+2,y1:P-4+2},c);if(!o){e.fillText(g(y),i,M.y1-2);return}d.push(()=>{e.save(),e.font=`${Q(-1)}px system-ui, sans-serif`,e.fillStyle=l,e.textAlign="left",e.textBaseline="alphabetic",Ct(e,g(y),i,M.y1-2,o.halo),e.restore()})})}else{const p=k.map(S=>{const y=w+r.frac(S.value)*a,P=g(S),u=S.label.split(/\s+/)[0],i=J(S.value,n.unit),M=u&&u!==S.label?[P,`${u} ${i}`,u]:[P],B=k.filter(E=>E!==S).map(E=>w+r.frac(E.value)*a),{text:F,side:T}=to(y,M,E=>e.measureText(E).width,w,w+a,S.end==="lo",B),L=e.measureText(F).width;return{l:S,x:y,side:T,shown:F,start:T<0?y-3-L:y+3,end:T<0?y-3:y+3+L}}),A=en(p,4);p.forEach(({l:S,x:y,side:P,shown:u,start:i,end:M},B)=>{e.globalAlpha=.7,nn(e,S,l,y,v,y,v+f),e.globalAlpha=1;const F=P<0?"right":"left";e.fillStyle=l,e.textAlign=F,e.textBaseline="top";const T=v+2+A[B]*c,L=tn(qe,{x0:i,x1:M,y0:T,y1:T+c},c);if(!o){e.fillText(u,y+P*3,L.y0);return}d.push(()=>{e.save(),e.font=`${Q(-1)}px system-ui, sans-serif`,e.fillStyle=l,e.textAlign=F,e.textBaseline="top",Ct(e,u,y+P*3,L.y0,o.halo),e.restore()})})}return e.restore(),eo(e,x,{left:w,right:w+a,top:v,bottom:v+f},t==="y"?"vertical":"horizontal",l,p=>J(p,n.unit),void 0,qe),()=>d.forEach(p=>p())}function Tt(e,l,a,f){return Hn(Math.max(Math.abs(l),Math.abs(a)),e.unit,f,e.label)}function mt(e,l){return l?`${e.label} (${l})`:e.label}const wt=18,St=(e,l,a)=>{if(e.measureText(l).width<=a)return l;for(let f=l.length-1;f>0;f--){const r=`${l.slice(0,f)}\u2026`;if(e.measureText(r).width<=a)return r}return""};function Ke(e,l,a,f,r,h,n,t,o,d,k){qe=[],e.font=`${Q(-1)}px system-ui, sans-serif`;const x=()=>Kn,g=k?{ticks:k.map(y=>y.at),step:0}:t.log?It(t.lo,t.hi,h,x):pt(t.lo,t.hi,h,x),c=new Map(k?.map(y=>[y.at,y.label])),p=Tt(d,t.lo,t.hi,g.step||void 0),A=k?{tick:y=>c.get(y)??"",unitLabel:""}:p;e.strokeStyle=l.grid,e.lineWidth=.5,e.fillStyle=l.textMuted;let S="";if("labels"in n){const y=n.labels.length;if(n.labels.forEach((P,u)=>{const i=w+(u+.5)/y*r,M=P.length>22?`${P.slice(0,21)}\u2026`:P;n.rotate?(e.save(),e.translate(i,v+h+6),e.rotate(-Math.PI/4),e.textAlign="right",e.textBaseline="middle",e.fillText(M,0,0),e.restore()):(e.textAlign="center",e.textBaseline="top",e.fillText(M,i,v+h+4))}),n.nest){const P=n.nest[0]?.length??0,u=r/y;for(let i=P-2;i>=0;i--){const M=f-6-wt-i*wt,B=T=>n.nest[T].slice(0,i+1).join("");let F=0;for(let T=1;T<=y;T++){if(T<y&&B(T)===B(F))continue;const E=w+F*u,j=w+T*u;e.fillStyle=l.text,e.textAlign="center",e.textBaseline="bottom",e.fillText(St(e,n.nest[F][i],j-E-4),(E+j)/2,M),T<y&&(e.save(),e.strokeStyle=l.border,e.globalAlpha=i===0?.9:.45,e.lineWidth=1,e.beginPath(),e.moveTo(j,v),e.lineTo(j,M-2),e.stroke(),e.restore()),F=T}}e.fillStyle=l.textMuted}}else{const y=Zt(e,Math.max(Math.abs(n.lo),Math.abs(n.hi)),o.unit),P=n.log?It(n.lo,n.hi,r,y):pt(n.lo,n.hi,r,y),u=Tt(o,n.lo,n.hi,P.step||void 0);S=u.unitLabel;for(const i of P.ticks){const M=w+n.frac(i)*r;e.beginPath(),e.moveTo(M,v),e.lineTo(M,v+h),e.stroke(),e.textAlign="center",e.textBaseline="top",e.fillText(u.tick(i),M,v+h+4)}}for(const y of g.ticks){const P=v+(1-t.frac(y))*h;e.beginPath(),e.moveTo(w,P),e.lineTo(w+r,P),e.stroke(),e.textAlign="right",e.textBaseline="middle",e.fillText(A.tick(y),w-4,P)}e.font=`${Q(0)}px system-ui, sans-serif`,e.fillStyle=l.text,e.textAlign="center",e.textBaseline="bottom",e.fillText(mt(o,S),w+r/2,f-6),e.save(),e.translate(14,v+h/2),e.rotate(-Math.PI/2),e.textBaseline="top",e.fillText(mt(d,A.unitLabel),0,0),e.restore(),e.strokeStyle=l.border,e.lineWidth=1,e.beginPath(),e.moveTo(w,v),e.lineTo(w,v+h),e.moveTo(w,v+h),e.lineTo(w+r,v+h),e.stroke()}function cn(e,l,a,f,r,h,n,t,o,d,k,x={along:h,offset:0}){qe=[],e.font=`${Q(-1)}px system-ui, sans-serif`;const g=Zt(e,Math.max(Math.abs(o.lo),Math.abs(o.hi)),k.unit),c=o.log?It(o.lo,o.hi,r,g):pt(o.lo,o.hi,r,g),p=Tt(k,o.lo,o.hi,c.step||void 0);e.strokeStyle=l.grid,e.lineWidth=.5,e.fillStyle=l.textMuted;for(const i of c.ticks){const M=w+o.frac(i)*r;e.beginPath(),e.moveTo(M,v),e.lineTo(M,v+h),e.stroke(),e.textAlign="center",e.textBaseline="top",e.fillText(p.tick(i),M,v+h+4)}const A=n.labels.length,S=x.along/Math.max(1,A),y=v-x.offset;e.save(),e.beginPath(),e.rect(0,v-1,w+r+1,h+2),e.clip(),e.textAlign="right",e.textBaseline="middle";const P=Math.max(0,Math.floor(x.offset/S)),u=Math.min(A,Math.ceil((x.offset+h)/S));for(let i=P;i<u;i++)e.fillText(St(e,n.labels[i],t.inner),w-6,y+(i+.5)*S);if(n.nest){const i=n.nest[0]?.length??0;let M=w-6-t.inner-10;for(let B=i-2;B>=0;B--){const F=t.outer[B]??0,T=E=>n.nest[E].slice(0,B+1).join("");let L=0;for(let E=1;E<=A;E++){if(E<A&&T(E)===T(L))continue;const j=y+L*S,b=y+E*S;e.fillStyle=l.text,e.textAlign="right",e.textBaseline="middle",e.fillText(St(e,n.nest[L][B],F),M,(j+b)/2),E<A&&(e.save(),e.strokeStyle=l.border,e.globalAlpha=B===0?.9:.45,e.lineWidth=1,e.beginPath(),e.moveTo(M-F,b),e.lineTo(w+r,b),e.stroke(),e.restore()),L=E}M-=F+10}e.fillStyle=l.textMuted}e.restore(),e.font=`${Q(0)}px system-ui, sans-serif`,e.fillStyle=l.text,e.textAlign="center",e.textBaseline="bottom",e.fillText(mt(k,p.unitLabel),w+r/2,f-6),e.save(),e.translate(14,v+h/2),e.rotate(-Math.PI/2),e.textBaseline="top",e.fillText(mt(d,""),0,0),e.restore(),e.strokeStyle=l.border,e.lineWidth=1,e.beginPath(),e.moveTo(w,v),e.lineTo(w,v+h),e.moveTo(w,v+h),e.lineTo(w+r,v+h),e.stroke()}function fn(){if(!s||!C||s.marks?.type!=="scatter"||!s.x||!s.y)return;Xe(C,te??0);const e=je(W),{w:l,h:a,plotW:f,plotH:r}=Ne(),h=De(C,W,l,a);if(!h)return;const{ctx:n}=h,t=s.marks.points,o=O.size===0?t:t.filter(T=>O.has(T.group)),d=s.colorScale,k=bt(s.spec.colorGradient,s.spec.colorReverse,d?.diverging,d?.rating),x=Be(s.x),g=Be(s.y),c=ue(Fe(Re(t.map(T=>T.x),s.x),x,s.x),s.x),p=ue(Fe(Re(t.map(T=>T.y),s.y),g,s.y),s.y);Z={x:c,y:p,plotW:f,plotH:r},Ke(n,e,l,a,f,r,c,p,s.x,s.y),ze(n,e.limitLine,f,r,c,x,s.x,"x"),ze(n,e.limitLine,f,r,p,g,s.y,"y"),Je(n,f,r);const A=o.length>vt?Math.ceil(o.length/vt):1,S=[];for(let T=0;T<o.length;T+=A)S.push(o[T]);n.globalAlpha=Math.max(.15,Math.min(.7,200/S.length));let y;if(d)y=[S];else{const T=new Map;for(const L of S){const E=T.get(L.group);E?E.push(L):T.set(L.group,[L])}y=[...T.values()].sort((L,E)=>E.length-L.length)}le=[];const P=w,u=w+f,i=v,M=v+r;for(const T of y)for(const L of T){const E=w+c.frac(L.x)*f,j=v+(1-p.frac(L.y))*r;E<P||E>u||j<i||j>M||(le.push({p:L,cx:E,cy:j}),n.beginPath(),n.arc(E,j,2.5,0,Math.PI*2),n.fillStyle=d&&L.value!==void 0?k((L.value-d.lo)/(d.hi-d.lo||1)):ee(L.group),n.fill())}n.globalAlpha=1;const B=s.marks;if(B.type==="scatter"&&(B.identity||B.fits)){const T=(E,j)=>[w+c.frac(E)*f,v+(1-p.frac(j))*r],L=(E,j,b)=>{n.save(),n.beginPath(),n.rect(w,v,f,r),n.clip(),n.strokeStyle=j,n.lineWidth=2,n.setLineDash(b),n.beginPath();for(let ie=0;ie<=60;ie++){const We=ie/60,Ge=c.log?10**(Math.log10(c.lo)+(Math.log10(c.hi)-Math.log10(c.lo))*We):c.lo+(c.hi-c.lo)*We,[Le,fe]=T(Ge,E(Ge));ie===0?n.moveTo(Le,fe):n.lineTo(Le,fe)}n.stroke(),n.restore()};B.identity&&L(E=>E,e.textMuted,[6,4]);for(const E of B.fits??[])O.size>0&&!O.has(E.group)&&!d||L(j=>E.intercept+E.slope*j,d?e.text:ee(E.group),[])}const F=ge?le.find(T=>T.p===ge):void 0;if(F&&(n.beginPath(),n.arc(F.cx,F.cy,5,0,Math.PI*2),n.lineWidth=2,n.strokeStyle=e.text,n.stroke()),he.size>0){n.lineWidth=1.5,n.strokeStyle=e.text;let T=0;for(const L of he)if(!(O.size>0&&!O.has(L.group))){if(++T>vt)break;n.beginPath(),n.arc(w+c.frac(L.x)*f,v+(1-p.frac(L.y))*r,4,0,Math.PI*2),n.stroke()}}}function un(){if(Ee=void 0,!s||s.marks?.type!=="histogram"||!s.x)return;const e=s.marks.values,l=e.flat();if(l.length===0)return;const a=ue(Fe(Re(l,s.x),Be(s.x),s.x),s.x),f=Math.max(1,s.spec.bins??co),r=a.log?Math.log10(a.lo):a.lo,h=a.log?Math.log10(a.hi):a.hi,n=Array.from({length:f+1},(g,c)=>{const p=r+(h-r)*c/f;return a.log?10**p:p}),t=e.map(()=>new Array(f).fill(0));e.forEach((g,c)=>{for(const p of g){const A=a.log?p>0?Math.log10(p):NaN:p;if(!Number.isFinite(A)||A<r||A>h)continue;const S=Math.min(f-1,Math.floor((A-r)/(h-r)*f));t[c][S]++}});const o=s.spec.histNorm==="percent",d=o?t.map(g=>{const c=g.reduce((p,A)=>p+A,0);return g.map(p=>c?p/c*100:0)}):t;let k=0;const x=e.map(()=>{});if(s.spec.normalCurve&&!a.log){const g=(n[n.length-1]-n[0])/f;e.forEach((c,p)=>{const A=c.filter(Number.isFinite);if(A.length<2)return;const S=A.reduce((P,u)=>P+u,0)/A.length,y=Math.sqrt(Rn(A,S));y>0&&(x[p]={mean:S,sd:y},k=Math.max(k,(o?100:t[p].reduce((P,u)=>P+u,0))*g/(y*Math.sqrt(2*Math.PI))))})}Ee={edges:n,counts:t,heights:d,normal:x,maxCount:Math.max(o?.001:1,k,...d.map(g=>Te(g)))}}function hn(){if(!s||!C||!Ee||!s.x||!s.y)return;Xe(C,te??0);const e=je(W),{w:l,h:a,plotW:f,plotH:r}=Ne(),h=De(C,W,l,a);if(!h)return;const{ctx:n}=h,{edges:t,counts:o,heights:d,normal:k,maxCount:x}=Ee,g=ue([t[0],t[t.length-1]],{...s.x,min:t[0],max:t[t.length-1]}),c=ue([0,x],{label:s.y.label,scale:"linear",min:0,max:x*1.05});Z={x:g,y:c,plotW:f,plotH:r},Ke(n,e,l,a,f,r,g,c,s.x,s.y),ze(n,e.limitLine,f,r,g,Be(s.x),s.x,"x"),Je(n,f,r);const p=o.map((u,i)=>i).filter(u=>O.size===0||O.has(u)),A=p.length>1,S=p.length>=3,y=u=>w+Math.min(g.frac(t[u]),g.frac(t[u+1]))*f,P=u=>w+Math.max(g.frac(t[u]),g.frac(t[u+1]))*f;for(const u of p){if(S){n.fillStyle=ee(u),n.strokeStyle=ee(u),n.lineWidth=1.75,n.lineJoin="round",n.globalAlpha=.1,d[u].forEach((M,B)=>{if(M===0)return;const F=v+(1-c.frac(M))*r;n.fillRect(y(B),F,Math.max(1,P(B)-y(B)),v+r-F)}),n.globalAlpha=1,n.beginPath();let i=!1;d[u].forEach((M,B)=>{const F=v+(1-c.frac(M))*r;i||(n.moveTo(y(B),v+r),i=!0),n.lineTo(y(B),F),n.lineTo(P(B),F),B===d[u].length-1&&n.lineTo(P(B),v+r)}),n.stroke();continue}n.globalAlpha=A?.55:.85,n.fillStyle=ee(u),d[u].forEach((i,M)=>{if(i===0)return;const B=v+(1-c.frac(i))*r;n.fillRect(y(M),B,Math.max(1,P(M)-y(M)-1),v+r-B)})}if(n.globalAlpha=1,s.spec.normalCurve&&!g.log){const u=(t[t.length-1]-t[0])/(t.length-1);for(const i of p){const M=k[i];if(!M)continue;const{mean:B,sd:F}=M,T=(s.spec.histNorm==="percent"?100:o[i].reduce((L,E)=>L+E,0))*u;n.save(),n.beginPath(),n.rect(w,v,f,r),n.clip(),n.strokeStyle=ee(i),n.lineWidth=2,n.beginPath();for(let L=0;L<=120;L++){const E=t[0]+(t[t.length-1]-t[0])*L/120,j=Math.exp(-.5*((E-B)/F)**2)/(F*Math.sqrt(2*Math.PI)),b=w+g.frac(E)*f,ie=v+(1-c.frac(T*j))*r;L===0?n.moveTo(b,ie):n.lineTo(b,ie)}n.stroke(),n.restore()}}if(ye>=0){const u=w+Math.min(g.frac(t[ye]),g.frac(t[ye+1]))*f,i=w+Math.max(g.frac(t[ye]),g.frac(t[ye+1]))*f;n.lineWidth=1.5,n.strokeStyle=e.text,n.strokeRect(u,v,Math.max(1,i-u),r)}}function dn(){const e=s?.marks;if(!s||!C||!s.x||!s.y||!e||e.type!=="box"&&e.type!=="bar")return;Xe(C,te??0);const l=je(W),a=s.spec.orientation==="horizontal",f=e.nest?e.nest.parts.map($=>$[$.length-1]):e.categories,r=e.categories.length,h=s.groups.length,n=$=>O.size===0||O.has($),t=e.type==="bar"&&!!e.stack,o=($,N)=>e.type==="bar"?Number.isFinite(e.values[$][N]):e.cells[$][N].length>0,d=Array.from({length:r},($,N)=>Array.from({length:h},(I,G)=>G).filter(I=>n(I)&&o(I,N))),k=categoryLayout(t?d.map($=>$.length?[0]:[]):d),x=C.getContext("2d"),g=$=>x?x.measureText($).width:$.length*6;x&&(x.font=`${Q(-1)}px system-ui, sans-serif`);const c=($,N)=>$.length>N?`${$.slice(0,N-1)}\u2026`:$;let p=!1,A={inner:0,outer:[]},S=0;if(a){A={inner:Math.min(200,Te(f.map(N=>g(c(N,28))))),outer:e.nest?e.nest.levels.slice(0,-1).map((N,I)=>Math.min(140,Te(e.nest.parts.map(G=>g(c(G[I],20)))))):[]},w=Math.max(Wt,30+A.inner+6+A.outer.reduce((N,I)=>N+I+10,0)),e.cumulative&&(v=Rt+18),Ye=Ue;const $=Math.max(1,...d.map(N=>t?Math.min(1,N.length):N.length));S=Math.max(22,$*ao/.8)}else{const $=Te(f.map(G=>g(c(G,22)))),N=Math.max(10,R.clientWidth-w-xt)/Math.max(1,r);p=$+8>N;const I=e.nest?e.nest.levels.length-1:0;Ye=(p?Ue+Math.min(110,$*.72):Ue)+I*wt}const y=e.type==="bar"&&!t&&!!s.spec.valueLabels,P=s.y.unit,u=$=>J($,P),i=y?Te(e.values.flat().filter(Number.isFinite).map($=>g(u($))).concat(0))+8:0,M=a?i:e.cumulative?40:0,{w:B,h:F,plotW:T,plotH:L}=Ne(),E=Math.max(10,T-M),j=De(C,W,B,F);if(!j)return;const{ctx:b}=j,ie=$=>$.filter(Number.isFinite),We=Be(s.y),Ge=t?e.categories.map(($,N)=>e.values.reduce((I,G,Y)=>I+(n(Y)&&Number.isFinite(G[N])?G[N]:0),0)):[],Le=e.type==="bar"?[...t?Ge:ie(e.values.flat()),0]:ie(e.cells.flat(2));b.font=`${Q(-1)}px system-ui, sans-serif`;const fe=a?i:Q(-1)+6;if(y&&!a&&Le.length){const $=Te(Le);$>0&&Le.push($*(1+fe/Math.max(1,L-fe)))}const Ae=ue(Fe(Re(Le,s.y),We,s.y),s.y,{floorAtZero:e.type==="bar"});Z={x:Ae,y:Ae,plotW:E,plotH:L};const it=a?L:E,Qe=a&&r*S>L+.5,at=Qe?r*S:it,rt=Qe?Math.max(0,Math.min(lt,at-it)):0;lt=rt,$n(Qe,at,rt,E+M,L),a?cn(b,l,B,F,E,L,{labels:f,rotate:!1,nest:e.nest?.parts},A,Ae,s.x,s.y,{along:at,offset:rt}):Ke(b,l,B,F,E,L,{labels:f,rotate:p,nest:e.nest?.parts},Ae,s.x,s.y);const xe=at/r,Ce=k.width*xe;ne={slotW:xe,n:r,plotW:at,layout:k,across:a,offset:rt,view:it};const oe=$=>a?w+Ae.frac($)*E:v+(1-Ae.frac($))*L,ve=$=>a?v+$-rt:w+$,Lt=Ae.log?a?w:v+L:oe(0),et=($,N,I,G)=>a?[Math.min(I,G),ve($),Math.max(1,Math.abs(G-I)),N-$]:[ve($),Math.min(I,G),N-$,Math.max(1,Math.abs(G-I))],At=($,N,I,G)=>{b.beginPath(),a?(b.moveTo(N,ve($)),b.lineTo(G,ve(I))):(b.moveTo(ve($),N),b.lineTo(ve(I),G)),b.stroke()},tt=$=>a?$>=Lt:$<=Lt,En=$=>a?Q(-1)<=Ce+4:b.measureText(u($)).width<=Ce+6,Gt=y&&e.values.every($=>$.every(N=>!Number.isFinite(N)||En(N)));if(Gt){for(let $=0;$<h;$++)if(n($))for(let N=0;N<r;N++){const I=k.left(N,$),G=e.values[$][N];if(I===void 0||!Number.isFinite(G))continue;const Y=ve(N*xe+I*xe+Ce/2),H=b.measureText(u(G)).width,X=oe(G);a?qe.push(tt(X)?{x0:X,x1:X+H+6,y0:Y-fe/2,y1:Y+fe/2}:{x0:X-H-6,x1:X,y0:Y-8,y1:Y+8}):qe.push(tt(X)?{x0:Y-H/2,x1:Y+H/2,y0:X-fe,y1:X}:{x0:Y-H/2,x1:Y+H/2,y0:X,y1:X+fe})}}const Bn=ze(b,l.limitLine,E,L,Ae,We,s.y,a?"x":"y",{halo:l.bg}),Wn=Je(b,E,L,Qe);if(t){ne.stacks=[];for(let $=0;$<r;$++){const N=k.left($,0),I=[];if(ne.stacks.push(I),N===void 0)continue;let G=0;for(let Y=0;Y<h;Y++){const H=e.values[Y][$];if(!n(Y)||!Number.isFinite(H)||H<=0)continue;const X=oe(G),ae=oe(G+H);b.fillStyle=ee(Y),b.globalAlpha=.9;const re=$*xe+N*xe;b.fillRect(...et(re+1,re+Ce-1,X,ae)),b.globalAlpha=1,I.push({g:Y,p0:Math.min(X,ae),p1:Math.max(X,ae)}),G+=H}}}for(let $=0;$<h&&!t;$++){if(O.size>0&&!O.has($))continue;const N=ee($);for(let I=0;I<r;I++){const G=k.left(I,$);if(G===void 0)continue;const Y=I*xe+G*xe;if(e.type==="bar"){const H=e.values[$][I];if(!Number.isFinite(H))continue;const X=oe(H);if(b.fillStyle=N,b.globalAlpha=.9,b.fillRect(...et(Y+1,Y+Ce-1,Lt,X)),b.globalAlpha=1,Gt){b.fillStyle=l.text,b.font=`${Q(-1)}px system-ui, sans-serif`;const ae=ve(Y+Ce/2);a?(b.textBaseline="middle",b.textAlign=tt(X)?"left":"right",b.fillText(u(H),X+(tt(X)?3:-3),ae)):(b.textAlign="center",b.textBaseline=tt(X)?"bottom":"top",b.fillText(u(H),ae,X+(tt(X)?-2:2)))}}else{const H=e.cells[$][I];if(H.length===0)continue;const X=Y+Ce/2,ae=Math.max(3,Math.min(Ce*.7,46)),re=Ut(Float64Array.from(H));if(b.strokeStyle=N,b.fillStyle=N,b.lineWidth=1.5,H.length===1){b.beginPath(),a?b.arc(oe(re.median),ve(X),3,0,Math.PI*2):b.arc(ve(X),oe(re.median),3,0,Math.PI*2),b.fill();continue}At(X,oe(re.min),X,oe(re.max));for(const Dt of[re.min,re.max])At(X-ae/4,oe(Dt),X+ae/4,oe(Dt));b.globalAlpha=.35,b.fillRect(...et(X-ae/2,X+ae/2,oe(re.q1),oe(re.q3))),b.globalAlpha=1,b.strokeRect(...et(X-ae/2,X+ae/2,oe(re.q1),oe(re.q3))),b.lineWidth=2.5,At(X-ae/2,oe(re.median),X+ae/2,oe(re.median))}}}Wn(),Bn();const Xt=se?k.left(se.c,t?0:se.g):void 0;if(se&&Xt!==void 0){Qe&&(b.save(),b.beginPath(),b.rect(w-1,v,E+2,L),b.clip()),b.lineWidth=1.5,b.strokeStyle=l.text;const $=se.c*xe+Xt*xe,N=t?ne.stacks?.[se.c]?.find(I=>I.g===se.g):void 0;t&&N?b.strokeRect(...et($,$+Ce,N.p0,N.p1)):t||b.strokeRect(...et($,$+Ce,a?w:v,a?w+E:v+L)),Qe&&b.restore()}if(e.cumulative){const $=I=>a?w+I/100*E:v+(1-I/100)*L,N=(I,G)=>a?[$(G),ve((I+.5)*xe)]:[ve((I+.5)*xe),$(G)];if(b.save(),b.strokeStyle=l.text,b.fillStyle=l.text,b.lineWidth=1.5,b.beginPath(),e.cumulative.forEach((I,G)=>{const[Y,H]=N(G,I);G===0?b.moveTo(Y,H):b.lineTo(Y,H)}),b.stroke(),e.cumulative.forEach((I,G)=>{const[Y,H]=N(G,I);b.beginPath(),b.arc(Y,H,2.5,0,Math.PI*2),b.fill()}),b.strokeStyle=l.border,b.lineWidth=1,b.beginPath(),a?(b.moveTo(w,v),b.lineTo(w+E,v)):(b.moveTo(w+E,v),b.lineTo(w+E,v+L)),b.stroke(),b.fillStyle=l.textMuted,b.font=`${Q(-1)}px system-ui, sans-serif`,a){b.textAlign="center",b.textBaseline="bottom";for(const I of[0,25,50,75,100])b.fillText(`${I}%`,$(I),v-3)}else{b.textAlign="left",b.textBaseline="middle";for(const I of[0,25,50,75,100])b.fillText(`${I}%`,w+E+4,$(I))}b.restore()}}function mn(e){const l=s?.setTests,a=l?l.labels.indexOf(e):-1;return a>=0?{test:l.tests[a]}:{}}function $t(e){if(!ne||!C)return null;const l=C.getBoundingClientRect(),a=ne.across?e.clientY-l.top-v:e.clientX-l.left-w;if(a<0||a>ne.view)return null;const f=a+ne.offset,r=Math.min(ne.n-1,Math.floor(f/ne.slotW)),h=ne.layout.groupAt(r,(f-r*ne.slotW)/ne.slotW);if(h===void 0)return null;if(!ne.stacks)return{g:h,c:r};const n=ne.across?e.clientX-l.left:e.clientY-l.top,t=ne.stacks[r]?.find(o=>n>=o.p0&&n<=o.p1);return t?{g:t.g,c:r}:null}function pn(e,l,a){let f=0,r=0,h=0;for(const o of l)o===100&&f++;if(e.failSide)for(const o of a){const d=e.failSide[o];d<0?r++:d>0&&h++}const n=o=>o.toLocaleString("en-GB"),t=r+h>0?` (${n(r)} below the low limit \xB7 ${n(h)} above the high limit)`:"";return`${n(f)} pass \xB7 ${n(l.length-f)} fail${t} \xB7 n = ${n(l.length)}`}function gn(e){const l=s,a=l.marks,f=l.level==="die"?"dies":"wafers",r=a.cells[e.g][e.c],h=a.nest?a.nest.parts[e.c]:[a.categories[e.c]],n=l.groups.length>1&&!h.includes(He(e.g))?` \xB7 ${D(He(e.g))}`:"",t=`<strong>${D(a.categories[e.c])}${n}</strong><br>`,o=l.y?.unit;if(r.length===0)return`${t}no ${f}`;const d=`${r.length.toLocaleString("en-GB")} ${f}`,k=(a.categoryItems&&m.onOpenWafer?`<br><em>click to ${D(m.openActionLabel??"open this wafer")}</em>`:"")+(a.categoryItems&&m.onWaferMenu?Et:"");if(a.type==="bar"){const g=a.values[e.g][e.c],c=a.cumulative?`<br>${D(`cumulative: ${a.cumulative[e.c].toFixed(1)}% of the whole`)}`:"";return`${t}${D(`${l.y.label}: ${J(g,o)}`)}<br>${D(a.passFail?pn(a.passFail,r,a.cellUnits[e.g][e.c]):d)}${c}${k}`}const x=Ut(Float64Array.from(r));return`${t}${["max","q3","median","q1","min"].map(g=>D(`${{max:"max",q3:"upper quartile",median:"median",q1:"lower quartile",min:"min"}[g]}: ${J(x[g],o)}`)).join("<br>")}<br>${D(d)}${k}`}function yn(){const e=s?.marks;if(!s||!C||!s.x||!s.y||!e||e.type!=="line")return;Xe(C,te??0);const l=je(W),{w:a,h:f,plotW:r,plotH:h}=Ne(),n=De(C,W,a,f);if(!n)return;const{ctx:t}=n,o=Be(s.x),d=Be(s.y),k=ue(Fe(Re(e.xs,s.x),o,s.x),s.x),x=e.band?[...e.band.lo.flat(),...e.band.hi.flat()]:[],g=ue(Fe(Re([...e.values.flat(),...x].filter(Number.isFinite),s.y),d,s.y),s.y);if(Z={x:k,y:g,plotW:r,plotH:h},Ke(t,l,a,f,r,h,k,g,s.x,s.y),ze(t,l.limitLine,r,h,k,o,s.x,"x"),ze(t,l.limitLine,r,h,g,d,s.y,"y"),Je(t,r,h),de=e.xs.map(c=>w+k.frac(c)*r),e.band){const c=p=>v+(1-g.frac(p))*h;e.band.lo.forEach((p,A)=>{if(O.size>0&&!O.has(A))return;const S=e.band.hi[A];t.save(),t.fillStyle=ee(A),t.globalAlpha=.16;let y=[];const P=()=>{if(y.length===1){const u=y[0],i=de[u];t.globalAlpha=.55,t.strokeStyle=ee(A),t.lineWidth=2,t.beginPath(),t.moveTo(i,c(p[u])),t.lineTo(i,c(S[u])),t.moveTo(i-4,c(p[u])),t.lineTo(i+4,c(p[u])),t.moveTo(i-4,c(S[u])),t.lineTo(i+4,c(S[u])),t.stroke(),t.globalAlpha=.16}else if(y.length){t.beginPath(),y.forEach((u,i)=>{i===0?t.moveTo(de[u],c(S[u])):t.lineTo(de[u],c(S[u]))});for(let u=y.length-1;u>=0;u--)t.lineTo(de[y[u]],c(p[y[u]]));t.closePath(),t.fill()}y=[]};p.forEach((u,i)=>{Number.isFinite(u)&&Number.isFinite(S[i])?y.push(i):P()}),P(),t.restore()})}e.values.forEach((c,p)=>{if(O.size>0&&!O.has(p))return;t.strokeStyle=ee(p),t.fillStyle=ee(p),t.lineWidth=2;let A=!1;t.beginPath(),c.forEach((S,y)=>{if(!Number.isFinite(S)){A=!1;return}const P=de[y],u=v+(1-g.frac(S))*h;A?t.lineTo(P,u):t.moveTo(P,u),A=!0}),t.stroke(),c.forEach((S,y)=>{Number.isFinite(S)&&(t.beginPath(),t.arc(de[y],v+(1-g.frac(S))*h,ke===y?4.5:3,0,Math.PI*2),t.fill())})}),ke>=0&&(t.lineWidth=1,t.strokeStyle=l.textMuted,t.beginPath(),t.moveTo(de[ke],v),t.lineTo(de[ke],v+h),t.stroke())}function Pt(e){const l=s?.marks;if(!l||l.type!=="line"||!C||!s?.x||de.length===0)return null;const a=e.clientX-C.getBoundingClientRect().left;let f=-1,r=24;if(de.forEach((t,o)=>{const d=Math.abs(t-a);d<r&&(r=d,f=o)}),ke=f,f<0)return null;const h=s.level==="die"?"dies":"wafers",n=l.values.map((t,o)=>({g:o,v:t[f],n:l.counts[o][f]})).filter(t=>Number.isFinite(t.v)&&(O.size===0||O.has(t.g)));return`<strong>${D(`${s.x.label}: ${J(l.xs[f],s.x.unit)}`)}</strong><br>`+n.map(t=>{const o=l.band?.lo[t.g][f],d=l.band?.hi[t.g][f],k=o!==void 0&&d!==void 0&&Number.isFinite(o)&&Number.isFinite(d)?`, band ${J(o,s.y.unit)} to ${J(d,s.y.unit)}`:"";return D(`${s.groups.length>1?`${He(t.g)}: `:""}${J(t.v,s.y.unit)}${k} (${t.n.toLocaleString("en-GB")} ${h})`)}).join("<br>")+(m.onWaferMenu&&Ft(f)!==void 0?Et:"")}function Ft(e){const l=s?.marks;if(l?.type!=="line")return;const a=new Set(l.cellUnits.flatMap((f,r)=>O.size>0&&!O.has(r)?[]:(f[e]??[]).map(h=>l.unit(h).item)));return a.size===1?[...a][0]:void 0}function bn(){const e=s?.marks;if(!s||!C||!s.x||!s.y||e?.type!=="probability")return;Xe(C,te??0);const l=je(W),{w:a,h:f,plotW:r,plotH:h}=Ne(),n=De(C,W,a,f);if(!n)return;const{ctx:t}=n,o=s.spec.probabilityAxis==="linear",d=e.values.map((T,L)=>L).filter(T=>e.values[T].length>0&&(O.size===0||O.has(T))),k=Math.max(1,...d.map(T=>e.values[T].length)),x=Math.min(.01,ct(0,k)),g=T=>o?T:qt(T),c=o?0:g(x),p=o?1:g(1-x),A=o?0:(p-c)*.03,S=ue([c-A,p+A],{label:s.y.label,scale:"linear",min:c-A,max:p+A}),y=o?[0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1]:fo.filter(T=>T>=x&&T<=1-x),P=[],u=T=>v+(1-S.frac(T))*h,i=y.indexOf(.5);P.push({at:g(.5),label:sn(.5)});for(const T of[y.slice(i+1),y.slice(0,i).reverse()]){let L=u(g(.5));for(const E of T){const j=u(g(E));Math.abs(j-L)<16||(P.push({at:g(E),label:sn(E)}),L=j)}}const M=Be(s.x),B=ue(Fe(Re(d.flatMap(T=>[e.values[T][0],e.values[T][e.values[T].length-1]]),s.x),M,s.x),s.x);Z={x:B,y:S,plotW:r,plotH:h},Ke(t,l,a,f,r,h,B,S,s.x,s.y,P),ze(t,l.limitLine,r,h,B,M,s.x,"x"),Je(t,r,h),le=[],t.save(),t.beginPath(),t.rect(w,v,r,h),t.clip();for(const T of d){const L=e.values[T],E=L.length,j=uo(E);if(t.strokeStyle=ee(T),t.fillStyle=ee(T),t.lineWidth=1.5,t.beginPath(),j.forEach((b,ie)=>{const We=w+B.frac(L[b])*r,Ge=u(g(ct(b,E)));ie===0?t.moveTo(We,Ge):t.lineTo(We,Ge);const Le=T*2**26+b;let fe=Mt.get(Le);if(!fe){const{item:Ae,die:it}=e.unit(e.units[T][b]);fe={x:L[b],y:ct(b,E),group:T,item:Ae,die:it},Mt.set(Le,fe)}le.push({p:fe,cx:We,cy:Ge})}),t.stroke(),E<=300)for(const b of j)t.beginPath(),t.arc(w+B.frac(L[b])*r,u(g(ct(b,E))),2.5,0,Math.PI*2),t.fill()}if(t.restore(),he.size>0){t.lineWidth=1.5,t.strokeStyle=l.text;let T=0;for(const L of he)if(!(O.size>0&&!O.has(L.group))){if(++T>vt)break;t.beginPath(),t.arc(w+B.frac(L.x)*r,u(g(L.y)),4,0,Math.PI*2),t.stroke()}}const F=ge?le.find(T=>T.p===ge):void 0;F&&(t.beginPath(),t.arc(F.cx,F.cy,5,0,Math.PI*2),t.lineWidth=2,t.strokeStyle=l.text,t.stroke())}function xn(e){const l=s,f=l.marks.values[e.group].length,r=Math.round(e.y*f+.5),h=m.waferLabel(e.item),n=e.die&&e.die.x!==void 0?`die (${e.die.x}, ${e.die.y})`:"",t=[h,n].filter(Boolean).join(" \xB7 ");return(t?`<strong>${D(t)}</strong><br>`:"")+D(`${l.x.label}: ${J(e.x,l.x.unit)}`)+"<br>"+D(`rank ${r.toLocaleString("en-GB")} of ${f.toLocaleString("en-GB")}, plotted at ${+(e.y*100).toPrecision(3)}%`)+(l.groups.length>1?`<br>${D(He(e.group))}`:"")+(m.onOpenWafer?`<br><em>click to ${D(m.openActionLabel??"open this wafer")}</em>`:"")+(m.onWaferMenu&&!e.die?Et:"")}let _e,be=null;function vn(){const e=s?.marks;if(!s||!C||e?.type!=="heatmap")return;Xe(C,te??0);const l=je(W),a=C.getContext("2d");let f=0,r=0;a&&(a.font=`${Q(-1)}px system-ui, sans-serif`,f=Te(e.rows.map(u=>a.measureText(u.length>24?`${u.slice(0,23)}\u2026`:u).width)),r=Te(e.cols.map(u=>a.measureText(u.length>22?`${u.slice(0,21)}\u2026`:u).width)));const h=Math.min(180,Math.max(w,f+24)),n=Math.max(8,R.clientWidth-h-xt)/Math.max(1,e.cols.length),t=r+8>n;Ye=t?Ue+Math.min(110,r*.72):Ue;const{w:o,h:d}=Ne(),k=Math.max(10,o-h-xt),x=Math.max(10,d-v-Ye),g=De(C,W,o,d);if(!g)return;const{ctx:c}=g,p=k/e.cols.length,A=x/e.rows.length;_e={left:h,cw:p,ch:A,rows:e.rows.length,cols:e.cols.length};const S=s.colorScale,y=bt(s.spec.colorGradient,s.spec.colorReverse,S?.diverging,S?.rating),P=S&&S.hi-S.lo||1;c.font=`${Q(-1)}px system-ui, sans-serif`;for(let u=0;u<e.rows.length;u++)for(let i=0;i<e.cols.length;i++){const M=e.values[u][i],B=h+i*p,F=v+u*A,T=Number.isFinite(M)&&S?y((M-S.lo)/P):l.track;if(c.fillStyle=T,c.fillRect(B,F,p,A),Number.isFinite(M)&&p>=40&&A>=16){const L=so(T);c.fillStyle=L?"#1a1a1a":"#fff",c.textAlign="center",c.textBaseline="middle",Ct(c,Ot(M,s.y?.unit,S?.integer),B+p/2,F+A/2,L?"rgba(255,255,255,0.55)":"rgba(0,0,0,0.55)")}}c.lineWidth=1,c.setLineDash([]),c.strokeStyle=l.border,c.beginPath();for(let u=1;u<e.rows.length;u++){const i=Math.round(v+u*A)+.5;c.moveTo(h,i),c.lineTo(h+k,i)}for(let u=1;u<e.cols.length;u++){const i=Math.round(h+u*p)+.5;c.moveTo(i,v),c.lineTo(i,v+x)}c.stroke(),c.strokeStyle=l.textMuted,c.strokeRect(h+.5,v+.5,k-1,x-1),c.beginPath();for(let u=0;u<e.rows.length;u++){const i=Math.round(v+(u+.5)*A)+.5;c.moveTo(h-4,i),c.lineTo(h,i)}for(let u=0;u<e.cols.length;u++){const i=Math.round(h+(u+.5)*p)+.5;c.moveTo(i,v+x),c.lineTo(i,v+x+4)}c.stroke(),be&&(c.lineWidth=2,c.strokeStyle=l.text,c.strokeRect(h+be.c*p,v+be.r*A,p,A)),c.fillStyle=l.textMuted,c.textAlign="right",c.textBaseline="middle",e.rows.forEach((u,i)=>c.fillText(u.length>24?`${u.slice(0,23)}\u2026`:u,h-8,v+(i+.5)*A)),e.cols.forEach((u,i)=>{const M=u.length>22?`${u.slice(0,21)}\u2026`:u,B=h+(i+.5)*p;t?(c.save(),c.translate(B,v+x+8),c.rotate(-Math.PI/4),c.textAlign="right",c.textBaseline="middle",c.fillText(M,0,0),c.restore()):(c.textAlign="center",c.textBaseline="top",c.fillText(M,B,v+x+7))}),c.font=`${Q(0)}px system-ui, sans-serif`,c.fillStyle=l.text,c.textAlign="center",c.textBaseline="bottom",c.fillText(s.x?.label??"",h+k/2,d-6)}function zt(e){if(!_e||!C)return null;const l=C.getBoundingClientRect(),a=Math.floor((e.clientX-l.left-_e.left)/_e.cw),f=Math.floor((e.clientY-l.top-v)/_e.ch);return f>=0&&f<_e.rows&&a>=0&&a<_e.cols?{r:f,c:a}:null}function Mn(e){const l=s,a=l.marks,f=a.values[e.r][e.c],r=a.cellUnits[e.r][e.c].length,h=l.level==="die"?"dies":"wafers";return`<strong>${D(`${a.rows[e.r]} \xB7 ${a.cols[e.c]}`)}</strong><br>`+(Number.isFinite(f)?D(`${l.y?.label??"Count"}: ${Ot(f,l.y?.unit,l.colorScale?.integer)}`)+"<br>":"")+D(`${r.toLocaleString("en-GB")} ${h}`)+(r&&m.onSelectPoints?"<br><em>click to chart or tabulate them</em>":"")}function kn(){const e=s?.marks;if(!s||!C||!s.x||!s.y||e?.type!=="control")return;Xe(C,te??0);const l=je(W),{w:a,h:f,plotW:r,plotH:h}=Ne(),n=De(C,W,a,f);if(!n)return;const{ctx:t}=n,o=e.limits,d=Be(s.y),k=ue(Re(e.xs,s.x),s.x),x=ue(Fe(Re([...e.values,o.ucl,o.lcl],s.y),d,s.y),s.y);Z={x:k,y:x,plotW:r,plotH:h},Ke(t,l,a,f,r,h,k,x,s.x,s.y),ze(t,l.limitLine,r,h,x,d,s.y,"y"),Je(t,r,h);const g=i=>w+k.frac(e.xs[i])*r,c=i=>v+(1-x.frac(i))*h;t.save(),t.beginPath(),t.rect(w,v,r,h),t.clip();const p=(i,M,B,F=1)=>{t.globalAlpha=B,t.setLineDash(M),t.lineWidth=F,t.strokeStyle=l.text,t.beginPath(),t.moveTo(w,c(i)),t.lineTo(w+r,c(i)),t.stroke()};for(const i of[-2,-1,1,2])p(o.centre+i*o.sigma,[2,4],.25);p(o.centre,[],.6,1.5),p(o.ucl,[6,4],.85),p(o.lcl,[6,4],.85),t.restore(),t.fillStyle=l.textMuted,t.font=`${Q(-1)}px system-ui, sans-serif`,t.textAlign="right",t.textBaseline="bottom";const A=i=>J(i,s.y.unit);if(t.fillText(`UCL ${A(o.ucl)}`,w+r-4,c(o.ucl)-2),t.textBaseline="top",t.fillText(`LCL ${A(o.lcl)}`,w+r-4,c(o.lcl)+2),t.textBaseline="bottom",t.fillText(`Centre ${A(o.centre)}`,w+r-4,c(o.centre)-2),o.baseline<e.values.length){const i=(g(o.baseline-1)+g(o.baseline))/2;t.save(),t.strokeStyle=l.textMuted,t.setLineDash([2,3]),t.beginPath(),t.moveTo(i,v),t.lineTo(i,v+h),t.stroke(),t.restore(),t.textAlign="left",t.textBaseline="top",t.fillText("limits from here \u2190",w+4,v+2)}t.strokeStyle=ee(0),t.lineWidth=1.5,t.setLineDash([]);const S=O.size>0,y=i=>e.group?.[i]??0;if(!S)t.beginPath(),e.values.forEach((i,M)=>{M===0?t.moveTo(g(M),c(i)):t.lineTo(g(M),c(i))}),t.stroke();else{for(const i of[!1,!0]){t.globalAlpha=i?1:.2,t.beginPath();for(let M=1;M<e.values.length;M++)(O.has(y(M-1))&&O.has(y(M)))===i&&(t.moveTo(g(M-1),c(e.values[M-1])),t.lineTo(g(M),c(e.values[M])));t.stroke()}t.globalAlpha=1}const P=new Map(e.violations.map(i=>[i.index,i.rules]));le=[],e.values.forEach((i,M)=>{const B=P.get(M),F=y(M),T=S&&O.has(F),L=S&&!T;t.globalAlpha=L?.2:1,t.beginPath(),t.arc(g(M),c(i),(B?4.5:3)+(T?1.5:0),0,Math.PI*2),t.fillStyle=B?l.alert:ee(F),t.fill(),T&&(t.lineWidth=1.5,t.strokeStyle=l.text,t.stroke()),B&&(t.fillStyle=l.alert,t.textAlign="center",t.textBaseline="bottom",t.fillText(B.join(","),g(M),c(i)-(T?8:6))),t.globalAlpha=1,le.push({p:{x:e.xs[M],y:i,group:F,item:e.items[M]},cx:g(M),cy:c(i)})});const u=ge?le.find(i=>i.p===ge):void 0;u&&(t.beginPath(),t.arc(u.cx,u.cy,6,0,Math.PI*2),t.lineWidth=2,t.strokeStyle=l.text,t.stroke())}function Tn(e){const l=s,a=l.marks,f=a.items.indexOf(e.item),r=(e.y-a.limits.centre)/a.limits.sigma,h=a.violations.find(n=>n.index===f)?.rules;return`<strong>${D(m.waferLabel(e.item))}</strong><br>`+D(`${l.y.label}: ${J(e.y,l.y.unit)} (${r>=0?"+":"\u2212"}${Math.abs(r).toFixed(1)}\u03C3)`)+(h?`<br>${D(`breaks rule ${h.map(n=>`${n} (${On[n]})`).join(", ")}`)}`:"")+(m.onOpenWafer?`<br><em>click to ${D(m.openActionLabel??"open this wafer")}</em>`:"")}function wn(){const e=s?.marks;if(!s||!C||!s.x||!s.y||e?.type!=="goal")return;Xe(C,te??0);const l=je(W),{w:a,h:f,plotW:r,plotH:h}=Ne(),n=De(C,W,a,f);if(!n)return;const{ctx:t}=n,o=Math.max(.5,...e.points.map(i=>Math.abs(i.x))),d=ue([-o,o],{...s.x,min:s.x.min??-o*1.05,max:s.x.max??o*1.05}),k=ue([0,...e.points.map(i=>i.y),1/6],{...s.y,min:s.y.min??0},{floorAtZero:!0});Z={x:d,y:k,plotW:r,plotH:h},Ke(t,l,a,f,r,h,d,k,s.x,s.y);const x=i=>w+d.frac(i)*r,g=i=>v+(1-k.frac(i))*h;t.save(),t.beginPath(),t.rect(w,v,r,h),t.clip(),t.font=`${Q(-1)}px system-ui, sans-serif`;const c=[[1,[6,4]],[1.33,[]]];for(const[i,M]of c){const B=1/(6*i);t.strokeStyle=l.textMuted,t.lineWidth=1,t.setLineDash(M),t.beginPath(),t.moveTo(x(-.5),g(0)),t.lineTo(x(0),g(B)),t.lineTo(x(.5),g(0)),t.stroke(),t.fillStyle=l.textMuted,t.textAlign="left",t.textBaseline="bottom",t.fillText(`Ppk ${i}`,x(0)+4,g(B)-2)}t.restore(),le=[];const p=e.points.filter(i=>O.size===0||O.has(i.group));for(const i of p)t.beginPath(),t.arc(x(i.x),g(i.y),4,0,Math.PI*2),t.fillStyle=ee(i.group),t.fill(),le.push({p:{x:i.x,y:i.y,group:i.group,item:-1,value:i.testNumber},cx:x(i.x),cy:g(i.y)});const A=p.map(i=>({x0:x(i.x)-5,x1:x(i.x)+5,y0:g(i.y)-5,y1:g(i.y)+5})),S={x0:w,x1:w+r,y0:v,y1:v+h},y=i=>i.x0>=S.x0&&i.x1<=S.x1&&i.y0>=S.y0&&i.y1<=S.y1&&!A.some(M=>i.x0<M.x1&&i.x1>M.x0&&i.y0<M.y1&&i.y1>M.y0),P=Q(-1)+2;t.fillStyle=l.text,t.textBaseline="middle";for(const i of[...p].filter(M=>M.ppk<1.33).sort((M,B)=>M.ppk-B.ppk)){const M=x(i.x),B=g(i.y),F=t.measureText(i.label).width,T=[[M+7,B,"left"],[M-7,B,"right"],[M,B-13,"center"],[M,B+13,"center"]];for(const[L,E,j]of T){const b=j==="left"?L:j==="right"?L-F:L-F/2,ie={x0:b,x1:b+F,y0:E-P/2,y1:E+P/2};if(y(ie)){A.push(ie),t.textAlign=j,t.fillText(i.label,L,E);break}}}const u=ge?le.find(i=>i.p===ge):void 0;u&&(t.beginPath(),t.arc(u.cx,u.cy,6,0,Math.PI*2),t.lineWidth=2,t.strokeStyle=l.text,t.stroke())}function Sn(e){const l=s,f=l.marks.points.find(r=>r.testNumber===e.value&&r.group===e.group);return f?`<strong>${D(f.label)}</strong>${l.groups.length>1?` \xB7 ${D(He(f.group))}`:""}<br>`+D(`Ppk ${f.ppk.toFixed(2)} \xB7 ${f.n.toLocaleString("en-GB")} dies`)+"<br>"+D(`mean ${f.x>=0?"+":"\u2212"}${Math.abs(f.x*100).toFixed(1)}% of the window from its centre, \u03C3 ${(f.y*100).toFixed(1)}% of it (${f.basis==="spec"?"spec":"test"} limits)`):""}function $n(e,l,a,f,r){!Pe||!C||(Pe.style.display=e?"block":"none",e&&(Object.assign(Pe.style,{top:`${C.offsetTop+v}px`,height:`${r}px`,left:`${C.offsetLeft+w+f+1}px`}),Pe.firstElementChild.style.height=`${l}px`,Math.abs(Pe.scrollTop-a)>=1&&(Pe.scrollTop=a)))}function K(){Pe&&(Pe.style.display="none"),dt&&!an()&&(dt.style.maxHeight=Qt(W)?"":`${ro}px`),pe&&m.panel&&(pe.style.display=Qt(W)?"flex":"none"),w=Wt,v=Rt;const e=s?.marks?.type;e==="scatter"?fn():e==="control"?kn():e==="heatmap"?vn():e==="goal"?wn():e==="probability"?bn():e==="histogram"?hn():e==="box"||e==="bar"?dn():e==="line"&&yn(),Nt()}function Pn(e){const l=s,a=m.waferLabel(e.item),f=e.die&&e.die.x!==void 0?`die (${e.die.x}, ${e.die.y})`:"",r=[a,f].filter(Boolean).join(" \xB7 "),h=(n,t)=>`${n.label}: ${J(t,n.unit)}`;return(r?`<strong>${D(r)}</strong><br>`:"")+D(h(l.x,e.x))+"<br>"+D(h(l.y,e.y))+(l.colorScale&&e.value!==void 0?`<br>${D(`${l.colorScale.label}: ${J(e.value,l.colorScale.unit)}`)}`:"")+(l.groups.length>1?`<br>${D(He(e.group))}`:"")+(m.onOpenWafer?`<br><em>click to ${D(m.openActionLabel??"open this wafer")}</em>`:"")}function _t(e){if(!Ee||!Z||!s?.x)return null;const l=C.getBoundingClientRect(),f=(e.clientX-l.left-w)/Z.plotW;if(f<0||f>1)return null;const r=Z.x.reverse?1-f:f,{edges:h,counts:n}=Ee,t=h.length-1,o=Math.min(t-1,Math.max(0,Math.floor(r*t)));ye=o;const d=s.x,k=n.map((x,g)=>({g,n:x[o]})).filter(x=>x.n>0&&(O.size===0||O.has(x.g)));return`<strong>${D(`${J(h[o],d.unit)} to ${J(h[o+1],d.unit)}`)}</strong><br>`+(k.length?k.map(x=>{const g=s.spec.histNorm==="percent"?` (${Ee.heights[x.g][o].toFixed(1)}% of ${s.groups.length>1?"its colour":"them"})`:"";return D(`${s.groups.length>1?`${He(x.g)}: `:""}${x.n.toLocaleString("en-GB")} ${s.level==="die"?"dies":"wafers"}${g}`)}).join("<br>"):"none")}function Ln(e){const l=z.createElement("div");l.dataset.wmapPlotColorbar="1",Object.assign(l.style,{display:"flex",alignItems:"center",gap:me.md,marginTop:me.md,marginBottom:me.xl,fontSize:gt.body,color:nt.label});const a=z.createElement("canvas");a.width=200,a.height=10,Object.assign(a.style,{width:"200px",height:"10px",borderRadius:Fn.control,flex:"0 0 auto"});const f=a.getContext("2d"),r=bt(s?.spec.colorGradient,s?.spec.colorReverse,e.diverging,e.rating);if(f)for(let d=0;d<200;d++)f.fillStyle=r(d/199),f.fillRect(d,0,1,10);const h=d=>s?.marks?.type==="heatmap"?Ot(d,e.unit,e.integer):J(d,e.unit),n=z.createElement("span");n.textContent=h(e.lo);const t=z.createElement("span");t.textContent=h(e.hi);const o=z.createElement("span");return o.textContent=e.label,o.style.color=nt.text,l.append(o,n,a,t),l}const st=(e,l,a)=>l.map(f=>{const{item:r,die:h}=e(f);return{x:0,y:0,group:a,item:r,die:h}});function An(){Ze?.destroy(),Ze=void 0,R.innerHTML="",C=void 0,pe=void 0,te=void 0,ot=[],ge=null,ye=-1,le=[],Z=void 0,ne=void 0,se=null,Pe=void 0,lt=0,dt=void 0,_e=void 0,be=null,de=[],ke=-1,Ye=Ue,Mt=new Map,O.clear(),he.clear();for(const n of ce)n.destroy();ce=[],ut?.(),ut=void 0,R.style.overflowY="hidden",delete W.dataset.wmapPlotPanelsOwner,delete W.dataset.wmapPanelsHover,we(()=>W.querySelector("canvas"));const e=s,l=!!e?.facets&&!m.panel;if(W.style.flex=l?"0 0 auto":"1 1 0px",W.style.gridColumn=l?"1 / -1":"",!e)return;if(U.textContent=m.panel?m.panel.title:Nn(e),W.dataset.wmapChartTitle=U.textContent,e.issues.length||!e.marks){for(const n of e.issues)Jt(R,n);return}const a=z.createElement("div");Object.assign(a.style,{color:nt.label,fontSize:gt.body,marginBottom:me.xs});const f=m.openActionLabel??"open this wafer",r=e.marks.type==="scatter"?(m.onOpenWafer?` \xB7 click a point to ${f}`:"")+(m.onSelectPoints?` \xB7 drag to select ${e.level==="die"?"dies":"wafers"}`:""):e.marks.type==="box"||e.marks.type==="bar"?(m.onOpenWafer&&e.marks.categoryItems?` \xB7 click a ${e.marks.type} to ${f}`:m.onSelectPoints?` \xB7 click to ${m.selectActionLabel??"chart or tabulate what it counts"}`:"")+(e.marks.type==="box"?" \xB7 box: quartiles; whiskers: minimum and maximum":""):e.marks.type==="histogram"&&m.onSelectPoints?" \xB7 click a bar to chart or tabulate its dies":e.marks.type==="probability"?(m.onOpenWafer?` \xB7 click a point to ${f}`:"")+(m.onSelectPoints?` \xB7 drag to select ${e.level==="die"?"dies":"wafers"}`:""):e.marks.type==="line"&&m.onSelectPoints?` \xB7 click a point to ${m.selectActionLabel??"chart or tabulate what it averages"} \xB7 drag across the plot to select ${m.selectRangeLabel??"a run of points"}`:"";a.textContent=m.panel?`${e.population.wafers.toLocaleString("en-GB")} wafer${e.population.wafers===1?"":"s"} \xB7 ${e.population.dies.toLocaleString("en-GB")} dies`:Ht(e)+(e.facets?"":r),m.panel||Dn(a,Kt(e)),Vt(a),te=z.createElement("div"),te.style.display="flow-root",R.appendChild(te),te.appendChild(a);for(const n of e.fitNotes??[]){const t=z.createElement("div");t.dataset.wmapPlotFit="1",t.textContent=n,Object.assign(t.style,{color:nt.text,fontSize:gt.body,marginBottom:me.xxs,fontVariantNumeric:"tabular-nums"}),te.appendChild(t)}for(const n of e.notes){const t=z.createElement("div");Vt(t),t.textContent=n,Object.assign(t.style,{color:nt.warnText,fontSize:gt.body,marginBottom:me.xs}),te.appendChild(t)}if(e.plotted===0){Jt(R,"Nothing to plot: no mark has every value this plot needs.");return}if(e.groups.length>1&&!e.colorRepeatsX&&(pe=z.createElement("div"),pe.dataset.wmapPlotLegend="1",Object.assign(pe.style,{display:m.panel?"none":"flex",flexWrap:"wrap",gap:me.sm,marginTop:me.md,marginBottom:me.xl,overflowY:"auto"}),dt=pe,e.facets&&Object.assign(pe.style,{position:"sticky",top:"0",zIndex:"2",background:nt.menuBg,paddingBottom:me.sm,marginBottom:me.md}),e.groups.forEach((n,t)=>{const o=Vn(n||"(none)",ee(t),z);qn(o.el,W,_,`${n||"(none)"} \u2014 click to filter`),o.el.addEventListener("click",()=>{if(O.has(t)?O.delete(t):O.add(t),ot.forEach((d,k)=>d.setState({selected:O.has(k),dimmed:O.size>0&&!O.has(k)})),ce.length)for(const d of ce)d.setActiveGroups(O);else K()}),ot.push(o),pe.appendChild(o.el)}),ot.forEach((n,t)=>n.setState({selected:O.has(t),dimmed:O.size>0&&!O.has(t)})),te.appendChild(pe)),e.colorScale&&!m.panel&&(pe=Ln(e.colorScale),te.appendChild(pe)),e.facets){const n=z.createElement("div");n.dataset.wmapPlotPanels="1",n.style.isolation="isolate",Object.assign(n.style,{display:"grid",gap:me.md});const t=()=>{n.style.gridTemplateColumns=Ie?`repeat(${Ie}, minmax(0, 1fr))`:`repeat(auto-fill, minmax(${ho}px, 1fr))`};t();const o=Zn("Columns:",[{value:"auto",label:"Auto (0)"},...[1,2,3,4,5].map(x=>({value:String(x),label:`${x} (${x})`}))],Ie?String(Ie):"auto",x=>{Ie=x==="auto"?void 0:Number(x),t()},{hook:"panel-columns",maxWidth:"90px",ownerDocument:z});Object.assign(o.style,{justifyContent:"flex-end",marginBottom:me.sm});const d=o.querySelector("button");R.append(o,n),R.style.overflowY="auto",W.dataset.wmapPlotPanelsOwner="1",ht&&(W.dataset.wmapPanelsHover="1");const k=x=>{if(!ht||x.ctrlKey||x.metaKey||x.altKey||x.shiftKey||x.defaultPrevented||!/^[0-5]$/.test(x.key))return;const g=x.target;typeof g?.closest=="function"&&g.closest('input, textarea, select, [contenteditable="true"], [role="menu"]')||(x.preventDefault(),Ie=x.key==="0"?void 0:Number(x.key),t(),d&&(d.value=Ie?String(Ie):"auto"))};z.addEventListener("keydown",k),ut=()=>z.removeEventListener("keydown",k);for(const x of e.facets){const g=renderPlotChart({...m,panel:{title:x.label}});n.appendChild(g.card),g.setPlot(x.plot),ce.push(g)}we(()=>mo(n));return}C=z.createElement("canvas"),C.style.display="block",C.style.cursor=e.marks.type==="scatter"||e.marks.type==="probability"?"crosshair":"default",R.appendChild(C);const h=m.onWaferMenu?(n,t)=>{n.die||m.onWaferMenu(n.item,Ve(e.spec),t)}:void 0;if(e.marks.type==="scatter"){const n=e.marks,t=!!m.onSelectPoints;Ze=Bt({card:W,canvas:C,tooltip:_,drawn:()=>le,describe:Pn,clickable:()=>!!m.onOpenWafer,onOpen:o=>m.onOpenWafer?.(o.item,Ve(e.spec)),onContextMenu:h,onHover:o=>{ge=o,K()},select:t?{pick:({x0:o,y0:d,x1:k,y1:x})=>Z?(O.size===0?n.points:n.points.filter(c=>O.has(c.group))).filter(c=>{const p=w+Z.x.frac(c.x)*Z.plotW,A=v+(1-Z.y.frac(c.y))*Z.plotH;return p>=o&&p<=k&&A>=d&&A<=x}):[],onPicked:(o,d)=>{he.clear();for(const k of o)he.add(k);K(),o.length>0&&m.onSelectPoints(o,d,C)},onClear:()=>{he.clear(),K()},hasSelection:()=>he.size>0}:void 0})}else if(e.marks.type==="control"||e.marks.type==="goal"){const n=e.marks.type==="control";Ze=Bt({card:W,canvas:C,tooltip:_,drawn:()=>le,describe:n?Tn:Sn,clickable:()=>n&&!!m.onOpenWafer,onOpen:t=>{n&&m.onOpenWafer?.(t.item,Ve(e.spec))},onContextMenu:n?h:void 0,onHover:t=>{ge=t,K()}})}else if(e.marks.type==="probability"){const n=e.marks,t=e.spec.probabilityAxis==="linear";Ze=Bt({card:W,canvas:C,tooltip:_,drawn:()=>le,describe:xn,clickable:()=>!!m.onOpenWafer,onOpen:o=>m.onOpenWafer?.(o.item,Ve(e.spec)),onContextMenu:h,onHover:o=>{ge=o,K()},select:m.onSelectPoints?{pick:({x0:o,y0:d,x1:k,y1:x})=>{if(!Z)return[];const g=[];return n.values.forEach((c,p)=>{O.size>0&&!O.has(p)||c.forEach((A,S)=>{const y=ct(S,c.length),P=w+Z.x.frac(A)*Z.plotW,u=v+(1-Z.y.frac(t?y:qt(y)))*Z.plotH;if(P<o||P>k||u<d||u>x)return;const{item:i,die:M}=n.unit(n.units[p][S]);g.push({x:A,y,group:p,item:i,die:M})})}),g},onPicked:(o,d)=>{he.clear();for(const k of o)he.add(k);K(),o.length>0&&m.onSelectPoints(o,d,C)},onClear:()=>{he.clear(),K()},hasSelection:()=>he.size>0}:void 0})}else if(e.marks.type==="histogram"){un(),C.addEventListener("mousemove",t=>{const o=_t(t);if(!o){_.style.display="none",ye>=0&&(ye=-1,K());return}_.innerHTML=o,_.style.display="block",yt(_,W,t.clientX,t.clientY),K()}),C.addEventListener("mouseleave",()=>{_.style.display="none",ye>=0&&(ye=-1,K())});const n=e.marks;m.onSelectPoints&&(C.style.cursor="pointer",C.addEventListener("click",t=>{if(!Ee||_t(t)===null)return;const o=ye,{edges:d}=Ee,k=Z?.x.log,x=k?Math.log10(d[0]):d[0],g=k?Math.log10(d[d.length-1]):d[d.length-1],c=d.length-1,p=[];n.values.forEach((A,S)=>{if(O.size>0&&!O.has(S))return;const y=[];A.forEach((P,u)=>{const i=k?P>0?Math.log10(P):NaN:P;!Number.isFinite(i)||i<x||i>g||Math.min(c-1,Math.floor((i-x)/(g-x)*c))===o&&y.push(n.units[S][u])});for(const P of st(n.unit,y,S))p.push(P)}),p.length>0&&(_.style.display="none",m.onSelectPoints(p,{x:t.clientX,y:t.clientY},C,{range:{lo:d[o],hi:d[o+1]},...O.size===1?{group:e.groups[[...O][0]]}:{}}))}))}else if(e.marks.type==="heatmap"){const n=e.marks;C.addEventListener("mousemove",t=>{const o=zt(t);if(C.style.cursor=o&&m.onSelectPoints&&n.cellUnits[o.r][o.c].length?"pointer":"default",!o){_.style.display="none",be&&(be=null,K());return}_.innerHTML=Mn(o),_.style.display="block",yt(_,W,t.clientX,t.clientY),(!be||be.r!==o.r||be.c!==o.c)&&(be=o,K())}),C.addEventListener("mouseleave",()=>{_.style.display="none",be&&(be=null,K())}),C.addEventListener("click",t=>{const o=zt(t);if(!o||!m.onSelectPoints)return;const d=n.cellUnits[o.r][o.c];d.length&&(_.style.display="none",m.onSelectPoints(st(n.unit,d,0),{x:t.clientX,y:t.clientY},C))})}else if(e.marks.type==="box"||e.marks.type==="bar"){const n=e.marks;if(e.spec.orientation==="horizontal"){const t=z.createElement("div");t.dataset.wmapRowScroll="1",t.tabIndex=0,t.setAttribute("aria-label",`Scroll the ${n.categories.length} rows`),Object.assign(t.style,{position:"absolute",display:"none",width:"12px",overflowY:"auto",overflowX:"hidden",zIndex:"1"});const o=z.createElement("div");o.style.width="1px",t.appendChild(o),t.addEventListener("scroll",()=>{Math.abs(t.scrollTop-lt)<1||(lt=t.scrollTop,_.style.display="none",K())}),C.addEventListener("wheel",d=>{if(t.style.display==="none")return;const k=t.scrollTop;t.scrollTop+=d.deltaMode===1?d.deltaY*16:d.deltaY,t.scrollTop!==k&&d.preventDefault()},{passive:!1}),R.appendChild(t),Pe=t}C.addEventListener("mousemove",t=>{const o=$t(t);if(C.style.cursor=o&&(n.categoryItems&&m.onOpenWafer||m.onSelectPoints&&n.cellUnits[o.g][o.c].length>0)?"pointer":"default",!o){_.style.display="none",se&&(se=null,K());return}_.innerHTML=gn(o),_.style.display="block",yt(_,W,t.clientX,t.clientY),(!se||se.g!==o.g||se.c!==o.c)&&(se=o,K())}),C.addEventListener("mouseleave",()=>{_.style.display="none",se&&(se=null,K())}),m.onWaferMenu&&n.categoryItems&&C.addEventListener("contextmenu",t=>{const o=$t(t),d=o?n.categoryItems[o.c]:void 0;d===void 0||d<0||(_.style.display="none",m.onWaferMenu(d,Ve(e.spec),t))}),C.addEventListener("click",t=>{const o=$t(t);if(!o)return;const d=n.categoryItems?.[o.c];if(d!==void 0&&d>=0&&m.onOpenWafer){m.onOpenWafer(d,Ve(e.spec));return}const k=n.cellUnits[o.g][o.c];k.length>0&&m.onSelectPoints&&(_.style.display="none",m.onSelectPoints(st(n.unit,k,o.g),{x:t.clientX,y:t.clientY},C,{category:n.categories[o.c],...e.groups.length>1?{group:e.groups[o.g]}:{},...mn(n.categories[o.c])}))})}else if(e.marks.type==="line"){C.addEventListener("mousemove",t=>{const o=Pt(t);if(!o){_.style.display="none",K();return}_.innerHTML=o,_.style.display="block",yt(_,W,t.clientX,t.clientY),K()}),C.addEventListener("mouseleave",()=>{_.style.display="none",ke>=0&&(ke=-1,K())});const n=e.marks;if(m.onWaferMenu&&C.addEventListener("contextmenu",t=>{const o=Pt(t)===null?void 0:Ft(ke);o!==void 0&&(_.style.display="none",m.onWaferMenu(o,Ve(e.spec),t))}),m.onSelectPoints){C.style.cursor="pointer";let t=!1;C.addEventListener("mousedown",o=>{if(o.button!==0)return;t=!1;const d=C.getBoundingClientRect(),k=o.clientX-d.left,x=g=>{z.removeEventListener("mouseup",x);const c=g.clientX-d.left;if(Math.abs(c-k)<4)return;t=!0;const[p,A]=k<c?[k,c]:[c,k],S=de.map((P,u)=>P>=p&&P<=A?u:-1).filter(P=>P>=0),y=n.cellUnits.flatMap((P,u)=>O.size>0&&!O.has(u)?[]:S.flatMap(i=>st(n.unit,P[i]??[],u)));y.length>0&&(_.style.display="none",m.onSelectPoints(y,{x:g.clientX,y:g.clientY},C))};z.addEventListener("mouseup",x)}),C.addEventListener("click",o=>{if(t){t=!1;return}if(Pt(o)===null||ke<0)return;const d=n.cellUnits.flatMap((k,x)=>O.size>0&&!O.has(x)?[]:st(n.unit,k[ke]??[],x));d.length>0&&(_.style.display="none",m.onSelectPoints(d,{x:o.clientX,y:o.clientY},C))})}}K()}V(()=>{const e=s;if(!e||!e.marks)return;const l=bt(e.spec.colorGradient,e.spec.colorReverse,e.colorScale?.diverging,e.colorScale?.rating);return{caption:[Ht(e),...e.fitNotes??[],...e.notes,...oo(Kt(e))],legend:e.groups.length>1&&!e.colorRepeatsX?e.groups.map((a,f)=>({label:a||"(none)",color:ee(f)})):void 0,colorbar:e.colorScale?{label:e.colorScale.label,lo:J(e.colorScale.lo,e.colorScale.unit),hi:J(e.colorScale.hi,e.colorScale.unit),color:l}:void 0}});const Cn=jn(W,()=>K());return{card:W,actions:q,setPlot(e){s=e,Me(io(e.spec)),An()},setMenuRows(e){Oe=e},setActiveGroups(e){O.clear();for(const l of e)O.add(l);ot.forEach((l,a)=>l.setState({selected:O.has(a),dimmed:O.size>0&&!O.has(a)})),K()},destroy(){ut?.(),Cn.disconnect(),Ze?.destroy();for(const e of ce)e.destroy()}}}
|
|
1
|
+
import{escHtml as X,maxOf as Te,minOf as jt}from"../../core/utils.js";import{fiveNumberSummary as Ut,normalQuantile as Yt,sampleVariance as Rn}from"../../stats/math.js";import{CONTROL_RULE_TEXT as On}from"../../stats/control.js";import{PAT_VERDICT_LABEL as qt}from"../../stats/pat.js";import{fmt as J}from"../../renderer/fmt.js";import{fitTicks as pt}from"../../renderer/axisTicks.js";import{binOfLabel as In,plotFootnote as Ht,plotGuideSections as Kt,plotTestNumber as Ve,plotTitle as Nn,probabilityPosition as ct}from"../../stats/plotData.js";import{SPACE as me,RADIUS as Fn,fontPx as Q,FONT as gt,CLR as nt,markNoPrint as Vt}from"../toolbar.js";import{exportCsv as zn}from"../tableExport.js";import{tableToTsv as _n}from"../../core/tableCsv.js";import{plotTable as Dn}from"../../stats/plotTable.js";import{cardShell as Gn,appendGuideCites as Xn,observeResize as jn,makeTooltip as Un,attachChartTip as Yn,chartFillHeight as qn,applyCanvasFlow as Ge,prepareCanvas as Xe,positionChartTooltip as yt,resolveChartCanvasColors as je,makeAxisFormat as Hn,horizontalTickSpacing as Zt,VERTICAL_TICK_SPACING_PX as Kn,makeSeriesLegendItem as Vn,renderEmptyState as Jt,isExpandedCard as Qt,makeLabeledSelect as Zn,fillTextOnHalo as Ct,limitLines as Jn,limitExtent as Qn,stackLabelRows as en,placeClearOf as tn,strokeLimitLine as nn,drawOffAxisLimits as eo,limitLabelPlacement as to,shouldIncludeLimitsByDefault as no,WAFER_MENU_HINT as Et}from"./chartShell.js";import{guideCitesText as oo}from"../../stats/guideSections.js";import{categorical as lo}from"./palette.js";import{NO_DATA_FILL as on}from"../../renderer/colorMap.js";import{darkTextOnColor as so,plotGradient as bt}from"./divergingScale.js";import{wirePointInteractions as Bt}from"./pointInteractions.js";import{plotAbout as io}from"./cardAbout.js";const Wt=64,xt=16,Rt=12,Ue=52,ao=16,ro=92,co=16,vt=5e3,ln=1500,ft=100,fo=[1e-4,.001,.01,.05,.1,.25,.5,.75,.9,.95,.99,.999,.9999],sn=m=>`${+(m*100).toPrecision(4)}%`;export function categoryLayout(m){const W=.8/Math.max(1,...m.map(R=>R.length)),U=R=>(1-m[R].length*W)/2;return{width:W,left:(R,Y)=>{const V=m[R].indexOf(Y);return V<0?void 0:U(R)+V*W},groupAt:(R,Y)=>{const V=Math.floor((Y-U(R))/W);return V>=0&&V<m[R].length?m[R][V]:void 0}}}function uo(m){if(m<=ln)return Array.from({length:m},(R,Y)=>Y);const z=[];for(let R=0;R<ft;R++)z.push(R);const W=ln-2*ft,U=m-2*ft;for(let R=0;R<W;R++)z.push(ft+Math.floor(R*U/W));for(let R=m-ft;R<m;R++)z.push(R);return z}function Ot(m,z,W){return W?Math.round(m).toLocaleString("en-GB"):z?J(m,z):`${m<=-.005?"\u2212":""}${Math.abs(m).toFixed(2)}`}const ho=260;function mo(m){const z=[...m.querySelectorAll("[data-wmap-plot-panel]")],W=z[0]?.querySelector("canvas");if(!W)return null;const U=W.width/Math.max(1,W.getBoundingClientRect().width)||1,R=m.getBoundingClientRect(),Y=m.ownerDocument.createElement("canvas");Y.width=Math.ceil(R.width*U),Y.height=Math.ceil(R.height*U);const V=Y.getContext("2d");if(!V)return null;const we=getComputedStyle(m);V.fillStyle=we.backgroundColor&&we.backgroundColor!=="rgba(0, 0, 0, 0)"?we.backgroundColor:"#fff",V.fillRect(0,0,Y.width,Y.height);for(const Me of z){const Se=Me.querySelector("canvas"),ce=Me.dataset.wmapChartTitle??"",_=(Se??Me).getBoundingClientRect(),s=Me.getBoundingClientRect();V.fillStyle=getComputedStyle(Me).color||"#333",V.font=`600 ${Math.round(13*U)}px system-ui, sans-serif`,V.textBaseline="top",V.fillText(ce,(s.left-R.left+8)*U,(s.top-R.top+6)*U),Se&&V.drawImage(Se,(_.left-R.left)*U,(_.top-R.top)*U,_.width*U,_.height*U)}return Y}const Re=(m,z)=>z.extent?[...m,...z.extent]:m;function ue(m,z,W={}){const U=z.scale==="log";let R=m.length?jt(m):0,Y=m.length?Te(m):1;U&&(R=Math.log10(Math.max(R,Number.MIN_VALUE)),Y=Math.log10(Math.max(Y,Number.MIN_VALUE)));const V=(Y-R)*.05||(U?.5:1);R=W.floorAtZero&&!U&&R>=0?0:R-V,Y+=V;const Me=Pe=>Pe===void 0||!Number.isFinite(Pe)?void 0:U?Pe>0?Math.log10(Pe):void 0:Pe,Se=Me(z.min),ce=Me(z.max);Se!==void 0&&(R=Se),ce!==void 0&&(Y=ce),Y>R||(Y=R+1);const _=Y-R,s=!!z.reverse,Oe=Pe=>{const $=((U?Math.log10(Math.max(Pe,Number.MIN_VALUE)):Pe)-R)/_;return s?1-$:$};return{lo:U?10**R:R,hi:U?10**Y:Y,log:U,reverse:s,frac:Oe}}function It(m,z,W,U){if(z/m<10)return pt(m,z,W,U);const R=Math.floor(Math.log10(m)),Y=Math.ceil(Math.log10(z)),V=Se=>{const ce=[];for(let _=R;_<=Y;_++)for(const s of Se){const Oe=s*10**_;Oe>=m&&Oe<=z&&ce.push(Oe)}return ce},we=V([1]);return{ticks:we.length>=4?we:V([1,2,5]),step:0}}export function renderPlotChart(m){const z=m.ownerDocument??document,{card:W,heading:U,body:R,controlsRow:Y,setPngDecor:V,setPngCanvas:we,setAbout:Me,setMenuRows:Se}=Gn(m.panel?.title??"Plot",m.onSaveImage,z,{png:!m.panel});m.panel&&(W.dataset.wmapPlotPanel="1");let ce=[];W.dataset.wmapPlotCard="1";const _=Un(W);let s,Oe=()=>[];const Pe=()=>{const e=s&&(s.marks||s.facets?.length)?Dn(s,m.waferLabel):void 0;return e?[{label:"Save data as CSV",onClick:()=>{zn(e.columns,e.rows,`${e.name}.csv`,m.onSaveText)}},{label:"Copy data",onClick:()=>{z.defaultView?.navigator.clipboard?.writeText(_n(e.columns,e.rows))}}]:[]};Se(()=>[...Pe(),...Oe()]);let $,pe,te,Ze,Ie,ut,ht=!1;W.addEventListener("pointerenter",()=>{ht=!0,W.dataset.wmapPlotPanelsOwner&&(W.dataset.wmapPanelsHover="1")}),W.addEventListener("pointerleave",()=>{ht=!1,delete W.dataset.wmapPanelsHover});let ot=[];const O=new Set,he=new Set;let ge=null,ye=-1,le=[],Z,Ee,ne,se=null,lt=0,$e,dt;const an=()=>!!s?.facets;let de=[],be=-1,kt=new Map,Ye=[],qe=Ue,w=Wt,M=Rt;const rn={[qt.outlier]:"#e69f00",[qt.fails]:"#c0392b"},ee=e=>{const l=s?.groups[e]??"";if(s?.colorBins&&m.binColors){const a=In(l);return a===void 0?on:(s.colorBins==="hard"?m.binColors.hard:m.binColors.soft).get(a)??on}return rn[l]??lo(e)},He=e=>s?.groups[e]||"";function Ne(){const e=R.clientWidth,l=(m.panel?Math.max(170,Math.min(280,e*.72)):Math.max(220,Math.min(420,e*.62)))+(qe-Ue),a=qn(W,R,$,l);return{w:e,h:a,plotW:Math.max(10,e-w-xt),plotH:Math.max(10,a-M-qe)}}const Be=e=>{const l=(e?.references??[]).flatMap(a=>a.to===void 0?[{value:a.at,kind:"ref",end:"lo",label:a.label??""}]:[{value:a.at,kind:"ref",end:"lo",label:a.label??"",bandTo:a.to},{value:a.to,kind:"ref",end:"hi",label:a.label??""}]);return[...Jn(e?.limits,s?.spec.limits??"both"),...l].filter(a=>e?.scale!=="log"||a.value>0)},Fe=(e,l,a)=>{const h=a?.clip,r=h?[...e.filter(c=>c>=h.lo&&c<=h.hi),h.lo,h.hi]:e,d=r.filter(Number.isFinite);if(!l.length||d.length===0)return r;const{lo:n,hi:t}=Qn(l);return(a?s?.spec.axes?.[a===s?.x?"x":"y"]?.includeLimits:void 0)??no(jt(d),Te(d),n,t)?[...r,...l.map(c=>c.value)]:r};let Mt;function Je(e,l,a,h=!1){return!h&&!s?.x?.clip&&!s?.y?.clip?()=>{}:(e.save(),e.beginPath(),e.rect(w,M,l,a),e.clip(),Mt=e,Nt)}function Nt(){Mt?.restore(),Mt=void 0}function ze(e,l,a,h,r,d,n,t,o){const c=[];if(d.length===0)return()=>{};const y=d.filter(p=>p.value>=r.lo&&p.value<=r.hi),x=d.filter(p=>!y.includes(p)).map(p=>({value:p.value,label:p.label,side:p.value<r.lo?"lo":"hi"}));e.save(),e.font=`${Q(-1)}px system-ui, sans-serif`;const g=p=>`${p.label} ${J(p.value,n.unit)}`.trim(),f=Q(-1)+2;for(const p of d){if(p.bandTo===void 0)continue;const L=r.frac(Math.min(p.value,p.bandTo)),P=r.frac(Math.max(p.value,p.bandTo)),b=Math.max(0,Math.min(L,P)),C=Math.min(1,Math.max(L,P));C<=b||(e.save(),e.globalAlpha=.1,e.fillStyle=l,t==="y"?e.fillRect(w,M+(1-C)*h,a,(C-b)*h):e.fillRect(w+b*a,M,(C-b)*a,h),e.restore())}if(t==="y"){const p=y.map(b=>{const C=M+(1-r.frac(b.value))*h;return{l:b,y:C,start:C-6-f/2,end:C-6+f/2}}),L=en(p,1),P=p.length?Te(p.map(b=>e.measureText(g(b.l)).width))+8:0;p.forEach(({l:b,y:C},u)=>{e.globalAlpha=.7,nn(e,b,l,w,C,w+a,C),e.globalAlpha=1,e.fillStyle=l,e.textAlign="left",e.textBaseline="alphabetic";const i=w+3+L[u]*P,v=tn(Ye,{x0:i,x1:i+e.measureText(g(b)).width,y0:C-4-f+2,y1:C-4+2},f);if(!o){e.fillText(g(b),i,v.y1-2);return}c.push(()=>{e.save(),e.font=`${Q(-1)}px system-ui, sans-serif`,e.fillStyle=l,e.textAlign="left",e.textBaseline="alphabetic",Ct(e,g(b),i,v.y1-2,o.halo),e.restore()})})}else{const p=y.map(P=>{const b=w+r.frac(P.value)*a,C=g(P),u=P.label.split(/\s+/)[0],i=J(P.value,n.unit),v=u&&u!==P.label?[C,`${u} ${i}`,u]:[C],B=y.filter(E=>E!==P).map(E=>w+r.frac(E.value)*a),{text:F,side:T}=to(b,v,E=>e.measureText(E).width,w,w+a,P.end==="lo",B),A=e.measureText(F).width;return{l:P,x:b,side:T,shown:F,start:T<0?b-3-A:b+3,end:T<0?b-3:b+3+A}}),L=en(p,4);p.forEach(({l:P,x:b,side:C,shown:u,start:i,end:v},B)=>{e.globalAlpha=.7,nn(e,P,l,b,M,b,M+h),e.globalAlpha=1;const F=C<0?"right":"left";e.fillStyle=l,e.textAlign=F,e.textBaseline="top";const T=M+2+L[B]*f,A=tn(Ye,{x0:i,x1:v,y0:T,y1:T+f},f);if(!o){e.fillText(u,b+C*3,A.y0);return}c.push(()=>{e.save(),e.font=`${Q(-1)}px system-ui, sans-serif`,e.fillStyle=l,e.textAlign=F,e.textBaseline="top",Ct(e,u,b+C*3,A.y0,o.halo),e.restore()})})}return e.restore(),eo(e,x,{left:w,right:w+a,top:M,bottom:M+h},t==="y"?"vertical":"horizontal",l,p=>J(p,n.unit),void 0,Ye),()=>c.forEach(p=>p())}function Tt(e,l,a,h){return Hn(Math.max(Math.abs(l),Math.abs(a)),e.unit,h,e.label)}function mt(e,l){return l?`${e.label} (${l})`:e.label}const wt=18,St=(e,l,a)=>{if(e.measureText(l).width<=a)return l;for(let h=l.length-1;h>0;h--){const r=`${l.slice(0,h)}\u2026`;if(e.measureText(r).width<=a)return r}return""};function Ke(e,l,a,h,r,d,n,t,o,c,y){Ye=[],e.font=`${Q(-1)}px system-ui, sans-serif`;const x=()=>Kn,g=y?{ticks:y.map(b=>b.at),step:0}:t.log?It(t.lo,t.hi,d,x):pt(t.lo,t.hi,d,x),f=new Map(y?.map(b=>[b.at,b.label])),p=Tt(c,t.lo,t.hi,g.step||void 0),L=y?{tick:b=>f.get(b)??"",unitLabel:""}:p;e.strokeStyle=l.grid,e.lineWidth=.5,e.fillStyle=l.textMuted;let P="";if("labels"in n){const b=n.labels.length;if(n.labels.forEach((C,u)=>{const i=w+(u+.5)/b*r,v=C.length>22?`${C.slice(0,21)}\u2026`:C;n.rotate?(e.save(),e.translate(i,M+d+6),e.rotate(-Math.PI/4),e.textAlign="right",e.textBaseline="middle",e.fillText(v,0,0),e.restore()):(e.textAlign="center",e.textBaseline="top",e.fillText(v,i,M+d+4))}),n.nest){const C=n.nest[0]?.length??0,u=r/b;for(let i=C-2;i>=0;i--){const v=h-6-wt-i*wt,B=T=>n.nest[T].slice(0,i+1).join("");let F=0;for(let T=1;T<=b;T++){if(T<b&&B(T)===B(F))continue;const E=w+F*u,j=w+T*u;e.fillStyle=l.text,e.textAlign="center",e.textBaseline="bottom",e.fillText(St(e,n.nest[F][i],j-E-4),(E+j)/2,v),T<b&&(e.save(),e.strokeStyle=l.border,e.globalAlpha=i===0?.9:.45,e.lineWidth=1,e.beginPath(),e.moveTo(j,M),e.lineTo(j,v-2),e.stroke(),e.restore()),F=T}}e.fillStyle=l.textMuted}}else{const b=Zt(e,Math.max(Math.abs(n.lo),Math.abs(n.hi)),o.unit),C=n.log?It(n.lo,n.hi,r,b):pt(n.lo,n.hi,r,b),u=Tt(o,n.lo,n.hi,C.step||void 0);P=u.unitLabel;for(const i of C.ticks){const v=w+n.frac(i)*r;e.beginPath(),e.moveTo(v,M),e.lineTo(v,M+d),e.stroke(),e.textAlign="center",e.textBaseline="top",e.fillText(u.tick(i),v,M+d+4)}}for(const b of g.ticks){const C=M+(1-t.frac(b))*d;e.beginPath(),e.moveTo(w,C),e.lineTo(w+r,C),e.stroke(),e.textAlign="right",e.textBaseline="middle",e.fillText(L.tick(b),w-4,C)}e.font=`${Q(0)}px system-ui, sans-serif`,e.fillStyle=l.text,e.textAlign="center",e.textBaseline="bottom",e.fillText(mt(o,P),w+r/2,h-6),e.save(),e.translate(14,M+d/2),e.rotate(-Math.PI/2),e.textBaseline="top",e.fillText(mt(c,L.unitLabel),0,0),e.restore(),e.strokeStyle=l.border,e.lineWidth=1,e.beginPath(),e.moveTo(w,M),e.lineTo(w,M+d),e.moveTo(w,M+d),e.lineTo(w+r,M+d),e.stroke()}function cn(e,l,a,h,r,d,n,t,o,c,y,x={along:d,offset:0}){Ye=[],e.font=`${Q(-1)}px system-ui, sans-serif`;const g=Zt(e,Math.max(Math.abs(o.lo),Math.abs(o.hi)),y.unit),f=o.log?It(o.lo,o.hi,r,g):pt(o.lo,o.hi,r,g),p=Tt(y,o.lo,o.hi,f.step||void 0);e.strokeStyle=l.grid,e.lineWidth=.5,e.fillStyle=l.textMuted;for(const i of f.ticks){const v=w+o.frac(i)*r;e.beginPath(),e.moveTo(v,M),e.lineTo(v,M+d),e.stroke(),e.textAlign="center",e.textBaseline="top",e.fillText(p.tick(i),v,M+d+4)}const L=n.labels.length,P=x.along/Math.max(1,L),b=M-x.offset;e.save(),e.beginPath(),e.rect(0,M-1,w+r+1,d+2),e.clip(),e.textAlign="right",e.textBaseline="middle";const C=Math.max(0,Math.floor(x.offset/P)),u=Math.min(L,Math.ceil((x.offset+d)/P));for(let i=C;i<u;i++)e.fillText(St(e,n.labels[i],t.inner),w-6,b+(i+.5)*P);if(n.nest){const i=n.nest[0]?.length??0;let v=w-6-t.inner-10;for(let B=i-2;B>=0;B--){const F=t.outer[B]??0,T=E=>n.nest[E].slice(0,B+1).join("");let A=0;for(let E=1;E<=L;E++){if(E<L&&T(E)===T(A))continue;const j=b+A*P,k=b+E*P;e.fillStyle=l.text,e.textAlign="right",e.textBaseline="middle",e.fillText(St(e,n.nest[A][B],F),v,(j+k)/2),E<L&&(e.save(),e.strokeStyle=l.border,e.globalAlpha=B===0?.9:.45,e.lineWidth=1,e.beginPath(),e.moveTo(v-F,k),e.lineTo(w+r,k),e.stroke(),e.restore()),A=E}v-=F+10}e.fillStyle=l.textMuted}e.restore(),e.font=`${Q(0)}px system-ui, sans-serif`,e.fillStyle=l.text,e.textAlign="center",e.textBaseline="bottom",e.fillText(mt(y,p.unitLabel),w+r/2,h-6),e.save(),e.translate(14,M+d/2),e.rotate(-Math.PI/2),e.textBaseline="top",e.fillText(mt(c,""),0,0),e.restore(),e.strokeStyle=l.border,e.lineWidth=1,e.beginPath(),e.moveTo(w,M),e.lineTo(w,M+d),e.moveTo(w,M+d),e.lineTo(w+r,M+d),e.stroke()}function fn(){if(!s||!$||s.marks?.type!=="scatter"||!s.x||!s.y)return;Ge($,te??0);const e=je(W),{w:l,h:a,plotW:h,plotH:r}=Ne(),d=Xe($,W,l,a);if(!d)return;const{ctx:n}=d,t=s.marks.points,o=O.size===0?t:t.filter(T=>O.has(T.group)),c=s.colorScale,y=bt(s.spec.colorGradient,s.spec.colorReverse,c?.diverging,c?.rating),x=Be(s.x),g=Be(s.y),f=ue(Fe(Re(t.map(T=>T.x),s.x),x,s.x),s.x),p=ue(Fe(Re(t.map(T=>T.y),s.y),g,s.y),s.y);Z={x:f,y:p,plotW:h,plotH:r},Ke(n,e,l,a,h,r,f,p,s.x,s.y),ze(n,e.limitLine,h,r,f,x,s.x,"x"),ze(n,e.limitLine,h,r,p,g,s.y,"y"),Je(n,h,r);const L=o.length>vt?Math.ceil(o.length/vt):1,P=[];for(let T=0;T<o.length;T+=L)P.push(o[T]);n.globalAlpha=Math.max(.15,Math.min(.7,200/P.length));let b;if(c)b=[P];else{const T=new Map;for(const A of P){const E=T.get(A.group);E?E.push(A):T.set(A.group,[A])}b=[...T.values()].sort((A,E)=>E.length-A.length)}le=[];const C=w,u=w+h,i=M,v=M+r;for(const T of b)for(const A of T){const E=w+f.frac(A.x)*h,j=M+(1-p.frac(A.y))*r;E<C||E>u||j<i||j>v||(le.push({p:A,cx:E,cy:j}),n.beginPath(),n.arc(E,j,2.5,0,Math.PI*2),n.fillStyle=c&&A.value!==void 0?y((A.value-c.lo)/(c.hi-c.lo||1)):ee(A.group),n.fill())}n.globalAlpha=1;const B=s.marks;if(B.type==="scatter"&&(B.identity||B.fits)){const T=(E,j)=>[w+f.frac(E)*h,M+(1-p.frac(j))*r],A=(E,j,k)=>{n.save(),n.beginPath(),n.rect(w,M,h,r),n.clip(),n.strokeStyle=j,n.lineWidth=2,n.setLineDash(k),n.beginPath();for(let ie=0;ie<=60;ie++){const We=ie/60,De=f.log?10**(Math.log10(f.lo)+(Math.log10(f.hi)-Math.log10(f.lo))*We):f.lo+(f.hi-f.lo)*We,[Le,fe]=T(De,E(De));ie===0?n.moveTo(Le,fe):n.lineTo(Le,fe)}n.stroke(),n.restore()};B.identity&&A(E=>E,e.textMuted,[6,4]);for(const E of B.fits??[])O.size>0&&!O.has(E.group)&&!c||A(j=>E.intercept+E.slope*j,c?e.text:ee(E.group),[])}const F=ge?le.find(T=>T.p===ge):void 0;if(F&&(n.beginPath(),n.arc(F.cx,F.cy,5,0,Math.PI*2),n.lineWidth=2,n.strokeStyle=e.text,n.stroke()),he.size>0){n.lineWidth=1.5,n.strokeStyle=e.text;let T=0;for(const A of he)if(!(O.size>0&&!O.has(A.group))){if(++T>vt)break;n.beginPath(),n.arc(w+f.frac(A.x)*h,M+(1-p.frac(A.y))*r,4,0,Math.PI*2),n.stroke()}}}function un(){if(Ee=void 0,!s||s.marks?.type!=="histogram"||!s.x)return;const e=s.marks.values,l=e.flat();if(l.length===0)return;const a=ue(Fe(Re(l,s.x),Be(s.x),s.x),s.x),h=Math.max(1,s.spec.bins??co),r=a.log?Math.log10(a.lo):a.lo,d=a.log?Math.log10(a.hi):a.hi,n=Array.from({length:h+1},(g,f)=>{const p=r+(d-r)*f/h;return a.log?10**p:p}),t=e.map(()=>new Array(h).fill(0));e.forEach((g,f)=>{for(const p of g){const L=a.log?p>0?Math.log10(p):NaN:p;if(!Number.isFinite(L)||L<r||L>d)continue;const P=Math.min(h-1,Math.floor((L-r)/(d-r)*h));t[f][P]++}});const o=s.spec.histNorm==="percent",c=o?t.map(g=>{const f=g.reduce((p,L)=>p+L,0);return g.map(p=>f?p/f*100:0)}):t;let y=0;const x=e.map(()=>{});if(s.spec.normalCurve&&!a.log){const g=(n[n.length-1]-n[0])/h;e.forEach((f,p)=>{const L=f.filter(Number.isFinite);if(L.length<2)return;const P=L.reduce((C,u)=>C+u,0)/L.length,b=Math.sqrt(Rn(L,P));b>0&&(x[p]={mean:P,sd:b},y=Math.max(y,(o?100:t[p].reduce((C,u)=>C+u,0))*g/(b*Math.sqrt(2*Math.PI))))})}Ee={edges:n,counts:t,heights:c,normal:x,maxCount:Math.max(o?.001:1,y,...c.map(g=>Te(g)))}}function hn(){if(!s||!$||!Ee||!s.x||!s.y)return;Ge($,te??0);const e=je(W),{w:l,h:a,plotW:h,plotH:r}=Ne(),d=Xe($,W,l,a);if(!d)return;const{ctx:n}=d,{edges:t,counts:o,heights:c,normal:y,maxCount:x}=Ee,g=ue([t[0],t[t.length-1]],{...s.x,min:t[0],max:t[t.length-1]}),f=ue([0,x],{label:s.y.label,scale:"linear",min:0,max:x*1.05});Z={x:g,y:f,plotW:h,plotH:r},Ke(n,e,l,a,h,r,g,f,s.x,s.y),ze(n,e.limitLine,h,r,g,Be(s.x),s.x,"x"),Je(n,h,r);const p=o.map((u,i)=>i).filter(u=>O.size===0||O.has(u)),L=p.length>1,P=p.length>=3,b=u=>w+Math.min(g.frac(t[u]),g.frac(t[u+1]))*h,C=u=>w+Math.max(g.frac(t[u]),g.frac(t[u+1]))*h;for(const u of p){if(P){n.fillStyle=ee(u),n.strokeStyle=ee(u),n.lineWidth=1.75,n.lineJoin="round",n.globalAlpha=.1,c[u].forEach((v,B)=>{if(v===0)return;const F=M+(1-f.frac(v))*r;n.fillRect(b(B),F,Math.max(1,C(B)-b(B)),M+r-F)}),n.globalAlpha=1,n.beginPath();let i=!1;c[u].forEach((v,B)=>{const F=M+(1-f.frac(v))*r;i||(n.moveTo(b(B),M+r),i=!0),n.lineTo(b(B),F),n.lineTo(C(B),F),B===c[u].length-1&&n.lineTo(C(B),M+r)}),n.stroke();continue}n.globalAlpha=L?.55:.85,n.fillStyle=ee(u),c[u].forEach((i,v)=>{if(i===0)return;const B=M+(1-f.frac(i))*r;n.fillRect(b(v),B,Math.max(1,C(v)-b(v)-1),M+r-B)})}if(n.globalAlpha=1,s.spec.normalCurve&&!g.log){const u=(t[t.length-1]-t[0])/(t.length-1);for(const i of p){const v=y[i];if(!v)continue;const{mean:B,sd:F}=v,T=(s.spec.histNorm==="percent"?100:o[i].reduce((A,E)=>A+E,0))*u;n.save(),n.beginPath(),n.rect(w,M,h,r),n.clip(),n.strokeStyle=ee(i),n.lineWidth=2,n.beginPath();for(let A=0;A<=120;A++){const E=t[0]+(t[t.length-1]-t[0])*A/120,j=Math.exp(-.5*((E-B)/F)**2)/(F*Math.sqrt(2*Math.PI)),k=w+g.frac(E)*h,ie=M+(1-f.frac(T*j))*r;A===0?n.moveTo(k,ie):n.lineTo(k,ie)}n.stroke(),n.restore()}}if(ye>=0){const u=w+Math.min(g.frac(t[ye]),g.frac(t[ye+1]))*h,i=w+Math.max(g.frac(t[ye]),g.frac(t[ye+1]))*h;n.lineWidth=1.5,n.strokeStyle=e.text,n.strokeRect(u,M,Math.max(1,i-u),r)}}function dn(){const e=s?.marks;if(!s||!$||!s.x||!s.y||!e||e.type!=="box"&&e.type!=="bar")return;Ge($,te??0);const l=je(W),a=s.spec.orientation==="horizontal",h=e.nest?e.nest.parts.map(S=>S[S.length-1]):e.categories,r=e.categories.length,d=s.groups.length,n=S=>O.size===0||O.has(S),t=e.type==="bar"&&!!e.stack,o=(S,N)=>e.type==="bar"?Number.isFinite(e.values[S][N]):e.cells[S][N].length>0,c=Array.from({length:r},(S,N)=>Array.from({length:d},(I,D)=>D).filter(I=>n(I)&&o(I,N))),y=categoryLayout(t?c.map(S=>S.length?[0]:[]):c),x=$.getContext("2d"),g=S=>x?x.measureText(S).width:S.length*6;x&&(x.font=`${Q(-1)}px system-ui, sans-serif`);const f=(S,N)=>S.length>N?`${S.slice(0,N-1)}\u2026`:S;let p=!1,L={inner:0,outer:[]},P=0;if(a){L={inner:Math.min(200,Te(h.map(N=>g(f(N,28))))),outer:e.nest?e.nest.levels.slice(0,-1).map((N,I)=>Math.min(140,Te(e.nest.parts.map(D=>g(f(D[I],20)))))):[]},w=Math.max(Wt,30+L.inner+6+L.outer.reduce((N,I)=>N+I+10,0)),e.cumulative&&(M=Rt+18),qe=Ue;const S=Math.max(1,...c.map(N=>t?Math.min(1,N.length):N.length));P=Math.max(22,S*ao/.8)}else{const S=Te(h.map(D=>g(f(D,22)))),N=Math.max(10,R.clientWidth-w-xt)/Math.max(1,r);p=S+8>N;const I=e.nest?e.nest.levels.length-1:0;qe=(p?Ue+Math.min(110,S*.72):Ue)+I*wt}const b=e.type==="bar"&&!t&&!!s.spec.valueLabels,C=s.y.unit,u=S=>J(S,C),i=b?Te(e.values.flat().filter(Number.isFinite).map(S=>g(u(S))).concat(0))+8:0,v=a?i:e.cumulative?40:0,{w:B,h:F,plotW:T,plotH:A}=Ne(),E=Math.max(10,T-v),j=Xe($,W,B,F);if(!j)return;const{ctx:k}=j,ie=S=>S.filter(Number.isFinite),We=Be(s.y),De=t?e.categories.map((S,N)=>e.values.reduce((I,D,q)=>I+(n(q)&&Number.isFinite(D[N])?D[N]:0),0)):[],Le=e.type==="bar"?[...t?De:ie(e.values.flat()),0]:ie(e.cells.flat(2));k.font=`${Q(-1)}px system-ui, sans-serif`;const fe=a?i:Q(-1)+6;if(b&&!a&&Le.length){const S=Te(Le);S>0&&Le.push(S*(1+fe/Math.max(1,A-fe)))}const Ae=ue(Fe(Re(Le,s.y),We,s.y),s.y,{floorAtZero:e.type==="bar"});Z={x:Ae,y:Ae,plotW:E,plotH:A};const it=a?A:E,Qe=a&&r*P>A+.5,at=Qe?r*P:it,rt=Qe?Math.max(0,Math.min(lt,at-it)):0;lt=rt,Pn(Qe,at,rt,E+v,A),a?cn(k,l,B,F,E,A,{labels:h,rotate:!1,nest:e.nest?.parts},L,Ae,s.x,s.y,{along:at,offset:rt}):Ke(k,l,B,F,E,A,{labels:h,rotate:p,nest:e.nest?.parts},Ae,s.x,s.y);const ve=at/r,Ce=y.width*ve;ne={slotW:ve,n:r,plotW:at,layout:y,across:a,offset:rt,view:it};const oe=S=>a?w+Ae.frac(S)*E:M+(1-Ae.frac(S))*A,ke=S=>a?M+S-rt:w+S,Lt=Ae.log?a?w:M+A:oe(0),et=(S,N,I,D)=>a?[Math.min(I,D),ke(S),Math.max(1,Math.abs(D-I)),N-S]:[ke(S),Math.min(I,D),N-S,Math.max(1,Math.abs(D-I))],At=(S,N,I,D)=>{k.beginPath(),a?(k.moveTo(N,ke(S)),k.lineTo(D,ke(I))):(k.moveTo(ke(S),N),k.lineTo(ke(I),D)),k.stroke()},tt=S=>a?S>=Lt:S<=Lt,En=S=>a?Q(-1)<=Ce+4:k.measureText(u(S)).width<=Ce+6,Dt=b&&e.values.every(S=>S.every(N=>!Number.isFinite(N)||En(N)));if(Dt){for(let S=0;S<d;S++)if(n(S))for(let N=0;N<r;N++){const I=y.left(N,S),D=e.values[S][N];if(I===void 0||!Number.isFinite(D))continue;const q=ke(N*ve+I*ve+Ce/2),H=k.measureText(u(D)).width,G=oe(D);a?Ye.push(tt(G)?{x0:G,x1:G+H+6,y0:q-fe/2,y1:q+fe/2}:{x0:G-H-6,x1:G,y0:q-8,y1:q+8}):Ye.push(tt(G)?{x0:q-H/2,x1:q+H/2,y0:G-fe,y1:G}:{x0:q-H/2,x1:q+H/2,y0:G,y1:G+fe})}}const Bn=ze(k,l.limitLine,E,A,Ae,We,s.y,a?"x":"y",{halo:l.bg}),Wn=Je(k,E,A,Qe);if(t){ne.stacks=[];for(let S=0;S<r;S++){const N=y.left(S,0),I=[];if(ne.stacks.push(I),N===void 0)continue;let D=0;for(let q=0;q<d;q++){const H=e.values[q][S];if(!n(q)||!Number.isFinite(H)||H<=0)continue;const G=oe(D),ae=oe(D+H);k.fillStyle=ee(q),k.globalAlpha=.9;const re=S*ve+N*ve;k.fillRect(...et(re+1,re+Ce-1,G,ae)),k.globalAlpha=1,I.push({g:q,p0:Math.min(G,ae),p1:Math.max(G,ae)}),D+=H}}}for(let S=0;S<d&&!t;S++){if(O.size>0&&!O.has(S))continue;const N=ee(S);for(let I=0;I<r;I++){const D=y.left(I,S);if(D===void 0)continue;const q=I*ve+D*ve;if(e.type==="bar"){const H=e.values[S][I];if(!Number.isFinite(H))continue;const G=oe(H);if(k.fillStyle=N,k.globalAlpha=.9,k.fillRect(...et(q+1,q+Ce-1,Lt,G)),k.globalAlpha=1,Dt){k.fillStyle=l.text,k.font=`${Q(-1)}px system-ui, sans-serif`;const ae=ke(q+Ce/2);a?(k.textBaseline="middle",k.textAlign=tt(G)?"left":"right",k.fillText(u(H),G+(tt(G)?3:-3),ae)):(k.textAlign="center",k.textBaseline=tt(G)?"bottom":"top",k.fillText(u(H),ae,G+(tt(G)?-2:2)))}}else{const H=e.cells[S][I];if(H.length===0)continue;const G=q+Ce/2,ae=Math.max(3,Math.min(Ce*.7,46)),re=Ut(Float64Array.from(H));if(k.strokeStyle=N,k.fillStyle=N,k.lineWidth=1.5,H.length===1){k.beginPath(),a?k.arc(oe(re.median),ke(G),3,0,Math.PI*2):k.arc(ke(G),oe(re.median),3,0,Math.PI*2),k.fill();continue}At(G,oe(re.min),G,oe(re.max));for(const Xt of[re.min,re.max])At(G-ae/4,oe(Xt),G+ae/4,oe(Xt));k.globalAlpha=.35,k.fillRect(...et(G-ae/2,G+ae/2,oe(re.q1),oe(re.q3))),k.globalAlpha=1,k.strokeRect(...et(G-ae/2,G+ae/2,oe(re.q1),oe(re.q3))),k.lineWidth=2.5,At(G-ae/2,oe(re.median),G+ae/2,oe(re.median))}}}Wn(),Bn();const Gt=se?y.left(se.c,t?0:se.g):void 0;if(se&&Gt!==void 0){Qe&&(k.save(),k.beginPath(),k.rect(w-1,M,E+2,A),k.clip()),k.lineWidth=1.5,k.strokeStyle=l.text;const S=se.c*ve+Gt*ve,N=t?ne.stacks?.[se.c]?.find(I=>I.g===se.g):void 0;t&&N?k.strokeRect(...et(S,S+Ce,N.p0,N.p1)):t||k.strokeRect(...et(S,S+Ce,a?w:M,a?w+E:M+A)),Qe&&k.restore()}if(e.cumulative){const S=I=>a?w+I/100*E:M+(1-I/100)*A,N=(I,D)=>a?[S(D),ke((I+.5)*ve)]:[ke((I+.5)*ve),S(D)];if(k.save(),k.strokeStyle=l.text,k.fillStyle=l.text,k.lineWidth=1.5,k.beginPath(),e.cumulative.forEach((I,D)=>{const[q,H]=N(D,I);D===0?k.moveTo(q,H):k.lineTo(q,H)}),k.stroke(),e.cumulative.forEach((I,D)=>{const[q,H]=N(D,I);k.beginPath(),k.arc(q,H,2.5,0,Math.PI*2),k.fill()}),k.strokeStyle=l.border,k.lineWidth=1,k.beginPath(),a?(k.moveTo(w,M),k.lineTo(w+E,M)):(k.moveTo(w+E,M),k.lineTo(w+E,M+A)),k.stroke(),k.fillStyle=l.textMuted,k.font=`${Q(-1)}px system-ui, sans-serif`,a){k.textAlign="center",k.textBaseline="bottom";for(const I of[0,25,50,75,100])k.fillText(`${I}%`,S(I),M-3)}else{k.textAlign="left",k.textBaseline="middle";for(const I of[0,25,50,75,100])k.fillText(`${I}%`,w+E+4,S(I))}k.restore()}}function mn(e){const l=s?.setTests,a=l?l.labels.indexOf(e):-1;return a>=0?{test:l.tests[a]}:{}}function Pt(e){if(!ne||!$)return null;const l=$.getBoundingClientRect(),a=ne.across?e.clientY-l.top-M:e.clientX-l.left-w;if(a<0||a>ne.view)return null;const h=a+ne.offset,r=Math.min(ne.n-1,Math.floor(h/ne.slotW)),d=ne.layout.groupAt(r,(h-r*ne.slotW)/ne.slotW);if(d===void 0)return null;if(!ne.stacks)return{g:d,c:r};const n=ne.across?e.clientX-l.left:e.clientY-l.top,t=ne.stacks[r]?.find(o=>n>=o.p0&&n<=o.p1);return t?{g:t.g,c:r}:null}function pn(e,l,a){let h=0,r=0,d=0;for(const o of l)o===100&&h++;if(e.failSide)for(const o of a){const c=e.failSide[o];c<0?r++:c>0&&d++}const n=o=>o.toLocaleString("en-GB"),t=r+d>0?` (${n(r)} below the low limit \xB7 ${n(d)} above the high limit)`:"";return`${n(h)} pass \xB7 ${n(l.length-h)} fail${t} \xB7 n = ${n(l.length)}`}function gn(e){const l=s,a=l.marks,h=l.level==="die"?"dies":"wafers",r=a.cells[e.g][e.c],d=a.nest?a.nest.parts[e.c]:[a.categories[e.c]],n=l.groups.length>1&&!d.includes(He(e.g))?` \xB7 ${X(He(e.g))}`:"",t=`<strong>${X(a.categories[e.c])}${n}</strong><br>`,o=l.y?.unit;if(r.length===0)return`${t}no ${h}`;const c=`${r.length.toLocaleString("en-GB")} ${h}`,y=(a.categoryItems&&m.onOpenWafer?`<br><em>click to ${X(m.openActionLabel??"open this wafer")}</em>`:"")+(a.categoryItems&&m.onWaferMenu?Et:"");if(a.type==="bar"){const g=a.values[e.g][e.c],f=a.cumulative?`<br>${X(`cumulative: ${a.cumulative[e.c].toFixed(1)}% of the whole`)}`:"";return`${t}${X(`${l.y.label}: ${J(g,o)}`)}<br>${X(a.passFail?pn(a.passFail,r,a.cellUnits[e.g][e.c]):c)}${f}${y}`}const x=Ut(Float64Array.from(r));return`${t}${["max","q3","median","q1","min"].map(g=>X(`${{max:"max",q3:"upper quartile",median:"median",q1:"lower quartile",min:"min"}[g]}: ${J(x[g],o)}`)).join("<br>")}<br>${X(c)}${y}`}function yn(){const e=s?.marks;if(!s||!$||!s.x||!s.y||!e||e.type!=="line")return;Ge($,te??0);const l=je(W),{w:a,h,plotW:r,plotH:d}=Ne(),n=Xe($,W,a,h);if(!n)return;const{ctx:t}=n,o=Be(s.x),c=Be(s.y),y=ue(Fe(Re(e.xs,s.x),o,s.x),s.x),x=e.band?[...e.band.lo.flat(),...e.band.hi.flat()]:[],g=ue(Fe(Re([...e.values.flat(),...x].filter(Number.isFinite),s.y),c,s.y),s.y);if(Z={x:y,y:g,plotW:r,plotH:d},Ke(t,l,a,h,r,d,y,g,s.x,s.y),ze(t,l.limitLine,r,d,y,o,s.x,"x"),ze(t,l.limitLine,r,d,g,c,s.y,"y"),Je(t,r,d),de=e.xs.map(f=>w+y.frac(f)*r),e.band){const f=p=>M+(1-g.frac(p))*d;e.band.lo.forEach((p,L)=>{if(O.size>0&&!O.has(L))return;const P=e.band.hi[L];t.save(),t.fillStyle=ee(L),t.globalAlpha=.16;let b=[];const C=()=>{if(b.length===1){const u=b[0],i=de[u];t.globalAlpha=.55,t.strokeStyle=ee(L),t.lineWidth=2,t.beginPath(),t.moveTo(i,f(p[u])),t.lineTo(i,f(P[u])),t.moveTo(i-4,f(p[u])),t.lineTo(i+4,f(p[u])),t.moveTo(i-4,f(P[u])),t.lineTo(i+4,f(P[u])),t.stroke(),t.globalAlpha=.16}else if(b.length){t.beginPath(),b.forEach((u,i)=>{i===0?t.moveTo(de[u],f(P[u])):t.lineTo(de[u],f(P[u]))});for(let u=b.length-1;u>=0;u--)t.lineTo(de[b[u]],f(p[b[u]]));t.closePath(),t.fill()}b=[]};p.forEach((u,i)=>{Number.isFinite(u)&&Number.isFinite(P[i])?b.push(i):C()}),C(),t.restore()})}e.values.forEach((f,p)=>{if(O.size>0&&!O.has(p))return;t.strokeStyle=ee(p),t.fillStyle=ee(p),t.lineWidth=2;let L=!1;t.beginPath(),f.forEach((P,b)=>{if(!Number.isFinite(P)){L=!1;return}const C=de[b],u=M+(1-g.frac(P))*d;L?t.lineTo(C,u):t.moveTo(C,u),L=!0}),t.stroke(),f.forEach((P,b)=>{Number.isFinite(P)&&(t.beginPath(),t.arc(de[b],M+(1-g.frac(P))*d,be===b?4.5:3,0,Math.PI*2),t.fill())})}),be>=0&&(t.lineWidth=1,t.strokeStyle=l.textMuted,t.beginPath(),t.moveTo(de[be],M),t.lineTo(de[be],M+d),t.stroke())}function $t(e){const l=s?.marks;if(!l||l.type!=="line"||!$||!s?.x||de.length===0)return null;const a=e.clientX-$.getBoundingClientRect().left;let h=-1,r=24;if(de.forEach((t,o)=>{const c=Math.abs(t-a);c<r&&(r=c,h=o)}),be=h,h<0)return null;const d=s.level==="die"?"dies":"wafers",n=l.values.map((t,o)=>({g:o,v:t[h],n:l.counts[o][h]})).filter(t=>Number.isFinite(t.v)&&(O.size===0||O.has(t.g)));return`<strong>${X(`${s.x.label}: ${J(l.xs[h],s.x.unit)}`)}</strong><br>`+n.map(t=>{const o=l.band?.lo[t.g][h],c=l.band?.hi[t.g][h],y=o!==void 0&&c!==void 0&&Number.isFinite(o)&&Number.isFinite(c)?`, band ${J(o,s.y.unit)} to ${J(c,s.y.unit)}`:"";return X(`${s.groups.length>1?`${He(t.g)}: `:""}${J(t.v,s.y.unit)}${y} (${t.n.toLocaleString("en-GB")} ${d})`)}).join("<br>")+(m.onWaferMenu&&Ft(h)!==void 0?Et:"")}function Ft(e){const l=s?.marks;if(l?.type!=="line")return;const a=new Set(l.cellUnits.flatMap((h,r)=>O.size>0&&!O.has(r)?[]:(h[e]??[]).map(d=>l.unit(d).item)));return a.size===1?[...a][0]:void 0}function bn(){const e=s?.marks;if(!s||!$||!s.x||!s.y||e?.type!=="probability")return;Ge($,te??0);const l=je(W),{w:a,h,plotW:r,plotH:d}=Ne(),n=Xe($,W,a,h);if(!n)return;const{ctx:t}=n,o=s.spec.probabilityAxis==="linear",c=e.values.map((T,A)=>A).filter(T=>e.values[T].length>0&&(O.size===0||O.has(T))),y=Math.max(1,...c.map(T=>e.values[T].length)),x=Math.min(.01,ct(0,y)),g=T=>o?T:Yt(T),f=o?0:g(x),p=o?1:g(1-x),L=o?0:(p-f)*.03,P=ue([f-L,p+L],{label:s.y.label,scale:"linear",min:f-L,max:p+L}),b=o?[0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1]:fo.filter(T=>T>=x&&T<=1-x),C=[],u=T=>M+(1-P.frac(T))*d,i=b.indexOf(.5);C.push({at:g(.5),label:sn(.5)});for(const T of[b.slice(i+1),b.slice(0,i).reverse()]){let A=u(g(.5));for(const E of T){const j=u(g(E));Math.abs(j-A)<16||(C.push({at:g(E),label:sn(E)}),A=j)}}const v=Be(s.x),B=ue(Fe(Re(c.flatMap(T=>[e.values[T][0],e.values[T][e.values[T].length-1]]),s.x),v,s.x),s.x);Z={x:B,y:P,plotW:r,plotH:d},Ke(t,l,a,h,r,d,B,P,s.x,s.y,C),ze(t,l.limitLine,r,d,B,v,s.x,"x"),Je(t,r,d),le=[],t.save(),t.beginPath(),t.rect(w,M,r,d),t.clip();for(const T of c){const A=e.values[T],E=A.length,j=uo(E);if(t.strokeStyle=ee(T),t.fillStyle=ee(T),t.lineWidth=1.5,t.beginPath(),j.forEach((k,ie)=>{const We=w+B.frac(A[k])*r,De=u(g(ct(k,E)));ie===0?t.moveTo(We,De):t.lineTo(We,De);const Le=T*2**26+k;let fe=kt.get(Le);if(!fe){const{item:Ae,die:it}=e.unit(e.units[T][k]);fe={x:A[k],y:ct(k,E),group:T,item:Ae,die:it},kt.set(Le,fe)}le.push({p:fe,cx:We,cy:De})}),t.stroke(),E<=300)for(const k of j)t.beginPath(),t.arc(w+B.frac(A[k])*r,u(g(ct(k,E))),2.5,0,Math.PI*2),t.fill()}if(t.restore(),he.size>0){t.lineWidth=1.5,t.strokeStyle=l.text;let T=0;for(const A of he)if(!(O.size>0&&!O.has(A.group))){if(++T>vt)break;t.beginPath(),t.arc(w+B.frac(A.x)*r,u(g(A.y)),4,0,Math.PI*2),t.stroke()}}const F=ge?le.find(T=>T.p===ge):void 0;F&&(t.beginPath(),t.arc(F.cx,F.cy,5,0,Math.PI*2),t.lineWidth=2,t.strokeStyle=l.text,t.stroke())}function xn(e){const l=s,h=l.marks.values[e.group].length,r=Math.round(e.y*h+.5),d=m.waferLabel(e.item),n=e.die&&e.die.x!==void 0?`die (${e.die.x}, ${e.die.y})`:"",t=[d,n].filter(Boolean).join(" \xB7 ");return(t?`<strong>${X(t)}</strong><br>`:"")+X(`${l.x.label}: ${J(e.x,l.x.unit)}`)+"<br>"+X(`rank ${r.toLocaleString("en-GB")} of ${h.toLocaleString("en-GB")}, plotted at ${+(e.y*100).toPrecision(3)}%`)+(l.groups.length>1?`<br>${X(He(e.group))}`:"")+(m.onOpenWafer?`<br><em>click to ${X(m.openActionLabel??"open this wafer")}</em>`:"")+(m.onWaferMenu&&!e.die?Et:"")}let _e,xe=null;function vn(){const e=s?.marks;if(!s||!$||e?.type!=="heatmap")return;Ge($,te??0);const l=je(W),a=$.getContext("2d");let h=0,r=0;a&&(a.font=`${Q(-1)}px system-ui, sans-serif`,h=Te(e.rows.map(u=>a.measureText(u.length>24?`${u.slice(0,23)}\u2026`:u).width)),r=Te(e.cols.map(u=>a.measureText(u.length>22?`${u.slice(0,21)}\u2026`:u).width)));const d=Math.min(180,Math.max(w,h+24)),n=Math.max(8,R.clientWidth-d-xt)/Math.max(1,e.cols.length),t=r+8>n;qe=t?Ue+Math.min(110,r*.72):Ue;const{w:o,h:c}=Ne(),y=Math.max(10,o-d-xt),x=Math.max(10,c-M-qe),g=Xe($,W,o,c);if(!g)return;const{ctx:f}=g,p=y/e.cols.length,L=x/e.rows.length;_e={left:d,cw:p,ch:L,rows:e.rows.length,cols:e.cols.length};const P=s.colorScale,b=bt(s.spec.colorGradient,s.spec.colorReverse,P?.diverging,P?.rating),C=P&&P.hi-P.lo||1;f.font=`${Q(-1)}px system-ui, sans-serif`;for(let u=0;u<e.rows.length;u++)for(let i=0;i<e.cols.length;i++){const v=e.values[u][i],B=d+i*p,F=M+u*L,T=Number.isFinite(v)&&P?b((v-P.lo)/C):l.track;if(f.fillStyle=T,f.fillRect(B,F,p,L),Number.isFinite(v)&&p>=40&&L>=16){const A=so(T);f.fillStyle=A?"#1a1a1a":"#fff",f.textAlign="center",f.textBaseline="middle",Ct(f,Ot(v,s.y?.unit,P?.integer),B+p/2,F+L/2,A?"rgba(255,255,255,0.55)":"rgba(0,0,0,0.55)")}}f.lineWidth=1,f.setLineDash([]),f.strokeStyle=l.border,f.beginPath();for(let u=1;u<e.rows.length;u++){const i=Math.round(M+u*L)+.5;f.moveTo(d,i),f.lineTo(d+y,i)}for(let u=1;u<e.cols.length;u++){const i=Math.round(d+u*p)+.5;f.moveTo(i,M),f.lineTo(i,M+x)}f.stroke(),f.strokeStyle=l.textMuted,f.strokeRect(d+.5,M+.5,y-1,x-1),f.beginPath();for(let u=0;u<e.rows.length;u++){const i=Math.round(M+(u+.5)*L)+.5;f.moveTo(d-4,i),f.lineTo(d,i)}for(let u=0;u<e.cols.length;u++){const i=Math.round(d+(u+.5)*p)+.5;f.moveTo(i,M+x),f.lineTo(i,M+x+4)}f.stroke(),xe&&(f.lineWidth=2,f.strokeStyle=l.text,f.strokeRect(d+xe.c*p,M+xe.r*L,p,L)),f.fillStyle=l.textMuted,f.textAlign="right",f.textBaseline="middle",e.rows.forEach((u,i)=>f.fillText(u.length>24?`${u.slice(0,23)}\u2026`:u,d-8,M+(i+.5)*L)),e.cols.forEach((u,i)=>{const v=u.length>22?`${u.slice(0,21)}\u2026`:u,B=d+(i+.5)*p;t?(f.save(),f.translate(B,M+x+8),f.rotate(-Math.PI/4),f.textAlign="right",f.textBaseline="middle",f.fillText(v,0,0),f.restore()):(f.textAlign="center",f.textBaseline="top",f.fillText(v,B,M+x+7))}),f.font=`${Q(0)}px system-ui, sans-serif`,f.fillStyle=l.text,f.textAlign="center",f.textBaseline="bottom",f.fillText(s.x?.label??"",d+y/2,c-6)}function zt(e){if(!_e||!$)return null;const l=$.getBoundingClientRect(),a=Math.floor((e.clientX-l.left-_e.left)/_e.cw),h=Math.floor((e.clientY-l.top-M)/_e.ch);return h>=0&&h<_e.rows&&a>=0&&a<_e.cols?{r:h,c:a}:null}function kn(e){const l=s,a=l.marks,h=a.values[e.r][e.c],r=a.cellUnits[e.r][e.c].length,d=l.level==="die"?"dies":"wafers";return`<strong>${X(`${a.rows[e.r]} \xB7 ${a.cols[e.c]}`)}</strong><br>`+(Number.isFinite(h)?X(`${l.y?.label??"Count"}: ${Ot(h,l.y?.unit,l.colorScale?.integer)}`)+"<br>":"")+X(`${r.toLocaleString("en-GB")} ${d}`)+(r&&m.onSelectPoints?"<br><em>click or right-click to chart or tabulate them</em>":"")}function Mn(){const e=s?.marks;if(!s||!$||!s.x||!s.y||e?.type!=="control")return;Ge($,te??0);const l=je(W),{w:a,h,plotW:r,plotH:d}=Ne(),n=Xe($,W,a,h);if(!n)return;const{ctx:t}=n,o=e.limits,c=Be(s.y),y=ue(Re(e.xs,s.x),s.x),x=ue(Fe(Re([...e.values,o.ucl,o.lcl],s.y),c,s.y),s.y);Z={x:y,y:x,plotW:r,plotH:d},Ke(t,l,a,h,r,d,y,x,s.x,s.y),ze(t,l.limitLine,r,d,x,c,s.y,"y"),Je(t,r,d);const g=i=>w+y.frac(e.xs[i])*r,f=i=>M+(1-x.frac(i))*d;t.save(),t.beginPath(),t.rect(w,M,r,d),t.clip();const p=(i,v,B,F=1)=>{t.globalAlpha=B,t.setLineDash(v),t.lineWidth=F,t.strokeStyle=l.text,t.beginPath(),t.moveTo(w,f(i)),t.lineTo(w+r,f(i)),t.stroke()};for(const i of[-2,-1,1,2])p(o.centre+i*o.sigma,[2,4],.25);p(o.centre,[],.6,1.5),p(o.ucl,[6,4],.85),p(o.lcl,[6,4],.85),t.restore(),t.fillStyle=l.textMuted,t.font=`${Q(-1)}px system-ui, sans-serif`,t.textAlign="right",t.textBaseline="bottom";const L=i=>J(i,s.y.unit);if(t.fillText(`UCL ${L(o.ucl)}`,w+r-4,f(o.ucl)-2),t.textBaseline="top",t.fillText(`LCL ${L(o.lcl)}`,w+r-4,f(o.lcl)+2),t.textBaseline="bottom",t.fillText(`Centre ${L(o.centre)}`,w+r-4,f(o.centre)-2),o.baseline<e.values.length){const i=(g(o.baseline-1)+g(o.baseline))/2;t.save(),t.strokeStyle=l.textMuted,t.setLineDash([2,3]),t.beginPath(),t.moveTo(i,M),t.lineTo(i,M+d),t.stroke(),t.restore(),t.textAlign="left",t.textBaseline="top",t.fillText("limits from here \u2190",w+4,M+2)}t.strokeStyle=ee(0),t.lineWidth=1.5,t.setLineDash([]);const P=O.size>0,b=i=>e.group?.[i]??0;if(!P)t.beginPath(),e.values.forEach((i,v)=>{v===0?t.moveTo(g(v),f(i)):t.lineTo(g(v),f(i))}),t.stroke();else{for(const i of[!1,!0]){t.globalAlpha=i?1:.2,t.beginPath();for(let v=1;v<e.values.length;v++)(O.has(b(v-1))&&O.has(b(v)))===i&&(t.moveTo(g(v-1),f(e.values[v-1])),t.lineTo(g(v),f(e.values[v])));t.stroke()}t.globalAlpha=1}const C=new Map(e.violations.map(i=>[i.index,i.rules]));le=[],e.values.forEach((i,v)=>{const B=C.get(v),F=b(v),T=P&&O.has(F),A=P&&!T;t.globalAlpha=A?.2:1,t.beginPath(),t.arc(g(v),f(i),(B?4.5:3)+(T?1.5:0),0,Math.PI*2),t.fillStyle=B?l.alert:ee(F),t.fill(),T&&(t.lineWidth=1.5,t.strokeStyle=l.text,t.stroke()),B&&(t.fillStyle=l.alert,t.textAlign="center",t.textBaseline="bottom",t.fillText(B.join(","),g(v),f(i)-(T?8:6))),t.globalAlpha=1,le.push({p:{x:e.xs[v],y:i,group:F,item:e.items[v]},cx:g(v),cy:f(i)})});const u=ge?le.find(i=>i.p===ge):void 0;u&&(t.beginPath(),t.arc(u.cx,u.cy,6,0,Math.PI*2),t.lineWidth=2,t.strokeStyle=l.text,t.stroke())}function Tn(e){const l=s,a=l.marks,h=a.items.indexOf(e.item),r=(e.y-a.limits.centre)/a.limits.sigma,d=a.violations.find(n=>n.index===h)?.rules;return`<strong>${X(m.waferLabel(e.item))}</strong><br>`+X(`${l.y.label}: ${J(e.y,l.y.unit)} (${r>=0?"+":"\u2212"}${Math.abs(r).toFixed(1)}\u03C3)`)+(d?`<br>${X(`breaks rule ${d.map(n=>`${n} (${On[n]})`).join(", ")}`)}`:"")+(m.onOpenWafer?`<br><em>click to ${X(m.openActionLabel??"open this wafer")}</em>`:"")}function wn(){const e=s?.marks;if(!s||!$||!s.x||!s.y||e?.type!=="goal")return;Ge($,te??0);const l=je(W),{w:a,h,plotW:r,plotH:d}=Ne(),n=Xe($,W,a,h);if(!n)return;const{ctx:t}=n,o=Math.max(.5,...e.points.map(i=>Math.abs(i.x))),c=ue([-o,o],{...s.x,min:s.x.min??-o*1.05,max:s.x.max??o*1.05}),y=ue([0,...e.points.map(i=>i.y),1/6],{...s.y,min:s.y.min??0},{floorAtZero:!0});Z={x:c,y,plotW:r,plotH:d},Ke(t,l,a,h,r,d,c,y,s.x,s.y);const x=i=>w+c.frac(i)*r,g=i=>M+(1-y.frac(i))*d;t.save(),t.beginPath(),t.rect(w,M,r,d),t.clip(),t.font=`${Q(-1)}px system-ui, sans-serif`;const f=[[1,[6,4]],[1.33,[]]];for(const[i,v]of f){const B=1/(6*i);t.strokeStyle=l.textMuted,t.lineWidth=1,t.setLineDash(v),t.beginPath(),t.moveTo(x(-.5),g(0)),t.lineTo(x(0),g(B)),t.lineTo(x(.5),g(0)),t.stroke(),t.fillStyle=l.textMuted,t.textAlign="left",t.textBaseline="bottom",t.fillText(`Ppk ${i}`,x(0)+4,g(B)-2)}t.restore(),le=[];const p=e.points.filter(i=>O.size===0||O.has(i.group));for(const i of p)t.beginPath(),t.arc(x(i.x),g(i.y),4,0,Math.PI*2),t.fillStyle=ee(i.group),t.fill(),le.push({p:{x:i.x,y:i.y,group:i.group,item:-1,value:i.testNumber},cx:x(i.x),cy:g(i.y)});const L=p.map(i=>({x0:x(i.x)-5,x1:x(i.x)+5,y0:g(i.y)-5,y1:g(i.y)+5})),P={x0:w,x1:w+r,y0:M,y1:M+d},b=i=>i.x0>=P.x0&&i.x1<=P.x1&&i.y0>=P.y0&&i.y1<=P.y1&&!L.some(v=>i.x0<v.x1&&i.x1>v.x0&&i.y0<v.y1&&i.y1>v.y0),C=Q(-1)+2;t.fillStyle=l.text,t.textBaseline="middle";for(const i of[...p].filter(v=>v.ppk<1.33).sort((v,B)=>v.ppk-B.ppk)){const v=x(i.x),B=g(i.y),F=t.measureText(i.label).width,T=[[v+7,B,"left"],[v-7,B,"right"],[v,B-13,"center"],[v,B+13,"center"]];for(const[A,E,j]of T){const k=j==="left"?A:j==="right"?A-F:A-F/2,ie={x0:k,x1:k+F,y0:E-C/2,y1:E+C/2};if(b(ie)){L.push(ie),t.textAlign=j,t.fillText(i.label,A,E);break}}}const u=ge?le.find(i=>i.p===ge):void 0;u&&(t.beginPath(),t.arc(u.cx,u.cy,6,0,Math.PI*2),t.lineWidth=2,t.strokeStyle=l.text,t.stroke())}function Sn(e){const l=s,h=l.marks.points.find(r=>r.testNumber===e.value&&r.group===e.group);return h?`<strong>${X(h.label)}</strong>${l.groups.length>1?` \xB7 ${X(He(h.group))}`:""}<br>`+X(`Ppk ${h.ppk.toFixed(2)} \xB7 ${h.n.toLocaleString("en-GB")} dies`)+"<br>"+X(`mean ${h.x>=0?"+":"\u2212"}${Math.abs(h.x*100).toFixed(1)}% of the window from its centre, \u03C3 ${(h.y*100).toFixed(1)}% of it (${h.basis==="spec"?"spec":"test"} limits)`):""}function Pn(e,l,a,h,r){!$e||!$||($e.style.display=e?"block":"none",e&&(Object.assign($e.style,{top:`${$.offsetTop+M}px`,height:`${r}px`,left:`${$.offsetLeft+w+h+1}px`}),$e.firstElementChild.style.height=`${l}px`,Math.abs($e.scrollTop-a)>=1&&($e.scrollTop=a)))}function K(){$e&&($e.style.display="none"),dt&&!an()&&(dt.style.maxHeight=Qt(W)?"":`${ro}px`),pe&&m.panel&&(pe.style.display=Qt(W)?"flex":"none"),w=Wt,M=Rt;const e=s?.marks?.type;e==="scatter"?fn():e==="control"?Mn():e==="heatmap"?vn():e==="goal"?wn():e==="probability"?bn():e==="histogram"?hn():e==="box"||e==="bar"?dn():e==="line"&&yn(),Nt()}function $n(e){const l=s,a=m.waferLabel(e.item),h=e.die&&e.die.x!==void 0?`die (${e.die.x}, ${e.die.y})`:"",r=[a,h].filter(Boolean).join(" \xB7 "),d=(n,t)=>`${n.label}: ${J(t,n.unit)}`;return(r?`<strong>${X(r)}</strong><br>`:"")+X(d(l.x,e.x))+"<br>"+X(d(l.y,e.y))+(l.colorScale&&e.value!==void 0?`<br>${X(`${l.colorScale.label}: ${J(e.value,l.colorScale.unit)}`)}`:"")+(l.groups.length>1?`<br>${X(He(e.group))}`:"")+(m.onOpenWafer?`<br><em>click to ${X(m.openActionLabel??"open this wafer")}</em>`:"")}function _t(e){if(!Ee||!Z||!s?.x)return null;const l=$.getBoundingClientRect(),h=(e.clientX-l.left-w)/Z.plotW;if(h<0||h>1)return null;const r=Z.x.reverse?1-h:h,{edges:d,counts:n}=Ee,t=d.length-1,o=Math.min(t-1,Math.max(0,Math.floor(r*t)));ye=o;const c=s.x,y=n.map((x,g)=>({g,n:x[o]})).filter(x=>x.n>0&&(O.size===0||O.has(x.g)));return`<strong>${X(`${J(d[o],c.unit)} to ${J(d[o+1],c.unit)}`)}</strong><br>`+(y.length?y.map(x=>{const g=s.spec.histNorm==="percent"?` (${Ee.heights[x.g][o].toFixed(1)}% of ${s.groups.length>1?"its colour":"them"})`:"";return X(`${s.groups.length>1?`${He(x.g)}: `:""}${x.n.toLocaleString("en-GB")} ${s.level==="die"?"dies":"wafers"}${g}`)}).join("<br>"):"none")}function Ln(e){const l=z.createElement("div");l.dataset.wmapPlotColorbar="1",Object.assign(l.style,{display:"flex",alignItems:"center",gap:me.md,marginTop:me.md,marginBottom:me.xl,fontSize:gt.body,color:nt.label});const a=z.createElement("canvas");a.width=200,a.height=10,Object.assign(a.style,{width:"200px",height:"10px",borderRadius:Fn.control,flex:"0 0 auto"});const h=a.getContext("2d"),r=bt(s?.spec.colorGradient,s?.spec.colorReverse,e.diverging,e.rating);if(h)for(let c=0;c<200;c++)h.fillStyle=r(c/199),h.fillRect(c,0,1,10);const d=c=>s?.marks?.type==="heatmap"?Ot(c,e.unit,e.integer):J(c,e.unit),n=z.createElement("span");n.textContent=d(e.lo);const t=z.createElement("span");t.textContent=d(e.hi);const o=z.createElement("span");return o.textContent=e.label,o.style.color=nt.text,l.append(o,n,a,t),l}const st=(e,l,a)=>l.map(h=>{const{item:r,die:d}=e(h);return{x:0,y:0,group:a,item:r,die:d}});function An(){Ze?.destroy(),Ze=void 0,R.innerHTML="",$=void 0,pe=void 0,te=void 0,ot=[],ge=null,ye=-1,le=[],Z=void 0,ne=void 0,se=null,$e=void 0,lt=0,dt=void 0,_e=void 0,xe=null,de=[],be=-1,qe=Ue,kt=new Map,O.clear(),he.clear();for(const n of ce)n.destroy();ce=[],ut?.(),ut=void 0,R.style.overflowY="hidden",delete W.dataset.wmapPlotPanelsOwner,delete W.dataset.wmapPanelsHover,we(()=>W.querySelector("canvas"));const e=s,l=!!e?.facets&&!m.panel;if(W.style.flex=l?"0 0 auto":"1 1 0px",W.style.gridColumn=l?"1 / -1":"",!e)return;if(U.textContent=m.panel?m.panel.title:Nn(e),W.dataset.wmapChartTitle=U.textContent,e.issues.length||!e.marks){for(const n of e.issues)Jt(R,n);return}const a=z.createElement("div");Object.assign(a.style,{color:nt.label,fontSize:gt.body,marginBottom:me.xs});const h=m.openActionLabel??"open this wafer",r=e.marks.type==="scatter"?(m.onOpenWafer?` \xB7 click a point to ${h}`:"")+(m.onSelectPoints?` \xB7 drag to select ${e.level==="die"?"dies":"wafers"}`:""):e.marks.type==="box"||e.marks.type==="bar"?(m.onOpenWafer&&e.marks.categoryItems?` \xB7 click a ${e.marks.type} to ${h}`:m.onSelectPoints?` \xB7 click or right-click to ${m.selectActionLabel??"chart or tabulate what it counts"}`:"")+(e.marks.type==="box"?" \xB7 box: quartiles; whiskers: minimum and maximum":""):e.marks.type==="histogram"&&m.onSelectPoints?" \xB7 click or right-click a bar to chart or tabulate its dies":e.marks.type==="heatmap"&&m.onSelectPoints?` \xB7 click or right-click a cell to chart or tabulate its ${e.level==="die"?"dies":"wafers"}`:e.marks.type==="probability"?(m.onOpenWafer?` \xB7 click a point to ${h}`:"")+(m.onSelectPoints?` \xB7 drag to select ${e.level==="die"?"dies":"wafers"}`:""):e.marks.type==="line"&&m.onSelectPoints?` \xB7 click a point to ${m.selectActionLabel??"chart or tabulate what it averages"} \xB7 drag across the plot to select ${m.selectRangeLabel??"a run of points"}`:"";a.textContent=m.panel?`${e.population.wafers.toLocaleString("en-GB")} wafer${e.population.wafers===1?"":"s"} \xB7 ${e.population.dies.toLocaleString("en-GB")} dies`:Ht(e)+(e.facets?"":r),m.panel||Xn(a,Kt(e)),Vt(a),te=z.createElement("div"),te.style.display="flow-root",R.appendChild(te),te.appendChild(a);for(const n of e.fitNotes??[]){const t=z.createElement("div");t.dataset.wmapPlotFit="1",t.textContent=n,Object.assign(t.style,{color:nt.text,fontSize:gt.body,marginBottom:me.xxs,fontVariantNumeric:"tabular-nums"}),te.appendChild(t)}for(const n of e.notes){const t=z.createElement("div");Vt(t),t.textContent=n,Object.assign(t.style,{color:nt.warnText,fontSize:gt.body,marginBottom:me.xs}),te.appendChild(t)}if(e.plotted===0){Jt(R,"Nothing to plot: no mark has every value this plot needs.");return}if(e.groups.length>1&&!e.colorRepeatsX&&(pe=z.createElement("div"),pe.dataset.wmapPlotLegend="1",Object.assign(pe.style,{display:m.panel?"none":"flex",flexWrap:"wrap",gap:me.sm,marginTop:me.md,marginBottom:me.xl,overflowY:"auto"}),dt=pe,e.facets&&Object.assign(pe.style,{position:"sticky",top:"0",zIndex:"2",background:nt.menuBg,paddingBottom:me.sm,marginBottom:me.md}),e.groups.forEach((n,t)=>{const o=Vn(n||"(none)",ee(t),z);Yn(o.el,W,_,`${n||"(none)"} \u2014 click to filter`),o.el.addEventListener("click",()=>{if(O.has(t)?O.delete(t):O.add(t),ot.forEach((c,y)=>c.setState({selected:O.has(y),dimmed:O.size>0&&!O.has(y)})),ce.length)for(const c of ce)c.setActiveGroups(O);else K()}),ot.push(o),pe.appendChild(o.el)}),ot.forEach((n,t)=>n.setState({selected:O.has(t),dimmed:O.size>0&&!O.has(t)})),te.appendChild(pe)),e.colorScale&&!m.panel&&(pe=Ln(e.colorScale),te.appendChild(pe)),e.facets){const n=z.createElement("div");n.dataset.wmapPlotPanels="1",n.style.isolation="isolate",Object.assign(n.style,{display:"grid",gap:me.md});const t=()=>{n.style.gridTemplateColumns=Ie?`repeat(${Ie}, minmax(0, 1fr))`:`repeat(auto-fill, minmax(${ho}px, 1fr))`};t();const o=Zn("Columns:",[{value:"auto",label:"Auto (0)"},...[1,2,3,4,5].map(x=>({value:String(x),label:`${x} (${x})`}))],Ie?String(Ie):"auto",x=>{Ie=x==="auto"?void 0:Number(x),t()},{hook:"panel-columns",maxWidth:"90px",ownerDocument:z});Object.assign(o.style,{justifyContent:"flex-end",marginBottom:me.sm});const c=o.querySelector("button");R.append(o,n),R.style.overflowY="auto",W.dataset.wmapPlotPanelsOwner="1",ht&&(W.dataset.wmapPanelsHover="1");const y=x=>{if(!ht||x.ctrlKey||x.metaKey||x.altKey||x.shiftKey||x.defaultPrevented||!/^[0-5]$/.test(x.key))return;const g=x.target;typeof g?.closest=="function"&&g.closest('input, textarea, select, [contenteditable="true"], [role="menu"]')||(x.preventDefault(),Ie=x.key==="0"?void 0:Number(x.key),t(),c&&(c.value=Ie?String(Ie):"auto"))};z.addEventListener("keydown",y),ut=()=>z.removeEventListener("keydown",y);for(const x of e.facets){const g=renderPlotChart({...m,panel:{title:x.label}});n.appendChild(g.card),g.setPlot(x.plot),ce.push(g)}we(()=>mo(n));return}$=z.createElement("canvas"),$.style.display="block",$.style.cursor=e.marks.type==="scatter"||e.marks.type==="probability"?"crosshair":"default",R.appendChild($);const d=m.onWaferMenu||m.onSelectPoints?(n,t)=>{!n.die&&m.onWaferMenu?m.onWaferMenu(n.item,Ve(e.spec),t):n.die&&m.onSelectPoints&&(t.preventDefault(),m.onSelectPoints([n],{x:t.clientX,y:t.clientY},$))}:void 0;if(e.marks.type==="scatter"){const n=e.marks,t=!!m.onSelectPoints;Ze=Bt({card:W,canvas:$,tooltip:_,drawn:()=>le,describe:$n,clickable:()=>!!m.onOpenWafer,onOpen:o=>m.onOpenWafer?.(o.item,Ve(e.spec)),onContextMenu:d,onHover:o=>{ge=o,K()},select:t?{pick:({x0:o,y0:c,x1:y,y1:x})=>Z?(O.size===0?n.points:n.points.filter(f=>O.has(f.group))).filter(f=>{const p=w+Z.x.frac(f.x)*Z.plotW,L=M+(1-Z.y.frac(f.y))*Z.plotH;return p>=o&&p<=y&&L>=c&&L<=x}):[],onPicked:(o,c)=>{he.clear();for(const y of o)he.add(y);K(),o.length>0&&m.onSelectPoints(o,c,$)},onClear:()=>{he.clear(),K()},hasSelection:()=>he.size>0}:void 0})}else if(e.marks.type==="control"||e.marks.type==="goal"){const n=e.marks.type==="control";Ze=Bt({card:W,canvas:$,tooltip:_,drawn:()=>le,describe:n?Tn:Sn,clickable:()=>n&&!!m.onOpenWafer,onOpen:t=>{n&&m.onOpenWafer?.(t.item,Ve(e.spec))},onContextMenu:n?d:void 0,onHover:t=>{ge=t,K()}})}else if(e.marks.type==="probability"){const n=e.marks,t=e.spec.probabilityAxis==="linear";Ze=Bt({card:W,canvas:$,tooltip:_,drawn:()=>le,describe:xn,clickable:()=>!!m.onOpenWafer,onOpen:o=>m.onOpenWafer?.(o.item,Ve(e.spec)),onContextMenu:d,onHover:o=>{ge=o,K()},select:m.onSelectPoints?{pick:({x0:o,y0:c,x1:y,y1:x})=>{if(!Z)return[];const g=[];return n.values.forEach((f,p)=>{O.size>0&&!O.has(p)||f.forEach((L,P)=>{const b=ct(P,f.length),C=w+Z.x.frac(L)*Z.plotW,u=M+(1-Z.y.frac(t?b:Yt(b)))*Z.plotH;if(C<o||C>y||u<c||u>x)return;const{item:i,die:v}=n.unit(n.units[p][P]);g.push({x:L,y:b,group:p,item:i,die:v})})}),g},onPicked:(o,c)=>{he.clear();for(const y of o)he.add(y);K(),o.length>0&&m.onSelectPoints(o,c,$)},onClear:()=>{he.clear(),K()},hasSelection:()=>he.size>0}:void 0})}else if(e.marks.type==="histogram"){un(),$.addEventListener("mousemove",t=>{const o=_t(t);if(!o){_.style.display="none",ye>=0&&(ye=-1,K());return}_.innerHTML=o,_.style.display="block",yt(_,W,t.clientX,t.clientY),K()}),$.addEventListener("mouseleave",()=>{_.style.display="none",ye>=0&&(ye=-1,K())});const n=e.marks;if(m.onSelectPoints){$.style.cursor="pointer";const t=o=>{if(!Ee||_t(o)===null)return!1;const c=ye,{edges:y}=Ee,x=Z?.x.log,g=x?Math.log10(y[0]):y[0],f=x?Math.log10(y[y.length-1]):y[y.length-1],p=y.length-1,L=[];return n.values.forEach((P,b)=>{if(O.size>0&&!O.has(b))return;const C=[];P.forEach((u,i)=>{const v=x?u>0?Math.log10(u):NaN:u;!Number.isFinite(v)||v<g||v>f||Math.min(p-1,Math.floor((v-g)/(f-g)*p))===c&&C.push(n.units[b][i])});for(const u of st(n.unit,C,b))L.push(u)}),L.length===0?!1:(_.style.display="none",m.onSelectPoints(L,{x:o.clientX,y:o.clientY},$,{range:{lo:y[c],hi:y[c+1]},...O.size===1?{group:e.groups[[...O][0]]}:{}}),!0)};$.addEventListener("click",o=>{t(o)}),$.addEventListener("contextmenu",o=>{t(o)&&o.preventDefault()})}}else if(e.marks.type==="heatmap"){const n=e.marks;$.addEventListener("mousemove",o=>{const c=zt(o);if($.style.cursor=c&&m.onSelectPoints&&n.cellUnits[c.r][c.c].length?"pointer":"default",!c){_.style.display="none",xe&&(xe=null,K());return}_.innerHTML=kn(c),_.style.display="block",yt(_,W,o.clientX,o.clientY),(!xe||xe.r!==c.r||xe.c!==c.c)&&(xe=c,K())}),$.addEventListener("mouseleave",()=>{_.style.display="none",xe&&(xe=null,K())});const t=o=>{const c=zt(o);if(!c||!m.onSelectPoints)return!1;const y=n.cellUnits[c.r][c.c];return y.length?(_.style.display="none",m.onSelectPoints(st(n.unit,y,0),{x:o.clientX,y:o.clientY},$,{cell:{row:n.rows[c.r],column:n.cols[c.c]}}),!0):!1};$.addEventListener("click",o=>{t(o)}),$.addEventListener("contextmenu",o=>{t(o)&&o.preventDefault()})}else if(e.marks.type==="box"||e.marks.type==="bar"){const n=e.marks;if(e.spec.orientation==="horizontal"){const o=z.createElement("div");o.dataset.wmapRowScroll="1",o.tabIndex=0,o.setAttribute("aria-label",`Scroll the ${n.categories.length} rows`),Object.assign(o.style,{position:"absolute",display:"none",width:"12px",overflowY:"auto",overflowX:"hidden",zIndex:"1"});const c=z.createElement("div");c.style.width="1px",o.appendChild(c),o.addEventListener("scroll",()=>{Math.abs(o.scrollTop-lt)<1||(lt=o.scrollTop,_.style.display="none",K())}),$.addEventListener("wheel",y=>{if(o.style.display==="none")return;const x=o.scrollTop;o.scrollTop+=y.deltaMode===1?y.deltaY*16:y.deltaY,o.scrollTop!==x&&y.preventDefault()},{passive:!1}),R.appendChild(o),$e=o}$.addEventListener("mousemove",o=>{const c=Pt(o);if($.style.cursor=c&&(n.categoryItems&&m.onOpenWafer||m.onSelectPoints&&n.cellUnits[c.g][c.c].length>0)?"pointer":"default",!c){_.style.display="none",se&&(se=null,K());return}_.innerHTML=gn(c),_.style.display="block",yt(_,W,o.clientX,o.clientY),(!se||se.g!==c.g||se.c!==c.c)&&(se=c,K())}),$.addEventListener("mouseleave",()=>{_.style.display="none",se&&(se=null,K())});const t=(o,c)=>{const y=n.cellUnits[c.g][c.c];return y.length===0||!m.onSelectPoints?!1:(_.style.display="none",m.onSelectPoints(st(n.unit,y,c.g),{x:o.clientX,y:o.clientY},$,{category:n.categories[c.c],...e.groups.length>1?{group:e.groups[c.g]}:{},...mn(n.categories[c.c])}),!0)};$.addEventListener("contextmenu",o=>{const c=Pt(o);if(!c)return;const y=n.categoryItems?.[c.c];y!==void 0&&y>=0&&m.onWaferMenu?(_.style.display="none",m.onWaferMenu(y,Ve(e.spec),o)):t(o,c)&&o.preventDefault()}),$.addEventListener("click",o=>{const c=Pt(o);if(!c)return;const y=n.categoryItems?.[c.c];if(y!==void 0&&y>=0&&m.onOpenWafer){m.onOpenWafer(y,Ve(e.spec));return}t(o,c)})}else if(e.marks.type==="line"){$.addEventListener("mousemove",o=>{const c=$t(o);if(!c){_.style.display="none",K();return}_.innerHTML=c,_.style.display="block",yt(_,W,o.clientX,o.clientY),K()}),$.addEventListener("mouseleave",()=>{_.style.display="none",be>=0&&(be=-1,K())});const n=e.marks,t=()=>n.cellUnits.flatMap((o,c)=>O.size>0&&!O.has(c)?[]:st(n.unit,o[be]??[],c));if((m.onWaferMenu||m.onSelectPoints)&&$.addEventListener("contextmenu",o=>{if($t(o)===null||be<0)return;const c=Ft(be);if(c!==void 0&&m.onWaferMenu)_.style.display="none",m.onWaferMenu(c,Ve(e.spec),o);else if(m.onSelectPoints){const y=t();y.length>0&&(o.preventDefault(),_.style.display="none",m.onSelectPoints(y,{x:o.clientX,y:o.clientY},$))}}),m.onSelectPoints){$.style.cursor="pointer";let o=!1;$.addEventListener("mousedown",c=>{if(c.button!==0)return;o=!1;const y=$.getBoundingClientRect(),x=c.clientX-y.left,g=f=>{z.removeEventListener("mouseup",g);const p=f.clientX-y.left;if(Math.abs(p-x)<4)return;o=!0;const[L,P]=x<p?[x,p]:[p,x],b=de.map((u,i)=>u>=L&&u<=P?i:-1).filter(u=>u>=0),C=n.cellUnits.flatMap((u,i)=>O.size>0&&!O.has(i)?[]:b.flatMap(v=>st(n.unit,u[v]??[],i)));C.length>0&&(_.style.display="none",m.onSelectPoints(C,{x:f.clientX,y:f.clientY},$))};z.addEventListener("mouseup",g)}),$.addEventListener("click",c=>{if(o){o=!1;return}if($t(c)===null||be<0)return;const y=t();y.length>0&&(_.style.display="none",m.onSelectPoints(y,{x:c.clientX,y:c.clientY},$))})}}K()}V(()=>{const e=s;if(!e||!e.marks)return;const l=bt(e.spec.colorGradient,e.spec.colorReverse,e.colorScale?.diverging,e.colorScale?.rating);return{caption:[Ht(e),...e.fitNotes??[],...e.notes,...oo(Kt(e))],legend:e.groups.length>1&&!e.colorRepeatsX?e.groups.map((a,h)=>({label:a||"(none)",color:ee(h)})):void 0,colorbar:e.colorScale?{label:e.colorScale.label,lo:J(e.colorScale.lo,e.colorScale.unit),hi:J(e.colorScale.hi,e.colorScale.unit),color:l}:void 0}});const Cn=jn(W,()=>K());return{card:W,actions:Y,setPlot(e){s=e,Me(io(e.spec)),An()},setMenuRows(e){Oe=e},setActiveGroups(e){O.clear();for(const l of e)O.add(l);ot.forEach((l,a)=>l.setState({selected:O.has(a),dimmed:O.size>0&&!O.has(a)})),K()},destroy(){ut?.(),Cn.disconnect(),Ze?.destroy();for(const e of ce)e.destroy()}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parseRegionKey as
|
|
1
|
+
import{parseRegionKey as q,parseReticleCellKey as H}from"../../stats/regions.js";import{yieldFill as _}from"./palette.js";import{fontPx as L}from"../toolbar.js";import{escHtml as K}from"../../core/utils.js";import{makeTooltip as Q,positionChartTooltip as Z,cardShell as U,resolveChartCanvasColors as z,observeResize as J,growCardToFitContent as V,setChartGrow as x,isExpandedCard as ee,bodyRoom as te,renderEmptyState as ne,prepareCanvas as oe}from"./chartShell.js";import{cardAbout as le}from"./cardAbout.js";const N=320,ie=220,Y={SE:[0,Math.PI/2],SW:[Math.PI/2,Math.PI],NW:[Math.PI,1.5*Math.PI],NE:[1.5*Math.PI,2*Math.PI]};function B(e,i,o,g,M,r){e.beginPath(),e.moveTo(i+r,o),e.arcTo(i+g,o,i+g,o+M,r),e.arcTo(i+g,o+M,i,o+M,r),e.arcTo(i,o+M,i,o,r),e.arcTo(i,o,i+g,o,r),e.closePath()}function A(e,i,o,g,M,r){e.font=`${L(-1)}px system-ui, sans-serif`;const b=e.measureText(g).width;e.font="700 11px system-ui, sans-serif";const E=e.measureText(M).width,R=Math.max(b,E)+12,y=30;e.fillStyle=r.bg,B(e,i-R/2,o-y/2,R,y,4),e.fill(),e.strokeStyle=r.border,e.lineWidth=1,e.stroke(),e.textAlign="center",e.textBaseline="middle",e.fillStyle=r.textMuted,e.font=`${L(-1)}px system-ui, sans-serif`,e.fillText(g,i,o-7),e.fillStyle=r.text,e.font="700 11px system-ui, sans-serif",e.fillText(M,i,o+7)}function re(e,i,o,g,M){e.font="700 11px system-ui, sans-serif";const r=e.measureText(g).width+8;e.fillStyle=M.bg,B(e,i-r/2,o-8,r,16,3),e.fill(),e.textAlign="center",e.textBaseline="middle",e.fillStyle=M.text,e.fillText(g,i,o+.5)}export function renderRegionYieldDiagram(e){const{mode:i,rows:o,onSaveImage:g}=e,M=e.title??(i==="ring"?"Ring yield":i==="quadrant"?"Quadrant yield":"Reticle cell yield"),{card:r,body:b,setAbout:E}=U(M,g,e.ownerDocument);E(le(i==="ring"?"ring-yield":i==="quadrant"?"quadrant-yield":"reticle-yield")),x(r,"square");const R=o.length>0;if(!R)return ne(b,"No bin data available for regional yield."),{card:r,hasData:R,destroy:()=>{}};Object.assign(b.style,{display:"flex",justifyContent:"center",alignItems:"center"});const y=r.ownerDocument.createElement("canvas");Object.assign(y.style,{display:"block",flex:"none"}),b.appendChild(y);let C={cx:0,cy:0,R:0};const W=new Map;let S=0,p=0;if(i==="reticleCell"){for(const l of o){const n=H(l.key);n&&(W.set(`${n.column},${n.row}`,l),S=Math.max(S,n.column+1),p=Math.max(p,n.row+1))}S=Math.max(S,e.grid?.width??0),p=Math.max(p,e.grid?.height??0)}let k={x0:0,y0:0,side:0};const T=Q(r);function O(l,n,c,t){const a=Math.max(4,Math.floor(Math.min((n-40)/S,(n-40)/p))),f=Math.round((n-a*S)/2),s=Math.round((n-a*p)/2);k={x0:f,y0:s,side:a};for(let I=0;I<p;I++)for(let u=0;u<S;u++){const h=f+u*a,d=s+(p-1-I)*a,m=W.get(`${u},${I}`);l.fillStyle=m?t(m.yieldPercent):c.bg,l.fillRect(h,d,a,a),l.strokeStyle=c.border,l.lineWidth=1,l.strokeRect(h+.5,d+.5,a-1,a-1),!(!m||a<30)&&(a>=54?A(l,h+a/2,d+a/2,`(${u}, ${I})`,`${m.yieldPercent.toFixed(1)}%`,c):re(l,h+a/2,d+a/2,`${m.yieldPercent.toFixed(0)}%`,c))}l.strokeStyle=c.text,l.lineWidth=1.5,l.strokeRect(f+.5,s+.5,a*S-1,a*p-1)}function F(){const l=Math.max(ie,Math.min(b.clientWidth||N,N));V(r,b,l);const n=ee(r)?Math.max(l,Math.floor(Math.min(b.clientWidth,te(r,b,y)))):l,c=oe(y,r,n,n);if(!c)return;const{ctx:t}=c,P=z(r),a=_;if(i==="reticleCell"){O(t,n,P,a);return}const f=n/2,s=n/2,u=n/2-34;if(C={cx:f,cy:s,R:u},i==="ring"){const h=o.length;for(let d=0;d<h;d++){const m=d/h*u,w=(d+1)/h*u;t.beginPath(),t.arc(f,s,w,0,2*Math.PI),t.arc(f,s,m,0,2*Math.PI,!0),t.closePath(),t.fillStyle=a(o[d].yieldPercent),t.fill(),t.strokeStyle=P.border,t.lineWidth=1,t.stroke()}t.beginPath(),t.arc(f,s,u,0,2*Math.PI),t.strokeStyle=P.text,t.lineWidth=1.5,t.stroke();for(let d=0;d<h;d++){const m=(d+.5)/h*u,w=-Math.PI/2+d/h*2*Math.PI,$=f+Math.cos(w)*m,v=s+Math.sin(w)*m;A(t,$,v,o[d].label,`${o[d].yieldPercent.toFixed(1)}%`,P)}}else{for(const h of o){const d=q(h.key).quadrant,m=d?Y[d]:void 0;if(!m)continue;const[w,$]=m;t.beginPath(),t.moveTo(f,s),t.arc(f,s,u,w,$),t.closePath(),t.fillStyle=a(h.yieldPercent),t.fill(),t.strokeStyle=P.border,t.lineWidth=1,t.stroke();const v=(w+$)/2,j=f+Math.cos(v)*u*.6,G=s+Math.sin(v)*u*.6;A(t,j,G,h.label,`${h.yieldPercent.toFixed(1)}%`,P)}t.beginPath(),t.arc(f,s,u,0,2*Math.PI),t.strokeStyle=P.text,t.lineWidth=1.5,t.stroke()}}function D(l,n){if(i==="reticleCell"){const f=Math.floor((l-k.x0)/k.side),s=Math.floor((n-k.y0)/k.side);return k.side<=0||f<0||f>=S||s<0||s>=p?null:W.get(`${f},${p-1-s}`)??null}const c=l-C.cx,t=n-C.cy,P=Math.hypot(c,t);if(!(P<=C.R))return null;if(i==="ring")return o[Math.min(o.length-1,Math.floor(P/C.R*o.length))]??null;const a=(Math.atan2(t,c)+2*Math.PI)%(2*Math.PI);return o.find(f=>{const s=Y[q(f.key).quadrant??""];return!!s&&a>=s[0]&&a<s[1]})??null}if(y.addEventListener("mousemove",l=>{const n=y.getBoundingClientRect(),c=D(l.clientX-n.left,l.clientY-n.top);if(!c){T.style.display="none";return}T.innerHTML=`<strong>${K(c.label)}</strong><br>${c.yieldPercent.toFixed(1)}% yield (${c.passDies} of ${c.n} dies)`+(e.onSelectRegion?"<br><em>click to chart or tabulate these dies</em>":""),T.style.display="block",Z(T,r,l.clientX,l.clientY)}),y.addEventListener("mouseleave",()=>{T.style.display="none"}),e.onSelectRegion){y.style.cursor="pointer";const l=n=>{const c=y.getBoundingClientRect(),t=D(n.clientX-c.left,n.clientY-c.top);return t?(e.onSelectRegion(t,n),!0):!1};y.addEventListener("click",n=>{l(n)}),y.addEventListener("contextmenu",n=>{l(n)&&n.preventDefault()})}const X=J(r,()=>F());return F(),{card:r,hasData:R,destroy:()=>X.disconnect()}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{buildSweepData as ue,sweepAppliesTo as he}from"../../stats/sweep.js";import{fmt as
|
|
1
|
+
import{buildSweepData as ue,sweepAppliesTo as he}from"../../stats/sweep.js";import{fmt as D}from"../../renderer/fmt.js";import{SPACE as oe,fontPx as me,FONT as ie,CLR as Y,ALPHA as xe}from"../toolbar.js";import{categorical as ge}from"./palette.js";import{cardShell as be,observeResize as ye,makeTooltip as $e,positionChartTooltip as ve,renderEmptyState as re,chartFillHeight as Te,populationPhrase as Se,PADDING as J,prepareCanvas as Le,makeAxisFormat as Me,VERTICAL_TICK_SPACING_PX as we}from"./chartShell.js";import{fitTicks as Pe}from"../../renderer/axisTicks.js";import{escHtml as m}from"../../core/utils.js";import{unmarkedLabel as ke,derivedTestNote as Ae,DERIVED_MARK as Q,DERIVED_KEY as Ee}from"../../renderer/testLabel.js";import{plotAbout as Ce}from"./cardAbout.js";function O(p,g){return g?D(p,g).replace(/(\.\d*?)0+(?=\D|$)/,"$1").replace(/\.(?=\D|$)/,""):Number.isInteger(p)?String(p):String(Number(p.toPrecision(3)))}function Ie(p){const g=ke(p.label),C=g.length>9?`${g.slice(0,8)}\u2026`:g;return p.derived?`${Q} ${C}`:C}const le=260,De=62,Ne=56,He=3;function Be(p,g){const C=p.series[0]?.points[0]?.testNumber;return C===void 0?void 0:g?.find(j=>j.testNumber===C)?.unit}export function renderSweepPanel(p){const{testDefs:g,onSaveImage:C,population:j}=p;let $=p.spec,z=p.dies;const{card:b,body:M,heading:ae,controlsRow:ce,setAbout:fe}=be($.title,C,p.ownerDocument);fe(Ce({id:$.id,chart:"sweep"})),b.style.alignSelf="start";const V=b.ownerDocument,N=V.createElement("div");N.dataset.wmapCaption="1",Object.assign(N.style,{color:Y.label,fontSize:ie.body,marginBottom:oe.sm}),b.insertBefore(N,M);const W=V.createElement("div");Object.assign(W.style,{color:Y.label,fontSize:ie.body,marginTop:oe.sm}),b.appendChild(W);const w=$e(b);let F=null,L=null,v;const R=n=>$.series[n]?.color??ge(n);function K(){if(M.innerHTML="",W.innerHTML="",F?.disconnect(),F=null,$.series.length===0){N.textContent="",re(M,"This sweep defines no series.");return}if(L=ue(z,g,$),v=Be(L,g),L.series.filter(s=>s.points.some(l=>l.count>0)).length===0){N.textContent="",re(M,he($,g)?"No values recorded for the tests in this sweep.":"None of this sweep\u2019s tests are in this data \u2014 it may belong to another test program."),Z();return}N.textContent=`Line = median across ${Se(L.dieCount,z.length,j)}, band = p10\u2013p90`+($.series.length>2?" \xB7 crossing and separation measured between the first two series":"")+(p.onSelectPoint?" \xB7 click a level to chart or tabulate the dies measured there":"");const a=V.createElement("canvas");a.style.display="block",M.appendChild(a);let i=null;const P=()=>{const s=L;if(s===null)return;const l=Math.max(1,M.clientWidth),y=Math.max(le,Te(b,M,a,le)),T=Le(a,b,l,y);if(!T)return;const{ctx:e,theme:c}=T,x=t=>s.xScale==="log"?Math.log10(t):t;let f=1/0,o=-1/0,u=1/0,h=-1/0;for(const t of s.series)for(const r of t.points)r.count!==0&&(f=Math.min(f,r.p10),o=Math.max(o,r.p90),u=Math.min(u,x(r.x)),h=Math.max(h,x(r.x)));for(const t of s.separations)f=Math.min(f,t.y),o=Math.max(o,t.y);if(!Number.isFinite(f)||!Number.isFinite(o))return;f===o&&(f-=1,o+=1);const ee=(o-f)*.08;if(f-=ee,o+=ee,u===h&&(u-=.5,h+=.5),s.xScale==="log"){const t=(h-u)*.03;u-=t,h+=t}e.font=`${me(-1)}px system-ui, sans-serif`;const S=J+De,H=l-J-8,X=J,k=y-Ne,A=t=>k-(t-f)/(o-f)*(k-X),B=t=>S+(x(t)-u)/(h-u)*(H-S);i={left:S,right:H,xLo:u,xHi:h,yOf:A,ax:x},e.strokeStyle=c.border,e.lineWidth=1,e.beginPath(),e.moveTo(S,X),e.lineTo(S,k),e.lineTo(H,k),e.stroke(),e.fillStyle=c.text,e.textAlign="right",e.textBaseline="middle";const te=Pe(f,o,k-X,()=>we),pe=Me(Math.max(Math.abs(f),Math.abs(o)),v,te.step||void 0);for(const t of te.ticks)e.fillText(pe.tickWithUnit(t),S-6,A(t));for(const t of s.separations){const r=A(t.y);e.save(),e.strokeStyle=c.limitLine,e.globalAlpha=.55,e.setLineDash([4,3]),e.beginPath(),e.moveTo(S,r),e.lineTo(H,r),e.stroke(),e.restore()}if(s.series.forEach((t,r)=>{const E=t.points.filter(d=>d.count>0);if(!(E.length<2)){e.save(),e.fillStyle=R(r),e.globalAlpha=.14,e.beginPath(),E.forEach((d,I)=>{const _=B(d.x),se=A(d.p90);I===0?e.moveTo(_,se):e.lineTo(_,se)});for(let d=E.length-1;d>=0;d--){const I=E[d];e.lineTo(B(I.x),A(I.p10))}e.closePath(),e.fill(),e.restore()}}),s.series.forEach((t,r)=>{e.strokeStyle=R(r),e.lineWidth=1.75,e.beginPath();let E=!1;for(const d of t.points){if(d.count===0){E=!1;continue}const I=B(d.x),_=A(d.median);E?e.lineTo(I,_):e.moveTo(I,_),E=!0}e.stroke(),e.fillStyle=R(r);for(const d of t.points)d.count!==0&&(e.beginPath(),e.arc(B(d.x),A(d.median),He,0,Math.PI*2),e.fill())}),s.crossing){const t=B(s.crossing.x),r=A(s.crossing.y);e.save(),e.strokeStyle=c.text,e.globalAlpha=.45,e.setLineDash([3,3]),e.beginPath(),e.moveTo(t,r),e.lineTo(t,k),e.stroke(),e.restore(),e.save(),e.strokeStyle=c.text,e.lineWidth=2,e.beginPath(),e.arc(t,r,5.5,0,Math.PI*2),e.stroke(),e.restore()}e.fillStyle=c.text,e.textBaseline="top",e.textAlign="center";const G=s.xIsPhysical?[...new Set(s.series.flatMap(t=>t.points.map(r=>r.x)))].sort((t,r)=>t-r).map(t=>({x:t,label:O(t,s.xUnit)})):(s.series[0]?.points??[]).map(t=>({x:t.x,label:Ie(t)})),de=Math.max(1,Math.ceil(G.length*46/Math.max(1,H-S)));G.forEach((t,r)=>{r%de!==0&&r!==G.length-1||e.fillText(t.label,B(t.x),k+6)}),e.save(),e.fillStyle=c.text,e.globalAlpha=.75,e.textAlign="center",e.textBaseline="bottom",e.fillText(s.xLabel,(S+H)/2,y-4),e.restore(),e.save(),e.fillStyle=c.text,e.globalAlpha=.75,e.translate(12,(X+k)/2),e.rotate(-Math.PI/2),e.textBaseline="top",e.fillText(s.yLabel+(v?` (${v})`:""),0,0),e.restore(),e.textAlign="left",e.textBaseline="middle";let U=S+4;const ne=X+8;s.series.forEach((t,r)=>{e.fillStyle=R(r),e.fillRect(U,ne-4,10,3),U+=14,e.fillStyle=c.text,e.fillText(t.label,U,ne),U+=e.measureText(t.label).width+16})};P(),F=ye(M,P);const q=s=>{const l=L;if(l===null||i===null)return null;const y=s.clientX-a.getBoundingClientRect().left,T=i.xLo+(y-i.left)/Math.max(1,i.right-i.left)*(i.xHi-i.xLo);let e=null;for(const c of l.series)for(const x of c.points){if(x.count===0)continue;const f=Math.abs(i.ax(x.x)-T);(e===null||f<e.dist)&&(e={x:x.x,dist:f})}return e===null?null:e.x};if(a.addEventListener("mousemove",s=>{const l=L,y=q(s);if(l===null||y===null){w.style.display="none";return}const T={x:y},e=l.series.map(o=>o.points.find(u=>u.x===T.x&&u.count>0)),c=l.series.map((o,u)=>{const h=e[u];return h===void 0?"":`<span style="color:${R(u)}">\u25A0</span> ${h.derived?`${Q} `:""}${m(o.label)}: <strong>${m(D(h.median,v))}</strong><br><span style="opacity:${xe.muted}">p10 ${m(D(h.p10,v))} \u2013 p90 ${m(D(h.p90,v))} \xB7 n=${h.count.toLocaleString()}</span>`}).filter(Boolean);if(c.length===0){w.style.display="none";return}const x=l.series.flatMap(o=>o.points).find(o=>o.x===T.x&&o.count>0),f=[...new Set(e.map(o=>Ae(o)).filter(o=>o!==void 0))];w.innerHTML=`<strong>${m(x?.label??"")}</strong><br>`+c.join("<br>")+f.map(o=>`<br><em>${m(o)}</em>`).join(""),w.style.display="block",ve(w,b,s.clientX,s.clientY)}),a.addEventListener("mouseleave",()=>{w.style.display="none"}),p.onSelectPoint){a.style.cursor="pointer";const s=l=>{const y=L,T=q(l);if(y===null||T===null)return!1;const e=y.series.flatMap(c=>c.points.filter(x=>x.x===T&&x.count>0));return e.length===0?!1:(w.style.display="none",p.onSelectPoint({testNumbers:e.map(c=>c.testNumber),label:e[0].label},l),!0)};a.addEventListener("click",l=>{s(l)}),a.addEventListener("contextmenu",l=>{s(l)&&l.preventDefault()})}Z()}function Z(){const n=L;if(n===null)return;const a=[];n.notMeasured!==void 0?a.push(`<strong>Crossing and widths:</strong> not measured \u2014 ${m(n.notMeasured)}`):n.crossing?a.push(`<strong>Crossing:</strong> ${m(`${n.xLabel} = ${O(n.crossing.x,n.xUnit)}`)} \xB7 ${m(`${n.yLabel} ${D(n.crossing.y,v)}`)}`+(n.crossing.multiple?" <em>(first of several \u2014 the curves cross more than once)</em>":"")):($.crossing??!0)&&$.series.length>=2&&a.push("<strong>Crossing:</strong> the first two series do not cross over the swept range");for(const i of n.separations)if(i.distance!==null){const P=i.ratio!==void 0&&i.from!==void 0&&i.to!==void 0?`\xD7${Number(i.ratio.toPrecision(3))} on the ${n.xLabel} axis (${O(i.from,n.xUnit)} \u2192 ${O(i.to,n.xUnit)})`:`${O(i.distance,n.xUnit)} on the ${n.xLabel} axis`;a.push(`<strong>Width at ${m(D(i.y,v))}:</strong> ${m(P)}`)}else{const P=i.reason==="second-series-never-reaches"?n.series[1]?.label:n.series[0]?.label;a.push(`<strong>Width at ${m(D(i.y,v))}:</strong> not measurable \u2014 ${m(P??"a series")} never reaches this level`)}n.series.some(i=>i.points.some(P=>P.derived))&&a.push(`${Q} ${m(Ee)} \u2014 hover a point for the expression`);for(const i of n.warnings)a.push(`<span style="color:${Y.errText}">${m(i)}</span>`);W.innerHTML=a.join("<br>")}return K(),{card:b,destroy:()=>{F?.disconnect(),w.remove(),b.remove()},setDies:n=>{z=n,K()},setSpec:n=>{$=n,ae.textContent=n.title,b.dataset.wmapChartTitle=n.title,K()},actions:ce}}
|