{"spec_id":"count-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// count-basic: Basic Count Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\nimport { BarChart } from \"@mui/x-charts/BarChart\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst HEADROOM_SHARE = 0.05; // space above the tallest bar for its count label, as a share of the top count\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Support ticket category\"; // role: category\n\n// Data: replace this block with your own records, one object per observation\n// 620 raw support tickets, each tagged with one category; the plot counts them\nconst data = (() => {\n  let state = 42;\n  const rand = () => {\n    state = (state * 1664525 + 1013904223) % 4294967296;\n    return state / 4294967296;\n  };\n  const ticketTypes = [\"Technical\", \"Billing\", \"Shipping\", \"Account\", \"Feature Request\", \"Other\"];\n  const weights = [0.3, 0.24, 0.18, 0.14, 0.09, 0.05];\n  return Array.from({ length: 620 }, () => {\n    const r = rand();\n    let cumulative = 0;\n    const index = weights.findIndex((w) => {\n      cumulative += w;\n      return r < cumulative;\n    });\n    return { [CATEGORY]: ticketTypes[index === -1 ? ticketTypes.length - 1 : index] };\n  });\n})();\n\n// Plot: reads only data and the constants above\n// Count the observations per category, then order by descending count (ties keep first appearance)\nconst countByCategory = new Map<string, number>();\ndata\n  .filter((d) => d[CATEGORY] != null)\n  .forEach((d) => countByCategory.set(d[CATEGORY], (countByCategory.get(d[CATEGORY]) ?? 0) + 1));\nconst ranked = [...countByCategory.entries()]\n  .map(([category, count]) => ({ category, count }))\n  .sort((a, b) => b.count - a.count);\nconst total = ranked.reduce((sum, r) => sum + r.count, 0);\nconst topCount = ranked[0]?.count ?? 0;\n\nconst TITLE = \"Support Tickets · count-basic · javascript · muix · anyplot.ai\";\nconst subtitle = `n = ${total.toLocaleString(\"en-US\")} observations in ${ranked.length} categories, sorted by count`;\nconst HEADER_H = 88;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: HEADER_H, display: \"flex\", flexDirection: \"column\", justifyContent: \"center\", gap: \"6px\", px: \"56px\", pt: \"8px\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.2 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, lineHeight: 1.2 }}>{subtitle}</Typography>\n      </Box>\n      <BarChart\n        width={width}\n        height={height - HEADER_H}\n        dataset={ranked}\n        colors={[t.palette[0]]}\n        skipAnimation\n        hideLegend\n        borderRadius={4}\n        grid={{ horizontal: true }}\n        margin={{ top: 8, right: 48, bottom: 16, left: 32 }}\n        xAxis={[\n          {\n            scaleType: \"band\",\n            dataKey: \"category\",\n            label: CATEGORY,\n            height: 68,\n            categoryGapRatio: 0.4,\n            disableTicks: true,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            label: \"Count\",\n            width: 76,\n            min: 0,\n            max: topCount * (1 + HEADROOM_SHARE),\n            tickMinStep: 1, // counts are whole numbers\n            tickNumber: 8,\n            disableTicks: true,\n            disableLine: true,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n          },\n        ]}\n        series={[{ id: \"count\", dataKey: \"count\", label: \"Count\", barLabel: \"value\", barLabelPlacement: \"outside\" }]}\n        sx={{\n          \"& .MuiBarChart-label\": { fontSize: \"16px\", fontWeight: 600, fill: t.ink },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n        }}\n      />\n    </Box>\n  );\n}\n"}