{"spec_id":"heatmap-annotated","library":"muix","language":"javascript","code":"// anyplot.ai\n// heatmap-annotated: Annotated Heatmap\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n//# anyplot-orientation: square\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useDrawingArea, useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst W = window.ANYPLOT_SIZE.width;\nconst H = window.ANYPLOT_SIZE.height;\nconst FONT = \"system-ui, -apple-system, sans-serif\";\nconst TITLE = \"heatmap-annotated · javascript · muix · anyplot.ai\";\nconst DOMAIN_MIN = -1;\nconst DOMAIN_MID = 0;\nconst DOMAIN_MAX = 1;\nconst COLORBAR_TICKS = [-1.0, -0.5, 0.0, 0.5, 1.0];\n\n// Columns: set these to your data's column names\nconst COL_VAR = \"Column variable\"; // role: x\nconst ROW_VAR = \"Row variable\"; // role: y\nconst CORRELATION = \"Pearson r\"; // role: value\n\n// Data: replace this block with your own records\nconst data: Record<string, string | number>[] = [\n  { [ROW_VAR]: \"Sleep\", [COL_VAR]: \"Sleep\", [CORRELATION]: 1.0 },\n  { [ROW_VAR]: \"Sleep\", [COL_VAR]: \"Exercise\", [CORRELATION]: 0.35 },\n  { [ROW_VAR]: \"Sleep\", [COL_VAR]: \"Screen Time\", [CORRELATION]: -0.42 },\n  { [ROW_VAR]: \"Sleep\", [COL_VAR]: \"Stress\", [CORRELATION]: -0.58 },\n  { [ROW_VAR]: \"Sleep\", [COL_VAR]: \"Water\", [CORRELATION]: 0.22 },\n  { [ROW_VAR]: \"Sleep\", [COL_VAR]: \"Steps\", [CORRELATION]: 0.31 },\n  { [ROW_VAR]: \"Sleep\", [COL_VAR]: \"Mood\", [CORRELATION]: 0.61 },\n\n  { [ROW_VAR]: \"Exercise\", [COL_VAR]: \"Sleep\", [CORRELATION]: 0.35 },\n  { [ROW_VAR]: \"Exercise\", [COL_VAR]: \"Exercise\", [CORRELATION]: 1.0 },\n  { [ROW_VAR]: \"Exercise\", [COL_VAR]: \"Screen Time\", [CORRELATION]: -0.28 },\n  { [ROW_VAR]: \"Exercise\", [COL_VAR]: \"Stress\", [CORRELATION]: -0.33 },\n  { [ROW_VAR]: \"Exercise\", [COL_VAR]: \"Water\", [CORRELATION]: 0.45 },\n  { [ROW_VAR]: \"Exercise\", [COL_VAR]: \"Steps\", [CORRELATION]: 0.82 },\n  { [ROW_VAR]: \"Exercise\", [COL_VAR]: \"Mood\", [CORRELATION]: 0.57 },\n\n  { [ROW_VAR]: \"Screen Time\", [COL_VAR]: \"Sleep\", [CORRELATION]: -0.42 },\n  { [ROW_VAR]: \"Screen Time\", [COL_VAR]: \"Exercise\", [CORRELATION]: -0.28 },\n  { [ROW_VAR]: \"Screen Time\", [COL_VAR]: \"Screen Time\", [CORRELATION]: 1.0 },\n  { [ROW_VAR]: \"Screen Time\", [COL_VAR]: \"Stress\", [CORRELATION]: 0.49 },\n  { [ROW_VAR]: \"Screen Time\", [COL_VAR]: \"Water\", [CORRELATION]: -0.18 },\n  { [ROW_VAR]: \"Screen Time\", [COL_VAR]: \"Steps\", [CORRELATION]: -0.39 },\n  { [ROW_VAR]: \"Screen Time\", [COL_VAR]: \"Mood\", [CORRELATION]: -0.44 },\n\n  { [ROW_VAR]: \"Stress\", [COL_VAR]: \"Sleep\", [CORRELATION]: -0.58 },\n  { [ROW_VAR]: \"Stress\", [COL_VAR]: \"Exercise\", [CORRELATION]: -0.33 },\n  { [ROW_VAR]: \"Stress\", [COL_VAR]: \"Screen Time\", [CORRELATION]: 0.49 },\n  { [ROW_VAR]: \"Stress\", [COL_VAR]: \"Stress\", [CORRELATION]: 1.0 },\n  { [ROW_VAR]: \"Stress\", [COL_VAR]: \"Water\", [CORRELATION]: -0.15 },\n  { [ROW_VAR]: \"Stress\", [COL_VAR]: \"Steps\", [CORRELATION]: -0.36 },\n  { [ROW_VAR]: \"Stress\", [COL_VAR]: \"Mood\", [CORRELATION]: -0.71 },\n\n  { [ROW_VAR]: \"Water\", [COL_VAR]: \"Sleep\", [CORRELATION]: 0.22 },\n  { [ROW_VAR]: \"Water\", [COL_VAR]: \"Exercise\", [CORRELATION]: 0.45 },\n  { [ROW_VAR]: \"Water\", [COL_VAR]: \"Screen Time\", [CORRELATION]: -0.18 },\n  { [ROW_VAR]: \"Water\", [COL_VAR]: \"Stress\", [CORRELATION]: -0.15 },\n  { [ROW_VAR]: \"Water\", [COL_VAR]: \"Water\", [CORRELATION]: 1.0 },\n  { [ROW_VAR]: \"Water\", [COL_VAR]: \"Steps\", [CORRELATION]: 0.28 },\n  { [ROW_VAR]: \"Water\", [COL_VAR]: \"Mood\", [CORRELATION]: 0.19 },\n\n  { [ROW_VAR]: \"Steps\", [COL_VAR]: \"Sleep\", [CORRELATION]: 0.31 },\n  { [ROW_VAR]: \"Steps\", [COL_VAR]: \"Exercise\", [CORRELATION]: 0.82 },\n  { [ROW_VAR]: \"Steps\", [COL_VAR]: \"Screen Time\", [CORRELATION]: -0.39 },\n  { [ROW_VAR]: \"Steps\", [COL_VAR]: \"Stress\", [CORRELATION]: -0.36 },\n  { [ROW_VAR]: \"Steps\", [COL_VAR]: \"Water\", [CORRELATION]: 0.28 },\n  { [ROW_VAR]: \"Steps\", [COL_VAR]: \"Steps\", [CORRELATION]: 1.0 },\n  { [ROW_VAR]: \"Steps\", [COL_VAR]: \"Mood\", [CORRELATION]: 0.52 },\n\n  { [ROW_VAR]: \"Mood\", [COL_VAR]: \"Sleep\", [CORRELATION]: 0.61 },\n  { [ROW_VAR]: \"Mood\", [COL_VAR]: \"Exercise\", [CORRELATION]: 0.57 },\n  { [ROW_VAR]: \"Mood\", [COL_VAR]: \"Screen Time\", [CORRELATION]: -0.44 },\n  { [ROW_VAR]: \"Mood\", [COL_VAR]: \"Stress\", [CORRELATION]: -0.71 },\n  { [ROW_VAR]: \"Mood\", [COL_VAR]: \"Water\", [CORRELATION]: 0.19 },\n  { [ROW_VAR]: \"Mood\", [COL_VAR]: \"Steps\", [CORRELATION]: 0.52 },\n  { [ROW_VAR]: \"Mood\", [COL_VAR]: \"Mood\", [CORRELATION]: 1.0 },\n];\n\n// Plot: reads only data and the constants above\nconst xCategories = Array.from(new Set(data.map((r) => String(r[COL_VAR]))));\nconst yCategories = Array.from(new Set(data.map((r) => String(r[ROW_VAR]))));\n\nconst divStopInts = t.div.map((hex) => parseInt(hex.replace(\"#\", \"\"), 16));\nconst divStopsRgb: [number, number, number][] = divStopInts.map((v) => [\n  (v >> 16) & 255,\n  (v >> 8) & 255,\n  v & 255,\n]);\n\nconst cells = data.map((row) => {\n  const xKey = String(row[COL_VAR]);\n  const yKey = String(row[ROW_VAR]);\n  const value = Number(row[CORRELATION]);\n  const [r0, g0, b0] = divStopsRgb[0];\n  const [r1, g1, b1] = divStopsRgb[1];\n  const [r2, g2, b2] = divStopsRgb[2];\n\n  const isNeg = value <= DOMAIN_MID;\n  const [ra, ga, ba] = isNeg ? [r0, g0, b0] : [r1, g1, b1];\n  const [rb, gb, bb] = isNeg ? [r1, g1, b1] : [r2, g2, b2];\n  const f = isNeg\n    ? (value - DOMAIN_MIN) / (DOMAIN_MID - DOMAIN_MIN)\n    : (value - DOMAIN_MID) / (DOMAIN_MAX - DOMAIN_MID);\n\n  const r = Math.round(ra + (rb - ra) * f);\n  const g = Math.round(ga + (gb - ga) * f);\n  const b = Math.round(ba + (bb - ba) * f);\n  const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;\n  const textFill = luminance > 0.55 ? \"#1A1A17\" : \"#FAF8F1\";\n\n  return {\n    key: `${yKey}-${xKey}`,\n    xKey,\n    yKey,\n    value,\n    fill: `rgb(${r},${g},${b})`,\n    textFill,\n  };\n});\n\nfunction HeatmapLayer() {\n  const xScale = useXScale(\"vars_x\") as unknown as ((v: string) => number) & { bandwidth: () => number };\n  const yScale = useYScale(\"vars_y\") as unknown as ((v: string) => number) & { bandwidth: () => number };\n  const { left, top, width, height } = useDrawingArea();\n\n  const bw = xScale.bandwidth();\n  const bh = yScale.bandwidth();\n  const barX = left + width + 26;\n\n  return (\n    <g>\n      <defs>\n        <linearGradient id=\"divGrad\" x1=\"0\" y1=\"1\" x2=\"0\" y2=\"0\">\n          <stop offset=\"0%\" stopColor={t.div[0]} />\n          <stop offset=\"50%\" stopColor={t.div[1]} />\n          <stop offset=\"100%\" stopColor={t.div[2]} />\n        </linearGradient>\n      </defs>\n\n      {cells.map((cell) => {\n        const x0 = xScale(cell.xKey);\n        const y0 = yScale(cell.yKey);\n        return (\n          <g key={cell.key}>\n            <rect\n              x={x0 + 2}\n              y={y0 + 2}\n              width={bw - 4}\n              height={bh - 4}\n              fill={cell.fill}\n              rx={4}\n            />\n            <text\n              x={x0 + bw / 2}\n              y={y0 + bh / 2 + 7}\n              textAnchor=\"middle\"\n              fontSize={20}\n              fontWeight=\"600\"\n              fill={cell.textFill}\n              fontFamily={FONT}\n            >\n              {cell.value.toFixed(2)}\n            </text>\n          </g>\n        );\n      })}\n\n      {/* Diverging colorbar legend */}\n      <text\n        x={barX + 10}\n        y={top - 14}\n        textAnchor=\"middle\"\n        fontSize={14}\n        fontWeight=\"600\"\n        fill={t.inkSoft}\n        fontFamily={FONT}\n      >\n        {CORRELATION}\n      </text>\n      <rect x={barX} y={top} width={20} height={height} fill=\"url(#divGrad)\" rx={3} />\n      {COLORBAR_TICKS.map((tick) => {\n        const ty = top + height * (1 - (tick - DOMAIN_MIN) / (DOMAIN_MAX - DOMAIN_MIN));\n        return (\n          <g key={tick}>\n            <line x1={barX + 20} y1={ty} x2={barX + 27} y2={ty} stroke={t.inkSoft} strokeWidth={1} />\n            <text x={barX + 32} y={ty + 5} fontSize={14} fill={t.inkSoft} fontFamily={FONT}>\n              {tick.toFixed(1)}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const margin = { left: 20, top: 96, right: 118, bottom: 28 };\n  const subtitle = `${CORRELATION} matrix (${xCategories.length} × ${yCategories.length})`;\n\n  return (\n    <ChartsContainer\n      width={W}\n      height={H}\n      margin={margin}\n      series={[]}\n      skipAnimation\n      xAxis={[\n        {\n          id: \"vars_x\",\n          scaleType: \"band\",\n          data: xCategories,\n          height: 54,\n          categoryGapRatio: 0.04,\n          tickLabelStyle: { fontSize: 15, fill: t.inkSoft, fontFamily: FONT },\n        },\n      ]}\n      yAxis={[\n        {\n          id: \"vars_y\",\n          scaleType: \"band\",\n          data: yCategories,\n          width: 128,\n          categoryGapRatio: 0.04,\n          tickLabelStyle: { fontSize: 15, fill: t.inkSoft, fontFamily: FONT },\n        },\n      ]}\n    >\n      <text x={W / 2} y={38} textAnchor=\"middle\" fontSize={22} fontWeight=\"600\" fill={t.ink} fontFamily={FONT}>\n        {TITLE}\n      </text>\n      <text x={W / 2} y={64} textAnchor=\"middle\" fontSize={15} fill={t.inkSoft} fontFamily={FONT}>\n        {subtitle}\n      </text>\n      <HeatmapLayer />\n      <ChartsXAxis axisId=\"vars_x\" />\n      <ChartsYAxis axisId=\"vars_y\" />\n    </ChartsContainer>\n  );\n}\n"}