{"spec_id":"network-transport-static","library":"muix","language":"javascript","code":"// anyplot.ai\n// network-transport-static: Static Transport Network Diagram\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 { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst FONT = \"Inter, system-ui, -apple-system, sans-serif\";\nconst TITLE = \"network-transport-static · javascript · muix · anyplot.ai\";\nconst TITLE_H = 54;\nconst LEGEND_H = 36;\nconst CAPTION_H = 32;\nconst HUB_RADIUS = 46;\nconst BASE_STATION_RADIUS = 17;\nconst DEGREE_RADIUS_STEP = 1.4;\nconst CODE_FONT_SIZE = 14;\nconst CODE_CHAR_W = 11;\nconst CODE_CLEARANCE = 6;\nconst PLOT_MARGIN = { top: 64, right: 130, bottom: 64, left: 130 };\nconst DEFAULT_BOW_PX = 18;\nconst ROUTE_TYPE_STYLES: Record<\n  string,\n  { dashed: boolean; bowShare?: number; strokeWidth: number; labelT: number }\n> = {\n  Express: { dashed: true, bowShare: 0.28, strokeWidth: 2.5, labelT: 0.5 },\n};\nconst DEFAULT_ROUTE_STYLE = { dashed: false, strokeWidth: 2, labelT: 0.54 };\n\n// Columns: set these to your data's column names\nconst STATION_ID = \"Station code\"; // role: node\nconst STATION_NAME = \"Station name\"; // role: label\nconst STATION_X = \"East-west offset (km)\"; // role: x\nconst STATION_Y = \"North-south offset (km)\"; // role: y\nconst ROUTE_ID = \"Route ID\"; // role: id\nconst ROUTE_TYPE = \"Service type\"; // role: group\nconst DEP_TIME = \"Departure\"; // role: start\nconst ARR_TIME = \"Arrival\"; // role: end\nconst SOURCE = \"Origin station\"; // role: source\nconst TARGET = \"Destination station\"; // role: target\n\n// Data: replace this block with your own records\nconst stations: Array<Record<string, string | number>> = [\n  { [STATION_ID]: \"CTR\", [STATION_NAME]: \"Central\", [STATION_X]: 0, [STATION_Y]: 0 },\n  { [STATION_ID]: \"NJC\", [STATION_NAME]: \"North Jct.\", [STATION_X]: 0, [STATION_Y]: 2.4 },\n  { [STATION_ID]: \"EPT\", [STATION_NAME]: \"Eastport\", [STATION_X]: 2.8, [STATION_Y]: 0 },\n  { [STATION_ID]: \"WFD\", [STATION_NAME]: \"Westfield\", [STATION_X]: -2.8, [STATION_Y]: 0 },\n  { [STATION_ID]: \"SGT\", [STATION_NAME]: \"Southgate\", [STATION_X]: 0, [STATION_Y]: -2.6 },\n  { [STATION_ID]: \"UPT\", [STATION_NAME]: \"Uptown\", [STATION_X]: 0.9, [STATION_Y]: 4.6 },\n  { [STATION_ID]: \"HBS\", [STATION_NAME]: \"Harborside\", [STATION_X]: 5.6, [STATION_Y]: 0 },\n  { [STATION_ID]: \"WBK\", [STATION_NAME]: \"Westbrook\", [STATION_X]: -5.4, [STATION_Y]: 0.9 },\n  { [STATION_ID]: \"SPK\", [STATION_NAME]: \"Southpark\", [STATION_X]: 0.7, [STATION_Y]: -5.0 },\n];\n\nconst data: Array<Record<string, string>> = [\n  { [ROUTE_ID]: \"R10\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:05\", [ARR_TIME]: \"07:12\", [SOURCE]: \"CTR\", [TARGET]: \"NJC\" },\n  { [ROUTE_ID]: \"R11\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:20\", [ARR_TIME]: \"07:27\", [SOURCE]: \"NJC\", [TARGET]: \"CTR\" },\n  { [ROUTE_ID]: \"R20\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:00\", [ARR_TIME]: \"07:14\", [SOURCE]: \"CTR\", [TARGET]: \"EPT\" },\n  { [ROUTE_ID]: \"R21\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:20\", [ARR_TIME]: \"07:34\", [SOURCE]: \"EPT\", [TARGET]: \"CTR\" },\n  { [ROUTE_ID]: \"R30\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:05\", [ARR_TIME]: \"07:17\", [SOURCE]: \"CTR\", [TARGET]: \"WFD\" },\n  { [ROUTE_ID]: \"R31\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:25\", [ARR_TIME]: \"07:37\", [SOURCE]: \"WFD\", [TARGET]: \"CTR\" },\n  { [ROUTE_ID]: \"R40\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:10\", [ARR_TIME]: \"07:19\", [SOURCE]: \"CTR\", [TARGET]: \"SGT\" },\n  { [ROUTE_ID]: \"R41\", [ROUTE_TYPE]: \"Regional\", [DEP_TIME]: \"07:25\", [ARR_TIME]: \"07:34\", [SOURCE]: \"SGT\", [TARGET]: \"CTR\" },\n  { [ROUTE_ID]: \"L12\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:15\", [ARR_TIME]: \"07:26\", [SOURCE]: \"NJC\", [TARGET]: \"UPT\" },\n  { [ROUTE_ID]: \"L13\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:35\", [ARR_TIME]: \"07:46\", [SOURCE]: \"UPT\", [TARGET]: \"NJC\" },\n  { [ROUTE_ID]: \"L22\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:18\", [ARR_TIME]: \"07:33\", [SOURCE]: \"EPT\", [TARGET]: \"HBS\" },\n  { [ROUTE_ID]: \"L23\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:40\", [ARR_TIME]: \"07:55\", [SOURCE]: \"HBS\", [TARGET]: \"EPT\" },\n  { [ROUTE_ID]: \"L32\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:20\", [ARR_TIME]: \"07:34\", [SOURCE]: \"WFD\", [TARGET]: \"WBK\" },\n  { [ROUTE_ID]: \"L33\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:42\", [ARR_TIME]: \"07:56\", [SOURCE]: \"WBK\", [TARGET]: \"WFD\" },\n  { [ROUTE_ID]: \"L42\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:22\", [ARR_TIME]: \"07:38\", [SOURCE]: \"SGT\", [TARGET]: \"SPK\" },\n  { [ROUTE_ID]: \"L43\", [ROUTE_TYPE]: \"Local\", [DEP_TIME]: \"07:45\", [ARR_TIME]: \"08:01\", [SOURCE]: \"SPK\", [TARGET]: \"SGT\" },\n  { [ROUTE_ID]: \"EX1\", [ROUTE_TYPE]: \"Express\", [DEP_TIME]: \"07:00\", [ARR_TIME]: \"07:28\", [SOURCE]: \"CTR\", [TARGET]: \"HBS\" },\n  { [ROUTE_ID]: \"EX2\", [ROUTE_TYPE]: \"Express\", [DEP_TIME]: \"07:35\", [ARR_TIME]: \"08:03\", [SOURCE]: \"HBS\", [TARGET]: \"CTR\" },\n];\n\n// Plot: reads only data and the constants above\nconst routeTypes: string[] = [];\nfor (const row of data) {\n  const rt = String(row[ROUTE_TYPE]);\n  if (!routeTypes.includes(rt)) routeTypes.push(rt);\n}\nconst typeColorMap: Record<string, string> = {};\nfor (let i = 0; i < routeTypes.length; i += 1) {\n  typeColorMap[routeTypes[i]] = t.palette[i];\n}\n\ninterface StationInfo {\n  id: string;\n  label: string;\n  x: number;\n  y: number;\n  degree: number;\n  radius: number;\n  isHub: boolean;\n  neighbors: string[];\n}\n\nconst stationById = new Map<string, StationInfo>();\nconst stationList: StationInfo[] = [];\n\nfor (const row of stations) {\n  const info: StationInfo = {\n    id: String(row[STATION_ID]),\n    label: String(row[STATION_NAME]),\n    x: Number(row[STATION_X]),\n    y: Number(row[STATION_Y]),\n    degree: 0,\n    radius: BASE_STATION_RADIUS,\n    isHub: false,\n    neighbors: [],\n  };\n  stationById.set(info.id, info);\n  stationList.push(info);\n}\n\ninterface RouteInfo {\n  routeId: string;\n  routeType: string;\n  departureTime: string;\n  arrivalTime: string;\n  sourceId: string;\n  targetId: string;\n}\n\nconst routes: RouteInfo[] = [];\nfor (const row of data) {\n  const sId = String(row[SOURCE]);\n  const tId = String(row[TARGET]);\n  const sInfo = stationById.get(sId)!;\n  const tInfo = stationById.get(tId)!;\n  sInfo.degree += 1;\n  tInfo.degree += 1;\n  if (!sInfo.neighbors.includes(tId)) sInfo.neighbors.push(tId);\n  if (!tInfo.neighbors.includes(sId)) tInfo.neighbors.push(sId);\n  routes.push({\n    routeId: String(row[ROUTE_ID]),\n    routeType: String(row[ROUTE_TYPE]),\n    departureTime: String(row[DEP_TIME]),\n    arrivalTime: String(row[ARR_TIME]),\n    sourceId: sId,\n    targetId: tId,\n  });\n}\n\nconst maxStationDegree = Math.max(...stationList.map((s) => s.degree));\nlet hubStation = stationList[0];\nfor (const station of stationList) {\n  station.isHub = station.degree === maxStationDegree;\n  if (station.isHub) hubStation = station;\n  station.radius = station.isHub\n    ? HUB_RADIUS\n    : Math.max(\n        BASE_STATION_RADIUS + station.degree * DEGREE_RADIUS_STEP,\n        (station.id.length * CODE_CHAR_W) / 2 + CODE_CLEARANCE,\n      );\n}\n\nconst xs = stationList.map((s) => s.x);\nconst ys = stationList.map((s) => s.y);\nconst xDomain = [Math.min(...xs), Math.max(...xs)];\nconst yDomain = [Math.min(...ys), Math.max(...ys)];\n\nfunction TransportNetworkOverlay() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n\n  const edges = routes.map((route) => {\n    const source = stationById.get(route.sourceId)!;\n    const target = stationById.get(route.targetId)!;\n    const sx = Number(xScale(source.x));\n    const sy = Number(yScale(source.y));\n    const tx = Number(xScale(target.x));\n    const ty = Number(yScale(target.y));\n    const dx = tx - sx;\n    const dy = ty - sy;\n    const length = Math.hypot(dx, dy);\n\n    const [aId, bId] =\n      route.sourceId < route.targetId\n        ? [route.sourceId, route.targetId]\n        : [route.targetId, route.sourceId];\n    const aStation = stationById.get(aId)!;\n    const bStation = stationById.get(bId)!;\n    const aPoint = { x: Number(xScale(aStation.x)), y: Number(yScale(aStation.y)) };\n    const bPoint = { x: Number(xScale(bStation.x)), y: Number(yScale(bStation.y)) };\n    const canonLength = Math.hypot(bPoint.x - aPoint.x, bPoint.y - aPoint.y);\n    const perpX = -(bPoint.y - aPoint.y) / canonLength;\n    const perpY = (bPoint.x - aPoint.x) / canonLength;\n    const sign = route.sourceId === aId ? -1 : 1;\n\n    const rStyle = ROUTE_TYPE_STYLES[route.routeType] ?? DEFAULT_ROUTE_STYLE;\n    const offset = rStyle.bowShare !== undefined ? length * rStyle.bowShare : DEFAULT_BOW_PX;\n    const startX = sx + (dx / length) * (source.radius + 2);\n    const startY = sy + (dy / length) * (source.radius + 2);\n    const endX = tx - (dx / length) * (target.radius + 5);\n    const endY = ty - (dy / length) * (target.radius + 5);\n    const midX = (startX + endX) / 2 + perpX * offset * sign;\n    const midY = (startY + endY) / 2 + perpY * offset * sign;\n\n    const lt = rStyle.labelT;\n    const curveX = (1 - lt) ** 2 * startX + 2 * (1 - lt) * lt * midX + lt ** 2 * endX;\n    const curveY = (1 - lt) ** 2 * startY + 2 * (1 - lt) * lt * midY + lt ** 2 * endY;\n    const text = `${route.routeId} ${route.departureTime}→${route.arrivalTime}`;\n    const boxW = text.length * 7.4 + 10;\n\n    return {\n      route,\n      rStyle,\n      d: `M ${startX} ${startY} Q ${midX} ${midY} ${endX} ${endY}`,\n      text,\n      boxW,\n      labelX: curveX + perpX * sign * (boxW * 0.5 + 10),\n      labelY: curveY + perpY * sign * 22,\n      tooltip: `${route.routeId}: ${source.label} → ${target.label} (${route.departureTime} → ${route.arrivalTime}, ${route.routeType})`,\n    };\n  });\n\n  return (\n    <g fontFamily={FONT}>\n      <defs>\n        {routeTypes.map((type) => (\n          <marker\n            key={type}\n            id={`arrow-${type}`}\n            markerWidth=\"9\"\n            markerHeight=\"9\"\n            refX=\"6.5\"\n            refY=\"3\"\n            orient=\"auto\"\n          >\n            <path d=\"M0,0 L7,3 L0,6 Z\" fill={typeColorMap[type]} />\n          </marker>\n        ))}\n      </defs>\n\n      {edges.map(({ route, rStyle, d, tooltip }) => (\n        <path\n          key={`edge-${route.routeId}`}\n          d={d}\n          fill=\"none\"\n          stroke={typeColorMap[route.routeType]}\n          strokeWidth={rStyle.strokeWidth}\n          strokeDasharray={rStyle.dashed ? \"9 6\" : undefined}\n          markerEnd={`url(#arrow-${route.routeType})`}\n        >\n          <title>{tooltip}</title>\n        </path>\n      ))}\n\n      {edges.map(({ route, text, boxW, labelX, labelY, tooltip }) => (\n        <g key={`label-${route.routeId}`}>\n          <rect\n            x={labelX - boxW / 2}\n            y={labelY - 11}\n            width={boxW}\n            height={20}\n            rx={3}\n            fill={t.elevatedBg}\n            opacity={0.94}\n          >\n            <title>{tooltip}</title>\n          </rect>\n          <text\n            x={labelX}\n            y={labelY}\n            fontSize={14}\n            fill={t.ink}\n            textAnchor=\"middle\"\n            dominantBaseline=\"middle\"\n          >\n            {text}\n          </text>\n        </g>\n      ))}\n\n      {stationList.map((station) => {\n        const cx = Number(xScale(station.x));\n        const cy = Number(yScale(station.y));\n        const r = station.radius;\n        const fill = station.isHub ? t.ink : t.elevatedBg;\n        const codeColor = station.isHub ? t.pageBg : t.ink;\n\n        if (station.isHub) {\n          return (\n            <g key={station.id}>\n              <circle cx={cx} cy={cy} r={r} fill={fill} stroke={t.ink} strokeWidth={2}>\n                <title>{`${station.label} — major interchange, ${station.degree} connecting routes`}</title>\n              </circle>\n              <text\n                x={cx}\n                y={cy}\n                fontSize={18}\n                fontWeight={600}\n                fill={codeColor}\n                textAnchor=\"middle\"\n                dominantBaseline=\"middle\"\n              >\n                {station.label}\n              </text>\n            </g>\n          );\n        }\n\n        const edgeAngles = station.neighbors.map((nid) => {\n          const nStation = stationById.get(nid)!;\n          return Math.atan2(Number(yScale(nStation.y)) - cy, Number(xScale(nStation.x)) - cx);\n        });\n        let labelAngle = Math.PI / 2;\n        if (edgeAngles.length > 0) {\n          edgeAngles.sort((a, b) => a - b);\n          let bestGap = -1;\n          for (let k = 0; k < edgeAngles.length; k += 1) {\n            const start = edgeAngles[k];\n            const end =\n              edgeAngles[(k + 1) % edgeAngles.length] +\n              (k === edgeAngles.length - 1 ? 2 * Math.PI : 0);\n            const gap = end - start;\n            if (gap > bestGap) {\n              bestGap = gap;\n              labelAngle = start + gap / 2;\n            }\n          }\n        }\n        const cosA = Math.cos(labelAngle);\n        const textAnchor = cosA > 0.35 ? \"start\" : cosA < -0.35 ? \"end\" : \"middle\";\n        const labelDist = r + (textAnchor === \"middle\" ? 20 : 13);\n        const lx = cx + Math.cos(labelAngle) * labelDist;\n        const ly = cy + Math.sin(labelAngle) * labelDist;\n\n        return (\n          <g key={station.id}>\n            <circle cx={cx} cy={cy} r={r} fill={fill} stroke={t.ink} strokeWidth={2}>\n              <title>{`${station.label} (${station.id}) — ${station.degree} connecting route${station.degree === 1 ? \"\" : \"s\"}`}</title>\n            </circle>\n            <text\n              x={cx}\n              y={cy}\n              fontSize={CODE_FONT_SIZE}\n              fontWeight={600}\n              fill={codeColor}\n              textAnchor=\"middle\"\n              dominantBaseline=\"middle\"\n            >\n              {station.id}\n            </text>\n            <text\n              x={lx}\n              y={ly}\n              fontSize={15}\n              fontWeight={400}\n              fill={t.ink}\n              textAnchor={textAnchor}\n              dominantBaseline=\"middle\"\n            >\n              {station.label}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const W = window.ANYPLOT_SIZE.width;\n  const H = window.ANYPLOT_SIZE.height;\n  const chartH = H - TITLE_H - LEGEND_H - CAPTION_H;\n\n  return (\n    <div\n      style={{\n        width: W,\n        height: H,\n        backgroundColor: t.pageBg,\n        fontFamily: FONT,\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <div style={{ height: TITLE_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\" }}>\n        <span style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</span>\n      </div>\n      <div\n        style={{\n          height: LEGEND_H,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 28,\n          fontSize: 14,\n          color: t.ink,\n        }}\n      >\n        <span style={{ color: t.inkSoft, fontWeight: 500 }}>{`${ROUTE_TYPE}:`}</span>\n        {routeTypes.map((type) => {\n          const rStyle = ROUTE_TYPE_STYLES[type] ?? DEFAULT_ROUTE_STYLE;\n          return (\n            <span key={type} style={{ display: \"inline-flex\", alignItems: \"center\", gap: 8 }}>\n              <svg width={30} height={10}>\n                <line\n                  x1={0}\n                  y1={5}\n                  x2={30}\n                  y2={5}\n                  stroke={typeColorMap[type]}\n                  strokeWidth={rStyle.strokeWidth + 0.5}\n                  strokeDasharray={rStyle.dashed ? \"7 4\" : undefined}\n                />\n              </svg>\n              <span>{type}</span>\n            </span>\n          );\n        })}\n      </div>\n      <ChartsContainer\n        width={W}\n        height={chartH}\n        skipAnimation\n        series={[]}\n        margin={PLOT_MARGIN}\n        xAxis={[{ min: xDomain[0], max: xDomain[1], scaleType: \"linear\", position: \"none\" }]}\n        yAxis={[{ min: yDomain[0], max: yDomain[1], scaleType: \"linear\", position: \"none\" }]}\n      >\n        <TransportNetworkOverlay />\n      </ChartsContainer>\n      <div style={{ height: CAPTION_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\" }}>\n        <span style={{ fontSize: 14, color: t.inkSoft }}>\n          {`Arrows show travel direction · labels show ${ROUTE_ID} and scheduled ${DEP_TIME} → ${ARR_TIME} · larger circle marks the ${hubStation.label} interchange`}\n        </span>\n      </div>\n    </div>\n  );\n}\n"}