Skip to content

Commit

Permalink
feat(sankey): improve support for nodes sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Mar 23, 2019
1 parent fec85fa commit f63450f
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 198 deletions.
4 changes: 3 additions & 1 deletion packages/sankey/index.d.ts
Expand Up @@ -40,6 +40,8 @@ declare module '@nivo/sankey' {
| 'color'
| 'luminosity'

export type SankeySortFunction = (nodeA: SankeyDataNode, nodeB: SankeyDataNode) => number

export type SankeyProps = Partial<{
align: 'center' | 'justify' | 'left' | 'right'

Expand Down Expand Up @@ -77,7 +79,7 @@ declare module '@nivo/sankey' {

legends: LegendProps[]

sort?: (nodeA: SankeyDataNode, nodeB: SankeyDataNode) => number
sort: 'auto' | 'input' | 'ascending' | 'descending' | SankeySortFunction
}>

interface Dimensions {
Expand Down
2 changes: 1 addition & 1 deletion packages/sankey/package.json
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@nivo/core": "0.53.0",
"@nivo/legends": "0.53.0",
"d3-sankey": "0.12.1",
"d3-sankey": "^0.12.1",
"lodash": "^4.17.4",
"react-motion": "^0.5.2",
"recompose": "^0.30.0"
Expand Down
5 changes: 2 additions & 3 deletions packages/sankey/src/Sankey.js
Expand Up @@ -24,6 +24,7 @@ const Sankey = ({
data: _data,

align,
sortFunction,

margin,
width,
Expand Down Expand Up @@ -74,12 +75,10 @@ const Sankey = ({
tooltipFormat,

legends,

sort,
}) => {
const sankey = d3Sankey()
.nodeSort(sort)
.nodeAlign(sankeyAlignmentFromProp(align))
.nodeSort(sortFunction)
.nodeWidth(nodeWidth)
.nodePadding(nodePaddingY)
.size([width, height])
Expand Down
12 changes: 12 additions & 0 deletions packages/sankey/src/enhance.js
Expand Up @@ -38,5 +38,17 @@ export default Component =>
withPropsOnChange(['label', 'labelFormat'], ({ label, labelFormat }) => ({
getLabel: getLabelGenerator(label, labelFormat),
})),
withPropsOnChange(['sort'], ({ sort }) => {
let sortFunction
if (sort === 'input') {
sortFunction = null
} else if (sort === 'ascending') {
sortFunction = (a, b) => a.value - b.value
} else if (sort === 'descending') {
sortFunction = (a, b) => b.value - a.value
}

return { sortFunction }
}),
pure
)(Component)
5 changes: 5 additions & 0 deletions packages/sankey/src/props.js
Expand Up @@ -40,6 +40,10 @@ export const SankeyPropTypes = {
}).isRequired,

align: sankeyAlignmentPropType.isRequired,
sort: PropTypes.oneOfType([
PropTypes.oneOf(['auto', 'input', 'ascending', 'descending']),
PropTypes.func,
]).isRequired,

nodeOpacity: PropTypes.number.isRequired,
nodeHoverOpacity: PropTypes.number.isRequired,
Expand Down Expand Up @@ -79,6 +83,7 @@ export const SankeyPropTypes = {

export const SankeyDefaultProps = {
align: 'center',
sort: 'auto',

nodeOpacity: 0.75,
nodeHoverOpacity: 1,
Expand Down

0 comments on commit f63450f

Please sign in to comment.