{"spec_id":"lollipop-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// lollipop-basic: Basic Lollipop Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 84/100 | Updated: 2026-10-24\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst X_PAD_FRAC = 0.1;\nconst TITLE = \"lollipop-basic · javascript · muix · anyplot.ai\";\nconst TITLE_HEIGHT = 70;\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Book genre\"; // role: category\nconst VALUE = \"Annual sales (millions of units)\"; // role: value\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, string | number>> = [\n  { [CATEGORY]: \"Children's\", [VALUE]: 22.1 },\n  { [CATEGORY]: \"Romance\", [VALUE]: 18.7 },\n  { [CATEGORY]: \"Thriller\", [VALUE]: 16.3 },\n  { [CATEGORY]: \"Mystery\", [VALUE]: 15.2 },\n  { [CATEGORY]: \"Fantasy\", [VALUE]: 14.8 },\n  { [CATEGORY]: \"Sci-Fi\", [VALUE]: 12.4 },\n  { [CATEGORY]: \"Self-Help\", [VALUE]: 11.2 },\n  { [CATEGORY]: \"Business\", [VALUE]: 10.5 },\n  { [CATEGORY]: \"Cooking\", [VALUE]: 9.8 },\n  { [CATEGORY]: \"Biography\", [VALUE]: 8.9 },\n  { [CATEGORY]: \"History\", [VALUE]: 7.6 },\n  { [CATEGORY]: \"Literary Fiction\", [VALUE]: 6.4 },\n];\n\n// Plot: reads only data and the constants above\nconst sortedData = [...data].sort((a, b) => Number(b[VALUE]) - Number(a[VALUE]));\nconst genres = sortedData.map((r) => String(r[CATEGORY]));\nconst salesData = sortedData.map((r) => Number(r[VALUE]));\nconst maxVal = Math.max(...salesData);\nconst xAxisMax = maxVal * (1 + X_PAD_FRAC);\n\nfunction LollipopPlot() {\n  const xScale = useXScale(\"sales\") as (v: number) => number;\n  const yScale = useYScale(\"genre\") as unknown as ((v: string) => number) & {\n    bandwidth: () => number;\n  };\n  const bw = yScale.bandwidth();\n  const x0 = xScale(0);\n\n  return (\n    <g>\n      {sortedData.map((row) => {\n        const genre = String(row[CATEGORY]);\n        const val = Number(row[VALUE]);\n        const x1 = xScale(val);\n        const cy = yScale(genre) + bw / 2;\n\n        return (\n          <g key={genre}>\n            <line\n              x1={x0}\n              y1={cy}\n              x2={x1}\n              y2={cy}\n              stroke={t.palette[0]}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <circle cx={x1} cy={cy} r={11} fill={t.palette[0]} />\n            <text\n              x={x1 + 16}\n              y={cy + 5}\n              fill={t.inkSoft}\n              fontSize={14}\n              fontFamily=\"sans-serif\"\n            >\n              {val.toFixed(1)}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const chartWidth = window.ANYPLOT_SIZE.width;\n  const chartHeight = window.ANYPLOT_SIZE.height - TITLE_HEIGHT;\n\n  return (\n    <Box\n      sx={{\n        width: chartWidth,\n        height: window.ANYPLOT_SIZE.height,\n        bgcolor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <Box\n        sx={{\n          height: TITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          flexShrink: 0,\n        }}\n      >\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 500 }}>\n          {TITLE}\n        </Typography>\n      </Box>\n\n      <ChartsContainer\n        skipAnimation\n        width={chartWidth}\n        height={chartHeight}\n        margin={{ top: 10, right: 85, bottom: 16, left: 24 }}\n        series={[]}\n        xAxis={[\n          {\n            id: \"sales\",\n            scaleType: \"linear\",\n            min: 0,\n            max: xAxisMax,\n            height: 54,\n            label: VALUE,\n            labelStyle: { fontSize: 15, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"genre\",\n            scaleType: \"band\",\n            data: genres,\n            width: 180,\n            label: CATEGORY,\n            labelStyle: { fontSize: 15, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeOpacity: 0.5 },\n        }}\n      >\n        <ChartsGrid vertical />\n        <LollipopPlot />\n        <ChartsYAxis axisId=\"genre\" />\n        <ChartsXAxis axisId=\"sales\" />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}