{"spec_id":"barcode-ean13","library":"muix","language":"javascript","code":"// anyplot.ai\n// barcode-ean13: EAN-13 Barcode\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { BarPlot } from \"@mui/x-charts/BarChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// EAN-13 digit patterns: the L-code is canonical, the R-code is its bitwise\n// complement and the even-parity G-code is the R-code reversed\nconst L_CODE = [\"0001101\", \"0011001\", \"0010011\", \"0111101\", \"0100011\", \"0110001\", \"0101111\", \"0111011\", \"0110111\", \"0001011\"];\nconst R_CODE = L_CODE.map((bits) => Array.from(bits, (b) => (b === \"1\" ? \"0\" : \"1\")).join(\"\"));\nconst G_CODE = R_CODE.map((bits) => Array.from(bits).reverse().join(\"\"));\n// The leading digit is not drawn as bars: it sets the L/G parity of the left group\nconst PARITY = [\"LLLLLL\", \"LLGLGG\", \"LLGGLG\", \"LLGGGL\", \"LGLLGG\", \"LGGLLG\", \"LGGGLL\", \"LGLGLG\", \"LGLGGL\", \"LGGLGL\"];\nconst START_GUARD = \"101\";\nconst CENTER_GUARD = \"01010\";\nconst END_GUARD = \"101\";\nconst DIGITS_PER_HALF = 6;\nconst QUIET_ZONE_MODULES = 9; // minimum quiet zone on each side\n// A barcode keeps dark bars on a light ground in both themes so it still scans\nconst LABEL_BG = \"#FAF8F1\";\nconst BAR_INK = \"#1A1A17\";\nconst DIGIT_BAR_SHARE = 0.9; // digit bars end here; guard bars run on to 1, between the digit groups\nconst MAX_SYMBOL_SHARE = 0.6; // widest share of the canvas the symbol may take\nconst BAR_HEIGHT_SHARE = 0.6; // guard bar length as a share of the canvas height\nconst DIGIT_FONT = 32;\n\n// Data: replace this block with your own parameters\nconst CODE = \"590123412345\"; // role: code\n\n// Plot: reads only the parameters and the constants above\n// The check digit is computed from the first 12 digits (weights 1, 3, 1, 3, ...)\nconst payload = Array.from(CODE.slice(0, 2 * DIGITS_PER_HALF), Number);\nconst weightedSum = payload.reduce((sum, d, i) => sum + d * (i % 2 === 0 ? 1 : 3), 0);\nconst checkDigit = (10 - (weightedSum % 10)) % 10;\nconst digits = [...payload, checkDigit];\nconst parity = PARITY[digits[0]];\nconst leftDigits = digits.slice(1, 1 + DIGITS_PER_HALF);\nconst rightDigits = digits.slice(1 + DIGITS_PER_HALF);\nconst leftBits = leftDigits.map((d, i) => (parity[i] === \"L\" ? L_CODE[d] : G_CODE[d])).join(\"\");\nconst rightBits = rightDigits.map((d) => R_CODE[d]).join(\"\");\n\nconst quietZone = \"0\".repeat(QUIET_ZONE_MODULES);\nconst parts = [\n  { bits: quietZone, guard: false },\n  { bits: START_GUARD, guard: true },\n  { bits: leftBits, guard: false },\n  { bits: CENTER_GUARD, guard: true },\n  { bits: rightBits, guard: false },\n  { bits: END_GUARD, guard: true },\n  { bits: quietZone, guard: false },\n];\nconst modules = parts.flatMap((p) => Array.from(p.bits, (b) => ({ bar: b === \"1\", guard: p.guard })));\nconst moduleIndex = modules.map((_, i) => i);\nconst totalModules = modules.length;\n\n// Human-readable digits in module units: the leading digit centred in the left\n// quiet zone, every other digit centred under its own 7-module pattern\nconst digitWidth = L_CODE[0].length;\nconst leftStart = QUIET_ZONE_MODULES + START_GUARD.length;\nconst rightStart = leftStart + leftBits.length + CENTER_GUARD.length;\nconst digitSlots = [\n  { digit: digits[0], center: QUIET_ZONE_MODULES / 2 },\n  ...leftDigits.map((digit, i) => ({ digit, center: leftStart + (i + 0.5) * digitWidth })),\n  ...rightDigits.map((digit, i) => ({ digit, center: rightStart + (i + 0.5) * digitWidth })),\n];\n\nconst { width, height } = window.ANYPLOT_SIZE;\n// Whole pixels per module keep every bar edge sharp\nconst modulePx = Math.floor((width * MAX_SYMBOL_SHARE) / totalModules);\nconst symbolWidth = modulePx * totalModules;\nconst barHeight = Math.round(height * BAR_HEIGHT_SHARE);\n\nconst TITLE = \"barcode-ean13 · javascript · muix · anyplot.ai\";\nconst caption = `EAN-13 ${digits.join(\"\")} · check digit ${checkDigit} · left-half parity ${parity} set by leading digit ${digits[0]} · ${totalModules} modules incl. ${QUIET_ZONE_MODULES}-module quiet zones`;\n\nfunction DigitLabels() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const x0 = xScale(moduleIndex[0]);\n  const band = xScale.bandwidth();\n  return (\n    <g>\n      {digitSlots.map((slot, i) => (\n        <text\n          key={i}\n          x={x0 + slot.center * band}\n          y={yScale(1)}\n          textAnchor=\"middle\"\n          fontSize={DIGIT_FONT}\n          fontFamily=\"'JetBrains Mono', 'Courier New', monospace\"\n          fill={BAR_INK}\n        >\n          {slot.digit}\n        </text>\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  return (\n    <div\n      style={{\n        width,\n        height,\n        display: \"flex\",\n        flexDirection: \"column\",\n        alignItems: \"center\",\n        justifyContent: \"center\",\n        gap: 28,\n        background: t.pageBg,\n      }}\n    >\n      <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</div>\n      <div\n        style={{\n          background: LABEL_BG,\n          border: `1px solid ${t.grid}`,\n          borderRadius: 6,\n          padding: \"40px 0 30px\",\n        }}\n      >\n        <ChartsContainer\n          width={symbolWidth}\n          height={barHeight}\n          skipAnimation\n          margin={{ top: 0, bottom: 0, left: 0, right: 0 }}\n          xAxis={[{ scaleType: \"band\", data: moduleIndex, categoryGapRatio: 0, position: \"none\" }]}\n          yAxis={[{ min: 0, max: 1, reverse: true, position: \"none\" }]}\n          series={[\n            {\n              type: \"bar\",\n              id: \"digit-bars\",\n              stack: \"symbol\",\n              color: BAR_INK,\n              data: modules.map((m) => (m.bar && !m.guard ? DIGIT_BAR_SHARE : 0)),\n            },\n            {\n              type: \"bar\",\n              id: \"guard-bars\",\n              stack: \"symbol\",\n              color: BAR_INK,\n              data: modules.map((m) => (m.bar && m.guard ? 1 : 0)),\n            },\n          ]}\n        >\n          <BarPlot skipAnimation />\n          <DigitLabels />\n        </ChartsContainer>\n      </div>\n      <div style={{ fontSize: 16, color: t.inkSoft }}>{caption}</div>\n    </div>\n  );\n}\n"}