-
-
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(chord): improve Chord component
- Loading branch information
Raphaël Benitte
committed
Aug 31, 2017
1 parent
67f7876
commit 16af134
Showing
8 changed files
with
523 additions
and
98 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
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,130 @@ | ||
/* | ||
* 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 { TransitionMotion, spring } from 'react-motion' | ||
import { colorMotionSpring, getInterpolatedColor } from '../../../lib/colors' | ||
import BasicTooltip from '../../tooltip/BasicTooltip' | ||
|
||
const ArcTooltip = ({ arc, theme }) => | ||
<BasicTooltip id={arc.id} value={arc.value} color={arc.color} enableChip={true} theme={theme} /> | ||
|
||
const ChordArcs = ({ | ||
arcs, | ||
arcBorderWidth, | ||
getOpacity, | ||
shapeGenerator, | ||
theme, | ||
setCurrent, | ||
showTooltip, | ||
hideTooltip, | ||
|
||
// motion | ||
animate, | ||
motionDamping, | ||
motionStiffness, | ||
}) => { | ||
const commonProps = arc => { | ||
const arcTooltip = <ArcTooltip arc={arc} theme={theme} /> | ||
|
||
return { | ||
strokeWidth: arcBorderWidth, | ||
onMouseEnter: e => { | ||
setCurrent(arc) | ||
showTooltip(arcTooltip, e) | ||
}, | ||
onMouseMove: e => { | ||
showTooltip(arcTooltip, e) | ||
}, | ||
onMouseLeave: () => { | ||
setCurrent(null) | ||
hideTooltip() | ||
}, | ||
} | ||
} | ||
|
||
if (animate !== true) { | ||
return ( | ||
<g> | ||
{arcs.map(arc => { | ||
const opacity = getOpacity(arc) | ||
|
||
return ( | ||
<path | ||
key={arc.key} | ||
d={shapeGenerator(arc)} | ||
fill={arc.color} | ||
fillOpacity={opacity} | ||
stroke={arc.color} | ||
strokeOpacity={opacity} | ||
{...commonProps(arc)} | ||
/> | ||
) | ||
})} | ||
</g> | ||
) | ||
} | ||
|
||
const springConfig = { | ||
damping: motionDamping, | ||
stiffness: motionStiffness, | ||
} | ||
|
||
return ( | ||
<TransitionMotion | ||
styles={arcs.map(arc => { | ||
return { | ||
key: arc.key, | ||
data: arc, | ||
style: { | ||
startAngle: spring(arc.startAngle, springConfig), | ||
endAngle: spring(arc.endAngle, springConfig), | ||
opacity: spring(getOpacity(arc), springConfig), | ||
...colorMotionSpring(arc.color, springConfig), | ||
}, | ||
} | ||
})} | ||
> | ||
{interpolatedStyles => | ||
<g> | ||
{interpolatedStyles.map(({ key, style, data: arc }) => { | ||
const color = getInterpolatedColor(style) | ||
|
||
return ( | ||
<path | ||
key={key} | ||
d={shapeGenerator({ | ||
startAngle: style.startAngle, | ||
endAngle: style.endAngle, | ||
})} | ||
fill={color} | ||
fillOpacity={style.opacity} | ||
stroke={color} | ||
strokeOpacity={style.opacity} | ||
{...commonProps(arc)} | ||
/> | ||
) | ||
})} | ||
</g>} | ||
</TransitionMotion> | ||
) | ||
} | ||
|
||
ChordArcs.propTypes = { | ||
arcs: PropTypes.array.isRequired, | ||
shapeGenerator: PropTypes.func.isRequired, | ||
borderWidth: PropTypes.number.isRequired, | ||
getOpacity: PropTypes.func.isRequired, | ||
setCurrent: PropTypes.func.isRequired, | ||
theme: PropTypes.object.isRequired, | ||
showTooltip: PropTypes.func.isRequired, | ||
hideTooltip: PropTypes.func.isRequired, | ||
} | ||
|
||
export default ChordArcs |
Oops, something went wrong.