{"spec_id":"facet-grid","library":"muix","language":"javascript","code":"// anyplot.ai\n// facet-grid: Faceted Grid Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 85/100 | Updated: 2026-10-24\n\nimport * as React from \"react\";\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst FONT = \"system-ui, -apple-system, 'Segoe UI', sans-serif\";\nconst TITLE = \"facet-grid · javascript · muix · anyplot.ai\";\n\nconst AXIS_PAD_SHARE = 0.08;\nconst MARKER_SIZE = 6.5;\nconst MARKER_COLOR = `${t.palette[0]}CC`;\n\nconst TITLE_H = 68;\nconst Y_LABEL_W = 38;\nconst X_LABEL_H = 36;\nconst COL_STRIP_H = 38;\nconst ROW_STRIP_W = 42;\nconst GAP = 10;\nconst TICK_LABEL_STYLE = { fontSize: 14, fill: t.inkSoft, fontFamily: FONT };\nconst PANEL_MARGIN = { left: 8, right: 16, top: 12, bottom: 8 };\n\n// Columns: set these to your data's column names\nconst BILL_LENGTH = \"Bill length (mm)\"; // role: x\nconst BILL_DEPTH = \"Bill depth (mm)\"; // role: y\nconst SEX = \"Sex\"; // role: row_facet\nconst SPECIES = \"Species\"; // role: col_facet\n\n// Data: replace this block with your own records\nconst facetSpecs = [\n  { col: \"Adelie\", row: \"Female\", xMean: 37.3, xStd: 1.6, yMean: 17.6, yStd: 0.9, n: 45 },\n  { col: \"Chinstrap\", row: \"Female\", xMean: 46.6, xStd: 3.0, yMean: 17.6, yStd: 0.9, n: 45 },\n  { col: \"Gentoo\", row: \"Female\", xMean: 45.6, xStd: 2.0, yMean: 14.2, yStd: 0.8, n: 45 },\n  { col: \"Adelie\", row: \"Male\", xMean: 40.4, xStd: 2.0, yMean: 19.1, yStd: 1.0, n: 45 },\n  { col: \"Chinstrap\", row: \"Male\", xMean: 51.5, xStd: 1.6, yMean: 19.3, yStd: 0.8, n: 45 },\n  { col: \"Gentoo\", row: \"Male\", xMean: 49.5, xStd: 2.6, yMean: 15.7, yStd: 0.9, n: 45 },\n];\n\nlet lcgState = 42 >>> 0;\nconst data: {\n  [BILL_LENGTH]: number;\n  [BILL_DEPTH]: number;\n  [SEX]: string;\n  [SPECIES]: string;\n}[] = [];\n\nfor (const spec of facetSpecs) {\n  for (let i = 0; i < spec.n; i++) {\n    lcgState = (lcgState * 1664525 + 1013904223) >>> 0;\n    const u1 = Math.max(lcgState / 4294967296, 1e-9);\n    lcgState = (lcgState * 1664525 + 1013904223) >>> 0;\n    const u2 = lcgState / 4294967296;\n    const zX = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n\n    lcgState = (lcgState * 1664525 + 1013904223) >>> 0;\n    const u3 = Math.max(lcgState / 4294967296, 1e-9);\n    lcgState = (lcgState * 1664525 + 1013904223) >>> 0;\n    const u4 = lcgState / 4294967296;\n    const zY = Math.sqrt(-2 * Math.log(u3)) * Math.cos(2 * Math.PI * u4);\n\n    data.push({\n      [SPECIES]: spec.col,\n      [SEX]: spec.row,\n      [BILL_LENGTH]: Number((spec.xMean + spec.xStd * zX).toFixed(2)),\n      [BILL_DEPTH]: Number((spec.yMean + spec.yStd * zY).toFixed(2)),\n    });\n  }\n}\n\n// Plot: reads only data and the constants above\nconst colFacets = [...new Set(data.map((r) => r[SPECIES]))];\nconst rowFacets = [...new Set(data.map((r) => r[SEX]))];\n\nconst allX = data.map((r) => r[BILL_LENGTH]);\nconst allY = data.map((r) => r[BILL_DEPTH]);\nconst minX = Math.min(...allX);\nconst maxX = Math.max(...allX);\nconst minY = Math.min(...allY);\nconst maxY = Math.max(...allY);\nconst xPad = (maxX - minX) * AXIS_PAD_SHARE;\nconst yPad = (maxY - minY) * AXIS_PAD_SHARE;\nconst xDomainMin = minX - xPad;\nconst xDomainMax = maxX + xPad;\nconst yDomainMin = minY - yPad;\nconst yDomainMax = maxY + yPad;\n\nconst facets = rowFacets.map((rowVal) =>\n  colFacets.map((colVal) => {\n    const subset = data.filter((r) => r[SEX] === rowVal && r[SPECIES] === colVal);\n    return {\n      rowVal,\n      colVal,\n      points: subset.map((r, idx) => ({ id: idx, x: r[BILL_LENGTH], y: r[BILL_DEPTH] })),\n    };\n  })\n);\n\nexport default function Chart() {\n  const { width: W, height: H } = window.ANYPLOT_SIZE;\n  const nCols = colFacets.length;\n  const nRows = rowFacets.length;\n\n  const padRight = 18;\n  const padBottom = 10;\n  const gridW = W - Y_LABEL_W - ROW_STRIP_W - padRight - nCols * GAP;\n  const gridH = H - TITLE_H - X_LABEL_H - COL_STRIP_H - padBottom - nRows * GAP;\n  const panelW = Math.floor(gridW / nCols);\n  const panelH = Math.floor(gridH / nRows);\n\n  return (\n    <div\n      style={{\n        width: W,\n        height: H,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: FONT,\n      }}\n    >\n      <div\n        style={{\n          height: TITLE_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 4,\n        }}\n      >\n        <span style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</span>\n        <span style={{ fontSize: 14, color: t.inkSoft }}>\n          {BILL_LENGTH} vs {BILL_DEPTH} · faceted by {SPECIES} ({nCols}) × {SEX} ({nRows}), n = {data.length}\n        </span>\n      </div>\n\n      <div style={{ flex: 1, display: \"flex\" }}>\n        <div style={{ width: Y_LABEL_W, display: \"flex\", alignItems: \"center\", justifyContent: \"center\" }}>\n          <span\n            style={{\n              transform: \"rotate(-90deg)\",\n              whiteSpace: \"nowrap\",\n              fontSize: 16,\n              fontWeight: 600,\n              color: t.ink,\n            }}\n          >\n            {BILL_DEPTH}\n          </span>\n        </div>\n\n        <div style={{ flex: 1, display: \"flex\", flexDirection: \"column\" }}>\n          <div\n            style={{\n              display: \"grid\",\n              gridTemplateColumns: `repeat(${nCols}, ${panelW}px) ${ROW_STRIP_W}px`,\n              gridTemplateRows: `${COL_STRIP_H}px repeat(${nRows}, ${panelH}px)`,\n              columnGap: GAP,\n              rowGap: GAP,\n            }}\n          >\n            {colFacets.map((colVal, c) => (\n              <div\n                key={`col-${colVal}`}\n                style={{\n                  gridColumn: c + 1,\n                  gridRow: 1,\n                  display: \"flex\",\n                  alignItems: \"center\",\n                  justifyContent: \"center\",\n                  background: t.elevatedBg,\n                  border: `1px solid ${t.grid}`,\n                  borderRadius: 4,\n                  color: t.ink,\n                  fontSize: 15,\n                  fontWeight: 600,\n                }}\n              >\n                <span>\n                  {SPECIES}: {colVal}\n                </span>\n              </div>\n            ))}\n            <div style={{ gridColumn: nCols + 1, gridRow: 1 }} />\n\n            {rowFacets.map((rowVal, r) =>\n              colFacets.map((colVal, c) => {\n                const facet = facets[r][c];\n                const isLeftCol = c === 0;\n                const isBottomRow = r === nRows - 1;\n                return (\n                  <div\n                    key={`${colVal}-${rowVal}`}\n                    style={{\n                      gridColumn: c + 1,\n                      gridRow: r + 2,\n                      border: `1px solid ${t.grid}`,\n                      borderRadius: 4,\n                      boxSizing: \"border-box\",\n                    }}\n                  >\n                    <ChartsContainer\n                      width={panelW}\n                      height={panelH}\n                      skipAnimation\n                      margin={PANEL_MARGIN}\n                      xAxis={[\n                        {\n                          id: \"x\",\n                          scaleType: \"linear\",\n                          min: xDomainMin,\n                          max: xDomainMax,\n                          height: 34,\n                          tickLabelStyle: TICK_LABEL_STYLE,\n                          valueFormatter: (v: number) => (isBottomRow ? String(v) : \"\"),\n                        },\n                      ]}\n                      yAxis={[\n                        {\n                          id: \"y\",\n                          scaleType: \"linear\",\n                          min: yDomainMin,\n                          max: yDomainMax,\n                          width: 44,\n                          tickLabelStyle: TICK_LABEL_STYLE,\n                          valueFormatter: (v: number) => (isLeftCol ? String(v) : \"\"),\n                        },\n                      ]}\n                      series={[\n                        {\n                          id: `${colVal}-${rowVal}`,\n                          type: \"scatter\" as const,\n                          color: MARKER_COLOR,\n                          markerSize: MARKER_SIZE,\n                          data: facet.points,\n                        },\n                      ]}\n                      sx={{ \"& .MuiScatterChart-marker\": { stroke: t.pageBg, strokeWidth: 1 } }}\n                    >\n                      <ChartsGrid horizontal vertical sx={{ \"& .MuiChartsGrid-line\": { stroke: t.grid } }} />\n                      <ScatterPlot />\n                      <ChartsYAxis axisId=\"y\" />\n                      <ChartsXAxis axisId=\"x\" />\n                    </ChartsContainer>\n                  </div>\n                );\n              })\n            )}\n\n            {rowFacets.map((rowVal, r) => (\n              <div\n                key={`row-${rowVal}`}\n                style={{\n                  gridColumn: nCols + 1,\n                  gridRow: r + 2,\n                  display: \"flex\",\n                  alignItems: \"center\",\n                  justifyContent: \"center\",\n                  background: t.elevatedBg,\n                  border: `1px solid ${t.grid}`,\n                  borderRadius: 4,\n                  color: t.ink,\n                  fontSize: 15,\n                  fontWeight: 600,\n                }}\n              >\n                <span style={{ transform: \"rotate(90deg)\", whiteSpace: \"nowrap\" }}>\n                  {SEX}: {rowVal}\n                </span>\n              </div>\n            ))}\n          </div>\n\n          <div style={{ height: X_LABEL_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\" }}>\n            <span style={{ fontSize: 16, fontWeight: 600, color: t.ink }}>{BILL_LENGTH}</span>\n          </div>\n        </div>\n      </div>\n    </div>\n  );\n}\n"}