{"spec_id":"swimmer-clinical-timeline","library":"muix","language":"javascript","code":"// anyplot.ai\n// swimmer-clinical-timeline: Swimmer Plot for Clinical Trial Timelines\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\nimport { BarChart } from \"@mui/x-charts/BarChart\";\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 X_PAD_SHARE = 0.04; // room right of the longest bar for its ongoing arrow\nconst MARKER_SHARE = 0.42; // event marker radius as a share of the bar band\nconst MARKER_MAX = 13; // largest event marker radius (px)\n// Event symbols as unit paths around (0, 0), scaled to the marker radius\nconst STAR = `${Array.from({ length: 10 }, (_, i) => {\n  const r = i % 2 === 0 ? 1.15 : 0.48;\n  const a = (Math.PI / 5) * i - Math.PI / 2;\n  return `${i === 0 ? \"M\" : \"L\"} ${(r * Math.cos(a)).toFixed(3)} ${(r * Math.sin(a)).toFixed(3)}`;\n}).join(\" \")} Z`;\nconst SHAPE_PATH = {\n  triangle: \"M 0 -1 L 1 0.85 L -1 0.85 Z\",\n  star: STAR,\n  diamond: \"M 0 -1 L 1 0 L 0 1 L -1 0 Z\",\n  circle: \"M -0.72 0 A 0.72 0.72 0 1 0 0.72 0 A 0.72 0.72 0 1 0 -0.72 0 Z\",\n  arrow: \"M 0 -1.05 L 1.8 0 L 0 1.05 Z\",\n  square: \"M -0.75 -0.75 H 0.75 V 0.75 H -0.75 Z\",\n};\n// Clinical event vocabulary: a symbol per type, and the colours fixed by meaning\n// (progression in the matte-red loss anchor, adverse events in the warning amber,\n// the ongoing arrow in ink); other types take the palette positions left over\nconst EVENT_SHAPE = {\n  \"Partial response\": \"triangle\",\n  \"Complete response\": \"star\",\n  \"Progressive disease\": \"diamond\",\n  \"Adverse event\": \"circle\",\n  Ongoing: \"arrow\",\n};\nconst EVENT_COLOR = { \"Progressive disease\": t.palette[4], \"Adverse event\": t.amber, Ongoing: t.ink };\nconst ONGOING = \"Ongoing\"; // the event type that marks a patient still on treatment at data cutoff\nconst HEADER_H = 128;\n\n// Columns: set these to your data's column names\nconst PATIENT = \"Patient\"; // role: patient_id\nconst ARM = \"Treatment arm\"; // role: group\nconst DURATION = \"Time on study (weeks)\"; // role: duration\nconst EVENT_TIME = \"Event time (weeks)\"; // role: events\nconst EVENT_TYPE = \"Event\"; // role: events\n\n// Data: replace this block with your own records, one object per row\n// Simulated Phase II oncology trial: one row per clinical event of each patient\nconst data = [\n  { [PATIENT]: \"PT-001\", [ARM]: \"Targeted Therapy\", [DURATION]: 32, [EVENT_TIME]: 9, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-001\", [ARM]: \"Targeted Therapy\", [DURATION]: 32, [EVENT_TIME]: 32, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-002\", [ARM]: \"Targeted Therapy\", [DURATION]: 47, [EVENT_TIME]: 11, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-002\", [ARM]: \"Targeted Therapy\", [DURATION]: 47, [EVENT_TIME]: 25, [EVENT_TYPE]: \"Complete response\" },\n  { [PATIENT]: \"PT-002\", [ARM]: \"Targeted Therapy\", [DURATION]: 47, [EVENT_TIME]: 47, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-003\", [ARM]: \"Chemotherapy\", [DURATION]: 26, [EVENT_TIME]: 26, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-004\", [ARM]: \"Targeted Therapy\", [DURATION]: 54, [EVENT_TIME]: 12, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-004\", [ARM]: \"Targeted Therapy\", [DURATION]: 54, [EVENT_TIME]: 34, [EVENT_TYPE]: \"Complete response\" },\n  { [PATIENT]: \"PT-004\", [ARM]: \"Targeted Therapy\", [DURATION]: 54, [EVENT_TIME]: 54, [EVENT_TYPE]: \"Ongoing\" },\n  { [PATIENT]: \"PT-005\", [ARM]: \"Targeted Therapy\", [DURATION]: 36, [EVENT_TIME]: 36, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-006\", [ARM]: \"Chemotherapy\", [DURATION]: 22, [EVENT_TIME]: 10, [EVENT_TYPE]: \"Adverse event\" },\n  { [PATIENT]: \"PT-006\", [ARM]: \"Chemotherapy\", [DURATION]: 22, [EVENT_TIME]: 22, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-007\", [ARM]: \"Chemotherapy\", [DURATION]: 14, [EVENT_TIME]: 14, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-008\", [ARM]: \"Targeted Therapy\", [DURATION]: 41, [EVENT_TIME]: 10, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-008\", [ARM]: \"Targeted Therapy\", [DURATION]: 41, [EVENT_TIME]: 41, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-009\", [ARM]: \"Targeted Therapy\", [DURATION]: 28, [EVENT_TIME]: 14, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-009\", [ARM]: \"Targeted Therapy\", [DURATION]: 28, [EVENT_TIME]: 28, [EVENT_TYPE]: \"Ongoing\" },\n  { [PATIENT]: \"PT-010\", [ARM]: \"Chemotherapy\", [DURATION]: 18, [EVENT_TIME]: 18, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-011\", [ARM]: \"Targeted Therapy\", [DURATION]: 58, [EVENT_TIME]: 9, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-011\", [ARM]: \"Targeted Therapy\", [DURATION]: 58, [EVENT_TIME]: 27, [EVENT_TYPE]: \"Complete response\" },\n  { [PATIENT]: \"PT-011\", [ARM]: \"Targeted Therapy\", [DURATION]: 58, [EVENT_TIME]: 58, [EVENT_TYPE]: \"Ongoing\" },\n  { [PATIENT]: \"PT-012\", [ARM]: \"Targeted Therapy\", [DURATION]: 16, [EVENT_TIME]: 8, [EVENT_TYPE]: \"Adverse event\" },\n  { [PATIENT]: \"PT-012\", [ARM]: \"Targeted Therapy\", [DURATION]: 16, [EVENT_TIME]: 16, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-013\", [ARM]: \"Chemotherapy\", [DURATION]: 39, [EVENT_TIME]: 20, [EVENT_TYPE]: \"Adverse event\" },\n  { [PATIENT]: \"PT-013\", [ARM]: \"Chemotherapy\", [DURATION]: 39, [EVENT_TIME]: 22, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-013\", [ARM]: \"Chemotherapy\", [DURATION]: 39, [EVENT_TIME]: 39, [EVENT_TYPE]: \"Ongoing\" },\n  { [PATIENT]: \"PT-014\", [ARM]: \"Targeted Therapy\", [DURATION]: 12, [EVENT_TIME]: 12, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-015\", [ARM]: \"Targeted Therapy\", [DURATION]: 20, [EVENT_TIME]: 20, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-016\", [ARM]: \"Chemotherapy\", [DURATION]: 34, [EVENT_TIME]: 18, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-016\", [ARM]: \"Chemotherapy\", [DURATION]: 34, [EVENT_TIME]: 34, [EVENT_TYPE]: \"Ongoing\" },\n  { [PATIENT]: \"PT-017\", [ARM]: \"Chemotherapy\", [DURATION]: 50, [EVENT_TIME]: 14, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-017\", [ARM]: \"Chemotherapy\", [DURATION]: 50, [EVENT_TIME]: 40, [EVENT_TYPE]: \"Adverse event\" },\n  { [PATIENT]: \"PT-017\", [ARM]: \"Chemotherapy\", [DURATION]: 50, [EVENT_TIME]: 50, [EVENT_TYPE]: \"Ongoing\" },\n  { [PATIENT]: \"PT-018\", [ARM]: \"Targeted Therapy\", [DURATION]: 24, [EVENT_TIME]: 8, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-018\", [ARM]: \"Targeted Therapy\", [DURATION]: 24, [EVENT_TIME]: 24, [EVENT_TYPE]: \"Progressive disease\" },\n  { [PATIENT]: \"PT-019\", [ARM]: \"Chemotherapy\", [DURATION]: 44, [EVENT_TIME]: 16, [EVENT_TYPE]: \"Partial response\" },\n  { [PATIENT]: \"PT-019\", [ARM]: \"Chemotherapy\", [DURATION]: 44, [EVENT_TIME]: 44, [EVENT_TYPE]: \"Ongoing\" },\n  { [PATIENT]: \"PT-020\", [ARM]: \"Chemotherapy\", [DURATION]: 30, [EVENT_TIME]: 12, [EVENT_TYPE]: \"Adverse event\" },\n  { [PATIENT]: \"PT-020\", [ARM]: \"Chemotherapy\", [DURATION]: 30, [EVENT_TIME]: 30, [EVENT_TYPE]: \"Progressive disease\" },\n];\n\n// Plot: reads only data and the constants above\n// One bar per patient, longest time on study at the top\nconst patients = [...new Set(data.map((d) => d[PATIENT]))]\n  .map((id) => {\n    const rows = data.filter((d) => d[PATIENT] === id);\n    return {\n      id,\n      arm: rows[0][ARM],\n      duration: Math.max(...rows.map((d) => d[DURATION])),\n      events: rows.filter((d) => d[EVENT_TYPE] != null && Number.isFinite(d[EVENT_TIME])),\n    };\n  })\n  .sort((a, b) => b.duration - a.duration);\nconst arms = [...new Set(data.map((d) => d[ARM]))];\nconst armColor = Object.fromEntries(arms.map((arm, i) => [arm, t.palette[i]]));\n\n// Event types: the known vocabulary in clinical order, then any other type in order of appearance\nconst presentTypes = [...new Set(data.map((d) => d[EVENT_TYPE]).filter((v) => v != null))];\nconst eventTypes = [\n  ...Object.keys(EVENT_SHAPE).filter((type) => presentTypes.includes(type)),\n  ...presentTypes.filter((type) => !(type in EVENT_SHAPE)),\n];\nconst takenColors = [...Object.values(armColor), ...Object.values(EVENT_COLOR)];\nconst freeColors = t.palette.filter((c) => !takenColors.includes(c));\nconst freeTypes = eventTypes.filter((type) => !(type in EVENT_COLOR));\nconst eventColor = Object.fromEntries(\n  eventTypes.map((type) => [type, EVENT_COLOR[type] ?? freeColors[freeTypes.indexOf(type)]]),\n);\nconst eventPath = Object.fromEntries(eventTypes.map((type) => [type, SHAPE_PATH[EVENT_SHAPE[type] ?? \"square\"]]));\n\nconst durationMax = Math.max(...patients.map((p) => p.duration));\nconst timeUnit = DURATION.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst legendItems = [\n  ...arms.map((arm) => ({ key: `arm-${arm}`, label: arm, color: armColor[arm], path: null })),\n  ...eventTypes.map((type) => ({ key: `event-${type}`, label: type, color: eventColor[type], path: eventPath[type] })),\n];\n\nconst TITLE = \"swimmer-clinical-timeline · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${patients.length} patients across ${arms.length} ${ARM.toLowerCase()}s · sorted by ${DURATION.toLowerCase()} · arrow = ${ONGOING.toLowerCase()} at data cutoff`;\n\nfunction SwimmerLayer() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const band = yScale.bandwidth();\n  const r = Math.min(band * MARKER_SHARE, MARKER_MAX);\n\n  return (\n    <g>\n      {patients.map((p) =>\n        p.events.map((e, i) => (\n          <path\n            key={`${p.id}-${i}`}\n            d={eventPath[e[EVENT_TYPE]]}\n            transform={`translate(${xScale(e[EVENT_TIME])} ${(yScale(p.id) ?? 0) + band / 2}) scale(${r})`}\n            fill={eventColor[e[EVENT_TYPE]]}\n            stroke={e[EVENT_TYPE] === ONGOING ? \"none\" : t.pageBg}\n            strokeWidth={1.5}\n            vectorEffect=\"non-scaling-stroke\"\n          />\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 }}>\n      <Box sx={{ height: HEADER_H, pt: \"18px\", px: \"56px\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600, lineHeight: 1.25 }}>{TITLE}</Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: 15, mt: \"4px\" }}>{SUBTITLE}</Typography>\n        <Box sx={{ display: \"flex\", flexWrap: \"wrap\", gap: \"22px\", mt: \"14px\", alignItems: \"center\" }}>\n          {legendItems.map((item) => (\n            <Box key={item.key} sx={{ display: \"flex\", alignItems: \"center\", gap: \"7px\" }}>\n              <svg width={26} height={20} viewBox=\"-11 -10 26 20\">\n                {item.path ? (\n                  <path d={item.path} transform=\"scale(7.5)\" fill={item.color} />\n                ) : (\n                  <rect x={-10} y={-6} width={20} height={12} rx={2} fill={item.color} />\n                )}\n              </svg>\n              <Typography sx={{ color: t.inkSoft, fontSize: 15 }}>{item.label}</Typography>\n            </Box>\n          ))}\n        </Box>\n      </Box>\n\n      <BarChart\n        width={W}\n        height={H - HEADER_H}\n        layout=\"horizontal\"\n        skipAnimation\n        hideLegend\n        borderRadius={3}\n        grid={{ vertical: true }}\n        margin={{ top: 16, right: 40, bottom: 12, left: 24 }}\n        series={[\n          {\n            id: \"duration\",\n            label: DURATION,\n            data: patients.map((p) => p.duration),\n            valueFormatter: (v) => `${v} ${timeUnit}`,\n          },\n        ]}\n        xAxis={[\n          {\n            scaleType: \"linear\",\n            min: 0,\n            max: durationMax * (1 + X_PAD_SHARE),\n            label: DURATION,\n            height: 60,\n            tickNumber: 8,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"band\",\n            data: patients.map((p) => p.id),\n            colorMap: { type: \"ordinal\", values: patients.map((p) => p.id), colors: patients.map((p) => armColor[p.arm]) },\n            label: PATIENT,\n            width: 96,\n            categoryGapRatio: 0.32,\n            disableTicks: true,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      >\n        <SwimmerLayer />\n      </BarChart>\n    </Box>\n  );\n}\n"}