{"spec_id":"heatmap-polar","library":"muix","language":"javascript","code":"// anyplot.ai\n// heatmap-polar: Polar Heatmap for Cyclic Two-Dimensional Data\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 { useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst size = window.ANYPLOT_SIZE;\nconst TITLE = \"heatmap-polar · javascript · muix · anyplot.ai\";\nconst START_ANGLE = 0;\nconst RING_HOLE_RATIO = 0.28;\nconst LABEL_PAD = 56;\nconst DAY_LABEL_ANGLE = 180;\nconst COLORBAR_TICKS = [0, 0.25, 0.5, 0.75, 1];\nconst GRAD_ID = \"polar-heatmap-seq-grad\";\nconst DARK_INK = \"#1A1A17\";\nconst LIGHT_INK = \"#FAF8F1\";\n\n// Columns: set these to your data's column names\nconst HOUR = \"Hour of day\"; // role: angular\nconst DAY = \"Day of week\"; // role: radial\nconst VISITS = \"Visits per hour\"; // role: value\n\n// Data: replace this block with your own records\nconst DAY_SEEDS = [\n  { day: \"Mon\", base: 14, weekend: false, nightlife: false },\n  { day: \"Tue\", base: 18, weekend: false, nightlife: false },\n  { day: \"Wed\", base: 22, weekend: false, nightlife: false },\n  { day: \"Thu\", base: 26, weekend: false, nightlife: false },\n  { day: \"Fri\", base: 31, weekend: false, nightlife: true },\n  { day: \"Sat\", base: 27, weekend: true, nightlife: true },\n  { day: \"Sun\", base: 19, weekend: true, nightlife: false },\n];\n\nconst HOUR_LABELS = [\n  \"12am\", \"1am\", \"2am\", \"3am\", \"4am\", \"5am\",\n  \"6am\", \"7am\", \"8am\", \"9am\", \"10am\", \"11am\",\n  \"12pm\", \"1pm\", \"2pm\", \"3pm\", \"4pm\", \"5pm\",\n  \"6pm\", \"7pm\", \"8pm\", \"9pm\", \"10pm\", \"11pm\",\n];\n\ntype PolarHeatmapRow = {\n  [HOUR]: string;\n  [DAY]: string;\n  [VISITS]: number;\n};\n\nconst data: PolarHeatmapRow[] = [];\nconst totalHours = HOUR_LABELS.length;\n\nfor (let dIdx = 0; dIdx < DAY_SEEDS.length; dIdx++) {\n  const ds = DAY_SEEDS[dIdx];\n  for (let h = 0; h < totalHours; h++) {\n    const mMu = ds.weekend ? 10 : 8.5;\n    const mSig = ds.weekend ? 3.2 : 2.1;\n    const mAmp = ds.weekend ? 55 : 92;\n    const morning = mAmp * Math.exp(-((h - mMu) * (h - mMu)) / (2 * mSig * mSig));\n\n    const eMu = ds.weekend ? 15.5 : 19;\n    const eSig = ds.weekend ? 4.2 : 2.6;\n    const eAmp = ds.weekend ? 85 : 68;\n    const evening = eAmp * Math.exp(-((h - eMu) * (h - eMu)) / (2 * eSig * eSig));\n\n    const cycDist = Math.min(h, totalHours - h);\n    const nightlife = ds.nightlife\n      ? 48 * Math.exp(-(cycDist * cycDist) / (2 * 2.4 * 2.4))\n      : 0;\n\n    data.push({\n      [HOUR]: HOUR_LABELS[h],\n      [DAY]: ds.day,\n      [VISITS]: Number((ds.base + morning + evening + nightlife).toFixed(1)),\n    });\n  }\n}\n\n// Plot: reads only data and the constants above\nconst radialBins: string[] = [];\nconst angularBins: string[] = [];\nfor (const row of data) {\n  if (!radialBins.includes(row[DAY])) radialBins.push(row[DAY]);\n  if (!angularBins.includes(row[HOUR])) angularBins.push(row[HOUR]);\n}\n\nconst allVals = data.map((r) => r[VISITS]);\nconst minValue = Math.min(...allVals);\nconst maxValue = Math.max(...allVals);\nconst valSpan = maxValue - minValue;\n\nconst seqLowNum = parseInt(t.seq[0].slice(1), 16);\nconst seqHighNum = parseInt(t.seq[1].slice(1), 16);\nconst lowR = (seqLowNum >> 16) & 255;\nconst lowG = (seqLowNum >> 8) & 255;\nconst lowB = seqLowNum & 255;\nconst highR = (seqHighNum >> 16) & 255;\nconst highG = (seqHighNum >> 8) & 255;\nconst highB = seqHighNum & 255;\n\nconst cells = data.map((row) => {\n  const frac = (row[VISITS] - minValue) / valSpan;\n  const r = Math.round(lowR + (highR - lowR) * frac);\n  const g = Math.round(lowG + (highG - lowG) * frac);\n  const b = Math.round(lowB + (highB - lowB) * frac);\n  const lum = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;\n  return {\n    dayIdx: radialBins.indexOf(row[DAY]),\n    hourIdx: angularBins.indexOf(row[HOUR]),\n    day: row[DAY],\n    hour: row[HOUR],\n    value: row[VISITS],\n    fill: `rgb(${r}, ${g}, ${b})`,\n    labelFill: lum > 0.54 ? DARK_INK : LIGHT_INK,\n  };\n});\n\nlet peakRow = data[0];\nfor (const row of data) {\n  if (row[VISITS] > peakRow[VISITS]) peakRow = row;\n}\n\nconst anglePerBin = 360 / angularBins.length;\nconst spokeHourIdx = Math.floor(((DAY_LABEL_ANGLE - START_ANGLE + 360) % 360) / anglePerBin);\nconst SUBTITLE = `${VISITS} by ${HOUR} (${angularBins.length} sectors) & ${DAY} (${radialBins[0]} inner → ${radialBins[radialBins.length - 1]} outer) · Peak ${Math.round(peakRow[VISITS])} (${peakRow[DAY]} ${peakRow[HOUR]})`;\n\nfunction PolarHeatmapLayer() {\n  const area = useDrawingArea();\n  const cx = area.left + area.width / 2;\n  const cy = area.top + area.height / 2;\n  const outerR = Math.min(area.width, area.height) / 2 - LABEL_PAD;\n  const innerR = outerR * RING_HOLE_RATIO;\n  const ringThickness = (outerR - innerR) / radialBins.length;\n\n  const barW = Math.min(480, area.width * 0.52);\n  const barH = 18;\n  const barX = cx - barW / 2;\n  const barY = area.top + area.height + 54;\n\n  const daySpokeRad = ((START_ANGLE + (spokeHourIdx + 0.5) * anglePerBin) * Math.PI) / 180;\n\n  return (\n    <g>\n      <defs>\n        <linearGradient id={GRAD_ID} x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\">\n          <stop offset=\"0%\" stopColor={t.seq[0]} />\n          <stop offset=\"100%\" stopColor={t.seq[1]} />\n        </linearGradient>\n      </defs>\n\n      <text\n        x={size.width / 2}\n        y={44}\n        fill={t.ink}\n        fontSize={22}\n        fontWeight={600}\n        textAnchor=\"middle\"\n      >\n        {TITLE}\n      </text>\n      <text\n        x={size.width / 2}\n        y={72}\n        fill={t.inkSoft}\n        fontSize={15}\n        textAnchor=\"middle\"\n      >\n        {SUBTITLE}\n      </text>\n\n      {cells.map((cell) => {\n        const r0 = innerR + cell.dayIdx * ringThickness;\n        const r1 = r0 + ringThickness;\n        const a0 = START_ANGLE + cell.hourIdx * anglePerBin;\n        const a1 = a0 + anglePerBin;\n        const rad0 = (a0 * Math.PI) / 180;\n        const rad1 = (a1 * Math.PI) / 180;\n\n        const x0 = cx + r1 * Math.sin(rad0);\n        const y0 = cy - r1 * Math.cos(rad0);\n        const x1 = cx + r1 * Math.sin(rad1);\n        const y1 = cy - r1 * Math.cos(rad1);\n        const x2 = cx + r0 * Math.sin(rad1);\n        const y2 = cy - r0 * Math.cos(rad1);\n        const x3 = cx + r0 * Math.sin(rad0);\n        const y3 = cy - r0 * Math.cos(rad0);\n\n        const d = `M ${x0} ${y0} A ${r1} ${r1} 0 0 1 ${x1} ${y1} L ${x2} ${y2} A ${r0} ${r0} 0 0 0 ${x3} ${y3} Z`;\n        return (\n          <path\n            key={`${cell.day}-${cell.hour}`}\n            d={d}\n            fill={cell.fill}\n            stroke={t.pageBg}\n            strokeWidth={2}\n          />\n        );\n      })}\n\n      {angularBins.map((label, hIdx) => {\n        if (hIdx % 3 !== 0) return null;\n        const angle = START_ANGLE + (hIdx + 0.5) * anglePerBin;\n        const rad = (angle * Math.PI) / 180;\n        const lx = cx + (outerR + 24) * Math.sin(rad);\n        const ly = cy - (outerR + 24) * Math.cos(rad);\n        const dx = lx - cx;\n        const dy = ly - cy;\n        const anchor = dx > 12 ? \"start\" : dx < -12 ? \"end\" : \"middle\";\n        const baseline = dy > 12 ? \"hanging\" : dy < -12 ? \"auto\" : \"central\";\n        return (\n          <text\n            key={`hour-${label}`}\n            x={lx}\n            y={ly}\n            fill={t.inkSoft}\n            fontSize={15}\n            fontWeight={600}\n            textAnchor={anchor}\n            dominantBaseline={baseline}\n          >\n            {label}\n          </text>\n        );\n      })}\n\n      {radialBins.map((day, i) => {\n        const r = innerR + (i + 0.5) * ringThickness;\n        const lx = cx + r * Math.sin(daySpokeRad);\n        const ly = cy - r * Math.cos(daySpokeRad);\n        const spokeCell = cells.find((c) => c.dayIdx === i && c.hourIdx === spokeHourIdx)!;\n        return (\n          <text\n            key={`day-${day}`}\n            x={lx}\n            y={ly}\n            fill={spokeCell.labelFill}\n            fontSize={14}\n            fontWeight={700}\n            textAnchor=\"middle\"\n            dominantBaseline=\"central\"\n          >\n            {day}\n          </text>\n        );\n      })}\n\n      {/* Horizontal Sequential Colorbar */}\n      <text\n        x={cx}\n        y={barY - 12}\n        textAnchor=\"middle\"\n        fontSize={14}\n        fontWeight={600}\n        fill={t.ink}\n      >\n        {VISITS}\n      </text>\n      <rect\n        x={barX}\n        y={barY}\n        width={barW}\n        height={barH}\n        fill={`url(#${GRAD_ID})`}\n        stroke={t.grid}\n        strokeWidth={1}\n        rx={4}\n      />\n      {COLORBAR_TICKS.map((frac) => {\n        const val = Math.round(minValue + frac * valSpan);\n        const tx = barX + frac * barW;\n        return (\n          <g key={frac}>\n            <line\n              x1={tx}\n              y1={barY + barH}\n              x2={tx}\n              y2={barY + barH + 5}\n              stroke={t.inkSoft}\n              strokeWidth={1}\n            />\n            <text\n              x={tx}\n              y={barY + barH + 21}\n              textAnchor=\"middle\"\n              fontSize={14}\n              fill={t.inkSoft}\n            >\n              {val}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  return (\n    <ChartsContainer\n      width={size.width}\n      height={size.height}\n      series={[]}\n      xAxis={[{ position: \"none\" }]}\n      yAxis={[{ position: \"none\" }]}\n      margin={{ top: 100, bottom: 135, left: 60, right: 60 }}\n      skipAnimation\n    >\n      <PolarHeatmapLayer />\n    </ChartsContainer>\n  );\n}\n"}