{"spec_id":"datamatrix-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// datamatrix-basic: Basic Data Matrix 2D Barcode\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 81/100 | Updated: 2026-10-23\n//# anyplot-orientation: square\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst QUIET_ZONE = 2; // modules of blank margin (spec requires >= 1)\nconst TITLE_HEIGHT = 64;\nconst CAPTION_HEIGHT = 48;\nconst OUTER_MARGIN = 40;\nconst CARD_PADDING = 32;\n// Barcode symbology is black on white in both themes; only the page chrome flips\nconst MODULE_INK = \"#1A1A17\";\nconst SYMBOL_TILE = \"#FAF8F1\";\n\n// Columns: set these to your data's column names\nconst CONTENT = \"Content\"; // role: content\nconst SIZE = \"Size\"; // role: size\n\n// Data: replace this block with your own records, one object per row\nconst data = [{ [CONTENT]: \"PCB-SN:48291X7A\", [SIZE]: \"24x24\" }];\n\n// Plot: reads only data and the constants above\nconst encodedText = String(data[0][CONTENT]);\nconst moduleCount = parseInt(String(data[0][SIZE]).split(\"x\")[0], 10) || 24;\nconst TITLE = \"datamatrix-basic · javascript · muix · anyplot.ai\";\nconst CAPTION = `Part marking \"${encodedText}\" · ${moduleCount}×${moduleCount} modules · illustrative data region`;\n\n// Data region: the content's 8-bit ASCII, then a fixed-seed LCG fills the rest\nconst capacity = (moduleCount - 2) * (moduleCount - 2);\nconst dataBits = [];\nfor (const ch of encodedText) {\n  for (let shift = 7; shift >= 0; shift--) dataBits.push((ch.charCodeAt(0) >> shift) & 1);\n}\nlet lcgState = 20260902;\nwhile (dataBits.length < capacity) {\n  lcgState = (Math.imul(lcgState, 1103515245) + 12345) >>> 0;\n  dataBits.push((lcgState >>> 16) & 1);\n}\n\n// Symbol grid: finder L on the left column and bottom row, clock on top and right\nconst GRID = Array.from({ length: moduleCount }, () => new Array(moduleCount).fill(0));\nfor (let r = 0; r < moduleCount; r++) {\n  GRID[r][0] = 1;\n  GRID[r][moduleCount - 1] = (moduleCount - 1 - r) % 2 === 0 ? 1 : 0;\n}\nfor (let c = 0; c < moduleCount; c++) {\n  GRID[moduleCount - 1][c] = 1;\n  GRID[0][c] = c % 2 === 0 ? 1 : 0;\n}\nlet bitIndex = 0;\nfor (let r = 1; r < moduleCount - 1; r++) {\n  for (let c = 1; c < moduleCount - 1; c++) GRID[r][c] = dataBits[bitIndex++];\n}\n\nfunction DataMatrixModules() {\n  const { left, top, width } = useDrawingArea();\n  const cellSize = width / (moduleCount + QUIET_ZONE * 2);\n\n  const modules = [];\n  for (let r = 0; r < moduleCount; r++) {\n    for (let c = 0; c < moduleCount; c++) {\n      if (!GRID[r][c]) continue;\n      modules.push(\n        <rect\n          key={`${r}-${c}`}\n          x={left + (QUIET_ZONE + c) * cellSize}\n          y={top + (QUIET_ZONE + r) * cellSize}\n          width={cellSize + 0.6}\n          height={cellSize + 0.6}\n          fill={MODULE_INK}\n          shapeRendering=\"crispEdges\"\n        />,\n      );\n    }\n  }\n  return <g>{modules}</g>;\n}\n\nexport default function Chart() {\n  const size = window.ANYPLOT_SIZE;\n  const cardSide = Math.min(\n    size.width,\n    size.height - TITLE_HEIGHT - CAPTION_HEIGHT - OUTER_MARGIN * 2,\n  );\n  const matrixSide = cardSide - CARD_PADDING * 2;\n\n  return (\n    <div\n      style={{\n        width: size.width,\n        height: size.height,\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <div\n        style={{\n          height: TITLE_HEIGHT,\n          lineHeight: `${TITLE_HEIGHT}px`,\n          paddingLeft: 24,\n          fontSize: 22,\n          fontWeight: 500,\n          color: t.ink,\n        }}\n      >\n        {TITLE}\n      </div>\n      <div\n        style={{\n          flex: 1,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n        }}\n      >\n        <div\n          style={{\n            width: cardSide,\n            height: cardSide,\n            boxSizing: \"border-box\",\n            padding: CARD_PADDING,\n            backgroundColor: SYMBOL_TILE,\n            border: `1px solid ${t.grid}`,\n            borderRadius: 20,\n            display: \"flex\",\n            alignItems: \"center\",\n            justifyContent: \"center\",\n          }}\n        >\n          <ChartsContainer\n            width={matrixSide}\n            height={matrixSide}\n            series={[]}\n            xAxis={[{ position: \"none\" }]}\n            yAxis={[{ position: \"none\" }]}\n            margin={{ top: 0, right: 0, bottom: 0, left: 0 }}\n            skipAnimation\n          >\n            <DataMatrixModules />\n          </ChartsContainer>\n        </div>\n      </div>\n      <div\n        style={{\n          height: CAPTION_HEIGHT,\n          lineHeight: `${CAPTION_HEIGHT}px`,\n          textAlign: \"center\",\n          fontSize: 16,\n          color: t.inkSoft,\n        }}\n      >\n        {CAPTION}\n      </div>\n    </div>\n  );\n}\n"}