Skip to content

Commit

Permalink
feat(tooltip): prop to format values in tooltip was added (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahtohbi4 authored and Raphaël Benitte committed Oct 26, 2017
1 parent 000e2c3 commit 0dfafff
Show file tree
Hide file tree
Showing 41 changed files with 301 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/components/charts/bar/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const Bar = ({

// interactivity
isInteractive,
tooltipFormat,
onClick,
}) => {
const options = {
Expand Down Expand Up @@ -163,6 +164,7 @@ const Bar = ({
hideTooltip,
onClick,
theme,
tooltipFormat,
}

let bars
Expand Down
3 changes: 3 additions & 0 deletions src/components/charts/bar/BarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const BarItem = ({
showTooltip,
hideTooltip,
onClick,
tooltipFormat,

theme,
}) => {
Expand All @@ -43,6 +44,7 @@ const BarItem = ({
enableChip={true}
color={color}
theme={theme}
format={tooltipFormat}
/>,
e
)
Expand Down Expand Up @@ -100,6 +102,7 @@ BarItem.propTypes = {
shouldRenderLabel: PropTypes.bool.isRequired,
labelColor: PropTypes.string.isRequired,

tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
showTooltip: PropTypes.func.isRequired,
hideTooltip: PropTypes.func.isRequired,

Expand Down
1 change: 1 addition & 0 deletions src/components/charts/bar/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const BarPropTypes = {
// interactivity
isInteractive: PropTypes.bool,
onClick: PropTypes.func.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),

// canvas specific
pixelRatio: PropTypes.number.isRequired,
Expand Down
2 changes: 2 additions & 0 deletions src/components/charts/bubble/Bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const Bubble = ({
// interactivity
isInteractive,
onClick,
tooltipFormat,
isZoomable,
zoomToNode,
}) => {
Expand All @@ -59,6 +60,7 @@ const Bubble = ({
isZoomable,
zoomToNode,
theme,
tooltipFormat,
})

return (
Expand Down
12 changes: 11 additions & 1 deletion src/components/charts/bubble/interactivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ import BasicTooltip from '../../tooltip/BasicTooltip'

export const getNodeHandlers = (
node,
{ isInteractive, onClick, showTooltip, hideTooltip, isZoomable, zoomToNode, theme }
{
isInteractive,
onClick,
showTooltip,
hideTooltip,
isZoomable,
zoomToNode,
theme,
tooltipFormat,
}
) => {
if (!isInteractive) return {}

Expand All @@ -23,6 +32,7 @@ export const getNodeHandlers = (
enableChip={true}
color={node.color}
theme={theme}
format={tooltipFormat}
/>,
e
)
Expand Down
1 change: 1 addition & 0 deletions src/components/charts/bubble/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const commonPropTypes = {
isInteractive: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
isZoomable: PropTypes.bool.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
}

export const BubblePropTypes = {
Expand Down
3 changes: 3 additions & 0 deletions src/components/charts/chord/Chord.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const Chord = ({

// interactivity
isInteractive,
tooltipFormat,

// motion
animate,
Expand Down Expand Up @@ -83,6 +84,7 @@ const Chord = ({
getOpacity={getRibbonOpacity}
setCurrent={setCurrentRibbon}
theme={theme}
tooltipFormat={tooltipFormat}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
{...motionProps}
Expand All @@ -95,6 +97,7 @@ const Chord = ({
getOpacity={getArcOpacity}
setCurrent={setCurrentArc}
theme={theme}
tooltipFormat={tooltipFormat}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
{...motionProps}
Expand Down
11 changes: 9 additions & 2 deletions src/components/charts/chord/ChordArcTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import PropTypes from 'prop-types'
import pure from 'recompose/pure'
import BasicTooltip from '../../tooltip/BasicTooltip'

const ChordArcTooltip = ({ arc, theme }) => (
<BasicTooltip id={arc.id} value={arc.value} color={arc.color} enableChip={true} theme={theme} />
const ChordArcTooltip = ({ arc, theme, format }) => (
<BasicTooltip
id={arc.id}
value={arc.value}
color={arc.color}
enableChip={true}
theme={theme}
format={format}
/>
)

ChordArcTooltip.propTypes = {
Expand Down
3 changes: 2 additions & 1 deletion src/components/charts/chord/ChordArcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ChordArcs = ({
getOpacity,
shapeGenerator,
theme,
tooltipFormat,
setCurrent,
showTooltip,
hideTooltip,
Expand All @@ -30,7 +31,7 @@ const ChordArcs = ({
motionStiffness,
}) => {
const commonProps = arc => {
const arcTooltip = <ChordArcTooltip arc={arc} theme={theme} />
const arcTooltip = <ChordArcTooltip arc={arc} theme={theme} format={tooltipFormat} />

return {
strokeWidth: borderWidth,
Expand Down
20 changes: 16 additions & 4 deletions src/components/charts/chord/ChordRibbons.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
*/
import React from 'react'
import PropTypes from 'prop-types'
import { mapValues } from 'lodash'
import { isFunction, mapValues } from 'lodash'
import { TransitionMotion, spring } from 'react-motion'
import { format as d3Format } from 'd3-format'
import compose from 'recompose/compose'
import withPropsOnChange from 'recompose/withPropsOnChange'
import pure from 'recompose/pure'
import { colorMotionSpring, getInterpolatedColor } from '../../../lib/colors'
import { midAngle } from '../../../lib/polar'
Expand Down Expand Up @@ -83,6 +86,7 @@ const ChordRibbons = ({
getBorderColor,
getOpacity,
theme,
tooltipFormat,
setCurrent,
showTooltip,
hideTooltip,
Expand All @@ -100,12 +104,12 @@ const ChordRibbons = ({
[
<Chip color={ribbon.source.color} />,
<strong>{ribbon.source.id}</strong>,
ribbon.source.value,
tooltipFormat ? tooltipFormat(ribbon.source.value) : ribbon.source.value,
],
[
<Chip color={ribbon.target.color} />,
<strong>{ribbon.target.id}</strong>,
ribbon.target.value,
tooltipFormat ? tooltipFormat(ribbon.target.value) : ribbon.target.value,
],
]}
/>
Expand Down Expand Up @@ -221,4 +225,12 @@ ChordRibbons.propTypes = {
hideTooltip: PropTypes.func.isRequired,
}

export default pure(ChordRibbons)
const enhance = compose(
withPropsOnChange(['tooltipFormat'], ({ tooltipFormat }) => {
if (!tooltipFormat || isFunction(tooltipFormat)) return { tooltipFormat }
return { tooltipFormat: d3Format(tooltipFormat) }
}),
pure
)

export default enhance(ChordRibbons)
1 change: 1 addition & 0 deletions src/components/charts/chord/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const ChordPropTypes = {
arcHoverOthersOpacity: PropTypes.number.isRequired,
ribbonHoverOpacity: PropTypes.number.isRequired,
ribbonHoverOthersOpacity: PropTypes.number.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),

// canvas specific
pixelRatio: PropTypes.number.isRequired,
Expand Down
4 changes: 2 additions & 2 deletions src/components/charts/heatmap/HeatMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class HeatMap extends Component {
static propTypes = HeatMapPropTypes

handleNodeHover = (showTooltip, node, event) => {
const { setCurrentNode, theme } = this.props
const { setCurrentNode, theme, tooltipFormat } = this.props
setCurrentNode(node)
showTooltip(<HeatMapCellTooltip node={node} theme={theme} />, event)
showTooltip(<HeatMapCellTooltip node={node} theme={theme} format={tooltipFormat} />, event)
}

handleNodeLeave = hideTooltip => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/charts/heatmap/HeatMapCellTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import React from 'react'
import pure from 'recompose/pure'
import BasicTooltip from '../../tooltip/BasicTooltip'

const HeatMapCellTooltip = ({ node, theme }) => (
const HeatMapCellTooltip = ({ node, theme, format }) => (
<BasicTooltip
id={`${node.yKey} - ${node.xKey}`}
value={node.value}
enableChip={true}
color={node.color}
theme={theme}
format={format}
/>
)

Expand Down
1 change: 1 addition & 0 deletions src/components/charts/heatmap/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const HeatMapPropTypes = {
hoverTarget: PropTypes.oneOf(['cell', 'row', 'column', 'rowColumn']).isRequired,
cellHoverOpacity: PropTypes.number.isRequired,
cellHoverOthersOpacity: PropTypes.number.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),

// canvas specific
pixelRatio: PropTypes.number.isRequired,
Expand Down
2 changes: 2 additions & 0 deletions src/components/charts/line/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const Line = ({

// interactivity
isInteractive,
tooltipFormat,

// stackTooltip
enableStackTooltip,
Expand Down Expand Up @@ -137,6 +138,7 @@ const Line = ({
showTooltip={showTooltip}
hideTooltip={hideTooltip}
theme={theme}
tooltipFormat={tooltipFormat}
/>
)}
{enableDots && (
Expand Down
3 changes: 2 additions & 1 deletion src/components/charts/line/LineSlices.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import PropTypes from 'prop-types'
import pure from 'recompose/pure'
import LineSlicesItem from './LineSlicesItem'

const LineSlices = ({ slices, height, showTooltip, hideTooltip, theme }) => (
const LineSlices = ({ slices, height, showTooltip, hideTooltip, theme, tooltipFormat }) => (
<g>
{slices.map(slice => (
<LineSlicesItem
Expand All @@ -21,6 +21,7 @@ const LineSlices = ({ slices, height, showTooltip, hideTooltip, theme }) => (
showTooltip={showTooltip}
hideTooltip={hideTooltip}
theme={theme}
tooltipFormat={tooltipFormat}
/>
))}
</g>
Expand Down
27 changes: 19 additions & 8 deletions src/components/charts/line/LineSlicesItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
import React from 'react'
import PropTypes from 'prop-types'
import { isFunction } from 'lodash'
import { format as d3Format } from 'd3-format'
import compose from 'recompose/compose'
import pure from 'recompose/pure'
import withState from 'recompose/withState'
Expand Down Expand Up @@ -56,14 +58,23 @@ LineSlicesItem.propTypes = {

const enhance = compose(
withState('isHover', 'setIsHover', false),
withPropsOnChange(['slice', 'theme'], ({ slice, theme }) => ({
tooltip: (
<TableTooltip
theme={theme}
rows={slice.points.map(p => [<Chip color={p.color} />, p.id, p.value])}
/>
),
})),
withPropsOnChange(['slice', 'theme', 'tooltipFormat'], ({ slice, theme, tooltipFormat }) => {
const format =
!tooltipFormat || isFunction(tooltipFormat) ? tooltipFormat : d3Format(tooltipFormat)

return {
tooltip: (
<TableTooltip
theme={theme}
rows={slice.points.map(p => [
<Chip color={p.color} />,
p.id,
format ? format(p.value) : p.value,
])}
/>
),
}
}),
withHandlers({
showTooltip: ({ showTooltip, setIsHover, tooltip }) => e => {
setIsHover(true)
Expand Down
1 change: 1 addition & 0 deletions src/components/charts/line/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const LinePropTypes = {
// interactivity
isInteractive: PropTypes.bool.isRequired,
enableStackTooltip: PropTypes.bool.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
}

export const LineDefaultProps = {
Expand Down
2 changes: 2 additions & 0 deletions src/components/charts/pie/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const Pie = ({

// interactivity
isInteractive,
tooltipFormat,
}) => {
const centerX = width / 2
const centerY = height / 2
Expand Down Expand Up @@ -168,6 +169,7 @@ const Pie = ({
enableChip={true}
color={d.data.color}
theme={theme}
format={tooltipFormat}
/>,
e
)
Expand Down
1 change: 1 addition & 0 deletions src/components/charts/pie/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const PiePropTypes = {

// interactivity
isInteractive: PropTypes.bool,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
}

export const PieDefaultProps = {
Expand Down
3 changes: 3 additions & 0 deletions src/components/charts/radar/Radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const Radar = ({

// interactivity
isInteractive,
tooltipFormat,
}) => {
const motionProps = {
animate,
Expand Down Expand Up @@ -119,6 +120,7 @@ const Radar = ({
radius={radius}
angleStep={angleStep}
theme={theme}
tooltipFormat={tooltipFormat}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
/>
Expand Down Expand Up @@ -191,6 +193,7 @@ Radar.propTypes = {

// interactivity
isInteractive: PropTypes.bool.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
}

export const RadarDefaultProps = {
Expand Down
2 changes: 2 additions & 0 deletions src/components/charts/radar/RadarTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const RadarTooltip = ({
radius,
angleStep,
theme,
tooltipFormat,
showTooltip,
hideTooltip,
}) => {
Expand Down Expand Up @@ -51,6 +52,7 @@ const RadarTooltip = ({
radius={radius}
arcGenerator={arc}
theme={theme}
tooltipFormat={tooltipFormat}
showTooltip={showTooltip}
hideTooltip={hideTooltip}
/>
Expand Down
Loading

0 comments on commit 0dfafff

Please sign in to comment.