Skip to content

Commit

Permalink
Merge pull request #1652 from Shopify/envex/donut-series-formatter
Browse files Browse the repository at this point in the history
Use seriesNameFormatter in overflow legends
  • Loading branch information
envex authored Apr 3, 2024
2 parents 9859b78 + bfc21fe commit 0b82563
Show file tree
Hide file tree
Showing 5 changed files with 18 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 `<DonutChart />` overflow legends wouldn't use the `seriesNameFormatter`.

## [12.4.0] - 2024-04-03

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export function LegendValues({
lastVisibleIndex={allData.length - hiddenData.length}
setActivatorWidth={() => null}
dimensions={dimensions}
seriesNameFormatter={seriesNameFormatter}
/>
)}
</React.Fragment>
Expand Down
4 changes: 4 additions & 0 deletions packages/polaris-viz/src/components/Legend/Legend.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Fragment} from 'react';
import type {RefObject} from 'react';
import type {LabelFormatter} from '@shopify/polaris-viz-core';
import {DEFAULT_THEME_NAME} from '@shopify/polaris-viz-core';

import {useExternalHideEvents} from '../../hooks';
Expand All @@ -18,6 +19,7 @@ export interface LegendProps {
indexOffset?: number;
truncate?: boolean;
showLegendValues?: boolean;
seriesNameFormatter?: LabelFormatter;
}

export function Legend({
Expand All @@ -30,6 +32,7 @@ export function Legend({
backgroundColor,
truncate = false,
showLegendValues = false,
seriesNameFormatter,
}: LegendProps) {
const {hiddenIndexes} = useExternalHideEvents();

Expand All @@ -54,6 +57,7 @@ export function Legend({
}}
truncate={truncate}
showLegendValues={showLegendValues}
seriesNameFormatter={seriesNameFormatter}
/>
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {LabelFormatter} from '@shopify/polaris-viz-core';
import {
getColorVisionEventAttrs,
getColorVisionStylesForActiveIndex,
Expand Down Expand Up @@ -35,6 +36,7 @@ export interface LegendItemProps extends LegendData {
backgroundColor?: string;
truncate?: boolean;
showLegendValues?: boolean;
seriesNameFormatter?: LabelFormatter;
}

export function LegendItem({
Expand All @@ -52,6 +54,7 @@ export function LegendItem({
backgroundColor,
truncate = false,
showLegendValues = false,
seriesNameFormatter = (value) => `${value}`,
}: LegendItemProps) {
const selectedTheme = useTheme(theme);
const ref = useRef<HTMLButtonElement | null>(null);
Expand Down Expand Up @@ -118,7 +121,7 @@ export function LegendItem({
color: selectedTheme.legend.labelColor,
}}
>
{name}
{seriesNameFormatter(name)}
</span>
{!showLegendValues || value == null ? null : (
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useChartContext,
useTheme,
} from '@shopify/polaris-viz-core';
import type {BoundingRect} from '@shopify/polaris-viz-core';
import type {BoundingRect, LabelFormatter} from '@shopify/polaris-viz-core';

import type {LegendData} from '../../../types';
import {TOOLTIP_BG_OPACITY} from '../../../constants';
Expand All @@ -34,6 +34,7 @@ interface Props {
theme?: string;
lastVisibleIndex?: number;
dimensions?: BoundingRect;
seriesNameFormatter?: LabelFormatter;
}

export const LEGEND_TOOLTIP_ID = 'legend-toolip';
Expand All @@ -48,6 +49,7 @@ export function HiddenLegendTooltip({
lastVisibleIndex = 0,
setActivatorWidth,
dimensions,
seriesNameFormatter,
}: Props) {
const selectedTheme = useTheme();
const {isFirefox} = useBrowserCheck();
Expand Down Expand Up @@ -168,6 +170,7 @@ export function HiddenLegendTooltip({
theme={theme}
indexOffset={lastVisibleIndex}
backgroundColor="transparent"
seriesNameFormatter={seriesNameFormatter}
/>
</div>,
container,
Expand Down

0 comments on commit 0b82563

Please sign in to comment.