UIPackage
Menu

Framework

Change language

Boilerplate repo

Heatmap

heatmapui

React mirror of @uipkge/heatmap — see the Vue registry item for the canonical description.

Also available for Vue ->

Installation

$npx shadcn@latest add https://uipkge.dev/r/react/heatmap.json
Named registry:npx shadcn@latest add @uipkge-react/heatmapInstalls to:components/ui/charts/heatmap/

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
data[number, number, number][] // [xIndex, yIndex, value]required
xLabelsstring[]required
yLabelsstring[]required
heightnumber | string300optional
minnumberoptional
maxnumberoptional
optionanyoptional
classNamestringoptional
ariaLabel

Accessible name announced for the chart image. Defaults to "Chart".

stringoptional

Schema

Type aliases from this item's source — use them to shape the data you pass in.

ChartTheme
interface ChartTheme {
  colors: string[]
  textColor: string
  axisColor: string
  splitLineColor: string
  tooltipBg: string
  tooltipBorder: string
  tooltipText: string
  bgColor: string
  /** The app's accent, for the one highlighted element on a chart or map —
   *  a selected route, a focused bar. Category series keep using `colors`. */
  accentColor: string
  /** Out-of-bounds / failure colour for charts that encode good vs bad
   *  rather than a category — control limits, error series. CSS-level marks
   *  can use `var(--destructive)` directly; this exists because ECharts needs
   *  a resolved colour string on the canvas. */
  dangerColor: string
}

npm dependencies

Used by

Files installed (4)

  • components/ui/charts/heatmap/Heatmap.tsx3.2 kB
    'use client'
    
    import * as React from 'react'
    import ReactECharts from 'echarts-for-react/esm/core'
    import { ChartFrame, EChart } from '../shared'
    import { useChartTheme } from '../useChartTheme'
    
    // Heatmap
    // ─────────────────────────────────────────────────────────────────────────
    
    export interface HeatmapProps {
      data: [number, number, number][] // [xIndex, yIndex, value]
      xLabels: string[]
      yLabels: string[]
      height?: number | string
      min?: number
      max?: number
      option?: any
      className?: string
      /** Accessible name announced for the chart image. Defaults to "Chart". */
      ariaLabel?: string
    }
    
    export const Heatmap = React.forwardRef<HTMLDivElement, HeatmapProps>(
      ({ data, xLabels, yLabels, height = 300, min, max, option, className, ariaLabel }, ref) => {
        const theme = useChartTheme()
    
        const mergedOption = React.useMemo(() => {
          const values = data.map((d) => d[2])
          const computedMin = min ?? Math.min(...values)
          const computedMax = max ?? Math.max(...values)
    
          return {
            grid: { left: 16, right: 16, top: 16, bottom: 16, containLabel: true },
            tooltip: {
              position: 'top',
              backgroundColor: theme.tooltipBg,
              borderColor: theme.tooltipBorder,
              textStyle: { color: theme.tooltipText, fontSize: 12 },
              formatter: (params: any) => `${yLabels[params.value[1]]} / ${xLabels[params.value[0]]}: ${params.value[2]}`,
            },
            xAxis: {
              type: 'category',
              data: xLabels,
              splitArea: { show: true },
              axisLabel: { fontSize: 11 },
              axisTick: { show: false },
            },
            yAxis: {
              type: 'category',
              data: yLabels,
              splitArea: { show: true },
              axisLabel: { fontSize: 11 },
              axisTick: { show: false },
            },
            visualMap: {
              min: computedMin,
              max: computedMax,
              calculable: true,
              orient: 'horizontal',
              left: 'center',
              bottom: 0,
              itemWidth: 12,
              itemHeight: 80,
              inRange: {
                color: [theme.colors[0]!, theme.colors[1]!, theme.colors[2]!],
              },
              textStyle: { fontSize: 10 },
            },
            series: (() => {
              const series = [
                {
                  type: 'heatmap',
                  data,
                  label: { show: false },
                  itemStyle: { borderRadius: 2, borderWidth: 0 },
                  emphasis: {
                    itemStyle: { shadowBlur: 10, shadowColor: 'rgba(0,0,0,0.2)' },
                  },
                },
              ]
              const userSeries = (option as any)?.series
              return Array.isArray(userSeries) ? series.map((s, i) => ({ ...s, ...(userSeries[i] ?? {}) })) : series
            })(),
            // visualMap / tooltip / axes overrides still spread on top -- strip
            // `series` so it doesn't clobber the merged result above.
            ...(() => {
              const { series: _, ...rest } = (option as any) ?? {}
              return rest
            })(),
          }
        }, [data, xLabels, yLabels, min, max, option, theme])
    
        return (
          <ChartFrame ref={ref} height={height} className={className} ariaLabel={ariaLabel}>
            <EChart option={mergedOption} />
          </ChartFrame>
        )
      },
    )
    Heatmap.displayName = 'Heatmap'
    
  • components/ui/charts/heatmap/index.ts0.1 kB
  • components/ui/charts/useChartTheme.ts8.7 kB
  • components/ui/charts/shared.tsx3.7 kB

Raw manifest:https://uipkge.dev/r/react/heatmap.json