Menu

Change language

GitHub repo

Histogram Chart

histogram-chartui

Histogram wrapper around Apache ECharts. Auto-bins raw values or renders pre-binned counts with zero-gap bars and a highlighted peak. Theme-aware via registry tokens.

Installation

$npx uipkge-ng@latest add histogram-chart
Manifest:https://uipkge.dev/r/angular/histogram-chart.jsonInstalls to:src/app/components/ui/charts/histogram-chart/

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
values

Raw values to auto-bin. Ignored when data is provided.

number[][]optional
data

Pre-binned `{ bin, count }` rows. Takes precedence over values.

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

Bin count for auto-binning. Default 12.

number12optional
heightnumber | string300optional
optionRecord<string, unknown>—optional
ariaLabel

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

string—optional
classstring—optional

npm dependencies

Used by

Files installed (3)

  • src/app/components/ui/charts/histogram-chart/histogram-chart.component.ts6.1 kB
    import {
      AfterViewInit,
      Component,
      ElementRef,
      Input,
      OnChanges,
      OnDestroy,
      SimpleChanges,
      ViewChild,
      ChangeDetectionStrategy,
    } from '@angular/core'
    import type * as echarts from 'echarts/core'
    import { cn } from '@/lib/utils'
    import {
      getChartColors,
      getChartTextColor,
      getChartAxisColor,
      getChartSplitLineColor,
      getChartTooltipBg,
      getChartTooltipBorder,
      getChartTooltipText,
      mergeOptionBlock,
    } from '../use-chart-theme'
    
    /**
     * Angular port of the UIPKGE HistogramChart — auto-bins raw values or renders pre-binned counts. Standalone, theme-aware. Same inputs as the Vue `HistogramChart` (`values`, `data`, `bins`, `height`, `option`).
     */
    @Component({
      changeDetection: ChangeDetectionStrategy.Eager,
      selector: 'ui-histogram-chart, [ui-histogram-chart]',
      standalone: true,
      host: {
        '[attr.data-slot]': '"histogram-chart"',
        '[attr.data-uipkge]': '""',
        '[class]': 'hostClass',
        '[attr.role]': '"img"',
        '[attr.aria-label]': 'ariaLabel || "Chart"',
        '[attr.tabindex]': '0',
        '[style.height]': 'heightStyle',
      },
      template: `<div #chartEl class="size-full"></div>`,
    })
    export class UiHistogramChartComponent implements AfterViewInit, OnChanges, OnDestroy {
      /** Raw values to auto-bin. Ignored when data is provided. */
      @Input() values: number[] = []
      /** Pre-binned `{ bin, count }` rows. Takes precedence over values. */
      @Input() data: { bin: string; count: number }[] = []
      /** Bin count for auto-binning. Default 12. */
      @Input() bins = 12
      @Input() height: number | string = 300
      @Input() option?: Record<string, unknown>
      /** Accessible name announced for the chart image. Defaults to "Chart". */
      @Input() ariaLabel?: string
      @Input('class') className?: string
    
      @ViewChild('chartEl', { static: false }) chartEl?: ElementRef<HTMLDivElement>
    
      private chart: echarts.ECharts | null = null
      private unsubscribeTheme: (() => void) | null = null
    
      get hostClass(): string {
        return cn('block focus-visible:ring-ring w-full focus-visible:ring-2 focus-visible:outline-none', this.className)
      }
    
      get heightStyle(): string {
        return /^\d+$/.test(String(this.height)) ? `${this.height}px` : String(this.height)
      }
      /** Bin rows: pre-binned data wins, else equal-width bins over values. Pure. */
      binned(): { bin: string; count: number }[] {
        if (this.data?.length) return this.data
        const vals = this.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, this.bins)
        const counts = Array(n).fill(0)
        for (const v of vals) counts[Math.min(n - 1, Math.floor(((v - min) / span) * n))]++
        return counts.map((count: number, i: number) => ({
          bin: `${(min + (span * i) / n).toFixed(1)}–${(min + (span * (i + 1)) / n).toFixed(1)}`,
          count,
        }))
      }
    
      /** Build the merged ECharts option (pure — no DOM needed, unit-testable). */
      getOption(): Record<string, unknown> {
        const colors = getChartColors()
        const rows = this.binned()
        const peak = rows.reduce((m, d, i) => (d.count > (rows[m]?.count ?? -1) ? i : m), 0)
        const series = [
          {
            type: 'bar',
            data: rows.map((r) => r.count),
            barCategoryGap: '2%',
            itemStyle: {
              color: (p: { dataIndex: number }) => (p.dataIndex === peak ? colors[0] : colors[2]),
              borderRadius: [3, 3, 3, 3],
            },
          },
        ]
        const userOption: Record<string, any> = this.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: colors,
          grid: mergeOptionBlock(
            { left: 16, right: 16, top: 24, bottom: 24, outerBoundsMode: 'same', outerBoundsContain: 'axisLabel' },
            userGrid,
          ),
          tooltip: mergeOptionBlock(
            {
              trigger: 'axis',
              axisPointer: { type: 'shadow' },
              backgroundColor: getChartTooltipBg(),
              borderColor: getChartTooltipBorder(),
              textStyle: { color: getChartTooltipText(), fontSize: 12 },
            },
            userTooltip,
          ),
          xAxis: mergeOptionBlock(
            {
              type: 'category',
              data: rows.map((r) => r.bin),
              axisLine: { lineStyle: { color: getChartAxisColor() } },
              axisLabel: { color: getChartTextColor(), fontSize: 10, rotate: 30 },
              axisTick: { show: false },
            },
            userXAxis,
          ),
          yAxis: mergeOptionBlock(
            {
              type: 'value',
              splitLine: { lineStyle: { color: getChartSplitLineColor() } },
              axisLabel: { color: getChartTextColor(), fontSize: 11 },
            },
            userYAxis,
          ),
          series: mergedSeries,
          ...userRest,
        }
      }
    
      async ngAfterViewInit(): Promise<void> {
        if (typeof window === 'undefined' || !this.chartEl?.nativeElement) return
        const [{ use }, { CanvasRenderer }, { BarChart: EChartsBarChart }, comps] = await Promise.all([
          import('echarts/core'),
          import('echarts/renderers'),
          import('echarts/charts'),
          import('echarts/components'),
        ])
        use([CanvasRenderer, EChartsBarChart, comps.GridComponent, comps.TooltipComponent])
        const { init } = await import('echarts/core')
        this.chart = init(this.chartEl.nativeElement)
        this.chart.setOption(this.getOption())
        const { onChartThemeChange } = await import('../use-chart-theme')
        this.unsubscribeTheme = onChartThemeChange(() => this.chart?.setOption(this.getOption()))
        if (typeof ResizeObserver !== 'undefined') {
          new ResizeObserver(() => this.chart?.resize()).observe(this.chartEl.nativeElement)
        }
      }
    
      ngOnChanges(changes: SimpleChanges): void {
        if (this.chart && (changes['data'] || changes['option'] || changes['values'] || changes['bins'])) {
          this.chart.setOption(this.getOption())
        }
      }
    
      ngOnDestroy(): void {
        this.unsubscribeTheme?.()
        this.unsubscribeTheme = null
        try {
          this.chart?.dispose()
        } catch {
          /* already disposed */
        }
        this.chart = null
      }
    }
    
  • src/app/components/ui/charts/histogram-chart/index.ts0.1 kB
  • src/app/components/ui/charts/use-chart-theme.ts7.4 kB

Raw manifest:https://uipkge.dev/r/angular/histogram-chart.json