{"spec_id":"heatmap-basic","library":"chartjs","language":"javascript","code":"// anyplot.ai\n// heatmap-basic: Basic Heatmap\n// Library: chartjs 4.6.1 | JavaScript 22.23.5\n// Quality: 82/100 | Updated: 2026-12-05\n//# anyplot-orientation: square\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Data — Pearson correlations between 10 macroeconomic indicators.\n// Diverging polarity (−1..+1) → Imprint diverging ramp (fields: cells fill the plot).\nconst labels = [\n  \"GDP\",\n  \"Unemploy.\",\n  \"Inflation\",\n  \"Interest\",\n  \"Stocks\",\n  \"Housing\",\n  \"Cons. Conf.\",\n  \"Exports\",\n  \"Imports\",\n  \"Wages\",\n];\nconst N = labels.length;\n\nconst matrix = [\n  [ 1.00, -0.62,  0.18,  0.31,  0.74,  0.66,  0.71,  0.58,  0.45, -0.14],\n  [-0.62,  1.00, -0.22, -0.48, -0.55, -0.59, -0.81, -0.41, -0.35, -0.07],\n  [ 0.18, -0.22,  1.00,  0.69, -0.07, -0.31, -0.42, -0.05,  0.21,  0.46],\n  [ 0.31, -0.48,  0.69,  1.00,  0.12, -0.15, -0.33,  0.07,  0.18,  0.39],\n  [ 0.74, -0.55, -0.07,  0.12,  1.00,  0.58,  0.79,  0.61,  0.40, -0.21],\n  [ 0.66, -0.59, -0.31, -0.15,  0.58,  1.00,  0.66,  0.42,  0.28, -0.18],\n  [ 0.71, -0.81, -0.42, -0.33,  0.79,  0.66,  1.00,  0.48,  0.37,  0.12],\n  [ 0.58, -0.41, -0.05,  0.07,  0.61,  0.42,  0.48,  1.00,  0.73,  0.24],\n  [ 0.45, -0.35,  0.21,  0.18,  0.40,  0.28,  0.37,  0.73,  1.00,  0.31],\n  [-0.14, -0.07,  0.46,  0.39, -0.21, -0.18,  0.12,  0.24,  0.31,  1.00],\n];\n\n// Plot\n\n// Color at v (0..1) along [position, hex] stops, blended linearly in sRGB.\nconst rampAt = (stops, v) => {\n  const x = Math.min(1, Math.max(0, v));\n  const i = Math.max(1, stops.findIndex(([p]) => p >= x));\n  const [[p0, c0], [p1, c1]] = [stops[i - 1], stops[i]];\n  const f = (x - p0) / (p1 - p0);\n  const ch = (c, k) => parseInt(c.slice(1 + 2 * k, 3 + 2 * k), 16);\n  return \"#\" + [0, 1, 2].map((k) => Math.round(ch(c0, k) + (ch(c1, k) - ch(c0, k)) * f).toString(16).padStart(2, \"0\")).join(\"\");\n};\nconst cellColor = (v) => rampAt(t.divFields, (v + 1) / 2);\n\n// WCAG relative luminance of a \"#rrggbb\" color.\nconst luminance = (hex) => {\n  const lin = (k) => {\n    const c = parseInt(hex.slice(1 + 2 * k, 3 + 2 * k), 16) / 255;\n    return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;\n  };\n  return 0.2126 * lin(0) + 0.7152 * lin(1) + 0.0722 * lin(2);\n};\nconst contrast = (a, b) => {\n  const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);\n  return (hi + 0.05) / (lo + 0.05);\n};\n// In-cell text: whichever of the theme's ink and page color contrasts more with the fill.\nconst textOn = (fill) => (contrast(fill, t.ink) >= contrast(fill, t.pageBg) ? t.ink : t.pageBg);\n\nconst FONT_STACK =\n  '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif';\n\n// Each column of the matrix is one stacked-bar dataset; every row is one bar\n// of unit length, so the bars tile into a grid and each segment is a cell.\nconst datasets = labels.map((name, j) => ({\n  label: name,\n  data: matrix.map(() => 1),\n  backgroundColor: matrix.map((row) => cellColor(row[j])),\n  borderColor: t.grid,\n  borderWidth: 1,\n  borderSkipped: false,\n  barPercentage: 1,\n  categoryPercentage: 1,\n}));\n\n// Draws the value inside every cell (text only) and the colorbar in the\n// right-hand padding.\nconst annotations = {\n  id: \"cell-values-colorbar\",\n  afterDatasetsDraw(chart) {\n    const { ctx, chartArea, width } = chart;\n    const { right, top, bottom } = chartArea;\n\n    ctx.save();\n    ctx.textAlign = \"center\";\n    ctx.textBaseline = \"middle\";\n    ctx.font = `500 16px ${FONT_STACK}`;\n    chart.data.datasets.forEach((_, j) => {\n      chart.getDatasetMeta(j).data.forEach((bar, i) => {\n        const v = matrix[i][j];\n        ctx.fillStyle = textOn(cellColor(v));\n        ctx.fillText(v.toFixed(2), bar.x - bar.width / 2, bar.y);\n      });\n    });\n    ctx.restore();\n\n    const cbLeft = right + 32;\n    const cbW = 24;\n    const cbH = bottom - top;\n    const grad = ctx.createLinearGradient(0, top, 0, bottom);\n    t.divFields.forEach(([p, hex]) => grad.addColorStop(1 - p, hex));\n    ctx.fillStyle = grad;\n    ctx.fillRect(cbLeft, top, cbW, cbH);\n\n    ctx.save();\n    ctx.font = `400 15px ${FONT_STACK}`;\n    ctx.fillStyle = t.inkSoft;\n    ctx.strokeStyle = t.inkSoft;\n    ctx.lineWidth = 1;\n    ctx.textAlign = \"left\";\n    ctx.textBaseline = \"middle\";\n    for (const v of [1, 0.5, 0, -0.5, -1]) {\n      const y = top + (1 - (v + 1) / 2) * cbH;\n      ctx.beginPath();\n      ctx.moveTo(cbLeft + cbW, y);\n      ctx.lineTo(cbLeft + cbW + 5, y);\n      ctx.stroke();\n      ctx.fillText(v.toFixed(1), cbLeft + cbW + 11, y);\n    }\n    ctx.restore();\n\n    // Colorbar title, rotated to read bottom-to-top.\n    ctx.save();\n    ctx.fillStyle = t.ink;\n    ctx.font = `500 17px ${FONT_STACK}`;\n    ctx.textAlign = \"center\";\n    ctx.textBaseline = \"middle\";\n    ctx.translate(width - 16, (top + bottom) / 2);\n    ctx.rotate(-Math.PI / 2);\n    ctx.fillText(\"Pearson correlation (r)\", 0, 0);\n    ctx.restore();\n  },\n};\n\nconst canvas = document.createElement(\"canvas\");\ndocument.getElementById(\"container\").appendChild(canvas);\n\nnew Chart(canvas, {\n  type: \"bar\",\n  data: { labels, datasets },\n  options: {\n    indexAxis: \"y\",\n    responsive: true,\n    maintainAspectRatio: false,\n    animation: false,\n    layout: { padding: { top: 4, right: 170, bottom: 4, left: 4 } },\n    plugins: {\n      title: {\n        display: true,\n        text: \"Macro Indicator Correlations · heatmap-basic · javascript · chartjs · anyplot.ai\",\n        color: t.ink,\n        font: { size: 20, weight: \"500\" },\n        padding: { top: 4, bottom: 18 },\n      },\n      legend: { display: false },\n      tooltip: { enabled: false },\n    },\n    scales: {\n      x: {\n        stacked: true,\n        min: 0,\n        max: N,\n        position: \"top\",\n        afterBuildTicks: (axis) => {\n          axis.ticks = Array.from({ length: N }, (_, j) => ({ value: j + 0.5 }));\n        },\n        ticks: {\n          color: t.inkSoft,\n          font: { size: 16 },\n          autoSkip: false,\n          callback: (v) => labels[Math.floor(v)],\n          maxRotation: 35,\n          minRotation: 35,\n        },\n        grid: { display: false },\n        border: { display: false },\n      },\n      y: {\n        stacked: true,\n        ticks: { color: t.inkSoft, font: { size: 16 }, autoSkip: false },\n        grid: { display: false },\n        border: { display: false },\n      },\n    },\n  },\n  plugins: [annotations],\n});\n"}