{"spec_id":"mohr-circle","library":"muix","language":"javascript","code":"// anyplot.ai\n// mohr-circle: Mohr's Circle for Stress Analysis\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\n//# anyplot-orientation: square\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { ChartsText } from \"@mui/x-charts/ChartsText\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"Steel Drive Shaft, Bending + Torsion · mohr-circle · javascript · muix · anyplot.ai\";\nconst TITLE_FONT_SIZE = Math.max(16, Math.round((22 * 67) / Math.max(67, TITLE.length)));\nconst TITLE_HEIGHT = 64;\nconst X_AXIS_HEIGHT = 60;\nconst Y_AXIS_WIDTH = 90;\nconst PAD_RATIO = 0.68;\nconst ARC_RADIUS_RATIO = 0.32;\nconst ARC_STEPS = 32;\nconst X_AXIS_PREFIX = \"Normal Stress σ\";\nconst Y_AXIS_PREFIX = \"Shear Stress τ\";\n\n// Columns: set these to your data's column names\nconst SIGMA_X = \"Normal Stress σx (MPa)\"; // role: sigma_x\nconst SIGMA_Y = \"Normal Stress σy (MPa)\"; // role: sigma_y\nconst TAU_XY = \"Shear Stress τxy (MPa)\"; // role: tau_xy\n\n// Data: replace this block with your own records\n// Surface point of a steel drive shaft: bending stress σx, no transverse load, torsional shear τxy\nconst data: Array<Record<string, number>> = [\n  {\n    [SIGMA_X]: 74,\n    [SIGMA_Y]: 0,\n    [TAU_XY]: 32,\n  },\n];\n\n// Plot: reads only data and the constants above\nconst unitMatch = /\\(([^)]+)\\)/.exec(SIGMA_X);\nconst unit = unitMatch ? unitMatch[1] : \"\";\nconst unitSuffix = unit ? ` ${unit}` : \"\";\nconst xAxisLabel = unit ? `${X_AXIS_PREFIX} (${unit})` : X_AXIS_PREFIX;\nconst yAxisLabel = unit ? `${Y_AXIS_PREFIX} (${unit})` : Y_AXIS_PREFIX;\n\nconst stateRow = data[0];\nconst sigmaX = Number(stateRow[SIGMA_X]);\nconst sigmaY = Number(stateRow[SIGMA_Y]);\nconst tauXY = Number(stateRow[TAU_XY]);\n\nconst center = (sigmaX + sigmaY) / 2;\nconst radius = Math.sqrt(((sigmaX - sigmaY) / 2) ** 2 + tauXY ** 2);\nconst sigma1 = center + radius;\nconst sigma2 = center - radius;\nconst tauMax = radius;\nconst twoThetaPRad = Math.atan2(tauXY, (sigmaX - sigmaY) / 2);\nconst twoThetaPDeg = (twoThetaPRad * 180) / Math.PI;\n\nconst pad = radius * PAD_RATIO;\nconst xAxisMin = center - radius - pad;\nconst xAxisMax = center + radius + pad;\nconst yAxisMin = -radius - pad;\nconst yAxisMax = radius + pad;\n\nfunction MohrAnnotations() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n\n  const cx = Number(xScale(center));\n  const cy = Number(yScale(0));\n  const ax = Number(xScale(sigmaX));\n  const ay = Number(yScale(tauXY));\n  const bx = Number(xScale(sigmaY));\n  const by = Number(yScale(-tauXY));\n  const s1x = Number(xScale(sigma1));\n  const s1y = Number(yScale(0));\n  const s2x = Number(xScale(sigma2));\n  const s2y = Number(yScale(0));\n  const tmaxX = Number(xScale(center));\n  const tmaxY = Number(yScale(tauMax));\n  const tminX = Number(xScale(center));\n  const tminY = Number(yScale(-tauMax));\n\n  const circleRadiusPx = Math.abs(s1x - cx);\n  const arcRadius = radius * ARC_RADIUS_RATIO;\n\n  let arcPath = \"\";\n  for (let i = 0; i <= ARC_STEPS; i++) {\n    const angle = (twoThetaPRad * i) / ARC_STEPS;\n    const px = Number(xScale(center + arcRadius * Math.cos(angle)));\n    const py = Number(yScale(arcRadius * Math.sin(angle)));\n    arcPath += `${i === 0 ? \"M\" : \"L\"} ${px.toFixed(1)} ${py.toFixed(1)} `;\n  }\n\n  const angleLabelX = Number(xScale(center + arcRadius * 1.65 * Math.cos(twoThetaPRad / 2)));\n  const angleLabelY = Number(yScale(arcRadius * 1.65 * Math.sin(twoThetaPRad / 2)));\n\n  const stateColor = t.palette[1];\n  const circleColor = t.palette[0];\n  const landmarkColor = t.ink;\n\n  const principalPoints = [\n    { x: s1x, y: s1y },\n    { x: s2x, y: s2y },\n    { x: tmaxX, y: tmaxY },\n    { x: tminX, y: tminY },\n  ];\n  const statePoints = [\n    { x: ax, y: ay },\n    { x: bx, y: by },\n  ];\n\n  return (\n    <g>\n      <circle\n        cx={cx}\n        cy={cy}\n        r={circleRadiusPx}\n        fill={circleColor}\n        fillOpacity={0.07}\n        stroke={circleColor}\n        strokeWidth={3.5}\n      />\n\n      <line\n        x1={s2x}\n        y1={s2y}\n        x2={s1x}\n        y2={s1y}\n        stroke={landmarkColor}\n        strokeWidth={4}\n        strokeLinecap=\"round\"\n      />\n\n      <line\n        x1={ax}\n        y1={ay}\n        x2={bx}\n        y2={by}\n        stroke={t.ink}\n        strokeWidth={2}\n        strokeDasharray=\"7 6\"\n      />\n\n      <path d={arcPath.trim()} fill=\"none\" stroke={t.inkSoft} strokeWidth={2} />\n\n      <circle cx={cx} cy={cy} r={6} fill={landmarkColor} />\n\n      {principalPoints.map((p, i) => (\n        <circle\n          key={`p-${i}`}\n          cx={p.x}\n          cy={p.y}\n          r={10}\n          fill={landmarkColor}\n          stroke={t.pageBg}\n          strokeWidth={2.5}\n        />\n      ))}\n\n      {statePoints.map((p, i) => (\n        <circle\n          key={`s-${i}`}\n          cx={p.x}\n          cy={p.y}\n          r={10}\n          fill={stateColor}\n          stroke={t.pageBg}\n          strokeWidth={2.5}\n        />\n      ))}\n\n      <ChartsText\n        x={ax + 18}\n        y={ay - 16}\n        text={`A(${sigmaX}, ${tauXY})`}\n        style={{ fontSize: 16, fontWeight: 600, fill: t.ink, dominantBaseline: \"central\" }}\n      />\n      <ChartsText\n        x={bx - 18}\n        y={by + 18}\n        text={`B(${sigmaY}, ${-tauXY})`}\n        style={{\n          fontSize: 16,\n          fontWeight: 600,\n          fill: t.ink,\n          textAnchor: \"end\",\n          dominantBaseline: \"central\",\n        }}\n      />\n      <ChartsText\n        x={s1x + 14}\n        y={s1y + 28}\n        text={`σ₁ = ${sigma1.toFixed(1)}${unitSuffix}`}\n        style={{ fontSize: 17, fontWeight: 700, fill: t.ink, dominantBaseline: \"central\" }}\n      />\n      <ChartsText\n        x={s2x - 14}\n        y={s2y + 28}\n        text={`σ₂ = ${sigma2.toFixed(1)}${unitSuffix}`}\n        style={{\n          fontSize: 17,\n          fontWeight: 700,\n          fill: t.ink,\n          textAnchor: \"end\",\n          dominantBaseline: \"central\",\n        }}\n      />\n      <ChartsText\n        x={tmaxX + 16}\n        y={tmaxY - 14}\n        text={`τ_max = ${tauMax.toFixed(1)}${unitSuffix}`}\n        style={{ fontSize: 17, fontWeight: 700, fill: t.ink, dominantBaseline: \"central\" }}\n      />\n      <ChartsText\n        x={tminX + 16}\n        y={tminY + 14}\n        text={`−τ_max = ${(-tauMax).toFixed(1)}${unitSuffix}`}\n        style={{ fontSize: 17, fontWeight: 700, fill: t.ink, dominantBaseline: \"central\" }}\n      />\n      <ChartsText\n        x={cx - 14}\n        y={cy - 18}\n        text={`C(${center.toFixed(0)}, 0)`}\n        style={{\n          fontSize: 15,\n          fill: t.inkSoft,\n          textAnchor: \"end\",\n          dominantBaseline: \"central\",\n        }}\n      />\n      <ChartsText\n        x={angleLabelX + 8}\n        y={angleLabelY - 6}\n        text={`2θp = ${twoThetaPDeg.toFixed(0)}°`}\n        style={{ fontSize: 15, fontWeight: 600, fill: t.inkSoft, dominantBaseline: \"central\" }}\n      />\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const chartH = height - TITLE_HEIGHT;\n  const targetDrawSize = Math.min(width - Y_AXIS_WIDTH - 64, chartH - X_AXIS_HEIGHT - 44);\n  const marginLeft = Math.floor((width - Y_AXIS_WIDTH - targetDrawSize) / 2);\n  const marginRight = width - Y_AXIS_WIDTH - targetDrawSize - marginLeft;\n  const marginTop = Math.floor((chartH - X_AXIS_HEIGHT - targetDrawSize) / 2);\n  const marginBottom = chartH - X_AXIS_HEIGHT - targetDrawSize - marginTop;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: \"Inter, system-ui, sans-serif\",\n      }}\n    >\n      <div\n        style={{\n          height: TITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: TITLE_FONT_SIZE,\n          fontWeight: 600,\n          color: t.ink,\n        }}\n      >\n        {TITLE}\n      </div>\n      <ChartsContainer\n        width={width}\n        height={chartH}\n        margin={{\n          top: marginTop,\n          right: marginRight,\n          bottom: marginBottom,\n          left: marginLeft,\n        }}\n        skipAnimation\n        series={[]}\n        xAxis={[\n          {\n            id: \"sigma\",\n            scaleType: \"linear\",\n            min: xAxisMin,\n            max: xAxisMax,\n            height: X_AXIS_HEIGHT,\n            label: xAxisLabel,\n            labelStyle: { fontSize: 18, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 16, fill: t.inkSoft },\n            stroke: t.inkSoft,\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"tau\",\n            scaleType: \"linear\",\n            min: yAxisMin,\n            max: yAxisMax,\n            width: Y_AXIS_WIDTH,\n            label: yAxisLabel,\n            labelStyle: { fontSize: 18, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 16, fill: t.inkSoft },\n            stroke: t.inkSoft,\n          },\n        ]}\n      >\n        <ChartsGrid horizontal vertical />\n        <ChartsReferenceLine y={0} lineStyle={{ stroke: t.grid, strokeWidth: 2 }} />\n        <ChartsReferenceLine x={center} lineStyle={{ stroke: t.grid, strokeWidth: 2 }} />\n        <MohrAnnotations />\n        <ChartsXAxis axisId=\"sigma\" />\n        <ChartsYAxis axisId=\"tau\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}