Skip to content

Commit 6144dbb

Browse files
committedSep 11, 2021
feat(radial-bar): add the ability to specify a static max value
1 parent 587b179 commit 6144dbb

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed
 

‎packages/radial-bar/src/RadialBar.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type InnerRadialBarProps<D extends RadialBarDatum = RadialBarDatum> = Omit<
1717

1818
const InnerRadialBar = <D extends RadialBarDatum>({
1919
data,
20+
maxValue = svgDefaultProps.maxValue,
2021
valueFormat,
2122
startAngle: originalStartAngle = svgDefaultProps.startAngle,
2223
endAngle: originalEndAngle = svgDefaultProps.endAngle,
@@ -79,6 +80,7 @@ const InnerRadialBar = <D extends RadialBarDatum>({
7980
customLayerProps,
8081
} = useRadialBar<D>({
8182
data,
83+
maxValue,
8284
valueFormat,
8385
startAngle,
8486
endAngle,

‎packages/radial-bar/src/hooks.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { arc as d3Arc } from 'd3-shape'
44
import { degreesToRadians, useValueFormatter } from '@nivo/core'
55
import { Arc } from '@nivo/arcs'
66
import { useOrdinalColorScale } from '@nivo/colors'
7-
import { commonDefaultProps } from './props'
7+
import { commonDefaultProps, svgDefaultProps } from './props'
88
import {
99
ComputedBar,
1010
RadialBarCommonProps,
@@ -22,6 +22,7 @@ interface RadialBarGroup<D extends RadialBarDatum> {
2222

2323
export const useRadialBar = <D extends RadialBarDatum = RadialBarDatum>({
2424
data,
25+
maxValue: maxValueDirective = svgDefaultProps.maxValue,
2526
valueFormat,
2627
startAngle = commonDefaultProps.startAngle,
2728
endAngle = commonDefaultProps.endAngle,
@@ -35,6 +36,7 @@ export const useRadialBar = <D extends RadialBarDatum = RadialBarDatum>({
3536
tracksColor = commonDefaultProps.tracksColor,
3637
}: {
3738
data: RadialBarDataProps<D>['data']
39+
maxValue: RadialBarCommonProps<D>['maxValue']
3840
valueFormat?: RadialBarCommonProps<D>['valueFormat']
3941
startAngle: RadialBarCommonProps<D>['startAngle']
4042
innerRadiusRatio: RadialBarCommonProps<D>['innerRadius']
@@ -91,10 +93,14 @@ export const useRadialBar = <D extends RadialBarDatum = RadialBarDatum>({
9193
})
9294
})
9395

94-
result.maxValue = Math.max(...result.groups.map(group => group.total))
96+
if (maxValueDirective === 'auto') {
97+
result.maxValue = Math.max(...result.groups.map(group => group.total))
98+
} else {
99+
result.maxValue = maxValueDirective
100+
}
95101

96102
return result
97-
}, [data])
103+
}, [data, maxValueDirective])
98104

99105
const valueScale = useMemo(
100106
() => scaleLinear<number, number>().domain([0, maxValue]).range([startAngle, endAngle]),

‎packages/radial-bar/src/props.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { ComputedBar, RadialBarLayerId } from './types'
33
import { RadialBarTooltip } from './RadialBarTooltip'
44

55
export const commonDefaultProps = {
6+
maxValue: 'auto' as const,
7+
68
layers: ['grid', 'tracks', 'bars', 'labels', 'legends'] as RadialBarLayerId[],
79

810
startAngle: 0,

‎packages/radial-bar/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface RadialBarTrackDatum {
6464
}
6565

6666
export type RadialBarCommonProps<D extends RadialBarDatum = RadialBarDatum> = {
67+
maxValue: 'auto' | number
6768
valueFormat: ValueFormat<number>
6869

6970
margin: Box

‎website/src/data/components/radial-bar/props.ts

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ const props: ChartProperty[] = [
7171
`,
7272
flavors: ['svg'],
7373
},
74+
{
75+
key: 'maxValue',
76+
group: 'Base',
77+
type: `'auto' | number`,
78+
required: false,
79+
help: `If 'auto', the max value is derived from the data, otherwise use a static value.`,
80+
flavors: ['svg'],
81+
defaultValue: svgDefaultProps.maxValue,
82+
},
7483
{
7584
key: 'valueFormat',
7685
group: 'Base',

0 commit comments

Comments
 (0)
Please sign in to comment.