diff --git a/packages/polaris-viz/src/components/BarChart/stories/data.tsx b/packages/polaris-viz/src/components/BarChart/stories/data.tsx index 3b87de5f7..3a756efd1 100644 --- a/packages/polaris-viz/src/components/BarChart/stories/data.tsx +++ b/packages/polaris-viz/src/components/BarChart/stories/data.tsx @@ -1,3 +1,4 @@ +import React, {useState} from 'react'; import type {DataSeries} from '@shopify/polaris-viz-core'; import type {Story} from '@storybook/react'; @@ -5,7 +6,28 @@ import type {BarChartProps} from '../BarChart'; import {BarChart} from '../BarChart'; export const Template: Story = (args: BarChartProps) => { - return ; + const [firstValNegative, setFirstValNegative] = useState(false); + const data = [ + { + name: 'Breakfast', + data: [ + {key: 'Monday', value: firstValNegative ? -20 : 3}, + {key: 'Tuesday', value: firstValNegative ? -100 : 50}, + {key: 'Wednesday', value: firstValNegative ? 100 : 3}, + {key: 'Thursday', value: 8}, + {key: 'Friday', value: 50}, + {key: 'Saturday', value: 0}, + {key: 'Sunday', value: 0.1}, + ], + }, + ]; + + return ( + + + + + ); }; export const DEFAULT_DATA: DataSeries[] = [ diff --git a/packages/polaris-viz/src/components/VerticalBarChart/components/VerticalBar/VerticalBar.tsx b/packages/polaris-viz/src/components/VerticalBarChart/components/VerticalBar/VerticalBar.tsx index 848f94bb6..96fbfe7c1 100644 --- a/packages/polaris-viz/src/components/VerticalBarChart/components/VerticalBar/VerticalBar.tsx +++ b/packages/polaris-viz/src/components/VerticalBarChart/components/VerticalBar/VerticalBar.tsx @@ -4,6 +4,7 @@ import { DataType, getRoundedRectPath, useTheme, + usePrevious, } from '@shopify/polaris-viz-core'; import {useBarSpringConfig} from '../../../../hooks/useBarSpringConfig'; @@ -69,15 +70,31 @@ export const VerticalBar = memo(function Bar({ const springConfig = useBarSpringConfig({animationDelay}); const rotate = `${treatAsNegative ? 'rotate(180)' : 'rotate(0)'}`; + const prevValue = usePrevious(rawValue); + const signIsChanging = + prevValue == null ? false : Math.sign(prevValue) !== Math.sign(rawValue); + const {pathD, transform} = useSpring({ from: { pathD: getPath(1, width), transform: `translate(0 ${zeroPosition}) ${rotate}`, }, - to: { - pathD: getPath(height, width), - transform: `translate(0 ${yPosition}) ${rotate}`, - }, + to: signIsChanging + ? [ + { + pathD: getPath(0, width), + transform: `translate(0 ${zeroPosition}) ${rotate}`, + immediate: true, + }, + { + pathD: getPath(height, width), + transform: `translate(0 ${yPosition}) ${rotate}`, + }, + ] + : { + pathD: getPath(height, width), + transform: `translate(0 ${yPosition}) ${rotate}`, + }, ...springConfig, });