{"spec_id":"strip-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// strip-basic: Basic Strip Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst BASELINE = 0; // durations start at zero\nconst JITTER_WIDTH = 0.27; // half-width of the random sideways spread, in category slots\nconst POINT_ALPHA = 0.62; // points overlap often, so piled-up values read darker\nconst MEAN_HALF_WIDTH = 0.34; // the mean line spans slightly past the strip\nconst CATEGORY_PAD = 0.6; // room beside the outer strips, in category slots\nconst Y_PAD_SHARE = 0.05;\nconst JITTER_SEED = 7;\n\n// Columns: set these to your data's column names\nconst GROUP = \"Treatment group\"; // role: category\nconst TIME = \"Time to relief (minutes)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\n// Time to symptom relief after treatment, 42 patients in each of four groups\nconst data = (() => {\n  let seed = 42;\n  const rand = () => {\n    seed = (1664525 * seed + 1013904223) >>> 0;\n    return seed / 4294967296;\n  };\n  const groups = [\n    { name: \"Placebo\", mean: 46, std: 12 },\n    { name: \"Paracetamol 1 g\", mean: 30, std: 10 },\n    { name: \"Ibuprofen 400 mg\", mean: 19, std: 7 },\n    { name: \"Naproxen 500 mg\", mean: 11, std: 5 },\n  ];\n  return groups.flatMap((g) =>\n    Array.from({ length: 42 }, () => {\n      const normal = Math.sqrt(-2 * Math.log(Math.max(rand(), 1e-9))) * Math.cos(2 * Math.PI * rand());\n      return { [GROUP]: g.name, [TIME]: Number(Math.max(2, g.mean + g.std * normal).toFixed(1)) };\n    }),\n  );\n})();\n\n// Plot: reads only data and the constants above\n// Each group sits at its slot index; a seeded sideways jitter spreads its points, the values stay exact\nconst rows = data.filter((d) => d[GROUP] != null && d[TIME] != null);\nconst groups = [...new Set(rows.map((d) => d[GROUP] as string))];\nlet jitterState = JITTER_SEED;\nconst series = groups.map((group, i) => ({\n  type: \"scatter\" as const,\n  id: `group-${i}`,\n  label: group,\n  color: t.palette[i],\n  markerSize: 9,\n  data: rows\n    .filter((d) => d[GROUP] === group)\n    .map((d, k) => {\n      jitterState = (1664525 * jitterState + 1013904223) >>> 0;\n      return { x: i + ((jitterState / 4294967296) * 2 - 1) * JITTER_WIDTH, y: d[TIME] as number, id: `${i}-${k}` };\n    }),\n}));\nconst groupMeans = series.map((s) => s.data.reduce((acc, p) => acc + p.y, 0) / s.data.length);\nconst valueMax = Math.max(...rows.map((d) => d[TIME] as number));\nconst yMax = valueMax + (valueMax - BASELINE) * Y_PAD_SHARE;\n\nconst TITLE = \"strip-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${rows.length} observations in ${groups.length} groups · line: group mean`;\nconst HEADER_H = 76;\n\n// A short line at each group's mean, in the group's colour\nfunction MeanLines() {\n  const xScale = useXScale() as (v: number) => number;\n  const yScale = useYScale() as (v: number) => number;\n  return (\n    <g>\n      {groupMeans.map((mean, i) => (\n        <line\n          key={groups[i]}\n          x1={xScale(i - MEAN_HALF_WIDTH)}\n          x2={xScale(i + MEAN_HALF_WIDTH)}\n          y1={yScale(mean)}\n          y2={yScale(mean)}\n          stroke={t.palette[i]}\n          strokeWidth={3.5}\n          strokeLinecap=\"round\"\n        />\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width: W, height: H } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width: W, height: H, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box\n        sx={{\n          height: HEADER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: \"6px\",\n        }}\n      >\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600, lineHeight: 1.2 }}>{TITLE}</Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: 15, lineHeight: 1.2 }}>{SUBTITLE}</Typography>\n      </Box>\n      <ChartsContainer\n        width={W}\n        height={H - HEADER_H}\n        skipAnimation\n        margin={{ top: 16, right: 48, bottom: 16, left: 24 }}\n        series={series}\n        xAxis={[\n          {\n            id: \"group\",\n            min: -CATEGORY_PAD,\n            max: groups.length - 1 + CATEGORY_PAD,\n            tickInterval: groups.map((_, i) => i),\n            valueFormatter: (v: number) => groups[Math.round(v)] ?? \"\",\n            label: GROUP,\n            height: 64,\n            tickLabelStyle: { fontSize: 15, fill: t.ink },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"time\",\n            min: BASELINE,\n            max: yMax,\n            label: TIME,\n            tickNumber: 8,\n            width: 72,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        sx={{\n          \"& .MuiScatterChart-marker\": { fillOpacity: POINT_ALPHA },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      >\n        <ChartsGrid horizontal />\n        <ScatterPlot />\n        <MeanLines />\n        <ChartsXAxis />\n        <ChartsYAxis />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}