UIPackage
Menu

Framework

Change language

Boilerplate repo

Histogram Chart

histogram-chartui

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

Also available for Vue ->

Installation

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

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
data

Pre-binned data. Pass raw numbers via `values` + `bins` instead.

{ bin: string; count: number }[]optional
values

Raw values to bin automatically. Ignored when `data` is set.

number[]optional
bins

Bin count for auto-binning. Default 12.

number12optional
heightnumber | string300optional
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/histogram-chart/HistogramChart.tsx3.7 kB
    'use client'
    
    import * as React from 'react'
    import { ChartFrame, EChart } from '../shared'
    import { useChartTheme, mergeOptionBlock } from '../useChartTheme'
    
    // HistogramChart
    // ─────────────────────────────────────────────────────────────────────────
    
    export interface HistogramChartProps {
      /** Pre-binned data. Pass raw numbers via `values` + `bins` instead. */
      data?: { bin: string; count: number }[]
      /** Raw values to bin automatically. Ignored when `data` is set. */
      values?: number[]
      /** Bin count for auto-binning. Default 12. */
      bins?: number
      height?: number | string
      option?: any
      className?: string
      /** Accessible name announced for the chart image. Defaults to "Chart". */
      ariaLabel?: string
    }
    
    export const HistogramChart = React.forwardRef<HTMLDivElement, HistogramChartProps>(
      ({ data, values, bins = 12, height = 300, option, className, ariaLabel }, ref) => {
        const theme = useChartTheme()
    
        const binned = React.useMemo(() => {
          if (data?.length) return data
          const vals = values ?? []
          if (!vals.length) return []
          const min = Math.min(...vals)
          const max = Math.max(...vals)
          const span = max - min || 1
          const n = Math.max(1, bins)
          const counts: number[] = Array(n).fill(0)
          for (const v of vals) counts[Math.min(n - 1, Math.floor(((v - min) / span) * n))]++
          return counts.map((count, i) => ({
            bin: `${(min + (span * i) / n).toFixed(1)}${(min + (span * (i + 1)) / n).toFixed(1)}`,
            count,
          }))
        }, [data, values, bins])
    
        const mergedOption = React.useMemo(() => {
          const peak = binned.reduce((m, d, i) => (d.count > (binned[m]?.count ?? -1) ? i : m), 0)
          const series = [
            {
              type: 'bar',
              barCategoryGap: '2%',
              itemStyle: {
                color: (p: any) => (p.dataIndex === peak ? theme.colors[0] : theme.colors[2]),
                borderRadius: [3, 3, 3, 3],
              },
              data: binned.map((d) => d.count),
            },
          ]
          const userOption: any = option ?? {}
          const {
            series: userSeries,
            xAxis: userXAxis,
            yAxis: userYAxis,
            grid: userGrid,
            tooltip: userTooltip,
            ...userRest
          } = userOption
          const mergedSeries = Array.isArray(userSeries)
            ? series.map((s, i) => ({ ...s, ...(userSeries[i] ?? {}) }))
            : series
          return {
            color: theme.colors,
            grid: mergeOptionBlock({ left: 16, right: 16, top: 24, bottom: 24, containLabel: true }, userGrid),
            tooltip: mergeOptionBlock(
              {
                trigger: 'axis',
                axisPointer: { type: 'shadow' },
                backgroundColor: theme.tooltipBg,
                borderColor: theme.tooltipBorder,
                textStyle: { color: theme.tooltipText, fontSize: 12 },
              },
              userTooltip,
            ),
            legend: { show: false },
            xAxis: mergeOptionBlock(
              {
                type: 'category',
                data: binned.map((d) => d.bin),
                axisLine: { lineStyle: { color: theme.axisColor } },
                axisLabel: { color: theme.textColor, fontSize: 10, rotate: 30 },
                axisTick: { show: false },
              },
              userXAxis,
            ),
            yAxis: mergeOptionBlock(
              {
                type: 'value',
                splitLine: { lineStyle: { color: theme.splitLineColor } },
                axisLabel: { color: theme.textColor, fontSize: 11 },
              },
              userYAxis,
            ),
            series: mergedSeries,
            ...userRest,
          }
        }, [binned, option, theme])
    
        return (
          <ChartFrame ref={ref} height={height} className={className} ariaLabel={ariaLabel}>
            <EChart option={mergedOption} />
          </ChartFrame>
        )
      },
    )
    HistogramChart.displayName = 'HistogramChart'
    
  • components/ui/charts/histogram-chart/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/histogram-chart.json