From 6144dbb17c2d851edba784f95c61aacf04f79b59 Mon Sep 17 00:00:00 2001 From: plouc Date: Sat, 11 Sep 2021 21:34:52 +0900 Subject: [PATCH] feat(radial-bar): add the ability to specify a static max value --- packages/radial-bar/src/RadialBar.tsx | 2 ++ packages/radial-bar/src/hooks.ts | 12 +++++++++--- packages/radial-bar/src/props.ts | 2 ++ packages/radial-bar/src/types.ts | 1 + website/src/data/components/radial-bar/props.ts | 9 +++++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/radial-bar/src/RadialBar.tsx b/packages/radial-bar/src/RadialBar.tsx index e228a1877..c3556551a 100644 --- a/packages/radial-bar/src/RadialBar.tsx +++ b/packages/radial-bar/src/RadialBar.tsx @@ -17,6 +17,7 @@ type InnerRadialBarProps = Omit< const InnerRadialBar = ({ data, + maxValue = svgDefaultProps.maxValue, valueFormat, startAngle: originalStartAngle = svgDefaultProps.startAngle, endAngle: originalEndAngle = svgDefaultProps.endAngle, @@ -79,6 +80,7 @@ const InnerRadialBar = ({ customLayerProps, } = useRadialBar({ data, + maxValue, valueFormat, startAngle, endAngle, diff --git a/packages/radial-bar/src/hooks.ts b/packages/radial-bar/src/hooks.ts index 800279c8e..c6f27602b 100644 --- a/packages/radial-bar/src/hooks.ts +++ b/packages/radial-bar/src/hooks.ts @@ -4,7 +4,7 @@ import { arc as d3Arc } from 'd3-shape' import { degreesToRadians, useValueFormatter } from '@nivo/core' import { Arc } from '@nivo/arcs' import { useOrdinalColorScale } from '@nivo/colors' -import { commonDefaultProps } from './props' +import { commonDefaultProps, svgDefaultProps } from './props' import { ComputedBar, RadialBarCommonProps, @@ -22,6 +22,7 @@ interface RadialBarGroup { export const useRadialBar = ({ data, + maxValue: maxValueDirective = svgDefaultProps.maxValue, valueFormat, startAngle = commonDefaultProps.startAngle, endAngle = commonDefaultProps.endAngle, @@ -35,6 +36,7 @@ export const useRadialBar = ({ tracksColor = commonDefaultProps.tracksColor, }: { data: RadialBarDataProps['data'] + maxValue: RadialBarCommonProps['maxValue'] valueFormat?: RadialBarCommonProps['valueFormat'] startAngle: RadialBarCommonProps['startAngle'] innerRadiusRatio: RadialBarCommonProps['innerRadius'] @@ -91,10 +93,14 @@ export const useRadialBar = ({ }) }) - result.maxValue = Math.max(...result.groups.map(group => group.total)) + if (maxValueDirective === 'auto') { + result.maxValue = Math.max(...result.groups.map(group => group.total)) + } else { + result.maxValue = maxValueDirective + } return result - }, [data]) + }, [data, maxValueDirective]) const valueScale = useMemo( () => scaleLinear().domain([0, maxValue]).range([startAngle, endAngle]), diff --git a/packages/radial-bar/src/props.ts b/packages/radial-bar/src/props.ts index 097205184..d626f06c5 100644 --- a/packages/radial-bar/src/props.ts +++ b/packages/radial-bar/src/props.ts @@ -3,6 +3,8 @@ import { ComputedBar, RadialBarLayerId } from './types' import { RadialBarTooltip } from './RadialBarTooltip' export const commonDefaultProps = { + maxValue: 'auto' as const, + layers: ['grid', 'tracks', 'bars', 'labels', 'legends'] as RadialBarLayerId[], startAngle: 0, diff --git a/packages/radial-bar/src/types.ts b/packages/radial-bar/src/types.ts index 79f184b22..30b156e63 100644 --- a/packages/radial-bar/src/types.ts +++ b/packages/radial-bar/src/types.ts @@ -64,6 +64,7 @@ export interface RadialBarTrackDatum { } export type RadialBarCommonProps = { + maxValue: 'auto' | number valueFormat: ValueFormat margin: Box diff --git a/website/src/data/components/radial-bar/props.ts b/website/src/data/components/radial-bar/props.ts index 3673effbc..69b26d67f 100644 --- a/website/src/data/components/radial-bar/props.ts +++ b/website/src/data/components/radial-bar/props.ts @@ -71,6 +71,15 @@ const props: ChartProperty[] = [ `, flavors: ['svg'], }, + { + key: 'maxValue', + group: 'Base', + type: `'auto' | number`, + required: false, + help: `If 'auto', the max value is derived from the data, otherwise use a static value.`, + flavors: ['svg'], + defaultValue: svgDefaultProps.maxValue, + }, { key: 'valueFormat', group: 'Base',