{"spec_id":"cartogram-area-distortion","library":"highcharts","language":"javascript","code":"// anyplot.ai\n// cartogram-area-distortion: Cartogram with Area Distortion by Data Value\n// Library: highcharts 13.1.1 | JavaScript 22.23.4\n// Quality: 85/100 | Updated: 2026-11-08\n\nconst t = window.ANYPLOT_TOKENS;\n\n// --- Data (in-memory, deterministic) ----------------------------------------\n// All 50 US states and DC on a tile grid map (col = west→east, row =\n// north→south, the common statebins layout). Every state owns one equal-area\n// tile, and the tiles of neighbouring states touch; the circle drawn inside\n// each tile has area ∝ population, so its size against the tile is the area\n// distortion. Fill encodes a GDP-per-capita tier. population in millions\n// (2023 est.), gdpPerCapita in thousand USD, landArea in thousand sq mi.\nconst states = [\n  { name: \"AK\", col: 0, row: 0, population: 0.7, gdpPerCapita: 80, landArea: 571 },\n  { name: \"ME\", col: 10, row: 0, population: 1.4, gdpPerCapita: 56, landArea: 30.8 },\n  { name: \"WI\", col: 5, row: 1, population: 5.9, gdpPerCapita: 65, landArea: 54.2 },\n  { name: \"VT\", col: 9, row: 1, population: 0.6, gdpPerCapita: 57, landArea: 9.2 },\n  { name: \"NH\", col: 10, row: 1, population: 1.4, gdpPerCapita: 71, landArea: 9.0 },\n  { name: \"WA\", col: 0, row: 2, population: 7.9, gdpPerCapita: 87, landArea: 66.5 },\n  { name: \"ID\", col: 1, row: 2, population: 2.0, gdpPerCapita: 52, landArea: 82.6 },\n  { name: \"MT\", col: 2, row: 2, population: 1.1, gdpPerCapita: 62, landArea: 145.5 },\n  { name: \"ND\", col: 3, row: 2, population: 0.8, gdpPerCapita: 78, landArea: 69.0 },\n  { name: \"MN\", col: 4, row: 2, population: 5.7, gdpPerCapita: 77, landArea: 79.6 },\n  { name: \"IL\", col: 5, row: 2, population: 12.6, gdpPerCapita: 79, landArea: 55.5 },\n  { name: \"MI\", col: 6, row: 2, population: 10.0, gdpPerCapita: 60, landArea: 56.5 },\n  { name: \"NY\", col: 8, row: 2, population: 19.6, gdpPerCapita: 96, landArea: 47.1 },\n  { name: \"MA\", col: 9, row: 2, population: 7.0, gdpPerCapita: 91, landArea: 7.8 },\n  { name: \"OR\", col: 0, row: 3, population: 4.2, gdpPerCapita: 71, landArea: 96.0 },\n  { name: \"NV\", col: 1, row: 3, population: 3.2, gdpPerCapita: 66, landArea: 109.8 },\n  { name: \"WY\", col: 2, row: 3, population: 0.6, gdpPerCapita: 74, landArea: 97.1 },\n  { name: \"SD\", col: 3, row: 3, population: 0.9, gdpPerCapita: 68, landArea: 75.8 },\n  { name: \"IA\", col: 4, row: 3, population: 3.2, gdpPerCapita: 65, landArea: 55.9 },\n  { name: \"IN\", col: 5, row: 3, population: 6.8, gdpPerCapita: 63, landArea: 35.8 },\n  { name: \"OH\", col: 6, row: 3, population: 11.8, gdpPerCapita: 64, landArea: 40.9 },\n  { name: \"PA\", col: 7, row: 3, population: 13.0, gdpPerCapita: 65, landArea: 44.7 },\n  { name: \"NJ\", col: 8, row: 3, population: 9.3, gdpPerCapita: 74, landArea: 7.4 },\n  { name: \"CT\", col: 9, row: 3, population: 3.6, gdpPerCapita: 82, landArea: 4.8 },\n  { name: \"RI\", col: 10, row: 3, population: 1.1, gdpPerCapita: 61, landArea: 1.0 },\n  { name: \"CA\", col: 0, row: 4, population: 39.0, gdpPerCapita: 91, landArea: 155.8 },\n  { name: \"UT\", col: 1, row: 4, population: 3.4, gdpPerCapita: 70, landArea: 82.2 },\n  { name: \"CO\", col: 2, row: 4, population: 5.9, gdpPerCapita: 75, landArea: 103.6 },\n  { name: \"NE\", col: 3, row: 4, population: 2.0, gdpPerCapita: 72, landArea: 76.8 },\n  { name: \"MO\", col: 4, row: 4, population: 6.2, gdpPerCapita: 60, landArea: 68.7 },\n  { name: \"KY\", col: 5, row: 4, population: 4.5, gdpPerCapita: 53, landArea: 39.5 },\n  { name: \"WV\", col: 6, row: 4, population: 1.8, gdpPerCapita: 51, landArea: 24.0 },\n  { name: \"VA\", col: 7, row: 4, population: 8.7, gdpPerCapita: 69, landArea: 39.5 },\n  { name: \"MD\", col: 8, row: 4, population: 6.2, gdpPerCapita: 70, landArea: 9.7 },\n  { name: \"DE\", col: 9, row: 4, population: 1.0, gdpPerCapita: 78, landArea: 1.9 },\n  { name: \"AZ\", col: 1, row: 5, population: 7.4, gdpPerCapita: 55, landArea: 113.6 },\n  { name: \"NM\", col: 2, row: 5, population: 2.1, gdpPerCapita: 55, landArea: 121.3 },\n  { name: \"KS\", col: 3, row: 5, population: 2.9, gdpPerCapita: 65, landArea: 81.8 },\n  { name: \"AR\", col: 4, row: 5, population: 3.0, gdpPerCapita: 50, landArea: 52.0 },\n  { name: \"TN\", col: 5, row: 5, population: 7.1, gdpPerCapita: 60, landArea: 41.2 },\n  { name: \"NC\", col: 6, row: 5, population: 10.8, gdpPerCapita: 63, landArea: 48.6 },\n  { name: \"SC\", col: 7, row: 5, population: 5.4, gdpPerCapita: 52, landArea: 30.1 },\n  { name: \"DC\", col: 8, row: 5, population: 0.7, gdpPerCapita: 220, landArea: 0.06 },\n  { name: \"OK\", col: 3, row: 6, population: 4.0, gdpPerCapita: 58, landArea: 68.6 },\n  { name: \"LA\", col: 4, row: 6, population: 4.6, gdpPerCapita: 58, landArea: 43.2 },\n  { name: \"MS\", col: 5, row: 6, population: 2.9, gdpPerCapita: 44, landArea: 46.9 },\n  { name: \"AL\", col: 6, row: 6, population: 5.1, gdpPerCapita: 51, landArea: 50.6 },\n  { name: \"GA\", col: 7, row: 6, population: 11.0, gdpPerCapita: 64, landArea: 57.5 },\n  { name: \"HI\", col: 0, row: 7, population: 1.4, gdpPerCapita: 63, landArea: 6.4 },\n  { name: \"TX\", col: 3, row: 7, population: 30.5, gdpPerCapita: 74, landArea: 261.2 },\n  { name: \"FL\", col: 8, row: 7, population: 22.6, gdpPerCapita: 60, landArea: 53.6 },\n];\n\n// --- Layout ------------------------------------------------------------------\n// Fixed top and bottom margins make the plot height known before the render,\n// so the side margins can give every grid cell the same width and height:\n// square tiles that touch their neighbours, and a circle scale that fits them.\nconst cols = Math.max(...states.map((s) => s.col)) + 1;\nconst rows = Math.max(...states.map((s) => s.row)) + 1;\nconst MARGIN_TOP = 86;\nconst MARGIN_BOTTOM = 52;\nconst cell = (window.ANYPLOT_SIZE.height - MARGIN_TOP - MARGIN_BOTTOM) / rows;\nconst marginSide = (window.ANYPLOT_SIZE.width - cell * cols) / 2;\n\n// Area ∝ value → radius ∝ sqrt(value), with no additive minimum, so a state\n// with twice the population covers twice the area. The most populous state's\n// circle nearly fills its tile.\nconst maxPopulation = Math.max(...states.map((s) => s.population));\nconst MAX_RADIUS = cell * 0.47;\nconst LABEL_INSIDE_RADIUS = 15; // smaller circles carry their label below them\n\n// The undistorted reference: an inset of the same grid in the empty south-east\n// corner where each state's square has area ∝ its land area, so the reader\n// sees how big each state is on a normal map next to how big it is by people.\nconst INSET_CELL = 32;\nconst INSET_RIGHT = window.ANYPLOT_SIZE.width - 16;\nconst INSET_BOTTOM = window.ANYPLOT_SIZE.height - MARGIN_BOTTOM;\nconst insetLeft = INSET_RIGHT - INSET_CELL * cols;\nconst insetTop = INSET_BOTTOM - INSET_CELL * rows;\nconst maxLandArea = Math.max(...states.map((s) => s.landArea));\n\n// The core bundle has no colorAxis module, so the GDP-per-capita tiers are\n// three ordinary series: ordered steps of imprint_seq (low → high), each with\n// a native legend entry. Labels inside a circle take the ink that contrasts\n// with its fill (dark on green 5.1:1, white on teal 4.5:1 and blue 5.7:1).\nconst tiers = [\n  { name: \"GDP per capita < $60k\", below: 60, color: t.seq[0], label: \"#1A1A17\" },\n  {\n    name: \"GDP per capita $60–74k\",\n    below: 75,\n    color: Highcharts.color(t.seq[0]).tweenTo(Highcharts.color(t.seq[1]), 0.5),\n    label: \"#FFFFFF\",\n  },\n  { name: \"GDP per capita ≥ $75k\", below: Infinity, color: t.seq[1], label: \"#FFFFFF\" },\n];\n\nconst series = tiers.map((tier, i) => ({\n  name: tier.name,\n  color: tier.color,\n  data: states\n    .filter((s) => s.gdpPerCapita < tier.below && (i === 0 || s.gdpPerCapita >= tiers[i - 1].below))\n    .map((s) => {\n      const radius = MAX_RADIUS * Math.sqrt(s.population / maxPopulation);\n      const inside = radius >= LABEL_INSIDE_RADIUS;\n      return {\n        name: s.name,\n        x: s.col,\n        y: s.row,\n        marker: { radius },\n        custom: { population: s.population, gdpPerCapita: s.gdpPerCapita, landArea: s.landArea },\n        dataLabels: inside\n          ? { verticalAlign: \"middle\", style: { color: tier.label } }\n          : { verticalAlign: \"top\", distance: radius + 3, style: { color: t.ink } },\n      };\n    }),\n}));\n\n// Legend entry for the equal-area tiles, which the SVG renderer draws below.\nseries.push({\n  name: \"Equal-area tile, one per state\",\n  color: t.grid,\n  marker: { symbol: \"square\", radius: 8, fillColor: t.grid, lineWidth: 0 },\n  enableMouseTracking: false,\n  data: [],\n});\n\n// --- Chart -------------------------------------------------------------------\nHighcharts.chart(\"container\", {\n  palette: { colorScheme: t.theme },\n  chart: {\n    type: \"scatter\",\n    backgroundColor: \"transparent\",\n    animation: false,\n    marginTop: MARGIN_TOP,\n    marginBottom: MARGIN_BOTTOM,\n    marginLeft: marginSide,\n    marginRight: marginSide,\n    style: { fontFamily: \"inherit\" },\n    events: {\n      // One equal-area tile per state, exactly one grid cell in size, so the\n      // tiles of neighbouring states share their edges: the baseline each\n      // population-scaled circle is measured against. A page-coloured hairline\n      // keeps the individual tiles readable. The land-area inset follows in the\n      // same group. Redrawn on every render because the pixel positions follow\n      // the axes.\n      render: function () {\n        if (this.tileGroup) this.tileGroup.destroy();\n        const r = this.renderer;\n        this.tileGroup = r.g(\"reference-tiles\").attr({ zIndex: 1 }).add();\n        const xa = this.xAxis[0];\n        const ya = this.yAxis[0];\n        states.forEach((s) => {\n          const x0 = xa.toPixels(s.col - 0.5);\n          const y0 = ya.toPixels(s.row - 0.5);\n          r.rect(x0, y0, xa.toPixels(s.col + 0.5) - x0, ya.toPixels(s.row + 0.5) - y0)\n            .attr({ fill: t.grid, stroke: t.pageBg, \"stroke-width\": 1.5 })\n            .add(this.tileGroup);\n        });\n\n        // Inset: square side ∝ sqrt(land area), centred in its cell, so the\n        // square areas compare like the states on an ordinary map.\n        r.text(\"Undistorted: square area ∝ land area\", insetLeft, insetTop - 10)\n          .css({ color: t.inkSoft, fontSize: \"13px\", fontWeight: \"600\" })\n          .add(this.tileGroup);\n        states.forEach((s) => {\n          const side = INSET_CELL * 0.96 * Math.sqrt(s.landArea / maxLandArea);\n          const cx = insetLeft + (s.col + 0.5) * INSET_CELL;\n          const cy = insetTop + (s.row + 0.5) * INSET_CELL;\n          r.rect(cx - INSET_CELL / 2, cy - INSET_CELL / 2, INSET_CELL, INSET_CELL)\n            .attr({ fill: t.grid, stroke: t.pageBg, \"stroke-width\": 1 })\n            .add(this.tileGroup);\n          r.rect(cx - side / 2, cy - side / 2, side, side)\n            .attr({ fill: t.inkSoft })\n            .add(this.tileGroup);\n        });\n      },\n    },\n  },\n  credits: { enabled: false },\n  title: {\n    text: \"cartogram-area-distortion · javascript · highcharts · anyplot.ai\",\n    style: { color: t.ink, fontSize: \"22px\", fontWeight: \"600\" },\n  },\n  subtitle: {\n    text: \"US states and DC resized by population against a contiguous equal-area tile grid\",\n    style: { color: t.inkSoft, fontSize: \"14px\" },\n  },\n  caption: {\n    text: \"Circle area ∝ population (millions, 2023) · fill = GDP-per-capita tier · inset = the same grid sized by land area, the undistorted geography · tile position follows rough state geography\",\n    align: \"center\",\n    style: { color: t.inkSoft, fontSize: \"14px\" },\n  },\n  xAxis: {\n    min: -0.5,\n    max: cols - 0.5,\n    visible: false,\n  },\n  yAxis: {\n    min: -0.5,\n    max: rows - 0.5,\n    startOnTick: false,\n    endOnTick: false,\n    reversed: true,\n    visible: false,\n  },\n  // The legend floats over the empty tiles north of the Great Plains, inside\n  // the grid's own footprint.\n  legend: {\n    enabled: true,\n    floating: true,\n    layout: \"vertical\",\n    align: \"left\",\n    verticalAlign: \"top\",\n    x: marginSide + cell,\n    y: MARGIN_TOP + 4,\n    itemMarginBottom: 6,\n    itemStyle: { color: t.inkSoft, fontSize: \"14px\", fontWeight: \"400\" },\n    itemHoverStyle: { color: t.ink },\n  },\n  plotOptions: {\n    series: { animation: false },\n    scatter: {\n      // The series radius sizes the legend dots; every point sets its own.\n      marker: { symbol: \"circle\", radius: 7, lineWidth: 0 },\n      dataLabels: {\n        enabled: true,\n        format: \"{point.name}\",\n        align: \"center\",\n        style: { fontSize: \"13px\", fontWeight: \"600\", textOutline: \"none\" },\n      },\n    },\n  },\n  tooltip: {\n    pointFormat:\n      \"Population: {point.custom.population}M<br/>GDP per capita: ${point.custom.gdpPerCapita}k<br/>Land area: {point.custom.landArea}k sq mi\",\n  },\n  series,\n});\n"}