{"spec_id":"funnel-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// funnel-basic: Basic Funnel Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 88/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useXScale, useYScale, useDrawingArea } 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 TITLE = \"funnel-basic · javascript · muix · anyplot.ai\";\nconst TOTAL_SHARE_SUFFIX = \"of initial stage\";\nconst LOSS_ANCHOR = \"#AE3030\"; // Imprint's bad/loss hue — never a funnel stage\nconst STAGE_COLORS = t.palette.filter((c: string) => c !== LOSS_ANCHOR);\n\nconst TITLE_HEIGHT = 82;\nconst MARGIN = { top: 20, right: 60, bottom: 28, left: 20 };\nconst Y_AXIS_WIDTH = 210;\n\n// Columns: set these to your data's column names\nconst STAGE = \"Stage\"; // role: stage\nconst SHOPPERS = \"Shoppers\"; // role: value\n\n// Data: replace this block with your own records\nconst data: Record<string, string | number>[] = [\n  { [STAGE]: \"Product Views\", [SHOPPERS]: 50000 },\n  { [STAGE]: \"Added to Cart\", [SHOPPERS]: 30000 },\n  { [STAGE]: \"Checkout Started\", [SHOPPERS]: 16000 },\n  { [STAGE]: \"Payment Submitted\", [SHOPPERS]: 9000 },\n  { [STAGE]: \"Order Completed\", [SHOPPERS]: 5500 },\n];\n\n// Plot: reads only data and the constants above\nconst maxVal = Math.max(...data.map((r) => Number(r[SHOPPERS])));\nconst firstVal = Number(data[0][SHOPPERS]);\nconst lastVal = Number(data[data.length - 1][SHOPPERS]);\nconst overallConversionPct = ((lastVal / firstVal) * 100).toFixed(1);\n\nconst stages = data.map((row, i) => {\n  const name = String(row[STAGE]);\n  const value = Number(row[SHOPPERS]);\n  const nextVal = i < data.length - 1 ? Number(data[i + 1][SHOPPERS]) : value;\n  const color = STAGE_COLORS[i % STAGE_COLORS.length];\n  const r = parseInt(color.slice(1, 3), 16);\n  const g = parseInt(color.slice(3, 5), 16);\n  const b = parseInt(color.slice(5, 7), 16);\n  const luminance = 0.299 * r + 0.587 * g + 0.114 * b;\n  const labelColor = luminance > 135 ? \"#1A1A17\" : \"#FAF8F1\";\n  const pct = Math.round((value / firstVal) * 100);\n  return { name, value, nextVal, color, labelColor, pct };\n});\n\nconst stageNames = stages.map((s) => s.name);\n\nfunction FunnelPlot() {\n  const xScale = useXScale() as (v: number) => number;\n  const yScale = useYScale() as unknown as ((v: string) => number) & { bandwidth: () => number };\n  const drawingArea = useDrawingArea();\n\n  const x0 = xScale(0);\n  const cx = drawingArea.left + drawingArea.width / 2;\n  const bandwidth = yScale.bandwidth();\n\n  return (\n    <g>\n      {stages.map((stage) => {\n        const y0 = yScale(stage.name);\n        const y1 = y0 + bandwidth;\n        const topW = xScale(stage.value) - x0;\n        const bottomW = xScale(stage.nextVal) - x0;\n\n        const points = [\n          `${cx - topW / 2},${y0}`,\n          `${cx + topW / 2},${y0}`,\n          `${cx + bottomW / 2},${y1}`,\n          `${cx - bottomW / 2},${y1}`,\n        ].join(\" \");\n\n        return (\n          <g key={stage.name}>\n            <polygon points={points} fill={stage.color} stroke={t.pageBg} strokeWidth={1.5} />\n            <text\n              x={cx}\n              y={y0 + bandwidth / 2 - 11}\n              fill={stage.labelColor}\n              fontSize={18}\n              fontWeight={600}\n              textAnchor=\"middle\"\n              fontFamily=\"sans-serif\"\n            >\n              {stage.value.toLocaleString()}\n            </text>\n            <text\n              x={cx}\n              y={y0 + bandwidth / 2 + 15}\n              fill={stage.labelColor}\n              fontSize={14}\n              fontWeight={500}\n              textAnchor=\"middle\"\n              fontFamily=\"sans-serif\"\n            >\n              {`${stage.pct}% ${TOTAL_SHARE_SUFFIX}`}\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  const subtitle = `${stages.length} ${STAGE}s · ${SHOPPERS} from ${firstVal.toLocaleString()} to ${lastVal.toLocaleString()} (${overallConversionPct}% end-to-end conversion)`;\n\n  return (\n    <Box\n      sx={{\n        width: chartWidth,\n        height: window.ANYPLOT_SIZE.height,\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <Box\n        sx={{\n          height: TITLE_HEIGHT,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          flexShrink: 0,\n        }}\n      >\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600 }}>\n          {TITLE}\n        </Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: 15, mt: \"2px\" }}>\n          {subtitle}\n        </Typography>\n      </Box>\n\n      <ChartsContainer\n        skipAnimation\n        width={chartWidth}\n        height={chartHeight}\n        margin={MARGIN}\n        series={[\n          {\n            type: \"bar\",\n            data: stages.map((s) => s.value),\n            layout: \"horizontal\",\n          },\n        ]}\n        xAxis={[{ scaleType: \"linear\", min: 0, max: maxVal, position: \"none\" }]}\n        yAxis={[\n          {\n            scaleType: \"band\",\n            data: stageNames,\n            width: Y_AXIS_WIDTH,\n            categoryGapRatio: 0.3,\n            disableLine: true,\n            disableTicks: true,\n            tickLabelStyle: { fontSize: 16, fill: t.inkSoft, fontWeight: 500 },\n          },\n        ]}\n      >\n        <FunnelPlot />\n        <ChartsYAxis />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}