-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sunburst): add innerRadiusRatio, renderRootNode prop to control size of inner circle #1794
Changes from all commits
db0ff0a
473266f
bb6404a
27b324e
4f7a04f
884177b
d579a16
d3b8e78
55258c3
6aa9aeb
1c9c4c9
2cd29b5
347eeaa
0c1643e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { useMemo } from 'react' | ||
import { partition as d3Partition, hierarchy as d3Hierarchy } from 'd3-hierarchy' | ||
import { scaleRadial as d3ScaleRadial } from 'd3-scale' | ||
import cloneDeep from 'lodash/cloneDeep' | ||
import sortBy from 'lodash/sortBy' | ||
import { usePropertyAccessor, useTheme, useValueFormatter } from '@nivo/core' | ||
|
@@ -21,6 +22,8 @@ export const useSunburst = <RawDatum>({ | |
valueFormat, | ||
radius, | ||
cornerRadius = defaultProps.cornerRadius, | ||
innerRadiusRatio = defaultProps.innerRadiusRatio, | ||
renderRootNode = defaultProps.renderRootNode, | ||
colors = defaultProps.colors, | ||
colorBy = defaultProps.colorBy, | ||
inheritColorFromParent = defaultProps.inheritColorFromParent, | ||
|
@@ -32,6 +35,8 @@ export const useSunburst = <RawDatum>({ | |
valueFormat?: DataProps<RawDatum>['valueFormat'] | ||
radius: number | ||
cornerRadius?: SunburstCommonProps<RawDatum>['cornerRadius'] | ||
innerRadiusRatio?: SunburstCommonProps<RawDatum>['innerRadiusRatio'] | ||
renderRootNode?: SunburstCommonProps<RawDatum>['renderRootNode'] | ||
colors?: SunburstCommonProps<RawDatum>['colors'] | ||
colorBy?: SunburstCommonProps<RawDatum>['colorBy'] | ||
inheritColorFromParent?: SunburstCommonProps<RawDatum>['inheritColorFromParent'] | ||
|
@@ -57,8 +62,10 @@ export const useSunburst = <RawDatum>({ | |
const hierarchy = d3Hierarchy(clonedData).sum(getValue) | ||
|
||
const partition = d3Partition<RawDatum>().size([2 * Math.PI, radius * radius]) | ||
// exclude root node | ||
const descendants = partition(hierarchy).descendants().slice(1) | ||
|
||
const descendants = renderRootNode | ||
? partition(hierarchy).descendants() | ||
: partition(hierarchy).descendants().slice(1) | ||
|
||
const total = hierarchy.value ?? 0 | ||
|
||
|
@@ -68,6 +75,12 @@ export const useSunburst = <RawDatum>({ | |
// are going to be computed first | ||
const sortedNodes = sortBy(descendants, 'depth') | ||
|
||
const innerRadiusOffset = radius * Math.min(innerRadiusRatio, 1) | ||
|
||
const maxDepth = Math.max(...sortedNodes.map(n => n.depth)) | ||
|
||
const scale = d3ScaleRadial().domain([0, maxDepth]).range([innerRadiusOffset, radius]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using d3 scaleLinear instead of scaleRadial would render better in my opinion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. scaleLinear looks quite different from the original implementation: Maybe we could update the sunburst props to include setters for the arc inner/outer radii. I'm not sure if this reaches a happy medium between customizability and ease of use, but my initial thought for the api would be something like:
@plouc Thoughts on this approach? |
||
|
||
return sortedNodes.reduce<ComputedDatum<RawDatum>[]>((acc, descendant) => { | ||
const id = getId(descendant.data) | ||
// d3 hierarchy node value is optional by default as it depends on | ||
|
@@ -82,8 +95,12 @@ export const useSunburst = <RawDatum>({ | |
const arc: Arc = { | ||
startAngle: descendant.x0, | ||
endAngle: descendant.x1, | ||
innerRadius: Math.sqrt(descendant.y0), | ||
outerRadius: Math.sqrt(descendant.y1), | ||
innerRadius: | ||
renderRootNode && descendant.depth === 0 ? 0 : scale(descendant.depth - 1), | ||
outerRadius: | ||
renderRootNode && descendant.depth === 0 | ||
? innerRadiusOffset | ||
: scale(descendant.depth), | ||
} | ||
|
||
let parent: ComputedDatum<RawDatum> | undefined | ||
|
@@ -125,6 +142,8 @@ export const useSunburst = <RawDatum>({ | |
getColor, | ||
inheritColorFromParent, | ||
getChildColor, | ||
innerRadiusRatio, | ||
renderRootNode, | ||
]) | ||
|
||
const arcGenerator = useArcGenerator({ cornerRadius }) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6457,6 +6457,13 @@ | |
dependencies: | ||
"@types/d3-time" "*" | ||
|
||
"@types/d3-scale@^3.2.3": | ||
version "3.3.2" | ||
resolved "https://registry.yarnpkg.com/@types/d3-scale/-/d3-scale-3.3.2.tgz#18c94e90f4f1c6b1ee14a70f14bfca2bd1c61d06" | ||
integrity sha512-gGqr7x1ost9px3FvIfUMi5XA/F/yAf4UkUDtdQhpH92XCT0Oa7zkkRzY61gPVJq+DxpHn/btouw5ohWkbBsCzQ== | ||
dependencies: | ||
"@types/d3-time" "^2" | ||
|
||
"@types/d3-shape@^1": | ||
version "1.3.8" | ||
resolved "https://registry.yarnpkg.com/@types/d3-shape/-/d3-shape-1.3.8.tgz#c3c15ec7436b4ce24e38de517586850f1fea8e89" | ||
|
@@ -6491,6 +6498,11 @@ | |
resolved "https://registry.yarnpkg.com/@types/d3-time/-/d3-time-1.1.1.tgz#6cf3a4242c3bbac00440dfb8ba7884f16bedfcbf" | ||
integrity sha512-ULX7LoqXTCYtM+tLYOaeAJK7IwCT+4Gxlm2MaH0ErKLi07R5lh8NHCAyWcDkCCmx1AfRcBEV6H9QE9R25uP7jw== | ||
|
||
"@types/d3-time@^2": | ||
version "2.1.1" | ||
resolved "https://registry.yarnpkg.com/@types/d3-time/-/d3-time-2.1.1.tgz#743fdc821c81f86537cbfece07093ac39b4bc342" | ||
integrity sha512-9MVYlmIgmRR31C5b4FVSWtuMmBHh2mOWQYfl7XAYOa8dsnb7iEmUmRSWSFgXFtkjxO65d7hTUHQC+RhR/9IWFg== | ||
|
||
"@types/debug@^0.0.30": | ||
version "0.0.30" | ||
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-0.0.30.tgz#dc1e40f7af3b9c815013a7860e6252f6352a84df" | ||
|
@@ -22270,7 +22282,7 @@ react-docgen@^5.0.0: | |
node-dir "^0.1.10" | ||
strip-indent "^3.0.0" | ||
|
||
[email protected], react-dom@^16.8.3, react-dom@^17.0.2: | ||
[email protected], react-dom@^17.0.2: | ||
version "17.0.2" | ||
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.2.tgz#ecffb6845e3ad8dbfcdc498f0d0a939736502c23" | ||
integrity sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA== | ||
|
@@ -22279,6 +22291,16 @@ [email protected], react-dom@^16.8.3, react-dom@^17.0.2: | |
object-assign "^4.1.1" | ||
scheduler "^0.20.2" | ||
|
||
react-dom@^16.8.3: | ||
version "16.14.0" | ||
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.14.0.tgz#7ad838ec29a777fb3c75c3a190f661cf92ab8b89" | ||
integrity sha512-1gCeQXDLoIqMgqD3IO2Ah9bnf0w9kzhwN5q4FGnHZ67hBm9yePzB5JJAIQCc8x3pFnNlwFq4RidZggNAAkzWWw== | ||
dependencies: | ||
loose-envify "^1.1.0" | ||
object-assign "^4.1.1" | ||
prop-types "^15.6.2" | ||
scheduler "^0.19.1" | ||
|
||
react-draggable@^4.0.3: | ||
version "4.2.0" | ||
resolved "https://registry.yarnpkg.com/react-draggable/-/react-draggable-4.2.0.tgz#40cc5209082ca7d613104bf6daf31372cc0e1114" | ||
|
@@ -22584,14 +22606,23 @@ react-transition-group@^4.3.0: | |
loose-envify "^1.4.0" | ||
prop-types "^15.6.2" | ||
|
||
[email protected], react@^16.8.3, react@^16.9.17, react@^17.0.2: | ||
[email protected], react@^17.0.2: | ||
version "17.0.2" | ||
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037" | ||
integrity sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA== | ||
dependencies: | ||
loose-envify "^1.1.0" | ||
object-assign "^4.1.1" | ||
|
||
react@^16.8.3, react@^16.9.17: | ||
version "16.14.0" | ||
resolved "https://registry.yarnpkg.com/react/-/react-16.14.0.tgz#94d776ddd0aaa37da3eda8fc5b6b18a4c9a3114d" | ||
integrity sha512-0X2CImDkJGApiAlcf0ODKIneSwBPhqJawOa5wCtKbu7ZECrmS26NvtSILynQ66cgkT/RJ4LidJOc3bUESwmU8g== | ||
dependencies: | ||
loose-envify "^1.1.0" | ||
object-assign "^4.1.1" | ||
prop-types "^15.6.2" | ||
|
||
reactcss@^1.2.0: | ||
version "1.2.3" | ||
resolved "https://registry.yarnpkg.com/reactcss/-/reactcss-1.2.3.tgz#c00013875e557b1cf0dfd9a368a1c3dab3b548dd" | ||
|
@@ -23593,6 +23624,14 @@ saxes@^5.0.0: | |
dependencies: | ||
xmlchars "^2.2.0" | ||
|
||
scheduler@^0.19.1: | ||
version "0.19.1" | ||
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.19.1.tgz#4f3e2ed2c1a7d65681f4c854fa8c5a1ccb40f196" | ||
integrity sha512-n/zwRWRYSUj0/3g/otKDRPMh6qv2SYMWNq85IEa8iZyAv8od9zDYpGSnpBEjNgcMNq6Scbu5KfIPxNF72R/2EA== | ||
dependencies: | ||
loose-envify "^1.1.0" | ||
object-assign "^4.1.1" | ||
|
||
scheduler@^0.20.2: | ||
version "0.20.2" | ||
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.20.2.tgz#4baee39436e34aa93b4874bddcbf0fe8b8b50e91" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This centers the root most label. Not sure if there is a more proper way to handle this, but I don't think it was accounted for originally
.