UIPackage
Menu

Framework

Change language

Boilerplate repo

Calendar Heatmap

calendar-heatmapui

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

Also available for Vue ->

Installation

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

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
data

[date string YYYY-MM-DD, value] tuples.

[string, number][]required
rangestring | [string, string]required
heightnumber | string200optional
colorRange

Cell colour ramp [from, to] - default: chart-1 from light to saturated.

[string, string]optional
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/calendar-heatmap/CalendarHeatmap.tsx2.9 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'
    
    // CalendarHeatmap
    // ─────────────────────────────────────────────────────────────────────────
    
    export interface CalendarHeatmapProps {
      /** [date string YYYY-MM-DD, value] tuples. */
      data: [string, number][]
      range: string | [string, string]
      height?: number | string
      /** Cell colour ramp [from, to] - default: chart-1 from light to saturated. */
      colorRange?: [string, string]
      option?: any
      className?: string
      /** Accessible name announced for the chart image. Defaults to "Chart". */
      ariaLabel?: string
    }
    
    export const CalendarHeatmap = React.forwardRef<HTMLDivElement, CalendarHeatmapProps>(
      ({ data, range, height = 200, colorRange, option, className, ariaLabel }, ref) => {
        const theme = useChartTheme()
        const resolvedColorRange = colorRange ?? [theme.colors[0]!, theme.colors[3]!]
    
        const mergedOption = React.useMemo(() => {
          const maxValue = data.reduce((m, [, v]) => Math.max(m, v), 0) || 1
    
          return {
            color: theme.colors,
            tooltip: {
              position: 'top',
              formatter: (p: any) => `<strong>${p.value[0]}</strong><br>${p.value[1]} contributions`,
              backgroundColor: theme.tooltipBg,
              borderColor: theme.tooltipBorder,
              textStyle: { color: theme.tooltipText, fontSize: 12 },
            },
            visualMap: {
              show: false,
              min: 0,
              max: maxValue,
              inRange: { color: resolvedColorRange },
            },
            calendar: {
              top: 24,
              left: 36,
              right: 12,
              cellSize: ['auto', 14],
              range,
              itemStyle: { color: theme.splitLineColor, borderWidth: 0 },
              splitLine: { show: false },
              dayLabel: {
                color: theme.textColor,
                fontSize: 10,
                firstDay: 1,
                nameMap: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
              },
              monthLabel: { color: theme.textColor, fontSize: 10, fontWeight: 600 },
              yearLabel: { show: false },
            },
            series: (() => {
              const series = [{ type: 'heatmap', coordinateSystem: 'calendar', data }]
              const userSeries = (option as any)?.series
              return Array.isArray(userSeries) ? series.map((s, i) => ({ ...s, ...(userSeries[i] ?? {}) })) : series
            })(),
            // Strip `series` from the rest spread so the merge above isn't clobbered.
            ...(() => {
              const { series: _, ...rest } = (option as any) ?? {}
              return rest
            })(),
          }
        }, [data, range, resolvedColorRange, option, theme])
    
        return (
          <ChartFrame ref={ref} height={height} className={className} ariaLabel={ariaLabel}>
            <EChart option={mergedOption} />
          </ChartFrame>
        )
      },
    )
    CalendarHeatmap.displayName = 'CalendarHeatmap'
    
  • components/ui/charts/calendar-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/calendar-heatmap.json