{"spec_id":"line-markers","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-markers: Line Plot with Markers\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 84/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { useDrawingArea, useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst LINE_WIDTH = 2.5;\nconst MARK_R = 9; // marker radius in px, about 3.5 line widths\n// One marker per series in order: a distinct shape, filled and hollow in turn, and a line dash.\n// Paths are centred on (0, 0) and moved onto each data point.\nconst MARKERS = [\n  { d: `M ${-MARK_R} 0 A ${MARK_R} ${MARK_R} 0 1 0 ${MARK_R} 0 A ${MARK_R} ${MARK_R} 0 1 0 ${-MARK_R} 0 Z`, filled: true, dash: \"none\" },\n  { d: `M 0 ${-MARK_R * 1.2} L ${MARK_R * 1.2} 0 L 0 ${MARK_R * 1.2} L ${-MARK_R * 1.2} 0 Z`, filled: false, dash: \"9 6\" },\n  { d: `M ${-MARK_R * 0.85} ${-MARK_R * 0.85} H ${MARK_R * 0.85} V ${MARK_R * 0.85} H ${-MARK_R * 0.85} Z`, filled: true, dash: \"3 5\" },\n  { d: `M 0 ${-MARK_R * 1.1} L ${MARK_R * 1.05} ${MARK_R * 0.75} L ${-MARK_R * 1.05} ${MARK_R * 0.75} Z`, filled: false, dash: \"13 5 2 5\" },\n  { d: `M 0 ${MARK_R * 1.1} L ${MARK_R * 1.05} ${-MARK_R * 0.75} L ${-MARK_R * 1.05} ${-MARK_R * 0.75} Z`, filled: true, dash: \"18 6\" },\n  { d: `M ${-MARK_R} ${-MARK_R * 0.35} H ${-MARK_R * 0.35} V ${-MARK_R} H ${MARK_R * 0.35} V ${-MARK_R * 0.35} H ${MARK_R} V ${MARK_R * 0.35} H ${MARK_R * 0.35} V ${MARK_R} H ${-MARK_R * 0.35} V ${MARK_R * 0.35} H ${-MARK_R} Z`, filled: false, dash: \"2 4 8 4\" },\n];\nconst X_PAD_SHARE = 0.03; // share of the x range added on both sides so the outer markers stay whole\nconst Y_PAD_SHARE = 0.15; // share of the y range added below and above the data\n\n// Columns: set these to your data's column names\nconst CHECKPOINT = \"QC checkpoint (sequential reading)\"; // role: x\nconst STATION = \"Assembly station\"; // role: series\nconst TORQUE = \"Torque (N·m)\"; // role: y\nconst TARGET = \"Target torque (N·m)\"; // role: extra\n\n// Data: replace this block with your own records, one object per row\n// Torque-wrench readings at 14 sequential fastener checkpoints on two assembly stations.\nconst data = Object.entries({\n  \"Station A\": [42.6, 42.3, 42.7, 42.4, 42.8, 42.2, 42.5, 42.6, 42.3, 42.7, 42.4, 42.5, 42.6, 42.3],\n  \"Station B\": [41.9, 42.0, 41.7, 42.1, 41.8, 41.6, 41.9, 41.5, 41.8, 41.7, 41.9, 41.6, 41.8, 41.7],\n}).flatMap(([station, readings]) =>\n  readings.map((torque, i) => ({ [CHECKPOINT]: i + 1, [STATION]: station, [TORQUE]: torque, [TARGET]: 42.5 })),\n);\n\n// Plot: reads only data and the constants above\nconst stations = [...new Set(data.map((d) => d[STATION]))];\nconst checkpoints = [...new Set(data.map((d) => d[CHECKPOINT]))].sort((a, b) => a - b);\nconst lines = stations.map((station, i) => ({\n  id: `series-${i}`,\n  label: station,\n  color: t.palette[i],\n  marker: MARKERS[i],\n  values: checkpoints.map(\n    (c) => data.find((d) => d[STATION] === station && d[CHECKPOINT] === c)?.[TORQUE] ?? null,\n  ),\n}));\nconst target = data.find((d) => d[TARGET] != null)?.[TARGET];\n\nconst torques = data.map((d) => d[TORQUE]);\nconst yLow = Math.min(...torques, target ?? Infinity);\nconst yHigh = Math.max(...torques, target ?? -Infinity);\nconst yPad = (yHigh - yLow) * Y_PAD_SHARE;\nconst xPad = (checkpoints[checkpoints.length - 1] - checkpoints[0]) * X_PAD_SHARE;\nconst unit = TORQUE.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst targetName = TARGET.replace(/\\s*\\(.*\\)\\s*$/, \"\");\n\nconst TITLE = \"Fastener Torque QC · line-markers · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${torques.length} readings at ${checkpoints.length} checkpoints from ${stations.length} stations · range ${Math.min(...torques).toFixed(1)}–${Math.max(...torques).toFixed(1)} ${unit}`;\nconst HEADER_H = 84;\n\n// Markers drawn at the exact (x, y) of every reading\nfunction MarkerLayer() {\n  const xScale = useXScale() as (v: number) => number;\n  const yScale = useYScale() as (v: number) => number;\n  return (\n    <g>\n      {lines.map((line) =>\n        line.values.map((v, j) =>\n          v == null ? null : (\n            <path\n              key={`${line.id}-${checkpoints[j]}`}\n              d={line.marker.d}\n              transform={`translate(${xScale(checkpoints[j])}, ${yScale(v)})`}\n              fill={line.marker.filled ? line.color : t.pageBg}\n              stroke={line.marker.filled ? t.pageBg : line.color}\n              strokeWidth={line.marker.filled ? 2 : 2.5}\n            />\n          ),\n        ),\n      )}\n    </g>\n  );\n}\n\n// The target's name and value in the right margin, beside the end of its line, clear of the data\nfunction TargetLabel() {\n  const yScale = useYScale() as (v: number) => number;\n  const { left, width } = useDrawingArea();\n  if (target == null) return null;\n  return (\n    <text x={left + width + 12} y={yScale(target)} fontSize={15} fill={t.inkSoft} dominantBaseline=\"central\">\n      <tspan x={left + width + 12} dy=\"-0.6em\">\n        {targetName}\n      </tspan>\n      <tspan x={left + width + 12} dy=\"1.2em\">\n        {target.toFixed(1)} {unit}\n      </tspan>\n    </text>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div\n        style={{\n          height: HEADER_H,\n          padding: \"16px 48px 0 104px\",\n          boxSizing: \"border-box\",\n          display: \"flex\",\n          justifyContent: \"space-between\",\n          alignItems: \"flex-start\",\n        }}\n      >\n        <div>\n          <div style={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.25 }}>{TITLE}</div>\n          <div style={{ fontSize: 15, color: t.inkSoft, marginTop: 6 }}>{SUBTITLE}</div>\n        </div>\n        <div style={{ display: \"flex\", gap: 28, paddingTop: 30 }}>\n          {lines.map((line) => (\n            <div key={line.id} style={{ display: \"flex\", alignItems: \"center\", gap: 10, fontSize: 16, color: t.ink }}>\n              <svg width={56} height={24} viewBox=\"-28 -12 56 24\">\n                <line x1={-26} y1={0} x2={26} y2={0} stroke={line.color} strokeWidth={LINE_WIDTH} strokeDasharray={line.marker.dash} />\n                <path\n                  d={line.marker.d}\n                  fill={line.marker.filled ? line.color : t.pageBg}\n                  stroke={line.marker.filled ? t.pageBg : line.color}\n                  strokeWidth={line.marker.filled ? 2 : 2.5}\n                />\n              </svg>\n              {line.label}\n            </div>\n          ))}\n        </div>\n      </div>\n      <LineChart\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true }}\n        margin={{ top: 16, right: 132, bottom: 16, left: 24 }}\n        xAxis={[\n          {\n            data: checkpoints,\n            scaleType: \"linear\",\n            min: checkpoints[0] - xPad,\n            max: checkpoints[checkpoints.length - 1] + xPad,\n            tickNumber: checkpoints.length,\n            tickMinStep: 1,\n            label: CHECKPOINT,\n            height: 64,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: yLow - yPad,\n            max: yHigh + yPad,\n            label: TORQUE,\n            width: 76,\n            valueFormatter: (v: number) => v.toFixed(1),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={lines.map((line) => ({\n          id: line.id,\n          data: line.values,\n          label: line.label,\n          color: line.color,\n          curve: \"linear\",\n          showMark: false,\n          valueFormatter: (v: number | null) => (v == null ? \"\" : `${v.toFixed(1)} ${unit}`),\n        }))}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: LINE_WIDTH },\n          ...Object.fromEntries(\n            lines.map((line) => [`& .MuiLineChart-line[data-series=\"${line.id}\"]`, { strokeDasharray: line.marker.dash }]),\n          ),\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      >\n        {target != null && (\n          <ChartsReferenceLine\n            y={target}\n            lineStyle={{ stroke: t.inkSoft, strokeDasharray: \"5 4\", strokeWidth: 1.5 }}\n          />\n        )}\n        <TargetLabel />\n        <MarkerLayer />\n      </LineChart>\n    </div>\n  );\n}\n"}