{"spec_id":"pictogram-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// pictogram-basic: Pictogram Chart (Isotype Visualization)\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useDrawingArea, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst MAX_ICONS = 8; // the longest row holds at most this many icons\nconst NICE_STEPS = [1, 2, 2.5, 5, 10]; // icon values are one of these times a power of ten\nconst ICON_SHARE = 0.4; // icon radius as a share of the smaller of row height and icon pitch\nconst MIN_REMAINDER = 0.02; // remainders below this share of an icon are not drawn\nconst TITLE_H = 52;\nconst LEGEND_H = 44;\nconst LEGEND_ICON = 20;\n\n// Columns: set these to your data's column names\nconst FRUIT = \"Fruit\"; // role: category\nconst HARVEST = \"Weekly harvest (tons)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\n// Weekly harvest of a small farm co-op\nconst data = [\n  { [FRUIT]: \"Apples\", [HARVEST]: 35 },\n  { [FRUIT]: \"Oranges\", [HARVEST]: 22 },\n  { [FRUIT]: \"Bananas\", [HARVEST]: 18 },\n  { [FRUIT]: \"Mangoes\", [HARVEST]: 27 },\n  { [FRUIT]: \"Grapes\", [HARVEST]: 14 },\n];\n\n// Plot: reads only data and the constants above\nconst fruits = data.map((d) => d[FRUIT]);\nconst maxValue = Math.max(...data.map((d) => d[HARVEST]));\n\n// Value of one icon: the smallest nice step that keeps the longest row within MAX_ICONS\nconst magnitude = 10 ** Math.floor(Math.log10(maxValue / MAX_ICONS));\nconst unitValue = magnitude * NICE_STEPS.find((step) => step * magnitude >= maxValue / MAX_ICONS);\nconst iconSlots = Math.ceil(maxValue / unitValue - 1e-9);\n\nconst rows = data.map((d) => {\n  const count = d[HARVEST] / unitValue;\n  const full = Math.floor(count + 1e-9);\n  const remainder = count - full;\n  return { fruit: d[FRUIT], full, remainder: remainder >= MIN_REMAINDER ? remainder : 0 };\n});\n\nconst unitMatch = HARVEST.match(/\\(([^)]+)\\)/);\nconst unit = unitMatch ? unitMatch[1] : \"\";\n\nconst TITLE = \"Weekly Fruit Harvest · pictogram-basic · javascript · muix · anyplot.ai\";\n\nfunction IconRows() {\n  const yScale = useYScale(\"fruit\");\n  const { left, width } = useDrawingArea();\n  // Icons sit at one pitch across the full drawing width, so the longest row reaches its right\n  // edge; the radius fits both that pitch and the row height, so every icon keeps one size\n  const pitch = width / iconSlots;\n  const radius = Math.min(yScale.bandwidth(), pitch) * ICON_SHARE;\n\n  return (\n    <g>\n      {rows.map((row, r) => {\n        const cy = yScale(row.fruit) + yScale.bandwidth() / 2;\n        const partialX = left + (row.full + 0.5) * pitch;\n        return (\n          <g key={row.fruit}>\n            {Array.from({ length: row.full }, (_, i) => (\n              <circle key={i} cx={left + (i + 0.5) * pitch} cy={cy} r={radius} fill={t.palette[0]} />\n            ))}\n            {row.remainder > 0 && (\n              <g>\n                <clipPath id={`pictogram-remainder-${r}`}>\n                  <rect x={partialX - radius} y={cy - radius} width={2 * radius * row.remainder} height={2 * radius} />\n                </clipPath>\n                <circle cx={partialX} cy={cy} r={radius} fill=\"none\" stroke={t.palette[0]} strokeWidth={2.5} strokeOpacity={0.7} />\n                <circle cx={partialX} cy={cy} r={radius} fill={t.palette[0]} clipPath={`url(#pictogram-remainder-${r})`} />\n              </g>\n            )}\n          </g>\n        );\n      })}\n    </g>\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 style={{ height: TITLE_H, display: \"flex\", alignItems: \"flex-end\", justifyContent: \"center\", fontSize: 22, fontWeight: 600, color: t.ink }}>\n        {TITLE}\n      </div>\n      <div style={{ height: LEGEND_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", gap: 10, fontSize: 16, color: t.inkSoft }}>\n        <svg width={LEGEND_ICON} height={LEGEND_ICON}>\n          <circle cx={LEGEND_ICON / 2} cy={LEGEND_ICON / 2} r={LEGEND_ICON / 2} fill={t.palette[0]} />\n        </svg>\n        <span>\n          = {unitValue.toLocaleString(\"en-US\")} {unit} · a cut icon shows the remainder\n        </span>\n      </div>\n      <ChartsContainer\n        width={width}\n        height={height - TITLE_H - LEGEND_H}\n        margin={{ top: 16, right: 64, bottom: 32, left: 24 }}\n        series={[]}\n        xAxis={[{ id: \"slots\", scaleType: \"linear\", min: 0, max: iconSlots, position: \"none\" }]}\n        yAxis={[\n          {\n            id: \"fruit\",\n            scaleType: \"band\",\n            data: fruits,\n            categoryGapRatio: 0.15,\n            label: FRUIT,\n            width: 140,\n            disableTicks: true,\n            disableLine: true,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        skipAnimation\n      >\n        <IconRows />\n        <ChartsYAxis axisId=\"fruit\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}