{"spec_id":"polar-bar","library":"muix","language":"javascript","code":"// anyplot.ai\n// polar-bar: Polar Bar Chart (Wind Rose)\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n//# anyplot-orientation: square\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"polar-bar · javascript · muix · anyplot.ai\";\nconst RING_COUNT = 4;\nconst WEDGE_GAP_RAD = 0.035;\nconst OUTER_PAD_PX = 64;\nconst LABEL_OFFSET_PX = 28;\nconst HEADER_HEIGHT = 64;\nconst FOOTER_HEIGHT = 56;\n\n// Columns: set these to your data's column names\nconst DIRECTION = \"Direction\"; // role: theta\nconst SPEED_BIN = \"Wind Speed\"; // role: group\nconst COUNT = \"Observations (obs)\"; // role: r\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, string | number>> = [\n  { [DIRECTION]: \"N\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 18 },\n  { [DIRECTION]: \"NNE\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 15 },\n  { [DIRECTION]: \"NE\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 12 },\n  { [DIRECTION]: \"ENE\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 10 },\n  { [DIRECTION]: \"E\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 14 },\n  { [DIRECTION]: \"ESE\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 20 },\n  { [DIRECTION]: \"SE\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 25 },\n  { [DIRECTION]: \"SSE\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 30 },\n  { [DIRECTION]: \"S\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 35 },\n  { [DIRECTION]: \"SSW\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 40 },\n  { [DIRECTION]: \"SW\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 45 },\n  { [DIRECTION]: \"WSW\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 42 },\n  { [DIRECTION]: \"W\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 38 },\n  { [DIRECTION]: \"WNW\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 30 },\n  { [DIRECTION]: \"NW\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 24 },\n  { [DIRECTION]: \"NNW\", [SPEED_BIN]: \"Light (0–10 mph)\", [COUNT]: 20 },\n\n  { [DIRECTION]: \"N\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 10 },\n  { [DIRECTION]: \"NNE\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 8 },\n  { [DIRECTION]: \"NE\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 6 },\n  { [DIRECTION]: \"ENE\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 5 },\n  { [DIRECTION]: \"E\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 8 },\n  { [DIRECTION]: \"ESE\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 14 },\n  { [DIRECTION]: \"SE\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 18 },\n  { [DIRECTION]: \"SSE\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 24 },\n  { [DIRECTION]: \"S\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 30 },\n  { [DIRECTION]: \"SSW\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 38 },\n  { [DIRECTION]: \"SW\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 46 },\n  { [DIRECTION]: \"WSW\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 40 },\n  { [DIRECTION]: \"W\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 32 },\n  { [DIRECTION]: \"WNW\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 22 },\n  { [DIRECTION]: \"NW\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 16 },\n  { [DIRECTION]: \"NNW\", [SPEED_BIN]: \"Moderate (10–20 mph)\", [COUNT]: 12 },\n\n  { [DIRECTION]: \"N\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 2 },\n  { [DIRECTION]: \"NNE\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 1 },\n  { [DIRECTION]: \"NE\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 1 },\n  { [DIRECTION]: \"ENE\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 1 },\n  { [DIRECTION]: \"E\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 2 },\n  { [DIRECTION]: \"ESE\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 4 },\n  { [DIRECTION]: \"SE\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 6 },\n  { [DIRECTION]: \"SSE\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 10 },\n  { [DIRECTION]: \"S\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 14 },\n  { [DIRECTION]: \"SSW\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 20 },\n  { [DIRECTION]: \"SW\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 28 },\n  { [DIRECTION]: \"WSW\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 22 },\n  { [DIRECTION]: \"W\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 15 },\n  { [DIRECTION]: \"WNW\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 8 },\n  { [DIRECTION]: \"NW\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 4 },\n  { [DIRECTION]: \"NNW\", [SPEED_BIN]: \"Strong (20+ mph)\", [COUNT]: 3 },\n];\n\n// Plot: reads only data and the constants above\nconst directions: string[] = [];\nconst speedBinLabels: string[] = [];\nfor (const row of data) {\n  const dir = String(row[DIRECTION]);\n  const bin = String(row[SPEED_BIN]);\n  if (!directions.includes(dir)) directions.push(dir);\n  if (!speedBinLabels.includes(bin)) speedBinLabels.push(bin);\n}\n\nconst seq0Num = parseInt(t.seq[0].replace(\"#\", \"\"), 16);\nconst seq1Num = parseInt(t.seq[1].replace(\"#\", \"\"), 16);\nconst r0 = (seq0Num >> 16) & 255;\nconst g0 = (seq0Num >> 8) & 255;\nconst b0 = seq0Num & 255;\nconst r1 = (seq1Num >> 16) & 255;\nconst g1 = (seq1Num >> 8) & 255;\nconst b1 = seq1Num & 255;\n\nconst speedBins = speedBinLabels.map((label, idx) => {\n  const ratio = speedBinLabels.length > 1 ? idx / (speedBinLabels.length - 1) : 1;\n  const r = Math.round(r0 + (r1 - r0) * ratio);\n  const g = Math.round(g0 + (g1 - g0) * ratio);\n  const b = Math.round(b0 + (b1 - b0) * ratio);\n  return {\n    label,\n    color: `rgb(${r}, ${g}, ${b})`,\n  };\n});\n\nconst countUnitMatch = /\\(([^)]+)\\)/.exec(COUNT);\nconst countUnit = countUnitMatch ? countUnitMatch[1] : \"\";\n\nconst dirCount = directions.length;\nconst sectorHalfWidth = Math.PI / dirCount;\n\nconst directionStacks = directions.map((dir, i) => {\n  const angle = (-90 + (i * 360) / dirCount) * (Math.PI / 180);\n  let cumulative = 0;\n  const segments = speedBins.map((bin) => {\n    const match = data.find(\n      (r) => String(r[DIRECTION]) === dir && String(r[SPEED_BIN]) === bin.label,\n    );\n    const val = match ? Number(match[COUNT]) : 0;\n    const startVal = cumulative;\n    cumulative += val;\n    return {\n      binLabel: bin.label,\n      color: bin.color,\n      startVal,\n      endVal: cumulative,\n    };\n  });\n  return { dir, index: i, angle, total: cumulative, segments };\n});\n\nconst rawMaxTotal = Math.max(...directionStacks.map((d) => d.total));\nconst rawStep = rawMaxTotal / RING_COUNT;\nconst stepMag = 10 ** Math.floor(Math.log10(rawStep));\nconst stepNorm = rawStep / stepMag;\nconst niceStep = [1, 2, 3, 4, 5, 6, 8, 10].find((s) => stepNorm <= s) ?? 10;\nconst ringStep = niceStep * stepMag;\nconst ringDecimals = Math.max(0, -Math.floor(Math.log10(ringStep) + 1e-9));\nconst maxValue = ringStep * RING_COUNT;\nconst rings: number[] = [];\nfor (let k = 1; k <= RING_COUNT; k += 1) {\n  rings.push(Number((k * ringStep).toFixed(ringDecimals)));\n}\n\nfunction WindRoseOverlay() {\n  const area = useDrawingArea();\n  const cx = area.left + area.width / 2;\n  const cy = area.top + area.height / 2;\n  const half = Math.min(area.width, area.height) / 2;\n  const radius = half - OUTER_PAD_PX;\n  const labelRadius = radius + LABEL_OFFSET_PX;\n  const pxPerUnit = radius / maxValue;\n\n  return (\n    <g>\n      {rings.map((level) => (\n        <circle\n          key={`ring-${level}`}\n          cx={cx}\n          cy={cy}\n          r={(level / maxValue) * radius}\n          fill=\"none\"\n          stroke={t.grid}\n          strokeWidth={level === maxValue ? 2 : 1}\n          strokeOpacity={level === maxValue ? 1 : 0.65}\n        />\n      ))}\n\n      {directionStacks.map((item) => {\n        const isPrimary = item.index % 2 === 0;\n        const ox = cx + radius * Math.cos(item.angle);\n        const oy = cy + radius * Math.sin(item.angle);\n        const lx = cx + labelRadius * Math.cos(item.angle);\n        const ly = cy + labelRadius * Math.sin(item.angle);\n        const cos = Math.cos(item.angle);\n        const sin = Math.sin(item.angle);\n        const anchor = cos > 0.25 ? \"start\" : cos < -0.25 ? \"end\" : \"middle\";\n        const baseline = sin > 0.4 ? \"hanging\" : sin < -0.4 ? \"auto\" : \"central\";\n        return (\n          <g key={`spoke-${item.dir}`}>\n            <line\n              x1={cx}\n              y1={cy}\n              x2={ox}\n              y2={oy}\n              stroke={t.grid}\n              strokeWidth={isPrimary ? 1.25 : 0.85}\n              strokeOpacity={isPrimary ? 0.9 : 0.5}\n            />\n            <text\n              x={lx}\n              y={ly}\n              fill={isPrimary ? t.ink : t.inkSoft}\n              fontSize={isPrimary ? 16 : 14}\n              fontWeight={isPrimary ? 600 : 500}\n              textAnchor={anchor}\n              dominantBaseline={baseline}\n            >\n              {item.dir}\n            </text>\n          </g>\n        );\n      })}\n\n      {directionStacks.map((item) => {\n        const a0 = item.angle - sectorHalfWidth + WEDGE_GAP_RAD;\n        const a1 = item.angle + sectorHalfWidth - WEDGE_GAP_RAD;\n        const cos0 = Math.cos(a0);\n        const sin0 = Math.sin(a0);\n        const cos1 = Math.cos(a1);\n        const sin1 = Math.sin(a1);\n\n        return item.segments.map((seg) => {\n          const r0Px = seg.startVal * pxPerUnit;\n          const r1Px = seg.endVal * pxPerUnit;\n          if (r1Px <= r0Px) return null;\n          const ox0 = cx + r1Px * cos0;\n          const oy0 = cy + r1Px * sin0;\n          const ox1 = cx + r1Px * cos1;\n          const oy1 = cy + r1Px * sin1;\n          let pathD = \"\";\n          if (r0Px < 0.5) {\n            pathD = `M ${cx},${cy} L ${ox0},${oy0} A ${r1Px} ${r1Px} 0 0 1 ${ox1},${oy1} Z`;\n          } else {\n            const ix0 = cx + r0Px * cos0;\n            const iy0 = cy + r0Px * sin0;\n            const ix1 = cx + r0Px * cos1;\n            const iy1 = cy + r0Px * sin1;\n            pathD =\n              `M ${ix0},${iy0} L ${ox0},${oy0} ` +\n              `A ${r1Px} ${r1Px} 0 0 1 ${ox1},${oy1} L ${ix1},${iy1} ` +\n              `A ${r0Px} ${r0Px} 0 0 0 ${ix0},${iy0} Z`;\n          }\n          return (\n            <path\n              key={`wedge-${item.dir}-${seg.binLabel}`}\n              d={pathD}\n              fill={seg.color}\n              stroke={t.pageBg}\n              strokeWidth={2}\n            />\n          );\n        });\n      })}\n\n      {rings.map((level) => {\n        const ty = cy - (level / maxValue) * radius;\n        const levelStr = level.toFixed(ringDecimals);\n        const tickText = countUnit ? `${levelStr} ${countUnit}` : levelStr;\n        const pillWidth = tickText.length * 8.5 + 14;\n        return (\n          <g key={`tick-${level}`}>\n            <rect x={cx + 6} y={ty - 11} width={pillWidth} height={22} rx={4} fill={t.pageBg} />\n            <text\n              x={cx + 12}\n              y={ty}\n              fill={t.inkSoft}\n              fontSize={14}\n              textAnchor=\"start\"\n              dominantBaseline=\"central\"\n            >\n              {tickText}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const size = window.ANYPLOT_SIZE;\n  const chartHeight = size.height - HEADER_HEIGHT - FOOTER_HEIGHT;\n\n  return (\n    <div\n      style={{\n        width: size.width,\n        height: size.height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        boxSizing: \"border-box\",\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: 22,\n          fontWeight: 700,\n          color: t.ink,\n        }}\n      >\n        {TITLE}\n      </div>\n      <ChartsContainer\n        width={size.width}\n        height={chartHeight}\n        series={[]}\n        xAxis={[{ position: \"none\" }]}\n        yAxis={[{ position: \"none\" }]}\n        margin={{ top: 16, bottom: 16, left: 24, right: 24 }}\n        skipAnimation\n      >\n        <WindRoseOverlay />\n      </ChartsContainer>\n      <div\n        style={{\n          height: FOOTER_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 36,\n        }}\n      >\n        {speedBins.map((bin) => (\n          <div\n            key={`legend-${bin.label}`}\n            style={{ display: \"flex\", alignItems: \"center\", gap: 10 }}\n          >\n            <span\n              style={{\n                width: 20,\n                height: 20,\n                borderRadius: 4,\n                backgroundColor: bin.color,\n                display: \"inline-block\",\n              }}\n            />\n            <span style={{ fontSize: 15, color: t.ink, fontWeight: 500 }}>{bin.label}</span>\n          </div>\n        ))}\n      </div>\n    </div>\n  );\n}\n"}