Skip to content

Commit

Permalink
feat(sunburst): explain non-null assertions and remove linter warning…
Browse files Browse the repository at this point in the history
…s for those
  • Loading branch information
plouc authored and wyze committed Apr 23, 2021
1 parent d6386d9 commit 378c52d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/sunburst/src/Arcs.tsx
Expand Up @@ -49,7 +49,7 @@ export const Arcs = <RawDatum,>({
showTooltipFromEvent(createElement(tooltip, datum), event)
onMouseEnter?.(datum, event)
}
}, [isInteractive, showTooltipFromEvent, onMouseEnter])
}, [isInteractive, showTooltipFromEvent, tooltip, onMouseEnter])

const handleMouseMove = useMemo(() => {
if (!isInteractive) return undefined
Expand All @@ -58,7 +58,7 @@ export const Arcs = <RawDatum,>({
showTooltipFromEvent(createElement(tooltip, datum), event)
onMouseMove?.(datum, event)
}
}, [isInteractive, showTooltipFromEvent, onMouseMove])
}, [isInteractive, showTooltipFromEvent, tooltip, onMouseMove])

const handleMouseLeave = useMemo(() => {
if (!isInteractive) return undefined
Expand Down
13 changes: 12 additions & 1 deletion packages/sunburst/src/hooks.ts
Expand Up @@ -55,7 +55,10 @@ export const useSunburst = <RawDatum>({
const partition = d3Partition<RawDatum>().size([2 * Math.PI, radius * radius])
const descendants = partition(hierarchy)
.descendants()
.filter(descendant => descendant.depth > 0)
.filter(descendant => {
console.log(descendant.ancestors())
return descendant.depth > 0
})

const total = hierarchy.value ?? 0

Expand All @@ -67,6 +70,11 @@ export const useSunburst = <RawDatum>({

return sortedNodes.reduce<ComputedDatum<RawDatum>[]>((acc, descendant) => {
const id = getId(descendant.data)
// d3 hierarchy node value is optional by default as it depends on
// a call to `count()` or `sum()`, and we previously called `sum()`,
// d3 typings could be improved and make it non optional when calling
// one of those.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const value = descendant.value!
const percentage = (100 * value) / total
const path = descendant.ancestors().map(ancestor => getId(ancestor.data))
Expand All @@ -80,6 +88,9 @@ export const useSunburst = <RawDatum>({

let parent: ComputedDatum<RawDatum> | undefined
if (descendant.parent) {
// as the parent is defined by the input data, and we sorted the data
// by `depth`, we can safely assume it's defined.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parent = acc.find(node => node.id === getId(descendant.parent!.data))
}

Expand Down

0 comments on commit 378c52d

Please sign in to comment.