Heatmap
ChartsColor-coded data density heatmap grid.
Preview
0
19.79185702081816
38.449806808524734
95.81472452513718
4.478342659776747
46.14898600184637
81.15607139490206
54.54091068712672
5.3648148616727624
20.885690456619344
92.8611616351462
12.300798655592693
94.85917091708362
1
19.225226184901246
50.24683788655917
50.58098067132569
59.83128067855652
13.207354843388808
0.6514516335572873
57.262059438322645
84.55723874695538
88.55465242288993
34.6545384475436
0.4448739418534964
93.96609855340057
2
72.43183506692357
24.46855728727132
24.882921226592636
4.727721821597686
5.090466077870381
47.78410770259149
23.9830409290119
86.1919594726123
38.722592726486795
98.56887179516421
26.428757462272568
10.557814174027836
3
54.409919115513375
72.7690716282316
82.67494303402472
23.399833684980788
16.38630338421819
74.5319208524119
48.395155968981875
57.32922963400756
63.46410863640509
93.88744537401818
53.80803177000468
98.43346486319851
4
69.28422167490578
8.183793645037873
54.472567833404426
35.818597869760914
83.3787920743814
65.78210772268179
17.114178410357027
54.29828207300685
62.249556164272605
57.44497214111235
57.356329192760036
10.156799659754368
5
40.97560496026534
70.02314038836424
50.10487269088275
14.333714159011436
56.17840490963511
29.716717053789054
71.29320452238979
75.28622421277348
98.46932339383062
32.92112838978613
80.83117486644028
31.445393439745516
6
91.61695786311878
53.48387001458131
14.87570127754273
62.49999036088665
13.943750893212615
73.04344208392696
41.60923900974917
34.7752992760383
77.62197205831625
37.91298860431519
26.26337877069751
46.968535800997934
Usage
example.jsx
import { Heatmap } from "@/components/ui/heatmap";
export default function Example() {
return <Heatmap />;
}Source Code
Copy this file into components/ui/heatmap.jsx in your project.
heatmap.jsx
"use client";
import { forwardRef } from "react";
import { cn } from "@/lib/utils";
const Heatmap = forwardRef(({ className, data = [], xLabels = [], yLabels = [], colors = ["#f0fdf4", "#166534"], ...props }, ref) => {
const maxVal = Math.max(...data.flat(), 1);
const interpolate = (val) => {
const ratio = val / maxVal;
return `opacity: ${0.15 + ratio * 0.85}`;
};
return (
<div ref={ref} className={cn("overflow-auto", className)} {...props}>
<div className="inline-grid" style={{ gridTemplateColumns: `auto repeat(${xLabels.length || (data[0]?.length || 0)}, 2rem)` }}>
<div />
{xLabels.map((l, i) => <div key={i} className="text-center text-[10px] text-muted-foreground">{l}</div>)}
{data.map((row, ri) => (
<>
<div key={`l${ri}`} className="flex items-center pr-2 text-[10px] text-muted-foreground">{yLabels[ri] || ri}</div>
{row.map((val, ci) => (
<div key={`${ri}-${ci}`} className="flex h-8 w-8 items-center justify-center rounded-sm bg-primary text-[9px] font-medium text-primary-foreground"
style={{ opacity: 0.15 + (val / maxVal) * 0.85 }}
title={`${val}`}
>{val}</div>
))}
</>
))}
</div>
</div>
);
});
Heatmap.displayName = "Heatmap";
export { Heatmap };
Quick Install
Make sure you have the cn() utility set up. It requires clsx and tailwind-merge.
npm install clsx tailwind-merge