Skip to content

Commit

Permalink
feat(sankey): Support complete configuration of the tooltips (#78)
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 5ac962b commit f3aecf6
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/components/charts/sankey/Sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const Sankey = ({
theme,
getColor, // computed

// tooltip
nodeTooltip,
linkTooltip,

// motion
animate,
motionDamping,
Expand Down Expand Up @@ -147,6 +151,7 @@ const Sankey = ({
currentLink={currentLink}
isCurrentLink={isCurrentLink}
onClick={onClick}
tooltip={linkTooltip}
theme={theme}
{...motionProps}
/>
Expand All @@ -165,6 +170,7 @@ const Sankey = ({
currentLink={currentLink}
isCurrentNode={isCurrentNode}
onClick={onClick}
tooltip={nodeTooltip}
theme={theme}
{...motionProps}
/>
Expand Down
4 changes: 4 additions & 0 deletions src/components/charts/sankey/SankeyLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const SankeyLinks = ({
isCurrentLink,
onClick,

tooltip,
theme,
}) => {
const getOpacity = link => {
Expand All @@ -63,6 +64,7 @@ const SankeyLinks = ({
hideTooltip={hideTooltip}
setCurrent={setCurrentLink}
onClick={onClick}
tooltip={tooltip}
theme={theme}
/>
))}
Expand Down Expand Up @@ -96,6 +98,7 @@ const SankeyLinks = ({
hideTooltip={hideTooltip}
setCurrent={setCurrentLink}
onClick={onClick}
tooltip={tooltip}
theme={theme}
/>
)}
Expand Down Expand Up @@ -128,6 +131,7 @@ SankeyLinks.propTypes = {
linkContract: PropTypes.number.isRequired,

theme: PropTypes.object.isRequired,
tooltip: PropTypes.func,

...motionPropTypes,

Expand Down
13 changes: 10 additions & 3 deletions src/components/charts/sankey/SankeyLinksItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ SankeyLinksItem.propTypes = {
}

const enhance = compose(
withPropsOnChange(['link', 'theme'], ({ link, theme }) => ({
tooltip: <BasicTooltip id={<TooltipContent link={link} />} theme={theme} />,
})),
withPropsOnChange(['link', 'theme', 'tooltip'], ({ link, theme, tooltip }) => {
if (tooltip) {
return {
tooltip: <BasicTooltip id={tooltip(link)} enableChip={false} theme={theme} />,
}
}
return {
tooltip: <BasicTooltip id={<TooltipContent link={link} />} theme={theme} />,
}
}),
withPropsOnChange(['onClick', 'link'], ({ onClick, link }) => ({
onClick: event => onClick(link, event),
})),
Expand Down
4 changes: 4 additions & 0 deletions src/components/charts/sankey/SankeyNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const SankeyNodes = ({
isCurrentNode,
onClick,

tooltip,
theme,
}) => {
const getOpacity = node => {
Expand Down Expand Up @@ -65,6 +66,7 @@ const SankeyNodes = ({
hideTooltip={hideTooltip}
setCurrent={setCurrentNode}
onClick={onClick}
tooltip={tooltip}
theme={theme}
/>
))}
Expand Down Expand Up @@ -115,6 +117,7 @@ const SankeyNodes = ({
hideTooltip={hideTooltip}
setCurrent={setCurrentNode}
onClick={onClick}
tooltip={tooltip}
theme={theme}
/>
)
Expand Down Expand Up @@ -145,6 +148,7 @@ SankeyNodes.propTypes = {
getNodeBorderColor: PropTypes.func.isRequired,

theme: PropTypes.object.isRequired,
tooltip: PropTypes.func,

...motionPropTypes,

Expand Down
18 changes: 13 additions & 5 deletions src/components/charts/sankey/SankeyNodesItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,23 @@ SankeyNodesItem.propTypes = {
setCurrent: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,

tooltip: PropTypes.element.isRequired,
theme: PropTypes.object.isRequired,
}

const enhance = compose(
withPropsOnChange(['node', 'theme'], ({ node, theme }) => ({
tooltip: (
<BasicTooltip id={node.label} enableChip={true} color={node.color} theme={theme} />
),
})),
withPropsOnChange(['node', 'theme', 'tooltip'], ({ node, theme, tooltip }) => {
if (tooltip) {
return {
tooltip: <BasicTooltip id={tooltip(node)} enableChip={false} theme={theme} />,
}
}
return {
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/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export const SankeyPropTypes = {
labelFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
getLabel: PropTypes.func.isRequired, // computed

// tooltip
nodeTooltip: PropTypes.func,
linkTooltip: PropTypes.func,

// interactivity
isInteractive: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
Expand Down
12 changes: 12 additions & 0 deletions stories/charts/sankey.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ stories.add('click listener (console)', () => (
stories.add('label formatter', () => (
<Sankey {...commonProperties} label={node => `${node.id} 😁`} />
))

stories.add('custom tooltip', () => (
<Sankey
{...commonProperties}
nodeTooltip={node => <span>Custom tooltip for node: {node.label}</span>}
linkTooltip={node => (
<span>
Custom tooltip for link: {node.source.label} to {node.target.label}
</span>
)}
/>
))

0 comments on commit f3aecf6

Please sign in to comment.