{"spec_id":"chernoff-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// chernoff-basic: Chernoff Faces for Multivariate Data\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useDrawingArea, useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst HEADER_H = 122;\nconst FOOTER_H = 64;\nconst FACE_H_SHARE = 0.315; // base face half-height as a share of the cell height\nconst FACE_W_SHARE = 0.4; // cap on the base face half-height as a share of the cell width\n// Facial features as shares of the base face half-height\nconst FACE_RX_SHARE = 0.806;\nconst EYE_R_SHARE = 0.125;\nconst EYE_DX_SHARE = 0.306;\nconst BROW_LEN_SHARE = 0.361;\nconst BROW_GAP_SHARE = 0.111;\nconst NOSE_LEN_SHARE = 0.306;\nconst MOUTH_HALF_W_SHARE = 0.361;\nconst MOUTH_CURVE_SHARE = 0.347;\n// Features in mapping order; variable i drives feature i\nconst FEATURES = [\n  \"Face width\",\n  \"Face height\",\n  \"Eye size\",\n  \"Eye spacing\",\n  \"Eyebrow slant\",\n  \"Nose length\",\n  \"Mouth smile\",\n];\nconst NEUTRAL = 0.5; // value of a feature that no variable drives\n\n// Columns: set these to your data's column names\nconst OBSERVATION = \"Company\"; // role: observation_id\nconst GROUP = \"Sector\"; // role: extra\nconst VARIABLES = [\n  \"Revenue growth (%)\",\n  \"Profit margin (%)\",\n  \"Liquidity ratio\",\n  \"Market share (%)\",\n  \"Debt-to-equity\",\n  \"Innovation score\",\n  \"Employee retention (%)\",\n]; // role: variable\n\n// Data: replace this block with your own records, one object per row\nconst data = [\n  {\n    [OBSERVATION]: \"Nova Systems\",\n    [GROUP]: \"Technology\",\n    [VARIABLES[0]]: 28.4,\n    [VARIABLES[1]]: 24.1,\n    [VARIABLES[2]]: 2.15,\n    [VARIABLES[3]]: 14.2,\n    [VARIABLES[4]]: 0.32,\n    [VARIABLES[5]]: 91,\n    [VARIABLES[6]]: 93,\n  },\n  {\n    [OBSERVATION]: \"Quantum Byte\",\n    [GROUP]: \"Technology\",\n    [VARIABLES[0]]: 31.8,\n    [VARIABLES[1]]: 21.6,\n    [VARIABLES[2]]: 1.92,\n    [VARIABLES[3]]: 11.8,\n    [VARIABLES[4]]: 0.24,\n    [VARIABLES[5]]: 95,\n    [VARIABLES[6]]: 89,\n  },\n  {\n    [OBSERVATION]: \"CloudPeak\",\n    [GROUP]: \"Technology\",\n    [VARIABLES[0]]: 24.9,\n    [VARIABLES[1]]: 26.8,\n    [VARIABLES[2]]: 2.38,\n    [VARIABLES[3]]: 18.5,\n    [VARIABLES[4]]: 0.41,\n    [VARIABLES[5]]: 84,\n    [VARIABLES[6]]: 91,\n  },\n  {\n    [OBSERVATION]: \"Vertex Labs\",\n    [GROUP]: \"Technology\",\n    [VARIABLES[0]]: 29.6,\n    [VARIABLES[1]]: 19.4,\n    [VARIABLES[2]]: 1.78,\n    [VARIABLES[3]]: 9.6,\n    [VARIABLES[4]]: 0.28,\n    [VARIABLES[5]]: 88,\n    [VARIABLES[6]]: 87,\n  },\n  {\n    [OBSERVATION]: \"Harborline Retail\",\n    [GROUP]: \"Retail\",\n    [VARIABLES[0]]: 14.2,\n    [VARIABLES[1]]: 11.5,\n    [VARIABLES[2]]: 2.45,\n    [VARIABLES[3]]: 24.6,\n    [VARIABLES[4]]: 0.88,\n    [VARIABLES[5]]: 46,\n    [VARIABLES[6]]: 76,\n  },\n  {\n    [OBSERVATION]: \"Meadow Mart\",\n    [GROUP]: \"Retail\",\n    [VARIABLES[0]]: 11.8,\n    [VARIABLES[1]]: 9.8,\n    [VARIABLES[2]]: 2.62,\n    [VARIABLES[3]]: 21.4,\n    [VARIABLES[4]]: 0.74,\n    [VARIABLES[5]]: 39,\n    [VARIABLES[6]]: 79,\n  },\n  {\n    [OBSERVATION]: \"Urban Goods\",\n    [GROUP]: \"Retail\",\n    [VARIABLES[0]]: 16.5,\n    [VARIABLES[1]]: 13.2,\n    [VARIABLES[2]]: 2.18,\n    [VARIABLES[3]]: 19.2,\n    [VARIABLES[4]]: 0.96,\n    [VARIABLES[5]]: 54,\n    [VARIABLES[6]]: 73,\n  },\n  {\n    [OBSERVATION]: \"Riverside Shops\",\n    [GROUP]: \"Retail\",\n    [VARIABLES[0]]: 12.9,\n    [VARIABLES[1]]: 10.4,\n    [VARIABLES[2]]: 2.51,\n    [VARIABLES[3]]: 26.1,\n    [VARIABLES[4]]: 0.82,\n    [VARIABLES[5]]: 42,\n    [VARIABLES[6]]: 77,\n  },\n  {\n    [OBSERVATION]: \"IronWorks Mfg\",\n    [GROUP]: \"Manufacturing\",\n    [VARIABLES[0]]: 7.4,\n    [VARIABLES[1]]: 15.6,\n    [VARIABLES[2]]: 1.42,\n    [VARIABLES[3]]: 17.8,\n    [VARIABLES[4]]: 1.54,\n    [VARIABLES[5]]: 64,\n    [VARIABLES[6]]: 66,\n  },\n  {\n    [OBSERVATION]: \"Steelframe Co\",\n    [GROUP]: \"Manufacturing\",\n    [VARIABLES[0]]: 9.1,\n    [VARIABLES[1]]: 14.2,\n    [VARIABLES[2]]: 1.55,\n    [VARIABLES[3]]: 20.3,\n    [VARIABLES[4]]: 1.38,\n    [VARIABLES[5]]: 59,\n    [VARIABLES[6]]: 69,\n  },\n  {\n    [OBSERVATION]: \"Forge Dynamics\",\n    [GROUP]: \"Manufacturing\",\n    [VARIABLES[0]]: 6.2,\n    [VARIABLES[1]]: 16.9,\n    [VARIABLES[2]]: 1.34,\n    [VARIABLES[3]]: 15.4,\n    [VARIABLES[4]]: 1.68,\n    [VARIABLES[5]]: 71,\n    [VARIABLES[6]]: 63,\n  },\n  {\n    [OBSERVATION]: \"Anvil Industries\",\n    [GROUP]: \"Manufacturing\",\n    [VARIABLES[0]]: 8.3,\n    [VARIABLES[1]]: 13.5,\n    [VARIABLES[2]]: 1.48,\n    [VARIABLES[3]]: 22.0,\n    [VARIABLES[4]]: 1.46,\n    [VARIABLES[5]]: 61,\n    [VARIABLES[6]]: 67,\n  },\n];\n\n// Plot: reads only data and the constants above\nconst groups = Array.from(new Set(data.map((d) => String(d[GROUP]))));\nconst mappedVariables = VARIABLES.slice(0, FEATURES.length);\n\nconst extents = mappedVariables.map((col) => {\n  const vals = data.map((d) => Number(d[col]));\n  const min = Math.min(...vals);\n  return { min, span: Math.max(...vals) - min || 1 };\n});\n\nconst numCols = Math.max(\n  ...groups.map((g) => data.filter((d) => String(d[GROUP]) === g).length),\n);\nconst numRows = groups.length;\n\nconst groupCounts = new Map<string, number>();\nconst faces = data.map((d) => {\n  const groupName = String(d[GROUP]);\n  const rowIdx = groups.indexOf(groupName);\n  const colIdx = groupCounts.get(groupName) ?? 0;\n  groupCounts.set(groupName, colIdx + 1);\n\n  return {\n    name: String(d[OBSERVATION]),\n    row: rowIdx,\n    col: colIdx,\n    color: t.palette[rowIdx],\n    // One value in [0, 1] per feature: min-max normalised, or neutral when unmapped\n    f: FEATURES.map((_, i) =>\n      i < mappedVariables.length\n        ? (Number(d[mappedVariables[i]]) - extents[i].min) / extents[i].span\n        : NEUTRAL,\n    ),\n  };\n});\n\nconst featureKey = mappedVariables.map((v, i) => `${FEATURES[i]} → ${v}`);\nconst keySplit = Math.ceil(featureKey.length / 2);\nconst featureKeyLine1 = featureKey.slice(0, keySplit).join(\" · \");\nconst featureKeyLine2 = featureKey.slice(keySplit).join(\" · \");\n\nconst TITLE = \"chernoff-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${faces.length} faces (${OBSERVATION}) · ${groups.length} groups (${GROUP}) · ${mappedVariables.length} variables normalized to [0, 1] and mapped to facial features`;\n\nfunction FaceGridLayer() {\n  const xs = useXScale(\"col\");\n  const ys = useYScale(\"row\");\n  const { width: areaW, height: areaH } = useDrawingArea();\n  const unit = Math.min(\n    (areaH / numRows) * FACE_H_SHARE,\n    (areaW / numCols) * FACE_W_SHARE,\n  );\n\n  return (\n    <g>\n      {faces.map((face) => {\n        const cx = xs(face.col + 0.5);\n        const cy = ys(face.row + 0.45);\n        const f = face.f;\n        const rx = unit * FACE_RX_SHARE * (0.72 + 0.54 * f[0]);\n        const ry = unit * (0.74 + 0.48 * f[1]);\n        const eyeR = unit * EYE_R_SHARE * (0.62 + 0.78 * f[2]);\n        const eyeDx = unit * EYE_DX_SHARE * (0.72 + 0.58 * f[3]);\n        const browAngle = -10 + 38 * f[4];\n        const noseLen = unit * NOSE_LEN_SHARE * (0.62 + 0.78 * f[5]);\n        const mouthCurve = (f[6] - 0.5) * 2 * unit * MOUTH_CURVE_SHARE;\n        const browLen = unit * BROW_LEN_SHARE;\n        const mouthHalfW = unit * MOUTH_HALF_W_SHARE;\n\n        const eyeY = cy - ry * 0.14;\n        const browY = eyeY - eyeR - unit * BROW_GAP_SHARE;\n        const noseY0 = cy + ry * 0.04;\n        const noseY1 = noseY0 + noseLen;\n        const mouthY = cy + ry * 0.48;\n\n        return (\n          <g key={face.name}>\n            <ellipse\n              cx={cx}\n              cy={cy}\n              rx={rx}\n              ry={ry}\n              fill={t.elevatedBg}\n              stroke={face.color}\n              strokeWidth={3.5}\n            />\n            <circle cx={cx - eyeDx} cy={eyeY} r={eyeR} fill={t.ink} />\n            <circle cx={cx + eyeDx} cy={eyeY} r={eyeR} fill={t.ink} />\n            <line\n              x1={cx - eyeDx - browLen / 2}\n              y1={browY}\n              x2={cx - eyeDx + browLen / 2}\n              y2={browY}\n              stroke={t.ink}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n              transform={`rotate(${browAngle} ${cx - eyeDx} ${browY})`}\n            />\n            <line\n              x1={cx + eyeDx - browLen / 2}\n              y1={browY}\n              x2={cx + eyeDx + browLen / 2}\n              y2={browY}\n              stroke={t.ink}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n              transform={`rotate(${-browAngle} ${cx + eyeDx} ${browY})`}\n            />\n            <path\n              d={`M ${cx} ${noseY0} L ${cx} ${noseY1} L ${cx + 5} ${noseY1 + 3}`}\n              fill=\"none\"\n              stroke={t.inkSoft}\n              strokeWidth={2.5}\n              strokeLinecap=\"round\"\n            />\n            <path\n              d={`M ${cx - mouthHalfW} ${mouthY} Q ${cx} ${mouthY + mouthCurve} ${cx + mouthHalfW} ${mouthY}`}\n              fill=\"none\"\n              stroke={t.ink}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <text\n              x={cx}\n              y={cy + ry + 24}\n              textAnchor=\"middle\"\n              fontSize={14}\n              fontWeight={600}\n              fill={t.ink}\n            >\n              {face.name}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const chartHeight = height - HEADER_H - FOOTER_H;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        background: t.pageBg,\n        fontFamily: \"Inter, system-ui, sans-serif\",\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 8,\n          paddingTop: 6,\n        }}\n      >\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>\n          {TITLE}\n        </div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{SUBTITLE}</div>\n        <div\n          style={{\n            display: \"flex\",\n            gap: 28,\n            alignItems: \"center\",\n            fontSize: 14,\n            color: t.inkSoft,\n          }}\n        >\n          {groups.map((g, i) => (\n            <div\n              key={g}\n              style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}\n            >\n              <span\n                style={{\n                  width: 14,\n                  height: 16,\n                  borderRadius: \"50%\",\n                  border: `3px solid ${t.palette[i]}`,\n                  background: t.elevatedBg,\n                  display: \"inline-block\",\n                }}\n              />\n              <span style={{ color: t.ink, fontWeight: 500 }}>{g}</span>\n            </div>\n          ))}\n        </div>\n      </div>\n\n      <ChartsContainer\n        width={width}\n        height={chartHeight}\n        series={[]}\n        margin={{ top: 10, right: 48, bottom: 18, left: 48 }}\n        xAxis={[\n          {\n            id: \"col\",\n            scaleType: \"linear\",\n            min: 0,\n            max: numCols,\n            height: 0,\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"row\",\n            scaleType: \"linear\",\n            min: 0,\n            max: numRows,\n            reverse: true,\n            width: 0,\n          },\n        ]}\n        skipAnimation\n      >\n        <FaceGridLayer />\n      </ChartsContainer>\n\n      <div\n        style={{\n          height: FOOTER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 4,\n          fontSize: 14,\n          color: t.inkSoft,\n        }}\n      >\n        <div>{featureKeyLine1}</div>\n        <div>{featureKeyLine2}</div>\n      </div>\n    </div>\n  );\n}\n"}