UIPackage
Menu

Framework

Change language

Boilerplate repo

Sankey Chart

sankey-chartui

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

Also available for Vue ->

Installation

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

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
nodes

Node names. If omitted, derived from the union of link sources + targets.

string[]optional
links

`{ source, target, value }` edges between nodes.

SankeyLink[]required
heightnumber | string360optional
curveness

Curvature of the link ribbons. 0 = straight, 1 = max curve.

number0optional
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.

SankeyLink
interface SankeyLink {
  source: string
  target: string
  value: number
}
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/sankey-chart/SankeyChart.tsx2.8 kB
    'use client'
    
    import * as React from 'react'
    import ReactECharts from 'echarts-for-react/esm/core'
    import { ChartFrame, EChart } from '../shared'
    import { useChartTheme } from '../useChartTheme'
    
    // SankeyChart
    // ─────────────────────────────────────────────────────────────────────────
    
    interface SankeyLink {
      source: string
      target: string
      value: number
    }
    
    // Sankey nodes keep a fixed categorical palette so the same flow retains its
    // visual identity across light and dark themes. Labels and tooltip chrome
    // still use theme tokens for contrast against the current surface.
    const SANKEY_COLORS = ['#3b82f6', '#f59e0b', '#10b981', '#ef4444', '#8b5cf6', '#06b6d4', '#ec4899', '#84cc16']
    
    export interface SankeyChartProps {
      /** Node names. If omitted, derived from the union of link sources + targets. */
      nodes?: string[]
      /** `{ source, target, value }` edges between nodes. */
      links: SankeyLink[]
      height?: number | string
      /** Curvature of the link ribbons. 0 = straight, 1 = max curve. */
      curveness?: number
      option?: any
      className?: string
      /** Accessible name announced for the chart image. Defaults to "Chart". */
      ariaLabel?: string
    }
    
    export const SankeyChart = React.forwardRef<HTMLDivElement, SankeyChartProps>(
      ({ nodes, links, height = 360, curveness = 0.5, option, className, ariaLabel }, ref) => {
        const theme = useChartTheme()
    
        const mergedOption = React.useMemo(() => {
          const nodeNames = nodes ?? Array.from(new Set(links.flatMap((l) => [l.source, l.target])))
    
          const series = [
            {
              type: 'sankey',
              left: 16,
              right: 72,
              top: 12,
              bottom: 12,
              data: nodeNames.map((name) => ({ name })),
              links,
              lineStyle: { color: 'gradient', curveness },
              label: { fontSize: 11, color: theme.tooltipText },
              emphasis: { focus: 'adjacency' },
              itemStyle: { borderWidth: 0 },
            },
          ]
    
          const userOption: any = option ?? {}
          const { series: userSeries, ...userRest } = userOption
          const mergedSeries = Array.isArray(userSeries)
            ? series.map((s, i) => ({ ...s, ...(userSeries[i] ?? {}) }))
            : series
    
          return {
            color: SANKEY_COLORS,
            tooltip: {
              trigger: 'item',
              triggerOn: 'mousemove',
              backgroundColor: theme.tooltipBg,
              borderColor: theme.tooltipBorder,
              textStyle: { color: theme.tooltipText, fontSize: 12 },
            },
            series: mergedSeries,
            ...userRest,
          }
        }, [nodes, links, curveness, option, theme])
    
        return (
          <ChartFrame ref={ref} height={height} className={className} focusable={false} ariaLabel={ariaLabel}>
            <EChart option={mergedOption} />
          </ChartFrame>
        )
      },
    )
    SankeyChart.displayName = 'SankeyChart'
    
  • components/ui/charts/sankey-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/sankey-chart.json