Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom node type typing and expose state modification functions for custom nodes in Sankey diagrams #2641

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/sankey/src/Sankey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SankeyLinkDatum,
SankeyNodeDatum,
SankeySvgProps,
LayerProps,
} from './types'

type InnerSankeyProps<N extends DefaultNode, L extends DefaultLink> = Omit<
Expand Down Expand Up @@ -131,7 +132,13 @@ const InnerSankey = <N extends DefaultNode, L extends DefaultLink>({
height,
outerWidth,
outerHeight,
}
nodeTooltip,
linkTooltip,
currentNode,
setCurrentNode,
currentLink,
setCurrentLink,
} as unknown as LayerProps<N, L>

const layerById: Record<SankeyLayerId, ReactNode> = {
links: null,
Expand Down
6 changes: 5 additions & 1 deletion packages/sankey/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SankeyLayerId, SankeyNodeDatum, SankeyAlignType } from './types'
import { InheritedColorConfig } from '@nivo/colors'
import { SankeyNodeTooltip } from './SankeyNodeTooltip'
import { SankeyLinkTooltip } from './SankeyLinkTooltip'
import { LayerProps } from './types'

export const sankeyAlignmentPropMapping = {
center: sankeyCenter,
Expand Down Expand Up @@ -56,7 +57,10 @@ export const svgDefaultProps = {

legends: [],

layers: ['links', 'nodes', 'labels', 'legends'] as SankeyLayerId[],
layers: ['links', 'nodes', 'labels', 'legends'] as (
| SankeyLayerId
| React.FunctionComponent<LayerProps<any, any>>
)[],

role: 'img',

Expand Down
19 changes: 18 additions & 1 deletion packages/sankey/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MotionProps,
PropertyAccessor,
ValueFormat,
Margin,
} from '@nivo/core'
import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors'
import { LegendProps } from '@nivo/legends'
Expand Down Expand Up @@ -102,6 +103,22 @@ export type SankeySortFunction<N extends DefaultNode, L extends DefaultLink> = (
b: SankeyNodeDatum<N, L>
) => number

export interface LayerProps<N extends DefaultNode, L extends DefaultLink> {
links: SankeyLinkDatum<N, L>[]
nodes: SankeyNodeDatum<N, L>[]
margin: Margin
width: number
height: number
outerWidth: number
outerHeight: number
nodeTooltip: SankeySvgProps<N, L>
linkTooltip: SankeySvgProps<N, L>
currentNode: SankeyNodeDatum<N, L>
setCurrentNode: (node: SankeyNodeDatum<N, L>) => void
currentLink: SankeyLinkDatum<N, L>
setCurrentLink: (node: SankeyLinkDatum<N, L>) => void
}

export interface SankeyCommonProps<N extends DefaultNode, L extends DefaultLink> {
// formatting for link value
valueFormat: ValueFormat<number>
Expand All @@ -110,7 +127,7 @@ export interface SankeyCommonProps<N extends DefaultNode, L extends DefaultLink>
align: SankeyAlignType | SankeyAlignFunction
sort: SankeySortType | SankeySortFunction<N, L>

layers: SankeyLayerId[]
layers: (SankeyLayerId | React.FunctionComponent<LayerProps<N, L>>)[]

margin: Box

Expand Down