{"spec_id":"cartogram-area-distortion","library":"muix","language":"javascript","code":"// anyplot.ai\n// cartogram-area-distortion: Cartogram with Area Distortion by Data Value\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\nimport { ChartsDataProvider } from \"@mui/x-charts/ChartsDataProvider\";\nimport { ChartsWrapper } from \"@mui/x-charts/ChartsWrapper\";\nimport { ChartsSurface } from \"@mui/x-charts/ChartsSurface\";\nimport { ChartsLegend } from \"@mui/x-charts/ChartsLegend\";\nimport { ChartsTooltip } from \"@mui/x-charts/ChartsTooltip\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst AREA_SHARE = 0.45; // all circles together cover this share of the true outline's area\nconst GAP_SHARE = 0.03; // free space between touching circles, as a share of the largest radius\nconst ITERATIONS = 400; // separation steps of the Dorling layout\nconst PULL = 0.08; // per-step drift back toward the centroid at the first step, falling to zero by the last\nconst PAD_SHARE = 0.03; // map padding as a share of the span of the outline and the circles\nconst LABEL_MIN_RADIUS = 12; // px: smaller circles stay unlabeled, a 14 px abbreviation would not fit\nconst LABEL_DARK = \"#1A1A17\";\nconst LABEL_LIGHT = \"#FAF8F1\";\n\n// Columns: set these to your data's column names\nconst STATE = \"State\"; // role: region\nconst ABBR = \"Abbreviation\"; // role: label\nconst LON = \"Centroid longitude\"; // role: geometry\nconst LAT = \"Centroid latitude\"; // role: geometry\nconst POPULATION = \"Population (millions)\"; // role: value\nconst CENSUS_REGION = \"Census region\"; // role: extra\n\n// Data: replace this block with your own records, one object per row\n// 2023 US Census population estimates for the 48 contiguous states,\n// each at its approximate geographic centroid.\nconst data = [\n  { [STATE]: \"California\", [ABBR]: \"CA\", [LON]: -119.4, [LAT]: 36.8, [POPULATION]: 38.9, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Texas\", [ABBR]: \"TX\", [LON]: -99.3, [LAT]: 31.0, [POPULATION]: 30.5, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Florida\", [ABBR]: \"FL\", [LON]: -81.6, [LAT]: 27.8, [POPULATION]: 22.6, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"New York\", [ABBR]: \"NY\", [LON]: -75.5, [LAT]: 43.0, [POPULATION]: 19.6, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Pennsylvania\", [ABBR]: \"PA\", [LON]: -77.8, [LAT]: 40.9, [POPULATION]: 13.0, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Illinois\", [ABBR]: \"IL\", [LON]: -89.2, [LAT]: 40.0, [POPULATION]: 12.5, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Ohio\", [ABBR]: \"OH\", [LON]: -82.8, [LAT]: 40.4, [POPULATION]: 11.8, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Georgia\", [ABBR]: \"GA\", [LON]: -83.5, [LAT]: 32.6, [POPULATION]: 11.0, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"North Carolina\", [ABBR]: \"NC\", [LON]: -79.4, [LAT]: 35.5, [POPULATION]: 10.8, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Michigan\", [ABBR]: \"MI\", [LON]: -85.0, [LAT]: 44.3, [POPULATION]: 10.0, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"New Jersey\", [ABBR]: \"NJ\", [LON]: -74.7, [LAT]: 40.1, [POPULATION]: 9.3, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Virginia\", [ABBR]: \"VA\", [LON]: -78.2, [LAT]: 37.5, [POPULATION]: 8.7, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Washington\", [ABBR]: \"WA\", [LON]: -120.4, [LAT]: 47.0, [POPULATION]: 7.8, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Arizona\", [ABBR]: \"AZ\", [LON]: -111.4, [LAT]: 34.0, [POPULATION]: 7.4, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Tennessee\", [ABBR]: \"TN\", [LON]: -86.3, [LAT]: 35.8, [POPULATION]: 7.1, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Massachusetts\", [ABBR]: \"MA\", [LON]: -72.0, [LAT]: 42.4, [POPULATION]: 7.0, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Indiana\", [ABBR]: \"IN\", [LON]: -86.1, [LAT]: 39.9, [POPULATION]: 6.8, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Maryland\", [ABBR]: \"MD\", [LON]: -76.8, [LAT]: 39.0, [POPULATION]: 6.2, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Missouri\", [ABBR]: \"MO\", [LON]: -92.5, [LAT]: 38.5, [POPULATION]: 6.2, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Wisconsin\", [ABBR]: \"WI\", [LON]: -89.6, [LAT]: 44.6, [POPULATION]: 5.9, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Colorado\", [ABBR]: \"CO\", [LON]: -105.5, [LAT]: 39.0, [POPULATION]: 5.9, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Minnesota\", [ABBR]: \"MN\", [LON]: -94.3, [LAT]: 46.0, [POPULATION]: 5.7, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"South Carolina\", [ABBR]: \"SC\", [LON]: -80.9, [LAT]: 33.9, [POPULATION]: 5.4, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Alabama\", [ABBR]: \"AL\", [LON]: -86.8, [LAT]: 32.8, [POPULATION]: 5.1, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Louisiana\", [ABBR]: \"LA\", [LON]: -92.0, [LAT]: 31.0, [POPULATION]: 4.6, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Kentucky\", [ABBR]: \"KY\", [LON]: -85.3, [LAT]: 37.5, [POPULATION]: 4.5, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Oregon\", [ABBR]: \"OR\", [LON]: -120.6, [LAT]: 43.9, [POPULATION]: 4.2, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Oklahoma\", [ABBR]: \"OK\", [LON]: -97.5, [LAT]: 35.6, [POPULATION]: 4.1, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Connecticut\", [ABBR]: \"CT\", [LON]: -72.7, [LAT]: 41.6, [POPULATION]: 3.6, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Utah\", [ABBR]: \"UT\", [LON]: -111.7, [LAT]: 39.3, [POPULATION]: 3.4, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Iowa\", [ABBR]: \"IA\", [LON]: -93.5, [LAT]: 42.1, [POPULATION]: 3.2, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Nevada\", [ABBR]: \"NV\", [LON]: -116.6, [LAT]: 39.3, [POPULATION]: 3.2, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Arkansas\", [ABBR]: \"AR\", [LON]: -92.4, [LAT]: 34.9, [POPULATION]: 3.1, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Mississippi\", [ABBR]: \"MS\", [LON]: -89.7, [LAT]: 32.7, [POPULATION]: 2.9, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"Kansas\", [ABBR]: \"KS\", [LON]: -98.4, [LAT]: 38.5, [POPULATION]: 2.9, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"New Mexico\", [ABBR]: \"NM\", [LON]: -106.1, [LAT]: 34.4, [POPULATION]: 2.1, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Nebraska\", [ABBR]: \"NE\", [LON]: -99.8, [LAT]: 41.5, [POPULATION]: 2.0, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Idaho\", [ABBR]: \"ID\", [LON]: -114.6, [LAT]: 44.4, [POPULATION]: 2.0, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"West Virginia\", [ABBR]: \"WV\", [LON]: -80.6, [LAT]: 38.6, [POPULATION]: 1.8, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"New Hampshire\", [ABBR]: \"NH\", [LON]: -71.6, [LAT]: 43.7, [POPULATION]: 1.4, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Maine\", [ABBR]: \"ME\", [LON]: -69.2, [LAT]: 45.4, [POPULATION]: 1.4, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Montana\", [ABBR]: \"MT\", [LON]: -109.6, [LAT]: 47.0, [POPULATION]: 1.1, [CENSUS_REGION]: \"West\" },\n  { [STATE]: \"Rhode Island\", [ABBR]: \"RI\", [LON]: -71.5, [LAT]: 41.7, [POPULATION]: 1.1, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Delaware\", [ABBR]: \"DE\", [LON]: -75.5, [LAT]: 39.0, [POPULATION]: 1.0, [CENSUS_REGION]: \"South\" },\n  { [STATE]: \"South Dakota\", [ABBR]: \"SD\", [LON]: -100.2, [LAT]: 44.4, [POPULATION]: 0.9, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"North Dakota\", [ABBR]: \"ND\", [LON]: -100.5, [LAT]: 47.5, [POPULATION]: 0.8, [CENSUS_REGION]: \"Midwest\" },\n  { [STATE]: \"Vermont\", [ABBR]: \"VT\", [LON]: -72.7, [LAT]: 44.0, [POPULATION]: 0.6, [CENSUS_REGION]: \"Northeast\" },\n  { [STATE]: \"Wyoming\", [ABBR]: \"WY\", [LON]: -107.6, [LAT]: 43.0, [POPULATION]: 0.6, [CENSUS_REGION]: \"West\" },\n];\n\n// Reference geography: simplified outline of the contiguous United States as a\n// [longitude, latitude] ring (Canada border, Atlantic and Gulf coasts, Mexico border, Pacific coast)\nconst US_OUTLINE: [number, number][] = [\n  [-123.3, 49], [-95.2, 49], [-89.6, 48], [-84.5, 46.5], [-82.4, 45], [-82.5, 43], [-83.1, 42], [-79, 42.8],\n  [-79, 43.3], [-76.3, 44.2], [-74.7, 45], [-71.5, 45], [-70, 46.7], [-69.2, 47.4], [-68, 47.3], [-67.8, 45.7],\n  [-67, 44.9], [-70, 43.5], [-70, 41.8], [-74, 40.5], [-76, 38], [-76, 35], [-78.5, 33.8], [-81, 31.5], [-80, 27],\n  [-80.5, 25.2], [-82, 26.5], [-82.8, 28], [-84, 30], [-86, 30.4], [-89, 30.2], [-90, 29], [-94, 29.7],\n  [-97.2, 27.5], [-97.2, 25.9], [-99.5, 27.5], [-101.4, 29.8], [-103, 29], [-104.5, 29.6], [-106.5, 31.8],\n  [-108.2, 31.3], [-111, 31.3], [-114.7, 32.7], [-117.1, 32.5], [-118.5, 34], [-120.6, 34.6], [-122.5, 37.7],\n  [-124, 40.4], [-124.2, 43], [-124, 46.2], [-124.7, 48.4],\n];\n\n// Plot: reads only data, the outline and the constants above\nconst { width: W, height: H } = window.ANYPLOT_SIZE;\nconst TITLE_H = 80;\nconst LEGEND_H = 44; // the region legend above the map adds its own height\nconst SIZE_LEGEND_W = 230;\nconst MARGIN = { top: 16, right: 16, bottom: 24, left: 40 };\nconst CHART_W = W - SIZE_LEGEND_W;\nconst CHART_H = H - TITLE_H - LEGEND_H;\nconst DRAW_W = CHART_W - MARGIN.left - MARGIN.right;\nconst DRAW_H = CHART_H - MARGIN.top - MARGIN.bottom;\n\n// Layout plane: equirectangular at the centroids' mean latitude, longitude scaled by\n// cos(mean latitude), so one unit is one degree of latitude on both axes and areas compare\nconst cosMeanLat = Math.cos(((data.reduce((s, d) => s + d[LAT], 0) / data.length) * Math.PI) / 180);\nconst home = data.map((d) => ({ x: d[LON] * cosMeanLat, y: d[LAT] }));\nconst outline = US_OUTLINE.map(([lon, lat]) => ({ x: lon * cosMeanLat, y: lat }));\nconst outlineArea =\n  Math.abs(outline.reduce((s, p, i) => s + p.x * outline[(i + 1) % outline.length].y - outline[(i + 1) % outline.length].x * p.y, 0)) / 2;\n\n// Dorling circles: area proportional to the value, all of them together AREA_SHARE of the outline's area\nconst values = data.map((d) => d[POPULATION]);\nconst total = values.reduce((s, v) => s + v, 0);\nconst maxValue = Math.max(...values);\nconst radius = values.map((v) => Math.sqrt((AREA_SHARE * outlineArea * v) / (total * Math.PI)));\nconst gap = GAP_SHARE * Math.max(...radius);\n\n// Separation pass, deterministic: each step pulls every circle back toward its centroid, with a\n// strength that falls to zero, then pushes every overlapping pair apart along the line between their\n// centers, the smaller circle moving more. Neighbors stay close and the last steps leave no overlap.\nconst pos = home.map((p) => ({ ...p }));\nfor (let step = 0; step < ITERATIONS; step += 1) {\n  const pull = PULL * (1 - step / (ITERATIONS - 1));\n  pos.forEach((p, i) => {\n    p.x += (home[i].x - p.x) * pull;\n    p.y += (home[i].y - p.y) * pull;\n  });\n  for (let i = 0; i < pos.length; i += 1) {\n    for (let j = i + 1; j < pos.length; j += 1) {\n      const dx = pos[j].x - pos[i].x;\n      const dy = pos[j].y - pos[i].y;\n      const dist = Math.hypot(dx, dy);\n      const overlap = radius[i] + radius[j] + gap - dist;\n      if (overlap > 0) {\n        // two circles on the same spot split along a direction fixed by their indices\n        const ux = dist > 0 ? dx / dist : Math.cos(i + j);\n        const uy = dist > 0 ? dy / dist : Math.sin(i + j);\n        const shareI = radius[j] ** 2 / (radius[i] ** 2 + radius[j] ** 2);\n        pos[i].x -= ux * overlap * shareI;\n        pos[i].y -= uy * overlap * shareI;\n        pos[j].x += ux * overlap * (1 - shareI);\n        pos[j].y += uy * overlap * (1 - shareI);\n      }\n    }\n  }\n}\n\n// View: the padded extent of the outline and the circles, grown along one axis until it fills the\n// drawing area; px per degree of latitude is px per degree of longitude divided by cos(mean latitude)\nconst extentX = [...outline.map((p) => p.x), ...pos.flatMap((p, i) => [p.x - radius[i], p.x + radius[i]])];\nconst extentY = [...outline.map((p) => p.y), ...pos.flatMap((p, i) => [p.y - radius[i], p.y + radius[i]])];\nconst [xMin, xMax, yMin, yMax] = [Math.min(...extentX), Math.max(...extentX), Math.min(...extentY), Math.max(...extentY)];\nconst spanX = Math.max(xMax - xMin, ((yMax - yMin) * DRAW_W) / DRAW_H) * (1 + 2 * PAD_SHARE);\nconst spanY = (spanX * DRAW_H) / DRAW_W;\nconst pxPerUnit = DRAW_W / spanX;\nconst maxRadius = Math.max(...radius) * pxPerUnit;\n\n// Census regions in order of first appearance; each label ink is the one with the higher\n// contrast against its region's color\nconst regions = [...new Set(data.map((d) => d[CENSUS_REGION]))];\nconst labelInk = regions.map((_, i) => {\n  const [r, g, b] = [1, 3, 5]\n    .map((k) => parseInt(t.palette[i].slice(k, k + 2), 16) / 255)\n    .map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));\n  const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;\n  return (lum + 0.05) / 0.06 >= 0.98 / (lum + 0.05) ? LABEL_DARK : LABEL_LIGHT;\n});\n// Each circle back in longitude and latitude, the coordinates of the axes\nconst placed = data.map((d, i) => ({\n  state: d[STATE],\n  text: d[ABBR],\n  region: d[CENSUS_REGION],\n  value: d[POPULATION],\n  lon: pos[i].x / cosMeanLat,\n  lat: pos[i].y,\n  r: radius[i] * pxPerUnit,\n}));\nconst series = regions.map((region, i) => ({\n  type: \"scatter\" as const,\n  id: `region-${i}`,\n  label: region,\n  color: t.palette[i],\n  sizeAxisId: \"value\",\n  data: placed.filter((c) => c.region === region).map((c) => ({ id: c.state, x: c.lon, y: c.lat, sizeValue: c.value })),\n  valueFormatter: (v: { id: string; sizeValue: number }) => `${v.id} · ${POPULATION} ${v.sizeValue}`,\n}));\nconst labels = placed\n  .filter((c) => c.r >= LABEL_MIN_RADIUS)\n  .map((c) => ({ ...c, ink: labelInk[regions.indexOf(c.region)] }));\n\n// Size legend: three reference values, the largest rounded down to one significant digit\nconst magnitude = 10 ** Math.floor(Math.log10(maxValue));\nconst topRef = Math.floor(maxValue / magnitude) * magnitude;\nconst sizeRefs = [topRef / 10, topRef / 3, topRef].map((v) => Number(v.toPrecision(1)));\nconst sizeRefRadii = sizeRefs.map((v) => maxRadius * Math.sqrt(v / maxValue));\n\nconst TITLE = \"cartogram-area-distortion · javascript · muix · anyplot.ai\";\nconst subtitle = `Dorling cartogram of ${data.length} ${STATE.toLowerCase()}s · circle area proportional to ${POPULATION.toLowerCase()} · circles pushed apart from their centroids until none overlap · color by ${CENSUS_REGION.toLowerCase()}`;\n\nfunction ReferenceOutline() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  return (\n    <path\n      d={`M${US_OUTLINE.map(([lon, lat]) => `${xScale(lon)},${yScale(lat)}`).join(\"L\")}Z`}\n      fill={t.grid}\n      stroke={t.inkSoft}\n      strokeOpacity={0.6}\n      strokeWidth={1}\n    />\n  );\n}\n\nfunction StateLabels() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  return (\n    <g pointerEvents=\"none\">\n      {labels.map((l) => (\n        <text\n          key={l.text}\n          x={xScale(l.lon)}\n          y={yScale(l.lat)}\n          textAnchor=\"middle\"\n          dominantBaseline=\"central\"\n          fontSize={l.r >= 2 * LABEL_MIN_RADIUS ? 16 : 14}\n          fontWeight={700}\n          fill={l.ink}\n        >\n          {l.text}\n        </text>\n      ))}\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 }}>\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={{ display: \"flex\", flexDirection: \"row\" }}>\n        <ChartsDataProvider\n          width={CHART_W}\n          height={CHART_H}\n          margin={MARGIN}\n          series={series}\n          xAxis={[{ min: (xMin + xMax - spanX) / 2 / cosMeanLat, max: (xMin + xMax + spanX) / 2 / cosMeanLat, position: \"none\" }]}\n          yAxis={[{ min: (yMin + yMax - spanY) / 2, max: (yMin + yMax + spanY) / 2, position: \"none\" }]}\n          zAxis={[\n            {\n              id: \"value\",\n              sizeMap: { type: \"continuous\", min: 0, max: maxValue, size: (share: number) => maxRadius * Math.sqrt(share) },\n            },\n          ]}\n          skipAnimation\n        >\n          <ChartsWrapper legendPosition={{ vertical: \"top\", horizontal: \"center\" }} legendDirection=\"horizontal\">\n            <ChartsLegend sx={{ fontSize: 15, color: t.inkSoft }} />\n            <ChartsSurface sx={{ \"& .MuiScatterChart-marker\": { stroke: t.pageBg, strokeWidth: 1.5 } }}>\n              <ReferenceOutline />\n              <ScatterPlot />\n              <StateLabels />\n            </ChartsSurface>\n            <ChartsTooltip trigger=\"item\" />\n          </ChartsWrapper>\n        </ChartsDataProvider>\n        <div style={{ width: SIZE_LEGEND_W, display: \"flex\", flexDirection: \"column\", justifyContent: \"center\", paddingRight: 24, boxSizing: \"border-box\" }}>\n          <div style={{ fontSize: 15, color: t.inkSoft, marginBottom: 14 }}>{POPULATION}</div>\n          {sizeRefs.map((v, i) => (\n            <div key={v} style={{ display: \"flex\", alignItems: \"center\", gap: 14, marginBottom: 12 }}>\n              <svg width={2 * sizeRefRadii[sizeRefRadii.length - 1] + 2} height={2 * sizeRefRadii[i] + 2}>\n                <circle\n                  cx={sizeRefRadii[sizeRefRadii.length - 1] + 1}\n                  cy={sizeRefRadii[i] + 1}\n                  r={sizeRefRadii[i]}\n                  fill={t.inkSoft}\n                  stroke={t.pageBg}\n                  strokeWidth={1.5}\n                />\n              </svg>\n              <span style={{ fontSize: 15, color: t.inkSoft }}>{v}</span>\n            </div>\n          ))}\n          <div style={{ fontSize: 14, color: t.inkSoft, marginTop: 6 }}>Shaded outline: true geography</div>\n        </div>\n      </div>\n    </div>\n  );\n}\n"}