Skip to content

Commit

Permalink
feat(sankey): Support configurable labels (pmndrs#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
bripkens authored and Raphaël Benitte committed Oct 20, 2017
1 parent 4cfd3a1 commit 5ac962b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/components/charts/sankey/Sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const Sankey = ({

// labels
enableLabels,
getLabel,
labelPosition,
labelPadding,
labelOrientation,
Expand Down Expand Up @@ -86,6 +87,7 @@ const Sankey = ({

data.nodes.forEach(node => {
node.color = getColor(node)
node.label = getLabel(node)
node.x = node.x0 + nodePaddingX
node.y = node.y0
node.width = Math.max(node.x1 - node.x0 - nodePaddingX * 2, 0)
Expand Down
6 changes: 4 additions & 2 deletions src/components/charts/sankey/SankeyLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const SankeyLabels = ({

return {
id: node.id,
label: node.label,
x,
y: node.y + node.height / 2,
textAnchor,
Expand All @@ -74,7 +75,7 @@ const SankeyLabels = ({
transform={`translate(${label.x}, ${label.y}) rotate(${labelRotation})`}
style={{ ...theme.sankey.label, fill: label.color }}
>
{label.id}
{label.label}
</text>
)
})}
Expand Down Expand Up @@ -119,7 +120,7 @@ const SankeyLabels = ({
pointerEvents: 'none',
}}
>
{data.id}
{data.label}
</text>
)
})}
Expand All @@ -133,6 +134,7 @@ SankeyLabels.propTypes = {
nodes: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
x1: PropTypes.number.isRequired,
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
Expand Down
2 changes: 2 additions & 0 deletions src/components/charts/sankey/SankeyLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ SankeyLinks.propTypes = {
PropTypes.shape({
source: PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
}).isRequired,
target: PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
}).isRequired,
width: PropTypes.number.isRequired,
color: PropTypes.string.isRequired,
Expand Down
6 changes: 4 additions & 2 deletions src/components/charts/sankey/SankeyLinksItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ const tooltipStyles = {
const TooltipContent = ({ link }) => (
<span style={tooltipStyles.container}>
<Chip color={link.source.color} style={tooltipStyles.sourceChip} />
<strong>{link.source.id}</strong>
<strong>{link.source.label}</strong>
&nbsp;>&nbsp;
<strong>{link.target.id}</strong>
<strong>{link.target.label}</strong>
<Chip color={link.target.color} style={tooltipStyles.targetChip} />
<strong>{link.value}</strong>
</span>
Expand Down Expand Up @@ -73,9 +73,11 @@ SankeyLinksItem.propTypes = {
link: PropTypes.shape({
source: PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
}).isRequired,
target: PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
}).isRequired,
color: PropTypes.string.isRequired,
value: PropTypes.number.isRequired,
Expand Down
5 changes: 4 additions & 1 deletion src/components/charts/sankey/SankeyNodesItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const SankeyNodesItem = ({
SankeyNodesItem.propTypes = {
node: PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
color: PropTypes.string.isRequired,
}),

Expand All @@ -78,7 +79,9 @@ SankeyNodesItem.propTypes = {

const enhance = compose(
withPropsOnChange(['node', 'theme'], ({ node, theme }) => ({
tooltip: <BasicTooltip id={node.id} enableChip={true} color={node.color} theme={theme} />,
tooltip: (
<BasicTooltip id={node.label} enableChip={true} color={node.color} theme={theme} />
),
})),
withPropsOnChange(['onClick', 'node'], ({ onClick, node }) => ({
onClick: event => onClick(node, event),
Expand Down
4 changes: 4 additions & 0 deletions src/components/charts/sankey/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import withState from 'recompose/withState'
import withPropsOnChange from 'recompose/withPropsOnChange'
import pure from 'recompose/pure'
import { getInheritedColorGenerator } from '../../../lib/colors'
import { getLabelGenerator } from '../../../lib/propertiesConverters'
import { withColors, withTheme, withDimensions, withMotion } from '../../../hocs'
import { SankeyDefaultProps } from './props'

Expand All @@ -35,5 +36,8 @@ export default Component =>
withPropsOnChange(['labelTextColor'], ({ labelTextColor }) => ({
getLabelTextColor: getInheritedColorGenerator(labelTextColor),
})),
withPropsOnChange(['label', 'labelFormat'], ({ label, labelFormat }) => ({
getLabel: getLabelGenerator(label, labelFormat),
})),
pure
)(Component)
4 changes: 4 additions & 0 deletions src/components/charts/sankey/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const SankeyPropTypes = {
labelOrientation: PropTypes.oneOf(['horizontal', 'vertical']).isRequired,
labelTextColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
getLabelTextColor: PropTypes.func.isRequired, // computed
label: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
labelFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
getLabel: PropTypes.func.isRequired, // computed

// interactivity
isInteractive: PropTypes.bool.isRequired,
Expand Down Expand Up @@ -77,6 +80,7 @@ export const SankeyDefaultProps = {

// labels
enableLabels: true,
label: 'id',
labelPosition: 'inside',
labelPadding: 9,
labelOrientation: 'horizontal',
Expand Down
4 changes: 4 additions & 0 deletions stories/charts/sankey.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ stories.add('contracting links', () => <Sankey {...commonProperties} linkContrac
stories.add('click listener (console)', () => (
<Sankey {...commonProperties} onClick={(data, event) => console.log({ data, event })} />
))

stories.add('label formatter', () => (
<Sankey {...commonProperties} label={node => `${node.id} 😁`} />
))

0 comments on commit 5ac962b

Please sign in to comment.