{"spec_id":"scatter-shot-chart","library":"muix","language":"javascript","code":"// anyplot.ai\n// scatter-shot-chart: Basketball Shot Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\n//# anyplot-orientation: square\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// NBA half court in feet, origin at the basket center\nconst COURT_WIDTH = 50;\nconst COURT_DEPTH = 47; // baseline to half-court line\nconst RIM_FROM_BASELINE = 5.25;\nconst BACKBOARD_FROM_BASELINE = 4;\nconst BACKBOARD_WIDTH = 6;\nconst RIM_RADIUS = 0.75;\nconst PAINT_WIDTH = 16;\nconst FREE_THROW_FROM_BASELINE = 19; // 15 ft from the backboard\nconst FREE_THROW_CIRCLE_R = 6;\nconst RESTRICTED_R = 4;\nconst THREE_ARC_R = 23.75;\nconst THREE_CORNER_X = 22;\nconst OUTCOME_LABELS = { made: \"Made\", missed: \"Missed\" };\nconst MARKER_RADII = [4.5, 6, 7.5]; // px, by shot type ranked on its mean distance from the basket\nconst MARKER_OPACITY = 0.8;\n\n// Columns: set these to your data's column names\nconst X = \"Court x (ft from basket)\"; // role: x\nconst Y = \"Court y (ft from basket)\"; // role: y\nconst MADE = \"Made\"; // role: made\nconst SHOT_TYPE = \"Shot type\"; // role: shot_type\n\n// Data: replace this block with your own records, one object per row\n// One player's season of 300 attempts: rim, mid-range, corner and arc threes, free throws.\nlet seed = 42;\nconst u = Array.from({ length: 2400 }, () => (seed = (Math.imul(1664525, seed) + 1013904223) >>> 0) / 4294967296);\nlet k = 0;\nconst data: Record<string, number | boolean | string>[] = [];\nfor (let i = 0; i < 300; i += 1) {\n  const zone = u[k++];\n  let x = 0;\n  let y = 0; // feet from the baseline while generating\n  let shotType = \"2-pointer\";\n  if (zone < 0.2) {\n    const angle = u[k++] * Math.PI;\n    const dist = u[k++] * 4 + 0.5;\n    x = Math.cos(angle) * dist;\n    y = RIM_FROM_BASELINE + Math.sin(angle) * dist;\n  } else if (zone < 0.3) {\n    x = (zone < 0.25 ? -1 : 1) * (u[k++] * 2.3 + 22.2);\n    y = u[k++] * 10 + 0.5;\n    shotType = \"3-pointer\";\n  } else if (zone < 0.58) {\n    const angle = u[k++] * Math.PI;\n    const dist = u[k++] * 8 + 9;\n    x = Math.cos(angle) * dist;\n    y = RIM_FROM_BASELINE + Math.sin(angle) * dist;\n  } else if (zone < 0.85) {\n    // the angle range shrinks with the distance, so every arc three lands inside the sidelines\n    const share = u[k++];\n    const dist = u[k++] * 4.5 + THREE_ARC_R;\n    const edge = Math.acos(Math.min(1, (COURT_WIDTH / 2 - 0.5) / dist));\n    const angle = edge + share * (Math.PI - 2 * edge);\n    x = Math.cos(angle) * dist;\n    y = RIM_FROM_BASELINE + Math.sin(angle) * dist;\n    shotType = \"3-pointer\";\n  } else {\n    x = u[k++] * 0.8 - 0.4;\n    y = FREE_THROW_FROM_BASELINE + u[k++] * 0.6 - 0.3;\n    shotType = \"free-throw\";\n  }\n  const near = Math.hypot(x, y - RIM_FROM_BASELINE) < 7;\n  const makePct = shotType === \"free-throw\" ? 0.78 : shotType === \"3-pointer\" ? 0.37 : near ? 0.6 : 0.41;\n  data.push({\n    [X]: Number(x.toFixed(2)),\n    [Y]: Number((y - RIM_FROM_BASELINE).toFixed(2)),\n    [MADE]: u[k++] < makePct,\n    [SHOT_TYPE]: shotType,\n  });\n}\n\n// Plot: reads only data and the constants above\nconst { width: W, height: H } = window.ANYPLOT_SIZE;\nconst outcomeColor = { made: t.palette[0], missed: t.palette[4] };\n\nconst types = [...new Set(data.map((d) => String(d[SHOT_TYPE])))];\nconst typeStats = types\n  .map((type) => {\n    const rows = data.filter((d) => d[SHOT_TYPE] === type);\n    const meanDist = rows.reduce((s, d) => s + Math.hypot(Number(d[X]), Number(d[Y])), 0) / rows.length;\n    return { type, attempts: rows.length, made: rows.filter((d) => d[MADE]).length, meanDist };\n  })\n  .sort((a, b) => a.meanDist - b.meanDist);\nconst radiusOf = new Map(typeStats.map((s, i) => [s.type, MARKER_RADII[i]]));\n\nconst shots = data.map((d, i) => ({\n  id: i,\n  x: Number(d[X]),\n  y: Number(d[Y]),\n  made: Boolean(d[MADE]),\n  r: radiusOf.get(String(d[SHOT_TYPE])) as number,\n}));\nconst missedShots = shots.filter((s) => !s.made);\nconst madeShots = shots.filter((s) => s.made);\nconst madeTotal = madeShots.length;\n\n// Court geometry in basket-centered feet\nconst baselineY = -RIM_FROM_BASELINE;\nconst halfCourtY = COURT_DEPTH - RIM_FROM_BASELINE;\nconst backboardY = BACKBOARD_FROM_BASELINE - RIM_FROM_BASELINE;\nconst freeThrowY = FREE_THROW_FROM_BASELINE - RIM_FROM_BASELINE;\nconst cornerTopY = Math.sqrt(THREE_ARC_R ** 2 - THREE_CORNER_X ** 2);\n\n// Layout: equal px per foot on both axes, court centered in the chart area\nconst HEADER_H = 92;\nconst LEGEND_H = 56;\nconst MIN_SIDE = 40;\nconst MIN_VERT = 14;\nconst chartH = H - HEADER_H - LEGEND_H;\nconst pxPerFt = Math.min((W - 2 * MIN_SIDE) / COURT_WIDTH, (chartH - 2 * MIN_VERT) / COURT_DEPTH);\nconst sideMargin = (W - pxPerFt * COURT_WIDTH) / 2;\nconst vertMargin = (chartH - pxPerFt * COURT_DEPTH) / 2;\n\nconst title = \"scatter-shot-chart · javascript · muix · anyplot.ai\";\nconst subtitle = [\n  `${shots.length} attempts, ${madeTotal} made (${((100 * madeTotal) / shots.length).toFixed(1)}%)`,\n  ...typeStats.map((s) => `${s.type} ${s.made}/${s.attempts} (${((100 * s.made) / s.attempts).toFixed(0)}%)`),\n].join(\" · \");\n\nfunction Court() {\n  const xs = useXScale(\"court-x\") as (v: number) => number;\n  const ys = useYScale(\"court-y\") as (v: number) => number;\n  const line = { fill: \"none\", stroke: t.inkSoft, strokeWidth: 2, strokeLinecap: \"round\" as const };\n  const ftR = FREE_THROW_CIRCLE_R * pxPerFt;\n  return (\n    <g>\n      <rect\n        x={xs(-COURT_WIDTH / 2)}\n        y={ys(halfCourtY)}\n        width={COURT_WIDTH * pxPerFt}\n        height={COURT_DEPTH * pxPerFt}\n        style={{ ...line, strokeWidth: 2.5 }}\n      />\n      <rect\n        x={xs(-PAINT_WIDTH / 2)}\n        y={ys(freeThrowY)}\n        width={PAINT_WIDTH * pxPerFt}\n        height={(freeThrowY - baselineY) * pxPerFt}\n        style={line}\n      />\n      <path\n        d={`M ${xs(-FREE_THROW_CIRCLE_R)} ${ys(freeThrowY)} A ${ftR} ${ftR} 0 0 1 ${xs(FREE_THROW_CIRCLE_R)} ${ys(freeThrowY)}`}\n        style={line}\n      />\n      <path\n        d={`M ${xs(-FREE_THROW_CIRCLE_R)} ${ys(freeThrowY)} A ${ftR} ${ftR} 0 0 0 ${xs(FREE_THROW_CIRCLE_R)} ${ys(freeThrowY)}`}\n        style={{ ...line, strokeDasharray: \"8 7\" }}\n      />\n      <path\n        d={`M ${xs(-RESTRICTED_R)} ${ys(backboardY)} L ${xs(-RESTRICTED_R)} ${ys(0)} A ${RESTRICTED_R * pxPerFt} ${RESTRICTED_R * pxPerFt} 0 0 1 ${xs(RESTRICTED_R)} ${ys(0)} L ${xs(RESTRICTED_R)} ${ys(backboardY)}`}\n        style={line}\n      />\n      <path\n        d={`M ${xs(-THREE_CORNER_X)} ${ys(baselineY)} L ${xs(-THREE_CORNER_X)} ${ys(cornerTopY)} A ${THREE_ARC_R * pxPerFt} ${THREE_ARC_R * pxPerFt} 0 0 1 ${xs(THREE_CORNER_X)} ${ys(cornerTopY)} L ${xs(THREE_CORNER_X)} ${ys(baselineY)}`}\n        style={line}\n      />\n      <line\n        x1={xs(-BACKBOARD_WIDTH / 2)}\n        y1={ys(backboardY)}\n        x2={xs(BACKBOARD_WIDTH / 2)}\n        y2={ys(backboardY)}\n        style={{ ...line, strokeWidth: 4 }}\n      />\n      <circle cx={xs(0)} cy={ys(0)} r={RIM_RADIUS * pxPerFt} style={{ ...line, strokeWidth: 2.5 }} />\n    </g>\n  );\n}\n\nfunction ShotMarkers() {\n  const xs = useXScale(\"court-x\") as (v: number) => number;\n  const ys = useYScale(\"court-y\") as (v: number) => number;\n  return (\n    <g>\n      {missedShots.map((s) => {\n        const px = xs(s.x);\n        const py = ys(s.y);\n        return (\n          <path\n            key={s.id}\n            d={`M ${px - s.r} ${py - s.r} L ${px + s.r} ${py + s.r} M ${px + s.r} ${py - s.r} L ${px - s.r} ${py + s.r}`}\n            stroke={outcomeColor.missed}\n            strokeWidth={s.r * 0.55}\n            strokeOpacity={MARKER_OPACITY}\n            strokeLinecap=\"round\"\n          />\n        );\n      })}\n      {madeShots.map((s) => (\n        <circle\n          key={s.id}\n          cx={xs(s.x)}\n          cy={ys(s.y)}\n          r={s.r}\n          fill={outcomeColor.made}\n          fillOpacity={MARKER_OPACITY}\n        />\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const glyph = MARKER_RADII[MARKER_RADII.length - 1];\n  return (\n    <div style={{ width: W, height: H, background: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <div\n        style={{\n          height: HEADER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          justifyContent: \"center\",\n          alignItems: \"center\",\n          gap: 8,\n        }}\n      >\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{title}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{subtitle}</div>\n      </div>\n      <ChartsContainer\n        width={W}\n        height={chartH}\n        skipAnimation\n        series={[]}\n        margin={{ top: vertMargin, bottom: vertMargin, left: sideMargin, right: sideMargin }}\n        xAxis={[{ id: \"court-x\", min: -COURT_WIDTH / 2, max: COURT_WIDTH / 2, position: \"none\" }]}\n        yAxis={[{ id: \"court-y\", min: baselineY, max: halfCourtY, position: \"none\" }]}\n      >\n        <Court />\n        <ShotMarkers />\n      </ChartsContainer>\n      <div\n        style={{\n          height: LEGEND_H,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 22,\n          fontSize: 16,\n          color: t.ink,\n        }}\n      >\n        <span style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n          <svg width={2 * glyph + 4} height={2 * glyph + 4}>\n            <circle cx={glyph + 2} cy={glyph + 2} r={glyph} fill={outcomeColor.made} fillOpacity={MARKER_OPACITY} />\n          </svg>\n          {OUTCOME_LABELS.made}\n        </span>\n        <span style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n          <svg width={2 * glyph + 4} height={2 * glyph + 4}>\n            <path\n              d={`M 2 2 L ${2 * glyph + 2} ${2 * glyph + 2} M ${2 * glyph + 2} 2 L 2 ${2 * glyph + 2}`}\n              stroke={outcomeColor.missed}\n              strokeWidth={glyph * 0.55}\n              strokeOpacity={MARKER_OPACITY}\n              strokeLinecap=\"round\"\n            />\n          </svg>\n          {OUTCOME_LABELS.missed}\n        </span>\n        <span style={{ color: t.inkSoft, marginLeft: 18 }}>{SHOT_TYPE} (marker size):</span>\n        {typeStats.map((s, i) => (\n          <span key={s.type} style={{ display: \"flex\", alignItems: \"center\", gap: 6 }}>\n            <svg width={2 * glyph + 4} height={2 * glyph + 4}>\n              <circle cx={glyph + 2} cy={glyph + 2} r={MARKER_RADII[i]} fill={t.inkSoft} fillOpacity={0.75} />\n            </svg>\n            {s.type}\n          </span>\n        ))}\n      </div>\n    </div>\n  );\n}\n"}