{"spec_id":"arc-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// arc-basic: Basic Arc Diagram\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 84/100 | Updated: 2026-10-24\nimport * as React from \"react\";\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 NODE_RADIUS = 8;\nconst X_PAD_SHARE = 0.04;\nconst Y_DOMAIN_MAX = 1.05;\nconst MIN_ARC_HEIGHT = 0.12;\nconst MAX_ARC_SPAN = 0.88;\nconst STROKE_MIN_PX = 2;\nconst STROKE_MAX_PX = 3.8;\nconst STROKE_ALPHA_MIN = 0.7;\nconst STROKE_ALPHA_MAX = 0.9;\nconst SEMANTIC_COLORS: Record<string, string> = {\n  Activates: t.palette[0],\n  Represses: t.palette[4],\n};\n\n// Columns: set these to your data's column names\nconst SOURCE = \"Source gene\"; // role: nodes\nconst TARGET = \"Target gene\"; // role: edges\nconst WEIGHT = \"Interaction strength\"; // role: weights\nconst INTERACTION_TYPE = \"Regulation type\"; // role: extra\n\n// Data: replace this block with your own records, one object per row\nconst data = [\n  { [SOURCE]: \"FOXP2\", [TARGET]: \"GATA3\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"GATA3\", [TARGET]: \"MYC\", [WEIGHT]: 3, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"MYC\", [TARGET]: \"KRAS\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"KRAS\", [TARGET]: \"EGFR\", [WEIGHT]: 3, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"EGFR\", [TARGET]: \"PIK3CA\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"PIK3CA\", [TARGET]: \"PTEN\", [WEIGHT]: 4, [INTERACTION_TYPE]: \"Represses\" },\n  { [SOURCE]: \"PTEN\", [TARGET]: \"TP53\", [WEIGHT]: 3, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"TP53\", [TARGET]: \"ATM\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"ATM\", [TARGET]: \"BRCA1\", [WEIGHT]: 3, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"BRCA1\", [TARGET]: \"BRCA2\", [WEIGHT]: 4, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"BRCA2\", [TARGET]: \"CDK4\", [WEIGHT]: 1, [INTERACTION_TYPE]: \"Represses\" },\n  { [SOURCE]: \"CDK4\", [TARGET]: \"ESR1\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"ESR1\", [TARGET]: \"NOTCH1\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Represses\" },\n  { [SOURCE]: \"NOTCH1\", [TARGET]: \"AKT1\", [WEIGHT]: 3, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"AKT1\", [TARGET]: \"VEGFA\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"VEGFA\", [TARGET]: \"APOE\", [WEIGHT]: 1, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"APOE\", [TARGET]: \"ACE2\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"MYC\", [TARGET]: \"TP53\", [WEIGHT]: 3, [INTERACTION_TYPE]: \"Represses\" },\n  { [SOURCE]: \"PIK3CA\", [TARGET]: \"AKT1\", [WEIGHT]: 4, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"TP53\", [TARGET]: \"BRCA1\", [WEIGHT]: 3, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"BRCA1\", [TARGET]: \"ACE2\", [WEIGHT]: 1, [INTERACTION_TYPE]: \"Represses\" },\n  { [SOURCE]: \"FOXP2\", [TARGET]: \"ACE2\", [WEIGHT]: 1, [INTERACTION_TYPE]: \"Activates\" },\n  { [SOURCE]: \"KRAS\", [TARGET]: \"BRCA2\", [WEIGHT]: 2, [INTERACTION_TYPE]: \"Represses\" },\n];\n\n// Plot: reads only data and the constants above\nconst nodes = [\n  ...new Set([...data.map((d) => d[SOURCE]), ...data.map((d) => d[TARGET])]),\n];\nconst nodeIndex = Object.fromEntries(nodes.map((n, i) => [n, i]));\nconst maxDistance = nodes.length - 1;\n\nconst types = [...new Set(data.map((d) => d[INTERACTION_TYPE]))];\nconst usedColors = new Set(\n  types.map((k) => SEMANTIC_COLORS[k]).filter(Boolean),\n);\nconst unusedPalette = t.palette.filter((c) => !usedColors.has(c));\nlet unusedIdx = 0;\nconst typeColor: Record<string, string> = {};\nconst typeDash: Record<string, string | undefined> = {};\ntypes.forEach((k, i) => {\n  typeColor[k] = SEMANTIC_COLORS[k] ?? unusedPalette[unusedIdx++];\n  typeDash[k] = i % 2 === 1 ? \"6 4\" : undefined;\n});\n\nconst weights = data.map((d) => d[WEIGHT]);\nconst minWeight = Math.min(...weights);\nconst maxWeight = Math.max(...weights);\nconst weightSpan = Math.max(1e-9, maxWeight - minWeight);\nconst xPad = maxDistance * X_PAD_SHARE;\n\nfunction ArcLayer() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const baselineY = yScale(0);\n  return (\n    <g data-drawing-container>\n      {data.map((row, i) => {\n        const a = nodeIndex[row[SOURCE]];\n        const b = nodeIndex[row[TARGET]];\n        const strength = (row[WEIGHT] - minWeight) / weightSpan;\n        const kind = row[INTERACTION_TYPE];\n        const x1 = xScale(a);\n        const x2 = xScale(b);\n        const archH =\n          MIN_ARC_HEIGHT + MAX_ARC_SPAN * (Math.abs(b - a) / maxDistance);\n        const apexY = yScale(archH);\n        const controlY = baselineY - 2 * (baselineY - apexY);\n        return (\n          <path\n            key={`edge-${i}`}\n            d={`M ${x1} ${baselineY} Q ${(x1 + x2) / 2} ${controlY} ${x2} ${baselineY}`}\n            fill=\"none\"\n            stroke={typeColor[kind]}\n            strokeWidth={STROKE_MIN_PX + strength * (STROKE_MAX_PX - STROKE_MIN_PX)}\n            strokeOpacity={\n              STROKE_ALPHA_MIN + strength * (STROKE_ALPHA_MAX - STROKE_ALPHA_MIN)\n            }\n            strokeDasharray={typeDash[kind]}\n          />\n        );\n      })}\n      {nodes.map((name, i) => {\n        const x = xScale(i);\n        const labelX = x - 4;\n        const labelY = baselineY + NODE_RADIUS + 18;\n        return (\n          <React.Fragment key={name}>\n            <circle\n              cx={x}\n              cy={baselineY}\n              r={NODE_RADIUS}\n              fill={t.palette[0]}\n              stroke={t.pageBg}\n              strokeWidth={1.5}\n            />\n            <text\n              x={labelX}\n              y={labelY}\n              textAnchor=\"end\"\n              fontSize={16}\n              fontWeight={500}\n              fill={t.inkSoft}\n              transform={`rotate(-40 ${labelX} ${labelY})`}\n            >\n              {name}\n            </text>\n          </React.Fragment>\n        );\n      })}\n    </g>\n  );\n}\n\nconst TITLE = \"arc-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${nodes.length} nodes · ${data.length} arcs · ${WEIGHT}: ${minWeight}–${maxWeight}`;\nconst HEADER_H = 100;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily:\n          '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif',\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_H,\n          padding: \"14px 50px 0 50px\",\n          display: \"flex\",\n          flexDirection: \"column\",\n          boxSizing: \"border-box\",\n        }}\n      >\n        <div\n          style={{\n            fontSize: 22,\n            fontWeight: 600,\n            color: t.ink,\n            lineHeight: 1.25,\n          }}\n        >\n          {TITLE}\n        </div>\n        <div\n          style={{\n            fontSize: 15,\n            color: t.inkSoft,\n            marginTop: 4,\n          }}\n        >\n          {SUBTITLE}\n        </div>\n        <div\n          style={{\n            display: \"flex\",\n            alignItems: \"center\",\n            gap: 22,\n            marginTop: 6,\n            fontSize: 14,\n            color: t.inkSoft,\n          }}\n        >\n          {types.map((kind) => (\n            <span\n              key={kind}\n              style={{ display: \"inline-flex\", alignItems: \"center\", gap: 7 }}\n            >\n              <svg width=\"20\" height=\"4\" style={{ display: \"inline-block\" }}>\n                <line\n                  x1=\"0\"\n                  y1=\"2\"\n                  x2=\"20\"\n                  y2=\"2\"\n                  stroke={typeColor[kind]}\n                  strokeWidth=\"3\"\n                  strokeDasharray={typeDash[kind]}\n                />\n              </svg>\n              <span>{kind}</span>\n            </span>\n          ))}\n          <span>· Arc height ∝ sequence distance · Stroke width ∝ {WEIGHT}</span>\n        </div>\n      </div>\n      <ChartsContainer\n        width={width}\n        height={height - HEADER_H}\n        series={[]}\n        margin={{ top: 20, bottom: 110, left: 60, right: 50 }}\n        xAxis={[\n          {\n            id: \"x\",\n            scaleType: \"linear\",\n            min: -xPad,\n            max: maxDistance + xPad,\n            position: \"none\",\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"y\",\n            scaleType: \"linear\",\n            min: 0,\n            max: Y_DOMAIN_MAX,\n            position: \"none\",\n          },\n        ]}\n        skipAnimation\n      >\n        <ArcLayer />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}