-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(radar): add support for tooltip on Radar component
- Loading branch information
Raphaël Benitte
committed
Aug 19, 2017
1 parent
93a43f8
commit acd9a4f
Showing
6 changed files
with
290 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import pure from 'recompose/pure' | ||
import { arc as d3Arc } from 'd3-shape' | ||
import RadarTooltipItem from './RadarTooltipItem' | ||
|
||
const RadarTooltip = ({ | ||
data, | ||
keys, | ||
getIndex, | ||
colorByKey, | ||
radius, | ||
angleStep, | ||
theme, | ||
showTooltip, | ||
hideTooltip, | ||
}) => { | ||
const arc = d3Arc().outerRadius(radius).innerRadius(0) | ||
|
||
const halfAngleStep = angleStep * 0.5 | ||
let rootStartAngle = -halfAngleStep | ||
|
||
return ( | ||
<g> | ||
{data.map((d, i) => { | ||
const index = getIndex(d) | ||
const startAngle = rootStartAngle | ||
const endAngle = startAngle + angleStep | ||
|
||
rootStartAngle += angleStep | ||
|
||
return ( | ||
<RadarTooltipItem | ||
key={index} | ||
datum={d} | ||
keys={keys} | ||
index={index} | ||
colorByKey={colorByKey} | ||
startAngle={startAngle} | ||
endAngle={endAngle} | ||
radius={radius} | ||
arcGenerator={arc} | ||
theme={theme} | ||
showTooltip={showTooltip} | ||
hideTooltip={hideTooltip} | ||
/> | ||
) | ||
})} | ||
</g> | ||
) | ||
} | ||
|
||
RadarTooltip.propTypes = { | ||
data: PropTypes.array.isRequired, | ||
keys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])).isRequired, | ||
getIndex: PropTypes.func.isRequired, | ||
colorByKey: PropTypes.object.isRequired, | ||
|
||
radius: PropTypes.number.isRequired, | ||
angleStep: PropTypes.number.isRequired, | ||
|
||
theme: PropTypes.object.isRequired, | ||
|
||
showTooltip: PropTypes.func.isRequired, | ||
hideTooltip: PropTypes.func.isRequired, | ||
} | ||
|
||
export default pure(RadarTooltip) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import compose from 'recompose/compose' | ||
import withState from 'recompose/withState' | ||
import withPropsOnChange from 'recompose/withPropsOnChange' | ||
import withHandlers from 'recompose/withHandlers' | ||
import pure from 'recompose/pure' | ||
import { sortBy } from 'lodash' | ||
import { positionFromAngle } from '../../../lib/arcUtils' | ||
import TableTooltip from '../../tooltip/TableTooltip' | ||
import Chip from '../../tooltip/Chip' | ||
|
||
const RadarTooltipItem = ({ path, tipX, tipY, showTooltip, hideTooltip, isHover }) => | ||
<g> | ||
<line x1={0} y1={0} x2={tipX} y2={tipY} stroke="#000" strokeOpacity={isHover ? 0.35 : 0} /> | ||
<path | ||
d={path} | ||
fill="#F00" | ||
fillOpacity={0} | ||
onMouseEnter={showTooltip} | ||
onMouseMove={showTooltip} | ||
onMouseLeave={hideTooltip} | ||
/> | ||
</g> | ||
|
||
RadarTooltipItem.propTypes = { | ||
datum: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired, | ||
keys: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])).isRequired, | ||
index: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, | ||
colorByKey: PropTypes.object.isRequired, | ||
|
||
startAngle: PropTypes.number.isRequired, | ||
endAngle: PropTypes.number.isRequired, | ||
radius: PropTypes.number.isRequired, | ||
tipX: PropTypes.number.isRequired, // computed | ||
tipY: PropTypes.number.isRequired, // computed | ||
|
||
arcGenerator: PropTypes.func.isRequired, // computed | ||
path: PropTypes.string.isRequired, // computed | ||
|
||
theme: PropTypes.object.isRequired, | ||
|
||
showTooltip: PropTypes.func.isRequired, // re-computed | ||
hideTooltip: PropTypes.func.isRequired, // re-computed | ||
|
||
isHover: PropTypes.bool.isRequired, // computed | ||
} | ||
|
||
const enhance = compose( | ||
withState('isHover', 'setIsHover', false), | ||
withPropsOnChange( | ||
['datum', 'keys', 'index', 'colorByKey', 'theme'], | ||
({ datum, keys, index, colorByKey, theme }) => ({ | ||
tooltip: ( | ||
<TableTooltip | ||
title={ | ||
<strong> | ||
{index} | ||
</strong> | ||
} | ||
rows={sortBy( | ||
keys.map(key => [<Chip color={colorByKey[key]} />, key, datum[key]]), | ||
'2' | ||
).reverse()} | ||
theme={theme} | ||
/> | ||
), | ||
}) | ||
), | ||
withPropsOnChange( | ||
['startAngle', 'endAngle', 'radius', 'arcGenerator'], | ||
({ startAngle, endAngle, radius, arcGenerator }) => { | ||
const position = positionFromAngle( | ||
startAngle + (endAngle - startAngle) * 0.5 - Math.PI / 2, | ||
radius | ||
) | ||
|
||
return { | ||
path: arcGenerator({ startAngle, endAngle }), | ||
tipX: position.x, | ||
tipY: position.y, | ||
} | ||
} | ||
), | ||
withHandlers({ | ||
showTooltip: ({ showTooltip, setIsHover, tooltip }) => e => { | ||
setIsHover(true) | ||
showTooltip(tooltip, e) | ||
}, | ||
hideTooltip: ({ hideTooltip, setIsHover }) => () => { | ||
setIsHover(false) | ||
hideTooltip() | ||
}, | ||
}), | ||
pure | ||
) | ||
|
||
export default enhance(RadarTooltipItem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import pure from 'recompose/pure' | ||
|
||
const Chip = ({ size, color }) => | ||
<span style={{ display: 'block', width: size, height: size, background: color }} /> | ||
|
||
Chip.propTypes = { | ||
size: PropTypes.number.isRequired, | ||
color: PropTypes.string.isRequired, | ||
} | ||
|
||
Chip.defaultProps = { | ||
size: 12, | ||
} | ||
|
||
export default pure(Chip) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters