Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit a0597dd

Browse files
committed
Fix issue where trend indicator would render outside chart bounds
1 parent 36fadba commit a0597dd

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

packages/polaris-viz/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8-
<!-- ## Unreleased -->
8+
## Unreleased
9+
10+
### Fixed
11+
12+
- Fixed issue in `<SimpleBarChart />` where the trend indicator was not being positioned correctly when the value was `null`.
913

1014
## [15.8.0] - 2025-01-16
1115

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type {Story} from '@storybook/react';
2+
3+
import {SimpleBarChart, SimpleBarChartProps} from '../../../../components';
4+
import {META} from '../meta';
5+
import type {SimpleBarChartDataSeries} from '../../types';
6+
import {
7+
DEFAULT_THEME_NAME,
8+
PolarisVizProvider,
9+
} from '@shopify/polaris-viz-core';
10+
11+
export default {
12+
...META,
13+
title: `${META.title}/Playground`,
14+
};
15+
16+
const DATA: SimpleBarChartDataSeries[] = [
17+
{
18+
name: '',
19+
data: [
20+
{key: 'One', value: 6000},
21+
{key: 'Two', value: 5300},
22+
],
23+
metadata: {
24+
trends: {
25+
'0': {},
26+
},
27+
},
28+
},
29+
{
30+
name: '',
31+
data: [
32+
{key: 'One', value: 4500},
33+
{key: 'Two', value: 1500},
34+
],
35+
},
36+
];
37+
38+
const Template: Story<SimpleBarChartProps> = () => {
39+
const svgStyle = `
40+
svg {
41+
background: rgba(0, 255, 0, 0.1);
42+
}
43+
`;
44+
45+
return (
46+
<div style={{height: 600, width: 800}}>
47+
<style>{svgStyle}</style>
48+
<PolarisVizProvider
49+
animated={{} as any}
50+
themes={{
51+
[DEFAULT_THEME_NAME]: {
52+
chartContainer: {
53+
backgroundColor: 'rgba(255, 0, 0, 0.1)',
54+
padding: '20px',
55+
},
56+
},
57+
}}
58+
>
59+
<SimpleBarChart data={DATA} />
60+
</PolarisVizProvider>
61+
</div>
62+
);
63+
};
64+
65+
export const NoTrendTight = Template.bind({});

packages/polaris-viz/src/components/SimpleBarChart/utilities.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {HORIZONTAL_BAR_LABEL_OFFSET} from '@shopify/polaris-viz-core';
22

33
import {
4+
TREND_INDICATOR_NO_VALUE_WIDTH,
45
estimateTrendIndicatorWidth,
56
TREND_INDICATOR_FONT_WEIGHT,
67
} from '../TrendIndicator';
@@ -24,15 +25,18 @@ export function getLongestTrendIndicator(
2425
for (const [index, trend] of trendEntries) {
2526
const dataPoint = seriesData[index];
2627

27-
if (trend == null || trend.value == null || dataPoint?.value == null) {
28+
if (trend == null || dataPoint?.value == null) {
2829
return longestTrendIndicator;
2930
}
3031

31-
const trendStringWidth = estimateTrendIndicatorWidth(
32-
trend.value,
33-
fontSize,
34-
TREND_INDICATOR_FONT_WEIGHT,
35-
).totalWidth;
32+
const trendStringWidth =
33+
trend.value == null
34+
? TREND_INDICATOR_NO_VALUE_WIDTH
35+
: estimateTrendIndicatorWidth(
36+
trend.value,
37+
fontSize,
38+
TREND_INDICATOR_FONT_WEIGHT,
39+
).totalWidth;
3640

3741
// Positive value
3842
if (dataPoint.value > 0) {

packages/polaris-viz/src/components/TrendIndicator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export type {TrendIndicatorProps} from './TrendIndicator';
33
export {estimateTrendIndicatorWidth} from './utilities/estimateTrendIndicatorWidth';
44
export {HEIGHT as TREND_INDICATOR_HEIGHT} from './constants';
55
export {FONT_WEIGHT as TREND_INDICATOR_FONT_WEIGHT} from './constants';
6+
export {NO_VALUE_WIDTH as TREND_INDICATOR_NO_VALUE_WIDTH} from './constants';

0 commit comments

Comments
 (0)