{"spec_id":"campbell-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// campbell-basic: Campbell Diagram\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 88/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { LinePlot } from \"@mui/x-charts/LineChart\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport { useDrawingArea, useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst ORDER_DASHES = [\"12 6\", \"7 5\", \"3 4\"]; // one step per engine order, in order\nconst ORDER_OPACITIES = [0.9, 0.75, 0.65];\nconst RPM_PER_HZ = 60;\nconst CRITICAL_PALETTE_IDX = 4; // red marks the critical speeds; the modes take the other positions\nconst Y_PAD_SHARE = 0.08;\nconst ORDER_LABEL_SHARE = 0.94; // order labels sit at this share of the speed range\nconst END_LABEL_CHAR_PX = 8.4; // width of one 14 px semibold character\nconst HEADER_H = 122;\n\n// Columns: set these to your data's column names\nconst SPEED = \"Rotational speed (RPM)\"; // role: speed\nconst MODE = \"Mode shape\"; // role: extra\nconst FREQ = \"Natural frequency (Hz)\"; // role: frequency_mode\n\n// Data: replace this block with your own records, one object per row\nconst engineOrders = [1, 2, 3];\nconst data = (() => {\n  const modeSpecs = [\n    { name: \"1st Bending\", base: 42, slope: 0.0027 },\n    { name: \"1st Torsional\", base: 88, slope: 0.0008 },\n    { name: \"2nd Bending\", base: 125, slope: -0.0025 },\n    { name: \"Axial\", base: 152, slope: -0.0012 },\n  ];\n  const rows: { [key: string]: string | number }[] = [];\n  for (const m of modeSpecs) {\n    for (let s = 0; s <= 6000; s += 100) {\n      rows.push({\n        [SPEED]: s,\n        [MODE]: m.name,\n        [FREQ]: Math.round((m.base + m.slope * s) * 100) / 100,\n      });\n    }\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst modeNames = Array.from(new Set(data.map((d) => String(d[MODE]))));\nconst speeds = Array.from(new Set(data.map((d) => Number(d[SPEED])))).sort(\n  (a, b) => a - b,\n);\nconst speedMin = speeds[0];\nconst speedMax = speeds[speeds.length - 1];\nconst modePalette = t.palette.filter((_, i) => i !== CRITICAL_PALETTE_IDX);\nconst criticalColor = t.palette[CRITICAL_PALETTE_IDX];\n\nconst modeSeries = modeNames.map((name, idx) => {\n  const bySpeed = new Map(\n    data\n      .filter((d) => String(d[MODE]) === name)\n      .map((r) => [Number(r[SPEED]), Number(r[FREQ])]),\n  );\n  return {\n    id: `mode-${idx}`,\n    label: name,\n    color: modePalette[idx],\n    values: speeds.map((s) => bySpeed.get(s) ?? null),\n  };\n});\n\nconst orderCurves = engineOrders.map((order, idx) => ({\n  id: `eo-${idx}`,\n  order,\n  label: `${order}x EO`,\n  shortLabel: `${order}x`,\n  dash: ORDER_DASHES[idx],\n  opacity: ORDER_OPACITIES[idx],\n  values: speeds.map((s) => (order * s) / RPM_PER_HZ),\n}));\n\nconst criticalSpeeds: {\n  id: string;\n  speed: number;\n  freq: number;\n}[] = [];\n\nfor (const mode of modeSeries) {\n  for (const order of orderCurves) {\n    for (let i = 0; i < speeds.length - 1; i += 1) {\n      const mA = mode.values[i];\n      const mB = mode.values[i + 1];\n      if (mA === null || mB === null) continue;\n      const diffA = mA - order.values[i];\n      const diffB = mB - order.values[i + 1];\n      if (diffA === 0 || diffA * diffB < 0) {\n        const frac = diffA / (diffA - diffB);\n        criticalSpeeds.push({\n          id: `${mode.id}-${order.id}-${i}`,\n          speed: speeds[i] + frac * (speeds[i + 1] - speeds[i]),\n          freq: mA + frac * (mB - mA),\n        });\n      }\n    }\n  }\n}\n\nconst allFreqs = [\n  ...data.map((d) => Number(d[FREQ])),\n  ...orderCurves.flatMap((o) => o.values),\n];\nconst freqMax = Math.max(...allFreqs) * (1 + Y_PAD_SHARE);\nconst endLabelWidth =\n  Math.max(...modeNames.map((name) => name.length)) * END_LABEL_CHAR_PX + 32;\n\nconst TITLE = \"campbell-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${modeSeries.length} rotor modes · ${engineOrders.length} engine orders (${orderCurves.map((o) => o.shortLabel).join(\", \")}) · ${criticalSpeeds.length} critical-speed intersections`;\n\nfunction CampbellLabels() {\n  const xScale = useXScale(\"speed\");\n  const yScale = useYScale(\"freq\");\n  const { left, width } = useDrawingArea();\n  const labelSpeed = speedMin + (speedMax - speedMin) * ORDER_LABEL_SHARE;\n  const lineEnd = left + width;\n\n  return (\n    <g>\n      {orderCurves.map((o) => {\n        const lx = xScale(labelSpeed);\n        const ly = yScale((o.order * labelSpeed) / RPM_PER_HZ) - 10;\n        return (\n          <g key={o.id}>\n            <rect\n              x={lx - 18}\n              y={ly - 11}\n              width={36}\n              height={20}\n              rx={4}\n              fill={t.pageBg}\n              fillOpacity={0.88}\n            />\n            <text\n              x={lx}\n              y={ly}\n              textAnchor=\"middle\"\n              dominantBaseline=\"central\"\n              fontSize={14}\n              fontWeight={600}\n              fill={t.inkSoft}\n            >\n              {o.shortLabel}\n            </text>\n          </g>\n        );\n      })}\n\n      {modeSeries.map((m) => {\n        const endVal = m.values[m.values.length - 1];\n        if (endVal === null) return null;\n        const ly = yScale(endVal);\n        return (\n          <g key={m.id}>\n            <line\n              x1={lineEnd + 6}\n              x2={lineEnd + 18}\n              y1={ly}\n              y2={ly}\n              stroke={m.color}\n              strokeWidth={3.2}\n              strokeLinecap=\"round\"\n            />\n            <text\n              x={lineEnd + 24}\n              y={ly}\n              dominantBaseline=\"central\"\n              fontSize={14}\n              fontWeight={600}\n              fill={t.ink}\n            >\n              {m.label}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const chartHeight = height - HEADER_H;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        background: t.pageBg,\n        fontFamily: \"Inter, system-ui, sans-serif\",\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 8,\n          paddingTop: 6,\n        }}\n      >\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>\n          {TITLE}\n        </div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{SUBTITLE}</div>\n        <div\n          style={{\n            display: \"flex\",\n            flexWrap: \"wrap\",\n            gap: 22,\n            alignItems: \"center\",\n            fontSize: 14,\n            color: t.inkSoft,\n          }}\n        >\n          {modeSeries.map((m) => (\n            <div\n              key={m.id}\n              style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}\n            >\n              <span\n                style={{\n                  width: 24,\n                  height: 3.5,\n                  background: m.color,\n                  borderRadius: 2,\n                  display: \"inline-block\",\n                }}\n              />\n              <span style={{ color: t.ink, fontWeight: 500 }}>{m.label}</span>\n            </div>\n          ))}\n          {orderCurves.map((o) => (\n            <div\n              key={o.id}\n              style={{ display: \"flex\", alignItems: \"center\", gap: 6 }}\n            >\n              <svg width={28} height={10}>\n                <line\n                  x1={0}\n                  y1={5}\n                  x2={28}\n                  y2={5}\n                  stroke={t.inkSoft}\n                  strokeWidth={2}\n                  strokeDasharray={o.dash}\n                  opacity={o.opacity}\n                />\n              </svg>\n              <span>{o.label}</span>\n            </div>\n          ))}\n          <div style={{ display: \"flex\", alignItems: \"center\", gap: 7 }}>\n            <svg width={20} height={20}>\n              <circle\n                cx={10}\n                cy={10}\n                r={8.5}\n                fill={criticalColor}\n                stroke={t.pageBg}\n                strokeWidth={2}\n              />\n            </svg>\n            <span style={{ color: t.ink, fontWeight: 500 }}>\n              Critical Speeds ({criticalSpeeds.length})\n            </span>\n          </div>\n        </div>\n      </div>\n\n      <ChartsContainer\n        width={width}\n        height={chartHeight}\n        skipAnimation\n        margin={{ top: 20, right: endLabelWidth, bottom: 20, left: 40 }}\n        series={[\n          ...orderCurves.map((o) => ({\n            type: \"line\",\n            id: o.id,\n            label: o.label,\n            data: o.values,\n            color: t.inkSoft,\n            showMark: false,\n            curve: \"linear\",\n          })),\n          ...modeSeries.map((m) => ({\n            type: \"line\",\n            id: m.id,\n            label: m.label,\n            data: m.values,\n            color: m.color,\n            showMark: false,\n            curve: \"linear\",\n          })),\n          {\n            type: \"scatter\",\n            id: \"critical\",\n            label: \"Critical Speeds\",\n            data: criticalSpeeds.map((cs) => ({\n              id: cs.id,\n              x: cs.speed,\n              y: cs.freq,\n            })),\n            color: criticalColor,\n            markerSize: 8.5,\n          },\n        ]}\n        xAxis={[\n          {\n            id: \"speed\",\n            scaleType: \"linear\",\n            data: speeds,\n            min: speedMin,\n            max: speedMax,\n            tickNumber: 7,\n            height: 64,\n            label: SPEED,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"freq\",\n            min: 0,\n            max: freqMax,\n            tickNumber: 8,\n            width: 88,\n            label: FREQ,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\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\": {\n            stroke: t.grid,\n            opacity: 0.65,\n            strokeDasharray: \"3 4\",\n          },\n          \"& .MuiLineChart-line\": {\n            strokeWidth: 3.2,\n            strokeLinecap: \"round\",\n            strokeLinejoin: \"round\",\n          },\n          ...Object.fromEntries(\n            orderCurves.map((o) => [\n              `& .MuiLineChart-line[data-series=\"${o.id}\"]`,\n              {\n                strokeWidth: 2,\n                strokeDasharray: o.dash,\n                strokeLinecap: \"butt\",\n                opacity: o.opacity,\n              },\n            ]),\n          ),\n          \"& .MuiScatterChart-marker\": { stroke: t.pageBg, strokeWidth: 2.2 },\n        }}\n      >\n        <ChartsGrid horizontal vertical />\n        <LinePlot />\n        <ScatterPlot />\n        <CampbellLabels />\n        <ChartsXAxis axisId=\"speed\" />\n        <ChartsYAxis axisId=\"freq\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}