Menu

Change language

GitHub repo

Pie Chart

pie-chartui

Pie chart wrapper around Apache ECharts. Category share with slice labels, legend and item tooltips. Theme-aware via registry tokens.

Installation

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

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
dataRecord<string, unknown>[][]optional
nameFieldstringnameoptional
valueFieldstringvalueoptional
donut

Hollow center. Default false.

booleanfalseoptional
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/pie-chart/pie-chart.component.ts4.9 kB
    import {
      AfterViewInit,
      Component,
      ElementRef,
      Input,
      OnChanges,
      OnDestroy,
      SimpleChanges,
      ViewChild,
      booleanAttribute,
      ChangeDetectionStrategy,
    } from '@angular/core'
    import type * as echarts from 'echarts/core'
    import { cn } from '@/lib/utils'
    import { getChartColors, getChartTooltipBg, getChartTooltipBorder, getChartTooltipText } from '../use-chart-theme'
    
    /**
     * Angular port of the UIPKGE PieChart — category share with a bottom legend and
     * item tooltips. Standalone, theme-aware. Same inputs as the Vue `PieChart`
     * (`data`, `nameField`, `valueField`, `height`, `donut`, `option`).
     */
    @Component({
      changeDetection: ChangeDetectionStrategy.Eager,
      selector: 'ui-pie-chart, [ui-pie-chart]',
      standalone: true,
      host: {
        '[attr.data-slot]': '"pie-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 UiPieChartComponent implements AfterViewInit, OnChanges, OnDestroy {
      @Input() data: Record<string, unknown>[] = []
      @Input() nameField = 'name'
      @Input() valueField = 'value'
      /** Hollow center. Default false. */
      @Input({ transform: booleanAttribute }) donut = false
      @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
    
      total(): number {
        return this.data.reduce((sum, d) => sum + (Number(d[this.valueField]) || 0), 0)
      }
      @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)
      }
    
      /** Build the merged ECharts option (pure — no DOM needed, unit-testable). */
      getOption(): Record<string, unknown> {
        const colors = getChartColors()
        const seriesData = this.data.map((d) => ({
          name: d[this.nameField],
          value: d[this.valueField],
        }))
    
        const series = {
          type: 'pie',
          radius: this.donut ? ['45%', '70%'] : '65%',
          center: ['50%', '45%'],
          itemStyle: { borderWidth: 0 },
          label: { show: false },
          data: seriesData,
        }
    
        // Per-index series merge — partial overrides keep computed `type`/`data`.
        const userOption: Record<string, any> = this.option ?? {}
        const { series: userSeries, ...userRest } = userOption
        const mergedSeries = Array.isArray(userSeries) ? [series].map((s, i) => ({ ...s, ...(userSeries[i] ?? {}) })) : [series]
    
        return {
          color: colors,
          tooltip: {
            trigger: 'item',
            backgroundColor: getChartTooltipBg(),
            borderColor: getChartTooltipBorder(),
            textStyle: { color: getChartTooltipText(), fontSize: 12 },
            formatter: '{b}: {c} ({d}%)',
          },
          legend: {
            bottom: 0,
            icon: 'circle',
            itemWidth: 8,
            itemHeight: 8,
            textStyle: { fontSize: 11 },
          },
          series: mergedSeries,
          ...userRest,
        }
      }
    
      /** ECharts chart + component modules, resolved lazily so SSR never loads canvas code. */
      private chartModules(charts: Record<string, unknown>, comps: Record<string, unknown>): unknown[] {
        return [charts['PieChart'], comps['TooltipComponent'], comps['LegendComponent'], comps['TitleComponent']]
      }
    
      async ngAfterViewInit(): Promise<void> {
        if (typeof window === 'undefined' || !this.chartEl?.nativeElement) return
        const [{ use }, { CanvasRenderer }, charts, comps] = await Promise.all([
          import('echarts/core'),
          import('echarts/renderers'),
          import('echarts/charts'),
          import('echarts/components'),
        ])
        use([CanvasRenderer, ...(this.chartModules(charts, comps) as never[])])
        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 && Object.keys(changes).length) {
          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/pie-chart/index.ts0.1 kB
  • src/app/components/ui/charts/use-chart-theme.ts7.4 kB

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