Skip to content
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

Fix bar chart animation when sign changes #1780

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Bar chart animation when value goes from positive to negative or vice versa

### Added

- Added support for responsive legends in `<LineChartRelational />`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DataType,
getRoundedRectPath,
useTheme,
usePrevious,
} from '@shopify/polaris-viz-core';

import {useBarSpringConfig} from '../../../../hooks/useBarSpringConfig';
Expand Down Expand Up @@ -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,
});

Expand Down
Loading