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

Remount RelatedAreas when data changes #1667

Merged
merged 1 commit into from
May 27, 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
8 changes: 8 additions & 0 deletions packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- Fixed issue where `<RelatedAreas />` would crash when the incoming data change had a different shape.

### Removed

- Removed animation from `<RelatedAreas />`.

### Added

- Added support for bucketing data into a "Other" group for Bar chart and Donut charts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {DataSeries} from '@shopify/polaris-viz-core';
import {
DEFAULT_CHART_PROPS,
DEFAULT_THEME_NAME,
Expand Down Expand Up @@ -45,6 +46,8 @@ export function LineChartRelational(props: LineChartRelationalProps) {
};
});

const relatedAreasKey = buildRelatedAreasKey(dataWithHiddenRelational);

return (
<LineChart
annotations={annotations}
Expand Down Expand Up @@ -75,7 +78,13 @@ export function LineChartRelational(props: LineChartRelationalProps) {
return (
<Fragment>
<MissingDataArea {...props} data={data} />
<RelatedAreas {...props} data={data} />
<RelatedAreas
data={data}
// remount the area otherwise it can't animate
// between areas that are differently sized
key={relatedAreasKey}
{...props}
/>
</Fragment>
);
},
Expand All @@ -88,3 +97,15 @@ export function LineChartRelational(props: LineChartRelationalProps) {
/>
);
}

function buildRelatedAreasKey(data: DataSeries[]) {
const relatedSeries = data.find((series) => {
return series?.metadata?.relatedIndex != null;
});

if (relatedSeries == null) {
return '';
}

return relatedSeries.data.map(({value}) => value?.toString()).join(':');
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import {useState} from 'react';
import type {DataPoint} from '@shopify/polaris-viz-core';
import {
BASE_ANIMATION_DURATION,
useSpringConfig,
useChartContext,
curveStepRounded,
} from '@shopify/polaris-viz-core';
import {useChartContext, curveStepRounded} from '@shopify/polaris-viz-core';
import {area as areaShape} from 'd3-shape';
import {animated, useSpring} from '@react-spring/web';
import type {LineChartSlotProps} from 'types';

import {useExternalHideEvents, useWatchActiveSeries} from '../../../../hooks';
Expand All @@ -28,24 +22,7 @@ export function RelatedAreas({yScale, xScale, data}: RelatedAreaProps) {
const percentileIndex = lineSeries.length + 1;

const {hiddenIndexes} = useExternalHideEvents();
const {shouldAnimate, id} = useChartContext();

const springConfig = useSpringConfig({
animationDelay: shouldAnimate
? BASE_ANIMATION_DURATION * (data.length + 1)
: 0,
});

const {opacity} = useSpring({
from: {
opacity: 0,
},
to: {
opacity: 1,
},
immediate: !shouldAnimate,
...springConfig,
});
const {id} = useChartContext();

useWatchActiveSeries(id ?? '', ({detail: {index}}) => {
setActiveIndex(index);
Expand Down Expand Up @@ -75,7 +52,7 @@ export function RelatedAreas({yScale, xScale, data}: RelatedAreaProps) {
}

return (
<animated.g style={{opacity}}>
<g>
{data.map((series, index) => {
if (
series.metadata?.relatedIndex == null ||
Expand All @@ -93,10 +70,10 @@ export function RelatedAreas({yScale, xScale, data}: RelatedAreaProps) {
index={percentileIndex}
key={index}
series={series}
shouldAnimate={shouldAnimate}
shouldAnimate={false}
/>
);
})}
</animated.g>
</g>
);
}
Loading
Loading