{"spec_id":"gantt-dependencies","library":"muix","language":"javascript","code":"// anyplot.ai\n// gantt-dependencies: Gantt Chart with Dependencies\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/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 DAY_MS = 86400000;\nconst TICK_TARGET = 7; // about this many date ticks across the schedule, at a whole-week step\nconst X_PAD_SHARE = 0.02; // time padding on both sides, as a share of the schedule span\nconst ARROW_STUB = 12; // px an arrow runs straight out of a bar end before it turns\nconst DEPENDENCY_SEPARATOR = \";\"; // separates several predecessors in one cell\nconst TASK_PREFIX = \"↳ \"; // marks a task row below its group header row\nconst GROUP_SPAN_LABEL = \"Group span (earliest start to latest end)\";\nconst DATE_LABEL = \"Date\";\nconst TITLE = \"gantt-dependencies · javascript · muix · anyplot.ai\";\nconst TITLE_H = 84;\nconst LEGEND_H = 44;\n\n// Columns: set these to your data's column names\nconst TASK = \"Task\"; // role: task\nconst START = \"Start\"; // role: start\nconst END = \"End\"; // role: end\nconst PHASE = \"Phase\"; // role: group\nconst DEPENDS_ON = \"Depends on\"; // role: depends_on\n\n// Data: replace this block with your own records, one object per row\n// Software release plan, four phases of dependent tasks; all links are finish-to-start\nconst data = [\n  { [TASK]: \"Gather requirements\", [PHASE]: \"Requirements\", [START]: \"2026-01-01\", [END]: \"2026-01-05\", [DEPENDS_ON]: \"\" },\n  { [TASK]: \"Stakeholder review\", [PHASE]: \"Requirements\", [START]: \"2026-01-05\", [END]: \"2026-01-08\", [DEPENDS_ON]: \"Gather requirements\" },\n  { [TASK]: \"Finalize scope\", [PHASE]: \"Requirements\", [START]: \"2026-01-08\", [END]: \"2026-01-10\", [DEPENDS_ON]: \"Stakeholder review\" },\n  { [TASK]: \"UI wireframes\", [PHASE]: \"Design\", [START]: \"2026-01-10\", [END]: \"2026-01-15\", [DEPENDS_ON]: \"Finalize scope\" },\n  { [TASK]: \"Database schema\", [PHASE]: \"Design\", [START]: \"2026-01-10\", [END]: \"2026-01-16\", [DEPENDS_ON]: \"Finalize scope\" },\n  { [TASK]: \"Architecture review\", [PHASE]: \"Design\", [START]: \"2026-01-16\", [END]: \"2026-01-18\", [DEPENDS_ON]: \"UI wireframes; Database schema\" },\n  { [TASK]: \"Backend API\", [PHASE]: \"Development\", [START]: \"2026-01-18\", [END]: \"2026-01-28\", [DEPENDS_ON]: \"Architecture review\" },\n  { [TASK]: \"Frontend UI\", [PHASE]: \"Development\", [START]: \"2026-01-18\", [END]: \"2026-01-30\", [DEPENDS_ON]: \"Architecture review\" },\n  { [TASK]: \"Integration\", [PHASE]: \"Development\", [START]: \"2026-01-30\", [END]: \"2026-02-03\", [DEPENDS_ON]: \"Backend API; Frontend UI\" },\n  { [TASK]: \"Unit testing\", [PHASE]: \"Testing\", [START]: \"2026-02-03\", [END]: \"2026-02-06\", [DEPENDS_ON]: \"Integration\" },\n  { [TASK]: \"QA testing\", [PHASE]: \"Testing\", [START]: \"2026-02-06\", [END]: \"2026-02-10\", [DEPENDS_ON]: \"Unit testing\" },\n  { [TASK]: \"UAT sign-off\", [PHASE]: \"Testing\", [START]: \"2026-02-10\", [END]: \"2026-02-13\", [DEPENDS_ON]: \"QA testing\" },\n  { [TASK]: \"Deployment\", [PHASE]: \"Testing\", [START]: \"2026-02-13\", [END]: \"2026-02-14\", [DEPENDS_ON]: \"UAT sign-off\" },\n];\n\n// Plot: reads only data and the constants above\nconst tasks = data\n  .map((d) => ({\n    name: d[TASK],\n    phase: d[PHASE],\n    start: Date.parse(d[START]),\n    end: Date.parse(d[END]),\n    predecessors: String(d[DEPENDS_ON] ?? \"\")\n      .split(DEPENDENCY_SEPARATOR)\n      .map((s) => s.trim())\n      .filter((s) => s.length > 0),\n  }))\n  .filter((d) => Number.isFinite(d.start) && Number.isFinite(d.end));\nconst phases = [...new Set(tasks.map((d) => d.phase))];\n\n// Rows top to bottom: each group header, then its tasks by start date\nconst rows = phases.flatMap((phase, phaseIndex) => {\n  const members = tasks.filter((d) => d.phase === phase).sort((a, b) => a.start - b.start);\n  return [\n    {\n      label: phase,\n      kind: \"group\",\n      phaseIndex,\n      start: Math.min(...members.map((d) => d.start)),\n      end: Math.max(...members.map((d) => d.end)),\n    },\n    ...members.map((d) => ({ label: `${TASK_PREFIX}${d.name}`, kind: \"task\", phaseIndex, start: d.start, end: d.end })),\n  ];\n});\nconst rowByTask = new Map(tasks.map((d) => [d.name, rows.find((r) => r.label === `${TASK_PREFIX}${d.name}`)]));\n\n// Finish-to-start links: predecessor end (right edge) to successor start (left edge)\nconst edges = tasks.flatMap((d) =>\n  d.predecessors\n    .filter((p) => rowByTask.has(p))\n    .map((p) => ({ from: rowByTask.get(p), to: rowByTask.get(d.name) })),\n);\n\nconst firstStart = Math.min(...rows.map((r) => r.start));\nconst lastEnd = Math.max(...rows.map((r) => r.end));\nconst xPad = (lastEnd - firstStart) * X_PAD_SHARE;\nconst tickStep = 7 * DAY_MS * Math.max(1, Math.round((lastEnd - firstStart) / TICK_TARGET / (7 * DAY_MS)));\nconst ticks = Array.from(\n  { length: Math.floor((lastEnd - firstStart) / tickStep) + 1 },\n  (_, i) => firstStart + i * tickStep,\n);\nconst dayFormat = new Intl.DateTimeFormat(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" });\nconst fullDateFormat = new Intl.DateTimeFormat(\"en-US\", { year: \"numeric\", month: \"short\", day: \"numeric\", timeZone: \"UTC\" });\nconst subtitle = `${tasks.length} tasks in ${phases.length} groups · ${edges.length} finish-to-start dependencies, arrow from predecessor end to successor start · ${fullDateFormat.format(firstStart)} – ${fullDateFormat.format(lastEnd)}`;\n\n// Floating bars: a transparent segment up to each row's start, stacked with the span of a group or a task\nconst spanLabels = rows.map((r) => `${dayFormat.format(r.start)} – ${dayFormat.format(r.end)}`);\nconst series = [\n  { id: \"offset\", stack: \"schedule\", color: \"transparent\", data: rows.map((r) => r.start), valueFormatter: () => null },\n  {\n    id: \"group-span\",\n    label: GROUP_SPAN_LABEL,\n    stack: \"schedule\",\n    color: t.inkSoft,\n    data: rows.map((r) => (r.kind === \"group\" ? r.end - r.start : null)),\n    valueFormatter: (v: number | null, { dataIndex }: { dataIndex: number }) => (v === null ? null : spanLabels[dataIndex]),\n  },\n  ...phases.map((phase, i) => ({\n    id: `phase-${i}`,\n    label: phase,\n    stack: \"schedule\",\n    color: t.palette[i],\n    data: rows.map((r) => (r.kind === \"task\" && r.phaseIndex === i ? r.end - r.start : null)),\n    valueFormatter: (v: number | null, { dataIndex }: { dataIndex: number }) => (v === null ? null : spanLabels[dataIndex]),\n  })),\n];\n\nfunction DependencyArrows() {\n  const xScale = useXScale() as unknown as (v: number) => number;\n  const yScale = useYScale() as unknown as ((v: string) => number) & { bandwidth: () => number; step: () => number };\n  const band = yScale.bandwidth();\n  const gap = yScale.step() - band;\n  return (\n    <g>\n      <defs>\n        <marker id=\"dependency-arrowhead\" markerUnits=\"userSpaceOnUse\" markerWidth=\"10\" markerHeight=\"10\" refX=\"9\" refY=\"5\" orient=\"auto\">\n          <path d=\"M0,0 L10,5 L0,10 Z\" fill={t.ink} />\n        </marker>\n      </defs>\n      {edges.map((edge) => {\n        const fromX = xScale(edge.from.end);\n        const fromY = yScale(edge.from.label) + band / 2;\n        const toX = xScale(edge.to.start);\n        const toY = yScale(edge.to.label) + band / 2;\n        // Out of the predecessor's end into the gap below its row, across to just left of the\n        // successor's start, then down and into its left edge, clear of bars that start with it.\n        // When a bar on a row in between covers that vertical leg, the leg runs right of the latest\n        // bar end among those rows and turns back left in the gap next to the successor's row.\n        const gapY = yScale(edge.from.label) + band + gap / 2;\n        const legX = toX - ARROW_STUB;\n        const between = rows.filter((r) => {\n          const y = yScale(r.label) + band / 2;\n          return y > Math.min(gapY, toY) && y < Math.max(gapY, toY);\n        });\n        const blocked = between.some((r) => xScale(r.start) <= legX && legX <= xScale(r.end));\n        const approachY = toY > gapY ? yScale(edge.to.label) - gap / 2 : yScale(edge.to.label) + band + gap / 2;\n        const route = blocked\n          ? `H ${Math.max(...between.map((r) => xScale(r.end))) + ARROW_STUB} V ${approachY} H ${legX}`\n          : `H ${legX}`;\n        const d = `M ${fromX} ${fromY} H ${fromX + ARROW_STUB} V ${gapY} ${route} V ${toY} H ${toX}`;\n        return (\n          <path\n            key={`${edge.from.label}->${edge.to.label}`}\n            d={d}\n            stroke={t.ink}\n            strokeWidth={1.75}\n            strokeLinejoin=\"round\"\n            fill=\"none\"\n            markerEnd=\"url(#dependency-arrowhead)\"\n          />\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  return (\n    <Box sx={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: TITLE_H, pt: \"20px\", textAlign: \"center\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.2 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, mt: \"6px\" }}>{subtitle}</Typography>\n      </Box>\n      <BarChart\n        width={width}\n        height={height - TITLE_H - LEGEND_H}\n        layout=\"horizontal\"\n        skipAnimation\n        series={series}\n        grid={{ vertical: true }}\n        margin={{ top: 12, right: 56, bottom: 12, left: 24 }}\n        xAxis={[\n          {\n            min: firstStart - xPad,\n            max: lastEnd + xPad,\n            tickInterval: ticks,\n            valueFormatter: (v: number) => dayFormat.format(v),\n            label: DATE_LABEL,\n            height: 64,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"band\",\n            data: rows.map((r) => r.label),\n            width: 230,\n            categoryGapRatio: 0.32,\n            disableTicks: true,\n            tickLabelStyle: { fontSize: 14, fill: t.ink },\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"center\" },\n            sx: { fontSize: 15, color: t.inkSoft, mb: \"8px\" },\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        <DependencyArrows />\n      </BarChart>\n    </Box>\n  );\n}\n"}