{"spec_id":"bump-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// bump-basic: Basic Bump Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst RANK_PAD_SHARE = 0.08; // rank-axis padding as a share of the rank span, keeps end marks inside\n\n// Columns: set these to your data's column names\nconst LANGUAGE = \"Language\"; // role: entity\nconst YEAR = \"Year\"; // role: period\nconst RANK = \"Popularity rank\"; // role: rank\n\n// Data: replace this block with your own records, one object per row\n// Programming-language popularity rank by year, 2018-2025 (each year ranks 1..6).\nconst data = [\n  { lang: \"JavaScript\", ranks: [1, 1, 1, 2, 2, 2, 2, 2] },\n  { lang: \"Python\", ranks: [3, 2, 2, 1, 1, 1, 1, 1] },\n  { lang: \"Java\", ranks: [2, 3, 3, 3, 3, 4, 4, 4] },\n  { lang: \"TypeScript\", ranks: [5, 5, 4, 4, 4, 3, 3, 3] },\n  { lang: \"Go\", ranks: [4, 4, 5, 5, 5, 5, 6, 6] },\n  { lang: \"Rust\", ranks: [6, 6, 6, 6, 6, 6, 5, 5] },\n].flatMap(({ lang, ranks }) =>\n  ranks.map((rank, i) => ({ [LANGUAGE]: lang, [YEAR]: String(2018 + i), [RANK]: rank })),\n);\n\n// Plot: reads only data and the constants above\n// Periods keep their order of appearance; entities are colored in order of first appearance\nconst periods = [...new Set(data.map((d) => d[YEAR]))];\nconst entities = [...new Set(data.map((d) => d[LANGUAGE]))];\nconst rankMap = new Map(data.map((d) => [`${d[LANGUAGE]}|${d[YEAR]}`, d[RANK]]));\nconst series = entities.map((entity, i) => ({\n  id: `entity-${i}`,\n  label: entity,\n  color: t.palette[i],\n  data: periods.map((p) => rankMap.get(`${entity}|${p}`) ?? null),\n  showMark: true,\n  shape: \"circle\" as const,\n  curve: \"monotoneX\" as const,\n}));\n\n// Whole-number rank ticks from 1 to the lowest rank in the data\nconst maxRank = Math.max(...data.map((d) => d[RANK]));\nconst rankTicks = Array.from({ length: maxRank }, (_, i) => i + 1);\nconst rankPad = Math.max(1, maxRank - 1) * RANK_PAD_SHARE;\n\n// Direct labels at each line's last ranked period\nconst endLabels = series.map((s) => {\n  const lastIndex = s.data.findLastIndex((v) => v !== null);\n  return { label: s.label, period: periods[lastIndex], rank: s.data[lastIndex] };\n});\n\nconst TITLE = \"Language Popularity Rankings · bump-basic · javascript · muix · anyplot.ai\";\nconst subtitle = `${RANK} of ${entities.length} ${LANGUAGE.toLowerCase()}s per ${YEAR.toLowerCase()}, ${periods[0]}–${periods[periods.length - 1]} · 1 = top`;\nconst TITLE_H = 80;\n\nfunction EndLabels() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const half = xScale.bandwidth() / 2;\n  return (\n    <g>\n      {endLabels.map((l) => (\n        <text\n          key={l.label}\n          x={xScale(l.period) + half + 16}\n          y={yScale(l.rank)}\n          dominantBaseline=\"middle\"\n          fontSize={16}\n          fontWeight={500}\n          fill={t.ink}\n        >\n          {l.label}\n        </text>\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\", background: t.pageBg }}>\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      <LineChart\n        width={width}\n        height={height - TITLE_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true }}\n        margin={{ top: 16, bottom: 16, left: 24, right: 140 }}\n        xAxis={[\n          {\n            scaleType: \"band\",\n            data: periods,\n            label: YEAR,\n            height: 64,\n            tickPlacement: \"middle\",\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            min: 1 - rankPad,\n            max: maxRank + rankPad,\n            reverse: true,\n            tickInterval: rankTicks,\n            valueFormatter: (v: number) => `#${v}`,\n            label: RANK,\n            width: 72,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        series={series}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 3.5 },\n          \"& .MuiLineChart-mark\": { r: 6.5, strokeWidth: 3 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      >\n        <EndLabels />\n      </LineChart>\n    </div>\n  );\n}\n"}