Menu

Change language

GitHub repo

Pareto Chart

pareto-chartui

Pareto chart around Apache ECharts. Sorted bars with an auto-computed cumulative % line on a dual axis. Theme-aware via registry tokens.

Installation

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

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
data

`{ category, value }` per cause. Sorted descending in getOption.

{ category: string; value: number }[][]optional
heightnumber | string320optional
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/pareto-chart/pareto-chart.component.ts6.6 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 ParetoChart — sorted bars with an auto-computed cumulative % line. Standalone, theme-aware. Same inputs as the Vue `ParetoChart` (`data`, `height`, `option`).
     */
    @Component({
      changeDetection: ChangeDetectionStrategy.Eager,
      selector: 'ui-pareto-chart, [ui-pareto-chart]',
      standalone: true,
      host: {
        '[attr.data-slot]': '"pareto-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 UiParetoChartComponent implements AfterViewInit, OnChanges, OnDestroy {
      /** `{ category, value }` per cause. Sorted descending in getOption. */
      @Input() data: { category: string; value: number }[] = []
      @Input() height: number | string = 320
      @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)
      }
      /** Descending rows with running cumulative percent. Pure — unit-tested. */
      ranked(): { category: string; value: number; cumPct: number }[] {
        const rows = [...this.data].sort((a, b) => b.value - a.value)
        const total = rows.reduce((s, r) => s + r.value, 0) || 1
        let acc = 0
        return rows.map((r) => {
          acc += r.value
          return { ...r, cumPct: +((acc / total) * 100).toFixed(1) }
        })
      }
    
      /** Build the merged ECharts option (pure — no DOM needed, unit-testable). */
      getOption(): Record<string, unknown> {
        const colors = getChartColors()
        const rows = this.ranked()
        const series = [
          {
            name: 'value',
            type: 'bar',
            yAxisIndex: 0,
            barMaxWidth: 30,
            itemStyle: { color: colors[0], borderRadius: [6, 6, 6, 6] },
            data: rows.map((r) => r.value),
          },
          {
            name: 'cumulative %',
            type: 'line',
            yAxisIndex: 1,
            smooth: true,
            symbol: 'circle',
            symbolSize: 7,
            lineStyle: { width: 2, color: colors[3] },
            itemStyle: { color: colors[3] },
            data: rows.map((r) => r.cumPct),
          },
        ]
        const userOption: Record<string, any> = this.option ?? {}
        const {
          series: userSeries,
          xAxis: userXAxis,
          yAxis: userYAxis,
          grid: userGrid,
          tooltip: userTooltip,
          legend: userLegend,
          ...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: 32, outerBoundsMode: 'same', outerBoundsContain: 'axisLabel' },
            userGrid,
          ),
          tooltip: mergeOptionBlock(
            {
              trigger: 'axis',
              backgroundColor: getChartTooltipBg(),
              borderColor: getChartTooltipBorder(),
              textStyle: { color: getChartTooltipText(), fontSize: 12 },
            },
            userTooltip,
          ),
          legend: mergeOptionBlock(
            {
              bottom: 0,
              icon: 'circle',
              itemWidth: 8,
              itemHeight: 8,
              textStyle: { fontSize: 11, color: getChartTextColor() },
            },
            userLegend,
          ),
          xAxis: mergeOptionBlock(
            {
              type: 'category',
              data: rows.map((r) => r.category),
              axisLine: { lineStyle: { color: getChartAxisColor() } },
              axisLabel: { color: getChartTextColor(), fontSize: 11, rotate: 20 },
              axisTick: { show: false },
            },
            userXAxis,
          ),
          yAxis: Array.isArray(userYAxis)
            ? userYAxis
            : [
                mergeOptionBlock(
                  {
                    type: 'value',
                    splitLine: { lineStyle: { color: getChartSplitLineColor() } },
                    axisLabel: { color: getChartTextColor(), fontSize: 11 },
                  },
                  (userYAxis as any)?.[0],
                ),
                mergeOptionBlock(
                  {
                    type: 'value',
                    max: 100,
                    splitLine: { show: false },
                    axisLabel: { color: getChartTextColor(), fontSize: 11, formatter: '{value}%' },
                  },
                  (userYAxis as any)?.[1],
                ),
              ],
          series: mergedSeries,
          ...userRest,
        }
      }
    
      async ngAfterViewInit(): Promise<void> {
        if (typeof window === 'undefined' || !this.chartEl?.nativeElement) return
        const [{ use }, { CanvasRenderer }, { BarChart: EChartsBarChart, LineChart: EChartsLineChart }, comps] =
          await Promise.all([
            import('echarts/core'),
            import('echarts/renderers'),
            import('echarts/charts'),
            import('echarts/components'),
          ])
        use([CanvasRenderer, EChartsBarChart, EChartsLineChart, comps.GridComponent, comps.TooltipComponent, comps.LegendComponent])
        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'])) {
          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/pareto-chart/index.ts0.1 kB
  • src/app/components/ui/charts/use-chart-theme.ts7.4 kB

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