-
-
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(sunburst): add mouse events and some labels (#880)
- Add slice labels at the first level - Add onClick/onMouseEnter/onMouseLeave events Co-authored-by: Manuele J Sarfatti <[email protected]> Co-authored-by: Julian Krieger <[email protected]> Co-authored-by: Asma Berkani <[email protected]> Co-authored-by: Neil Kistner <[email protected]>
- Loading branch information
1 parent
b968775
commit 1b3dd8f
Showing
7 changed files
with
353 additions
and
44 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,79 @@ | ||
/* | ||
* 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, { Component, Fragment } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { midAngle, positionFromAngle, radiansToDegrees, labelsThemePropType } from '@nivo/core' | ||
|
||
const sliceStyle = { | ||
pointerEvents: 'none', | ||
} | ||
|
||
export default class SunburstLabels extends Component { | ||
static propTypes = { | ||
nodes: PropTypes.arrayOf(PropTypes.shape({})).isRequired, | ||
label: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), | ||
skipAngle: PropTypes.number.isRequired, | ||
textColor: PropTypes.func.isRequired, | ||
theme: PropTypes.shape({ | ||
labels: labelsThemePropType.isRequired, | ||
}).isRequired, | ||
} | ||
|
||
static defaultProps = { | ||
skipAngle: 0, | ||
} | ||
|
||
render() { | ||
const { nodes, label, skipAngle, textColor, theme } = this.props | ||
|
||
let centerRadius = false | ||
|
||
return ( | ||
<Fragment> | ||
{nodes | ||
.filter(node => node.depth === 1) | ||
.map(node => { | ||
if (!centerRadius) { | ||
const innerRadius = Math.sqrt(node.y0) | ||
const outerRadius = Math.sqrt(node.y1) | ||
centerRadius = innerRadius + (outerRadius - innerRadius) / 2 | ||
} | ||
|
||
const startAngle = node.x0 | ||
const endAngle = node.x1 | ||
const angle = Math.abs(endAngle - startAngle) | ||
const angleDeg = radiansToDegrees(angle) | ||
|
||
if (angleDeg <= skipAngle) return null | ||
|
||
const middleAngle = midAngle({ startAngle, endAngle }) - Math.PI / 2 | ||
const position = positionFromAngle(middleAngle, centerRadius) | ||
|
||
return ( | ||
<g | ||
key={node.data.id} | ||
transform={`translate(${position.x}, ${position.y})`} | ||
style={sliceStyle} | ||
> | ||
<text | ||
textAnchor="middle" | ||
style={{ | ||
...theme.labels.text, | ||
fill: textColor(node.data, theme), | ||
}} | ||
> | ||
{label(node.data)} | ||
</text> | ||
</g> | ||
) | ||
})} | ||
</Fragment> | ||
) | ||
} | ||
} |
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
Oops, something went wrong.