{"spec_id":"bullet-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// bullet-basic: Basic Bullet Chart\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 { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { useXScale, useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst BAND_OPACITY_DARK = 0.32; // ink share of the lowest (poorest) band\nconst BAND_OPACITY_LIGHT = 0.11; // ink share of the highest (best) band\nconst MEASURE_SHARE = 0.36; // measure bar thickness as a share of the band height\nconst TARGET_SHARE = 0.72; // target marker length as a share of the band height\n\n// Columns: set these to your data's column names\nconst KPI = \"KPI\"; // role: label\nconst ACTUAL = \"Actual\"; // role: actual\nconst TARGET = \"Target\"; // role: target\nconst RANGES = [\"Poor\", \"Satisfactory\", \"Good\"]; // role: ranges (upper bound of each band, low to high)\n\n// Data: replace this block with your own records, one object per row\n// Quarterly KPI dashboard: actual performance against target, banded by\n// poor / satisfactory / good qualitative ranges.\nconst data = [\n  { [KPI]: \"Quarterly Revenue ($M)\", [ACTUAL]: 82, [TARGET]: 90, [RANGES[0]]: 50, [RANGES[1]]: 75, [RANGES[2]]: 100 },\n  { [KPI]: \"New Customers Acquired\", [ACTUAL]: 145, [TARGET]: 130, [RANGES[0]]: 80, [RANGES[1]]: 120, [RANGES[2]]: 160 },\n  { [KPI]: \"Support SLA Compliance (%)\", [ACTUAL]: 96, [TARGET]: 95, [RANGES[0]]: 85, [RANGES[1]]: 95, [RANGES[2]]: 100 },\n  { [KPI]: \"Project Milestones (%)\", [ACTUAL]: 68, [TARGET]: 80, [RANGES[0]]: 40, [RANGES[1]]: 70, [RANGES[2]]: 100 },\n];\n\n// Plot: reads only data and the constants above\n// Grayscale bands: ink at a falling opacity, darkest for the lowest range\nconst inkRgb = [1, 3, 5].map((i) => parseInt(t.ink.slice(i, i + 2), 16)).join(\", \");\nconst bandColors = RANGES.map((_, i) => {\n  const step = RANGES.length > 1 ? i / (RANGES.length - 1) : 0;\n  return `rgba(${inkRgb}, ${BAND_OPACITY_DARK - step * (BAND_OPACITY_DARK - BAND_OPACITY_LIGHT)})`;\n});\n\n// Each bullet runs on its own linear scale from zero to its largest value\nconst rows = data.map((d) => ({\n  label: d[KPI],\n  actual: d[ACTUAL],\n  target: d[TARGET],\n  bandWidths: RANGES.map((col, i) => d[col] - (i > 0 ? d[RANGES[i - 1]] : 0)),\n  max: Math.max(d[ACTUAL], d[TARGET], ...RANGES.map((col) => d[col])),\n}));\n\nconst { width: W, height: H } = window.ANYPLOT_SIZE;\nconst TITLE_H = 78;\nconst KEY_H = 40;\nconst LABEL_W = 300;\nconst VALUE_W = 160;\nconst SIDE_PAD = 40;\nconst X_AXIS_H = 34;\nconst CHART_W = W - 2 * SIDE_PAD - LABEL_W - VALUE_W;\nconst ROW_H = Math.floor((H - TITLE_H - KEY_H - 24) / rows.length);\n\nconst TITLE = \"bullet-basic · javascript · muix · anyplot.ai\";\nconst subtitle = `${rows.length} metrics · bands ${RANGES.join(\" / \")} · each bullet on its own scale from 0`;\n\nfunction MeasureAndTarget({ actual, target }: { actual: number; target: number }) {\n  const xScale = useXScale();\n  const area = useDrawingArea();\n  const mid = area.top + area.height / 2;\n  const barH = area.height * MEASURE_SHARE;\n  const targetH = area.height * TARGET_SHARE;\n  return (\n    <g>\n      <rect x={xScale(0)} y={mid - barH / 2} width={xScale(actual) - xScale(0)} height={barH} fill={t.palette[0]} />\n      <line x1={xScale(target)} x2={xScale(target)} y1={mid - targetH / 2} y2={mid + targetH / 2} stroke={t.ink} strokeWidth={4} />\n    </g>\n  );\n}\n\nexport default function Chart() {\n  return (\n    <div style={{ width: W, height: H, display: \"flex\", flexDirection: \"column\", background: t.pageBg, padding: `0 ${SIDE_PAD}px`, boxSizing: \"border-box\" }}>\n      <div style={{ height: TITLE_H, display: \"flex\", flexDirection: \"column\", alignItems: \"center\", justifyContent: \"center\", gap: 6, paddingTop: 10 }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{subtitle}</div>\n      </div>\n      <div style={{ height: KEY_H, display: \"flex\", alignItems: \"center\", gap: 24, marginLeft: LABEL_W, fontSize: 15, color: t.inkSoft }}>\n        {RANGES.map((name, i) => (\n          <div key={name} style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n            <span style={{ width: 16, height: 16, background: bandColors[i], borderRadius: 2 }} />\n            {name}\n          </div>\n        ))}\n        <div style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n          <span style={{ width: 26, height: 9, background: t.palette[0] }} />\n          {ACTUAL}\n        </div>\n        <div style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n          <span style={{ width: 4, height: 18, background: t.ink }} />\n          {TARGET}\n        </div>\n      </div>\n      {rows.map((row) => (\n        <div key={row.label} style={{ height: ROW_H, display: \"flex\", alignItems: \"center\" }}>\n          <div style={{ width: LABEL_W, paddingRight: 20, boxSizing: \"border-box\", textAlign: \"right\", fontSize: 16, fontWeight: 500, color: t.ink, marginBottom: X_AXIS_H }}>\n            {row.label}\n          </div>\n          <ChartsContainer\n            width={CHART_W}\n            height={ROW_H}\n            skipAnimation\n            margin={{ top: 14, bottom: 6, left: 12, right: 16 }}\n            xAxis={[\n              {\n                min: 0,\n                max: row.max,\n                height: X_AXIS_H,\n                tickNumber: 6,\n                tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n              },\n            ]}\n            yAxis={[{ scaleType: \"band\", data: [row.label], categoryGapRatio: 0, position: \"none\" }]}\n            series={row.bandWidths.map((value, i) => ({\n              type: \"bar\" as const,\n              id: `band-${i}`,\n              layout: \"horizontal\" as const,\n              stack: \"bands\",\n              data: [value],\n              color: bandColors[i],\n            }))}\n            sx={{\n              \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n              \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n            }}\n          >\n            <BarPlot skipAnimation />\n            <MeasureAndTarget actual={row.actual} target={row.target} />\n            <ChartsXAxis />\n          </ChartsContainer>\n          <div style={{ width: VALUE_W, paddingLeft: 16, boxSizing: \"border-box\", marginBottom: X_AXIS_H }}>\n            <span style={{ fontSize: 20, fontWeight: 600, color: t.ink }}>{row.actual}</span>\n            <span style={{ fontSize: 15, color: t.inkSoft }}>{`  ${TARGET} ${row.target}`}</span>\n          </div>\n        </div>\n      ))}\n    </div>\n  );\n}\n"}