Menu

Change language

GitHub repo

Sunburst Chart

sunburst-chartui

Sunburst wrapper around Apache ECharts. Concentric-ring hierarchical share visualization — pairs naturally with treemap for comparison.

Installation

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

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
data

Hierarchical `{ name, value?, children? }` nodes.

SunNode[][]optional
heightnumber | string360optional
radius

Inner / outer radius as percentages of the smaller container side. Default `['12%', '90%']`.

[string, string]['12%', '90%']optional
optionRecord<string, unknown>—optional
ariaLabel

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

string—optional
classstring—optional

Schema

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

SunNode
interface SunNode {
  name: string
  value?: number
  children?: SunNode[]
}

npm dependencies

Used by

Files installed (3)

  • src/app/components/ui/charts/sunburst-chart/sunburst-chart.component.ts4.9 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,
      getChartTooltipBg,
      getChartTooltipBorder,
      getChartTooltipText,
    } from '../use-chart-theme'
    
    export interface SunNode {
      name: string
      value?: number
      children?: SunNode[]
    }
    
    /**
     * Angular port of the UIPKGE SunburstChart — concentric-ring hierarchical shares. Standalone, theme-aware. Inputs mirror React/Vue (`data`, `height`, `radius`, `option`).
     */
    @Component({
      changeDetection: ChangeDetectionStrategy.Eager,
      selector: 'ui-sunburst-chart, [ui-sunburst-chart]',
      standalone: true,
      host: {
        '[attr.data-slot]': '"sunburst-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 UiSunburstChartComponent implements AfterViewInit, OnChanges, OnDestroy {
      /** Hierarchical `{ name, value?, children? }` nodes. */
      @Input() data: SunNode[] = []
      @Input() height: number | string = 360
      /** Inner / outer radius as percentages of the smaller container side. Default `['12%', '90%']`. */
      @Input() radius: [string, string] = ['12%', '90%']
      @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)
      }
    
      /** Build the merged ECharts option (pure — no DOM needed, unit-testable). */
      getOption(): Record<string, unknown> {
        const colors = getChartColors()
        const paint = (
          nodes: SunNode[],
          depth: number,
        ): { name: string; value?: number; children?: unknown[]; itemStyle: { color: string } }[] =>
          nodes.map((n, i) => ({
            name: n.name,
            value: n.value,
            children: n.children ? paint(n.children, depth + 1) : undefined,
            itemStyle: { color: colors[(depth + i) % colors.length]! },
          }))
        const series = [
          {
            type: 'sunburst',
            radius: this.radius,
            sort: undefined,
            label: { rotate: 'radial', minAngle: 4, color: getChartTextColor(), fontSize: 11 },
            labelLine: { show: false },
            itemStyle: { borderColor: getChartTextColor(), borderWidth: 1 },
            emphasis: { focus: 'ancestor' },
            data: paint(this.data, 0),
          },
        ]
        const userOption: Record<string, any> = this.option ?? {}
        const { series: userSeries, tooltip: userTooltip, ...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: getChartTextColor(), fontSize: 12 },
            ...(typeof userTooltip === 'object' && userTooltip !== null ? userTooltip : {}),
          },
          series: mergedSeries,
          ...userRest,
        }
      }
    
      async ngAfterViewInit(): Promise<void> {
        if (typeof window === 'undefined' || !this.chartEl?.nativeElement) return
        const [{ use }, { CanvasRenderer }, { SunburstChart: EChartsSunburstChart }, comps] = await Promise.all([
          import('echarts/core'),
          import('echarts/renderers'),
          import('echarts/charts'),
          import('echarts/components'),
        ])
        use([CanvasRenderer, EChartsSunburstChart, 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['radius'])) {
          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/sunburst-chart/index.ts0.1 kB
  • src/app/components/ui/charts/use-chart-theme.ts7.4 kB

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