Skip to content

Commit

Permalink
Fix issue with bar with when data changes from zero values
Browse files Browse the repository at this point in the history
  • Loading branch information
envex committed Sep 21, 2023
1 parent 68b0b2b commit b8f1ae7
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

<!-- ## Unreleased -->
## Unreleased

### Fixed

- Fixed issue where `<BarChart />` bar width would be 1px when data changes from all 0 values to data with non-zero values.

## [9.12.0] - 2023-09-19

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import type {Story} from '@storybook/react';

import {BarChart, BarChartProps} from '../../../../components';
import {META} from '../meta';
import {useState} from 'react';

export default {
...META,
title: `${META.title}/Playground`,
};

const DATA = [
{
data: [
{
value: 4,
key: '0',
},
{
value: 3,
key: '1',
},
{
value: 0,
key: '2',
},
{
value: 0,
key: '3',
},
{
value: 0,
key: '4',
},
{
value: 1,
key: '5',
},
{
value: 0,
key: '6',
},
{
value: 4,
key: '7',
},
{
value: 3,
key: '8',
},
{
value: 4,
key: '9',
},
{
value: 8,
key: '10',
},
{
value: 8,
key: '11',
},
{
value: 5,
key: '12',
},
{
value: 7,
key: '13',
},
{
value: 5,
key: '14',
},
],
name: 'First-time',
},
];

const ZERO_DATA = [
{...DATA[0], data: DATA[0].data.map((data) => ({...data, value: 0}))},
];

const Template: Story<BarChartProps> = () => {
const [data, setData] = useState(DATA);

return (
<>
<button
onClick={() => {
console.warn('SWAP');
setData((data) => {
return data === DATA ? ZERO_DATA : DATA;
});
}}
>
Flip
</button>
<div style={{width: 600, height: 400}}>
<BarChart data={data} />
</div>
</>
);
};

export const ZeroData = Template.bind({});
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const VerticalBar = memo(function Bar({
}: VerticalBarProps) {
const selectedTheme = useTheme();
const borderRadius = selectedTheme.bar.borderRadius;
const treatAsNegative = rawValue < 0 || rawValue === 0;
const treatAsNegative = rawValue < 0;
const zeroValue = rawValue === 0;

const yPosition = useMemo(() => {
Expand All @@ -67,7 +67,7 @@ export const VerticalBar = memo(function Bar({
);

const springConfig = useBarSpringConfig({animationDelay});
const rotate = `${treatAsNegative ? 'rotate(180)' : ''}`;
const rotate = `${treatAsNegative ? 'rotate(180)' : 'rotate(0)'}`;

const {pathD, transform} = useSpring({
from: {
Expand Down

0 comments on commit b8f1ae7

Please sign in to comment.