Skip to content

Commit

Permalink
Added seriesNameFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
envex committed Apr 4, 2024
1 parent 805dc5b commit cb3d7ed
Show file tree
Hide file tree
Showing 36 changed files with 380 additions and 24 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

### Added

- Added `seriesNameFormatter` prop to `<BarChart />`, `<ComboChart />`, `<LineChart />`, `<LineChartPredictive />`, `<LineChartRelational />`, `<SimpleBarChart />` & `<StackedAreaChart />`.

## [12.4.2] - 2024-04-04

Expand Down
5 changes: 5 additions & 0 deletions packages/polaris-viz/src/components/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
YAxisOptions,
ChartType,
ChartProps,
LabelFormatter,
} from '@shopify/polaris-viz-core';

import {getTooltipContentRenderer} from '../../utilities/getTooltipContentRenderer';
Expand Down Expand Up @@ -39,6 +40,7 @@ export type BarChartProps = {
direction?: Direction;
emptyStateText?: string;
renderLegendContent?: RenderLegendContent;
seriesNameFormatter?: LabelFormatter;
showLegend?: boolean;
skipLinkText?: string;
theme?: string;
Expand Down Expand Up @@ -70,6 +72,7 @@ export function BarChart(props: BarChartProps) {
yAxisOptions,
onError,
renderHiddenLegendLabel,
seriesNameFormatter = (value) => `${value}`,
} = {
...DEFAULT_CHART_PROPS,
...props,
Expand Down Expand Up @@ -105,6 +108,7 @@ export function BarChart(props: BarChartProps) {
emptyStateText={emptyStateText}
renderLegendContent={renderLegendContent}
renderTooltipContent={renderTooltip}
seriesNameFormatter={seriesNameFormatter}
showLegend={showLegend}
type={type}
xAxisOptions={xAxisOptionsWithDefaults}
Expand All @@ -118,6 +122,7 @@ export function BarChart(props: BarChartProps) {
renderHiddenLegendLabel={renderHiddenLegendLabel}
renderLegendContent={renderLegendContent}
renderTooltipContent={renderTooltip}
seriesNameFormatter={seriesNameFormatter}
showLegend={showLegend}
type={type}
xAxisOptions={xAxisOptionsWithDefaults}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type {Story} from '@storybook/react';

import {META} from './meta';

import type {BarChartProps} from '../../../components';

import {DEFAULT_DATA, Template} from './data';

export default {
...META,
title: 'polaris-viz/Chromatic/Charts/BarChart',
parameters: {
...META.parameters,
chromatic: {disableSnapshot: false},
},
};

export const FormattedValues: Story<BarChartProps> = Template.bind({});

FormattedValues.args = {
data: DEFAULT_DATA,
seriesNameFormatter: (value) => `Name: ${value}`,
xAxisOptions: {
labelFormatter: (value) => `xAxis: ${value}`,
},
yAxisOptions: {
labelFormatter: (value) => `yAxis: ${value}`,
},
onError: (a, b) => {
console.log({a, b});
},
};

export const FormattedValuesHorizontal: Story<BarChartProps> = Template.bind(
{},
);

FormattedValuesHorizontal.args = {
data: DEFAULT_DATA,
direction: 'horizontal',
seriesNameFormatter: (value) => `Name: ${value}`,
xAxisOptions: {
labelFormatter: (value) => `xAxis: ${value}`,
},
yAxisOptions: {
labelFormatter: (value) => `yAxis: ${value}`,
},
onError: (a, b) => {
console.log({a, b});
},
};
5 changes: 5 additions & 0 deletions packages/polaris-viz/src/components/ComboChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
DataGroup,
BoundingRect,
XAxisOptions,
LabelFormatter,
} from '@shopify/polaris-viz-core';

import {ChartElements} from '../ChartElements';
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface ChartProps {
annotationsLookupTable: AnnotationLookupTable;
data: DataGroup[];
renderTooltipContent(data: RenderTooltipContentData): React.ReactNode;
seriesNameFormatter: LabelFormatter;
showLegend: boolean;
theme: string;
xAxisOptions: Required<XAxisOptions>;
Expand All @@ -72,6 +74,7 @@ export function Chart({
theme,
xAxisOptions,
renderLegendContent,
seriesNameFormatter,
}: ChartProps) {
const selectedTheme = useTheme(theme);

Expand All @@ -89,6 +92,7 @@ export function Chart({
data,
dimensions,
showLegend,
seriesNameFormatter,
});

const {drawableHeight, chartYPosition, xAxisBounds, yAxisBounds} =
Expand Down Expand Up @@ -173,6 +177,7 @@ export function Chart({
renderTooltipContent,
data,
seriesColors: colors,
seriesNameFormatter,
});

const {hasXAxisAnnotations, hasYAxisAnnotations} = checkAvailableAnnotations(
Expand Down
4 changes: 4 additions & 0 deletions packages/polaris-viz/src/components/ComboChart/ComboChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
DataGroup,
ChartProps,
XAxisOptions,
LabelFormatter,
} from '@shopify/polaris-viz-core';
import type {ReactNode} from 'react';

Expand All @@ -27,6 +28,7 @@ export type ComboChartProps = {
data: DataGroup[];
annotations?: ComboAnnotation[];
renderTooltipContent?(data: RenderTooltipContentData): ReactNode;
seriesNameFormatter?: LabelFormatter;
showLegend?: boolean;
xAxisOptions?: Partial<XAxisOptions>;
renderLegendContent?: RenderLegendContent;
Expand All @@ -42,6 +44,7 @@ export function ComboChart(props: ComboChartProps) {
id,
isAnimated,
renderTooltipContent,
seriesNameFormatter = (value) => `${value}`,
showLegend = true,
theme = defaultTheme,
xAxisOptions,
Expand Down Expand Up @@ -84,6 +87,7 @@ export function ComboChart(props: ComboChartProps) {
annotationsLookupTable={annotationsLookupTable}
data={data}
renderTooltipContent={renderTooltip}
seriesNameFormatter={seriesNameFormatter}
showLegend={showLegend}
theme={theme}
xAxisOptions={xAxisOptionsWithDefaults}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type {ReactNode} from 'react';
import {useCallback} from 'react';
import type {Color, DataGroup, Shape} from '@shopify/polaris-viz-core';
import type {
Color,
DataGroup,
LabelFormatter,
Shape,
} from '@shopify/polaris-viz-core';
import {useChartContext} from '@shopify/polaris-viz-core';

import {flattenDataGroupToDataSeries} from '../../../utilities/flattenDataGroupToDataSeries';
Expand All @@ -14,12 +19,14 @@ export interface Props {
data: DataGroup[];
seriesColors: Color[];
renderTooltipContent: (data: RenderTooltipContentData) => ReactNode;
seriesNameFormatter: LabelFormatter;
}

export function useComboChartTooltipContent({
data,
renderTooltipContent,
seriesColors,
seriesNameFormatter,
}: Props) {
const {theme} = useChartContext();

Expand Down Expand Up @@ -51,7 +58,7 @@ export function useComboChartTooltipContent({
const {value} = seriesData[activeIndex];

data.data.push({
key: `${name}`,
key: `${seriesNameFormatter(name ?? '')}`,
value: yAxisOptionsWithDefaults.labelFormatter(value),
color: color ?? seriesColors[index],
isComparison,
Expand All @@ -71,6 +78,6 @@ export function useComboChartTooltipContent({
theme,
});
},
[data, seriesColors, renderTooltipContent, theme],
[data, seriesColors, renderTooltipContent, theme, seriesNameFormatter],
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type {Story} from '@storybook/react';

import {META} from './meta';

import type {ComboChartProps} from '../../../components';

import {DEFAULT_DATA, Template} from './data';

export default {
...META,
title: 'polaris-viz/Chromatic/Charts/ComboChart',
parameters: {
...META.parameters,
chromatic: {disableSnapshot: false},
},
};

export const FormattedValues: Story<ComboChartProps> = Template.bind({});

FormattedValues.args = {
data: DEFAULT_DATA,
seriesNameFormatter: (value) => `Name: ${value}`,
xAxisOptions: {
labelFormatter: (value) => `xAxis: ${value}`,
},
onError: (a, b) => {
console.log({a, b});
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export const SampleLegendContainer = ({theme} = {theme: 'Default'}) => {
showLegend: true,
dimensions: {height: 0, width: 0},
colors,
seriesNameFormatter: (value) => `${value}`,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function LegendValues({
data: [{series: allData, shape: 'Bar'}],
colors: seriesColors,
dimensions,
seriesNameFormatter,
});

const {displayedData, hiddenData} = useOverflowLegend({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
XAxisOptions,
YAxisOptions,
BoundingRect,
LabelFormatter,
} from '@shopify/polaris-viz-core';
import {animated} from '@react-spring/web';

Expand Down Expand Up @@ -59,6 +60,7 @@ export interface ChartProps {
annotationsLookupTable: AnnotationLookupTable;
data: DataSeries[];
renderTooltipContent: (data: RenderTooltipContentData) => ReactNode;
seriesNameFormatter: LabelFormatter;
showLegend: boolean;
type: ChartType;
xAxisOptions: Required<XAxisOptions>;
Expand All @@ -75,6 +77,7 @@ export function Chart({
renderHiddenLegendLabel,
renderLegendContent,
renderTooltipContent,
seriesNameFormatter,
showLegend,
type,
xAxisOptions,
Expand Down Expand Up @@ -103,6 +106,7 @@ export function Chart({
dimensions,
showLegend,
colors: seriesColors,
seriesNameFormatter,
});

const {allNumbers, longestLabel, areAllNegative} = useDataForHorizontalChart({
Expand Down Expand Up @@ -164,6 +168,7 @@ export function Chart({
data,
seriesColors,
renderTooltipContent,
seriesNameFormatter,
});

const {transitions} = useHorizontalTransitions({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
XAxisOptions,
YAxisOptions,
BoundingRect,
LabelFormatter,
} from '@shopify/polaris-viz-core';

import type {
Expand All @@ -18,6 +19,7 @@ import {Chart} from './Chart';
export interface HorizontalBarChartProps {
data: DataSeries[];
renderTooltipContent: (data: RenderTooltipContentData) => ReactNode;
seriesNameFormatter: LabelFormatter;
showLegend: boolean;
xAxisOptions: Required<XAxisOptions>;
yAxisOptions: Required<YAxisOptions>;
Expand All @@ -35,6 +37,7 @@ export function HorizontalBarChart({
renderHiddenLegendLabel,
renderLegendContent,
renderTooltipContent,
seriesNameFormatter,
showLegend,
type = 'default',
xAxisOptions,
Expand All @@ -46,6 +49,7 @@ export function HorizontalBarChart({
annotationsLookupTable={annotationsLookupTable}
data={data}
renderTooltipContent={renderTooltipContent}
seriesNameFormatter={seriesNameFormatter}
showLegend={showLegend}
type={type}
xAxisOptions={xAxisOptions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ function getAlteredDimensions(
export interface Props {
showLegend: boolean;
data: DataGroup[];
seriesNameFormatter: LabelFormatter;
colors?: Color[];
dimensions?: Dimensions;
direction?: Direction;
maxWidth?: number;
seriesNameFormatter?: LabelFormatter;
}

export function useLegend({
Expand All @@ -43,7 +43,7 @@ export function useLegend({
showLegend,
direction = 'horizontal',
maxWidth = 0,
seriesNameFormatter = (value) => `${value}`,
seriesNameFormatter,
}: Props) {
const defaultHeight = showLegend ? DEFAULT_LEGEND_HEIGHT : 0;

Expand Down
Loading

0 comments on commit cb3d7ed

Please sign in to comment.