{"spec_id":"map-tilegrid","library":"muix","language":"javascript","code":"// anyplot.ai\n// map-tilegrid: Tile Grid Map for Equal-Area Geographic Comparison\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\nimport { ChartsDataProvider } from \"@mui/x-charts/ChartsDataProvider\";\nimport { ChartsSurface } from \"@mui/x-charts/ChartsSurface\";\nimport { ContinuousColorLegend } from \"@mui/x-charts/ChartsLegend\";\nimport { getLuminance } from \"@mui/material/styles\";\nimport { useXScale, useYScale, useZColorScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TILE_GAP_SHARE = 0.08; // share of the tile pitch left open between neighbouring tiles\nconst INK_SWITCH = 0.19; // relative luminance where #1A1A17 and #FAF8F1 reach equal contrast on a tile\nconst DARK_INK = \"#1A1A17\";\nconst LIGHT_INK = \"#FAF8F1\";\n\n// Columns: set these to your data's column names\nconst COUNTRY = \"Country\"; // role: region\nconst SHARE = \"Renewable electricity share (%)\"; // role: value\nconst ROW = \"Grid row\"; // role: row\nconst COL = \"Grid column\"; // role: col\n\n// Data: replace this block with your own records, one object per row\n// Renewable share of electricity by European country on a compass tile grid (row 0 = north, col 0 = west).\nconst data = [\n  [\"IS\", 0, 0, 99], [\"NO\", 0, 3, 98], [\"SE\", 0, 5, 65], [\"FI\", 0, 7, 55],\n  [\"IE\", 1, 0, 40], [\"GB\", 1, 1, 43], [\"DK\", 1, 4, 62], [\"EE\", 1, 8, 32],\n  [\"NL\", 2, 2, 37], [\"DE\", 2, 4, 46], [\"PL\", 2, 6, 18], [\"LT\", 2, 8, 28],\n  [\"BE\", 3, 1, 24], [\"CZ\", 3, 4, 17], [\"SK\", 3, 5, 23], [\"UA\", 3, 8, 9],\n  [\"FR\", 4, 1, 25], [\"CH\", 4, 3, 72], [\"AT\", 4, 4, 78], [\"HU\", 4, 5, 14], [\"RO\", 4, 7, 43],\n  [\"PT\", 5, 0, 61], [\"ES\", 5, 1, 46], [\"IT\", 5, 4, 35], [\"HR\", 5, 5, 55], [\"RS\", 5, 6, 30], [\"BG\", 5, 7, 19],\n  [\"GR\", 6, 6, 40],\n].map(([country, row, col, share]) => ({ [COUNTRY]: country, [ROW]: row, [COL]: col, [SHARE]: share }));\n\n// Plot: reads only data and the constants above\nconst tiles = data.filter((d) => d[ROW] != null && d[COL] != null);\nconst nCols = Math.max(...tiles.map((d) => d[COL])) + 1;\nconst nRows = Math.max(...tiles.map((d) => d[ROW])) + 1;\nconst shares = tiles.map((d) => d[SHARE]).filter((v) => v != null);\nconst minShare = Math.min(...shares);\nconst maxShare = Math.max(...shares);\nconst unit = SHARE.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst lowest = tiles.find((d) => d[SHARE] === minShare);\nconst highest = tiles.find((d) => d[SHARE] === maxShare);\n\nconst TITLE = \"map-tilegrid · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${tiles.length} countries, one equal tile each · ${SHARE} from ${minShare}${unit} (${lowest[COUNTRY]}) to ${maxShare}${unit} (${highest[COUNTRY]})`;\nconst HEADER_H = 80;\nconst LEGEND_W = 190;\nconst PAD = 24;\n\nfunction Tiles() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const colorScale = useZColorScale();\n  const pitch = xScale(1) - xScale(0);\n  const gap = pitch * TILE_GAP_SHARE;\n  const size = pitch - gap;\n  return (\n    <g>\n      {tiles.map((d) => {\n        const x = xScale(d[COL]) + gap / 2;\n        const y = yScale(d[ROW]) + gap / 2;\n        const fill = d[SHARE] == null ? t.elevatedBg : colorScale(d[SHARE]);\n        // Fixed ink by the tile's relative luminance, so the label stays legible in both themes\n        const ink = d[SHARE] == null ? t.inkSoft : getLuminance(fill) > INK_SWITCH ? DARK_INK : LIGHT_INK;\n        return (\n          <g key={d[COUNTRY]}>\n            <rect x={x} y={y} width={size} height={size} rx={size * 0.1} fill={fill} />\n            <text\n              x={x + size / 2}\n              y={y + size * 0.44}\n              textAnchor=\"middle\"\n              dominantBaseline=\"central\"\n              fontSize={Math.max(14, size * 0.3)}\n              fontWeight={700}\n              fill={ink}\n            >\n              {d[COUNTRY]}\n            </text>\n            <text x={x + size / 2} y={y + size * 0.76} textAnchor=\"middle\" dominantBaseline=\"central\" fontSize={15} fill={ink}>\n              {d[SHARE] == null ? \"–\" : `${d[SHARE]}${unit}`}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const bodyH = height - HEADER_H;\n  // One pitch on both axes keeps every tile square; the grid sits centred left of the colour bar\n  const pitch = Math.floor(Math.min((width - LEGEND_W - 3 * PAD) / nCols, (bodyH - 2 * PAD) / nRows));\n  const gridW = pitch * nCols;\n  const gridH = pitch * nRows;\n  const left = Math.round((width - LEGEND_W - PAD - gridW) / 2);\n  const top = Math.round((bodyH - gridH) / 2);\n  const margin = { top, left, right: width - left - gridW, bottom: bodyH - top - gridH };\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: HEADER_H, padding: `16px 40px 0 ${left}px`, boxSizing: \"border-box\" }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.25 }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft, marginTop: 6 }}>{SUBTITLE}</div>\n      </div>\n      <div style={{ position: \"relative\", width, height: bodyH }}>\n        <ChartsDataProvider\n          width={width}\n          height={bodyH}\n          margin={margin}\n          series={[]}\n          skipAnimation\n          xAxis={[{ id: \"col\", scaleType: \"linear\", min: 0, max: nCols, position: \"none\" }]}\n          yAxis={[{ id: \"row\", scaleType: \"linear\", min: 0, max: nRows, reverse: true, position: \"none\" }]}\n          zAxis={[\n            {\n              id: \"share\",\n              colorMap: { type: \"continuous\", min: minShare, max: maxShare, color: [t.seq[0], t.seq[1]] },\n            },\n          ]}\n        >\n          <ChartsSurface>\n            <Tiles />\n          </ChartsSurface>\n          <div\n            style={{\n              position: \"absolute\",\n              left: left + gridW + 2 * PAD,\n              top: top + gridH * 0.2,\n              width: LEGEND_W,\n              height: gridH * 0.6,\n              display: \"flex\",\n              flexDirection: \"column\",\n            }}\n          >\n            <div style={{ fontSize: 16, fontWeight: 500, color: t.ink, marginBottom: 12 }}>{SHARE}</div>\n            <ContinuousColorLegend\n              axisDirection=\"z\"\n              axisId=\"share\"\n              direction=\"vertical\"\n              thickness={20}\n              minLabel={`${minShare}${unit}`}\n              maxLabel={`${maxShare}${unit}`}\n              sx={{ flex: 1, color: t.inkSoft, \"& *\": { fontSize: 15 } }}\n            />\n          </div>\n        </ChartsDataProvider>\n      </div>\n    </div>\n  );\n}\n"}