# SankeyChart
Package: @mantine/charts
Import: import { SankeyChart } from '@mantine/charts';
Description: Sankey diagram component

## Usage

`SankeyChart` is based on the [Sankey recharts component](https://recharts.github.io/en-US/api/Sankey).
It visualizes flow between nodes where the width of each link is proportional to the flow value:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <SankeyChart data={data} />;
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit' },
    { name: 'Direct-Favourite' },
    { name: 'Page-Click' },
    { name: 'Detail-Favourite' },
    { name: 'Lost' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```


## Data

Pass an object with `nodes` and `links` arrays to the `data` prop.
Each link references its source and target nodes by their zero-based index in the `nodes` array.
Links must not form a cycle:

```tsx
import { SankeyChartData } from '@mantine/charts';

export const data: SankeyChartData = {
  nodes: [{ name: 'Visitors' }, { name: 'Signups' }, { name: 'Purchases' }],
  links: [
    { source: 0, target: 1, value: 100 },
    { source: 1, target: 2, value: 30 },
  ],
};
```

## Node color

Set `color` on a node in the data to change its color. Theme colors and any valid CSS color are accepted:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <SankeyChart data={data} />;
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit', color: 'indigo.6' },
    { name: 'Direct-Favourite', color: 'teal.6' },
    { name: 'Page-Click', color: 'blue.6' },
    { name: 'Detail-Favourite', color: 'cyan.6' },
    { name: 'Lost', color: 'red.6' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```


## Custom colors

Use `colors` prop to set colors for nodes that do not have `color`. Colors repeat when there are more nodes than colors:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return (
    <SankeyChart
      data={data}
      colors={['indigo.6', 'cyan.6', 'teal.6', 'orange.6', 'red.6']}
    />
  );
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit' },
    { name: 'Direct-Favourite' },
    { name: 'Page-Click' },
    { name: 'Detail-Favourite' },
    { name: 'Lost' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```


## Node and link color

By default, each link has the color of its source node.
Set `linkColor` prop to use one color for all links.
Set `nodeColor` prop to use one color for all nodes that do not have `color`:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <SankeyChart data={data} nodeColor="indigo.6" linkColor="gray.6" />;
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit' },
    { name: 'Direct-Favourite' },
    { name: 'Page-Click' },
    { name: 'Detail-Favourite' },
    { name: 'Lost' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```


## Node width and padding

Use `nodeWidth` to control the width of each node and `nodePadding` to control
the spacing between nodes:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <SankeyChart data={data} nodeWidth={20} nodePadding={20} />;
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit' },
    { name: 'Direct-Favourite' },
    { name: 'Page-Click' },
    { name: 'Detail-Favourite' },
    { name: 'Lost' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```


## Link opacity

Use `linkOpacity` prop to control the opacity of links between nodes:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <SankeyChart data={data} linkOpacity={0.2} />;
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit', color: 'indigo.6' },
    { name: 'Direct-Favourite', color: 'teal.6' },
    { name: 'Page-Click', color: 'blue.6' },
    { name: 'Detail-Favourite', color: 'cyan.6' },
    { name: 'Lost', color: 'red.6' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```


## Value formatter

Use `valueFormatter` to format values in node labels and in the tooltip:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return (
    <SankeyChart
      data={data}
      valueFormatter={(value) => new Intl.NumberFormat('en-US').format(value)}
    />
  );
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit' },
    { name: 'Direct-Favourite' },
    { name: 'Page-Click' },
    { name: 'Detail-Favourite' },
    { name: 'Lost' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```


## Disable tooltip

To disable the tooltip, set `withTooltip={false}`:

```tsx
// Demo.tsx
import { SankeyChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <SankeyChart data={data} withTooltip={false} />;
}

// data.ts
export const data = {
  nodes: [
    { name: 'Visit' },
    { name: 'Direct-Favourite' },
    { name: 'Page-Click' },
    { name: 'Detail-Favourite' },
    { name: 'Lost' },
  ],
  links: [
    { source: 0, target: 1, value: 3728.3 },
    { source: 0, target: 2, value: 354170 },
    { source: 2, target: 3, value: 62429 },
    { source: 2, target: 4, value: 291741 },
  ],
};
```



#### Props

**SankeyChart props**

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| children | React.ReactNode | - | Additional elements rendered inside `Sankey` component |
| colors | DefaultMantineColor[] | - | Colors palette used for nodes that do not have `color` set when `nodeColor` is not set, cycled by node index. By default, a 12-color palette starting with `blue` |
| data | SankeyChartData | required | Data used to render chart, must include `nodes` and `links` arrays |
| height | number | 300 | Controls chart height |
| iterations | number | 32 | The number of relaxation iterations |
| linkColor | MantineColor | - | Color of all links. By default, each link uses the color of its source node |
| linkCurvature | number | 0.5 | The curvature of links between nodes |
| linkOpacity | number | 0.4 | Opacity of the link fill |
| nodeColor | MantineColor | - | Color of nodes that do not have `color` set, overrides `colors` palette |
| nodePadding | number | 10 | The padding between nodes |
| nodeWidth | number | 10 | The width of each node |
| sankeyProps | Partial<Omit<Props, "data" \| "ref">> | - | Props passed down to recharts `Sankey` component |
| textColor | MantineColor | - | Controls text color of labels. By default, `var(--mantine-color-text)` |
| tooltipAnimationDuration | number | 0 | Tooltip animation duration in ms |
| tooltipProps | RechartsProps | - | Props passed down to `Tooltip` recharts component |
| valueFormatter | (value: number) => string | - | A function to format values inside the tooltip and node labels |
| withTooltip | boolean | true | Determines whether the tooltip should be displayed when hovered |


#### Styles API

SankeyChart component supports Styles API. With Styles API, you can customize styles of any inner element. Follow the documentation to learn how to use CSS modules, CSS variables and inline styles to get full control over component styles.

**SankeyChart selectors**

| Selector | Static selector | Description |
|----------|----------------|-------------|
| root | .mantine-SankeyChart-root | Root element |
| tooltip | .mantine-SankeyChart-tooltip | Tooltip root element |
| tooltipBody | .mantine-SankeyChart-tooltipBody | Tooltip wrapper around all items |
| tooltipItem | .mantine-SankeyChart-tooltipItem | Tooltip item representing data series |
| tooltipItemBody | .mantine-SankeyChart-tooltipItemBody | Tooltip item wrapper around item color and name |
| tooltipItemColor | .mantine-SankeyChart-tooltipItemColor | Tooltip item color |
| tooltipItemName | .mantine-SankeyChart-tooltipItemName | Tooltip item name |
| tooltipItemData | .mantine-SankeyChart-tooltipItemData | Tooltip item data |
| tooltipLabel | .mantine-SankeyChart-tooltipLabel | Label of the tooltip |

**SankeyChart CSS variables**

| Selector | Variable | Description |
|----------|----------|-------------|
| root | --chart-node-color | Controls color of the chart nodes |
| root | --chart-link-color | Controls color of the chart links |
| root | --chart-text-color | Controls color of the node labels |
| root | --chart-height | Controls height of the chart |
