Skip to content

Commit 764565e

Browse files
committed
Revert removal of legend shape styles handling
1 parent 5def5a0 commit 764565e

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

projects/js-packages/charts/src/components/legend/hooks/use-chart-legend-items.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
import { formatPercentage } from '../../../utils';
88
import type { SeriesData, DataPointDate, DataPointPercentage } from '../../../types';
99
import type { BaseLegendItem } from '../types';
10-
import type { GlyphProps, LineStyles } from '@visx/xychart';
11-
import type { ReactNode, CSSProperties } from 'react';
10+
import type { LegendShape } from '@visx/legend/lib/types';
11+
import type { GlyphProps } from '@visx/xychart';
12+
import type { ReactNode } from 'react';
1213

1314
export type LegendValueDisplay = 'percentage' | 'value' | 'valueDisplay' | 'none';
1415

@@ -18,6 +19,7 @@ export interface ChartLegendOptions {
1819
renderGlyph?: < Datum extends object >( props: GlyphProps< Datum > ) => ReactNode;
1920
showValues?: boolean;
2021
legendValueDisplay?: LegendValueDisplay;
22+
legendShape?: LegendShape< SeriesData[], number >;
2123
}
2224

2325
/**
@@ -67,6 +69,7 @@ function formatPointValue(
6769
* @param withGlyph - Whether to include glyph rendering
6870
* @param glyphSize - Size of the glyph
6971
* @param renderGlyph - Component to render the glyph
72+
* @param legendShape - The shape to use for the item
7073
* @return Array of processed legend items
7174
*/
7275
function processSeriesData(
@@ -75,19 +78,21 @@ function processSeriesData(
7578
showValues: boolean,
7679
withGlyph: boolean,
7780
glyphSize: number,
78-
renderGlyph?: < Datum extends object >( props: GlyphProps< Datum > ) => ReactNode
81+
renderGlyph?: < Datum extends object >( props: GlyphProps< Datum > ) => ReactNode,
82+
legendShape?: LegendShape< SeriesData[], number >
7983
): BaseLegendItem[] {
8084
const mapper = ( series: SeriesData, index: number ) => {
81-
const { color, lineStyles, glyph } = getElementStyles( {
85+
const { color, glyph, shapeStyles } = getElementStyles( {
8286
data: series,
8387
index,
88+
legendShape,
8489
} );
8590

8691
const baseItem: BaseLegendItem = {
8792
label: series.label,
8893
value: showValues ? series.data?.length?.toString() || '0' : '',
8994
color,
90-
shapeStyle: lineStyles as CSSProperties & LineStyles,
95+
shapeStyles,
9196
};
9297

9398
if ( withGlyph ) {
@@ -116,6 +121,7 @@ function processSeriesData(
116121
* @param withGlyph - Whether to include glyph rendering
117122
* @param glyphSize - Size of the glyph
118123
* @param renderGlyph - Component to render the glyph
124+
* @param legendShape - The shape to use for the item
119125
* @return Array of processed legend items
120126
*/
121127
function processPointData(
@@ -125,19 +131,21 @@ function processPointData(
125131
legendValueDisplay: LegendValueDisplay,
126132
withGlyph: boolean,
127133
glyphSize: number,
128-
renderGlyph?: < Datum extends object >( props: GlyphProps< Datum > ) => ReactNode
134+
renderGlyph?: < Datum extends object >( props: GlyphProps< Datum > ) => ReactNode,
135+
legendShape?: LegendShape< SeriesData[], number >
129136
): BaseLegendItem[] {
130137
const mapper = ( point: DataPointDate | DataPointPercentage, index: number ) => {
131-
const { color, lineStyles, glyph } = getElementStyles( {
138+
const { color, glyph, shapeStyles } = getElementStyles( {
132139
data: point as DataPointPercentage,
133140
index,
141+
legendShape,
134142
} );
135143

136144
const baseItem: BaseLegendItem = {
137145
label: point.label,
138146
value: formatPointValue( point, showValues, legendValueDisplay ),
139147
color,
140-
shapeStyle: lineStyles as CSSProperties & LineStyles,
148+
shapeStyles,
141149
};
142150

143151
if ( withGlyph ) {
@@ -159,13 +167,18 @@ function processPointData(
159167

160168
/**
161169
* Hook to transform chart data into legend items
162-
* @param data - The chart data to transform
163-
* @param options - Configuration options for legend generation
170+
* @param data - The chart data to transform
171+
* @param options - Configuration options for legend generation
172+
* @param legendShape - The shape to use for the item
164173
* @return Array of legend items ready for display
165174
*/
166175
export function useChartLegendItems<
167176
T extends SeriesData[] | DataPointDate[] | DataPointPercentage[],
168-
>( data: T, options: ChartLegendOptions = {} ): BaseLegendItem[] {
177+
>(
178+
data: T,
179+
options: ChartLegendOptions = {},
180+
legendShape?: LegendShape< SeriesData[], number >
181+
): BaseLegendItem[] {
169182
const {
170183
showValues = false,
171184
legendValueDisplay = 'percentage',
@@ -188,7 +201,8 @@ export function useChartLegendItems<
188201
showValues,
189202
withGlyph,
190203
glyphSize,
191-
renderGlyph
204+
renderGlyph,
205+
legendShape
192206
);
193207
}
194208

@@ -200,7 +214,8 @@ export function useChartLegendItems<
200214
legendValueDisplay,
201215
withGlyph,
202216
glyphSize,
203-
renderGlyph
217+
renderGlyph,
218+
legendShape
204219
);
205220
}, [
206221
data,
@@ -210,5 +225,6 @@ export function useChartLegendItems<
210225
withGlyph,
211226
glyphSize,
212227
renderGlyph,
228+
legendShape,
213229
] );
214230
}

projects/js-packages/charts/src/components/legend/private/base-legend.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const BaseLegend: ForwardRefExoticComponent<
9191
const domain = legendScale.domain();
9292

9393
const getShapeStyle = useCallback(
94-
( { index }: { index: number } ) => items[ index ]?.shapeStyle,
94+
( { index }: { index: number } ) => items[ index ]?.shapeStyles,
9595
[ items ]
9696
);
9797

projects/js-packages/charts/src/components/legend/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ export type BaseLegendItem = {
3737
color: string;
3838
glyphSize?: number;
3939
renderGlyph?: < Datum extends object >( props: GlyphProps< Datum > ) => ReactNode;
40-
shapeStyle?: CSSProperties & LineStyles;
40+
shapeStyles?: CSSProperties & LineStyles;
4141
};

projects/js-packages/charts/src/components/line-chart/line-chart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ const LineChartInternal = forwardRef< SingleChartRef, LineChartProps >(
332332
);
333333

334334
// Create legend items using the reusable hook
335-
const legendItems = useChartLegendItems( dataSorted, legendOptions );
335+
const legendItems = useChartLegendItems( dataSorted, legendOptions, legendShape );
336336

337337
// Memoize metadata to prevent unnecessary re-registration
338338
const chartMetadata = useMemo(

projects/js-packages/charts/src/providers/chart-context/global-charts-provider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createContext, useCallback, useMemo, useState, useEffect, useRef } from 'react';
2-
import { getSeriesLineStyles, mergeThemes } from '../../utils';
2+
import { getItemShapeStyles, getSeriesLineStyles, mergeThemes } from '../../utils';
33
import { defaultTheme } from './themes';
44
import type { GlobalChartsContextValue, ChartRegistration } from './types';
55
import type { ChartTheme, CompleteChartTheme } from '../../types';
@@ -86,7 +86,7 @@ export const GlobalChartsProvider: FC< GlobalChartsProviderProps > = ( { childre
8686
);
8787

8888
const getElementStyles = useCallback< GlobalChartsContextValue[ 'getElementStyles' ] >(
89-
( { data, index, overrideColor } ) => {
89+
( { data, index, overrideColor, legendShape } ) => {
9090
const isSeriesData = data && typeof data === 'object' && 'data' in data && 'options' in data;
9191
const isPointPercentageData = data && typeof data === 'object' && 'percentage' in data;
9292

@@ -101,6 +101,9 @@ export const GlobalChartsProvider: FC< GlobalChartsProviderProps > = ( { childre
101101
} ),
102102
lineStyles: isSeriesData ? getSeriesLineStyles( data, index, providerTheme ) : {},
103103
glyph: providerTheme.glyphs?.[ index ],
104+
shapeStyles: isSeriesData
105+
? getItemShapeStyles( data, index, providerTheme, legendShape )
106+
: {},
104107
};
105108
},
106109
[ providerTheme, resolveColor ]

projects/js-packages/charts/src/providers/chart-context/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { ReactNode } from 'react';
1+
import { CSSProperties, ReactNode } from 'react';
22
import type { BaseLegendItem } from '../../components/legend';
33
import type { CompleteChartTheme, DataPointPercentage, SeriesData } from '../../types';
4+
import type { LegendShape } from '@visx/legend/lib/types';
45
import type { GlyphProps, LineStyles } from '@visx/xychart';
56

67
export interface ChartRegistration {
@@ -13,12 +14,14 @@ export type GetElementStylesParams = {
1314
index: number;
1415
data?: SeriesData | DataPointPercentage;
1516
overrideColor?: string;
17+
legendShape?: LegendShape< SeriesData[], number >;
1618
};
1719

1820
export type ElementStyles = {
1921
color: string;
2022
lineStyles: LineStyles;
2123
glyph: < Datum extends object >( props: GlyphProps< Datum > ) => ReactNode;
24+
shapeStyles: CSSProperties & LineStyles;
2225
};
2326

2427
export interface GlobalChartsContextValue {

0 commit comments

Comments
 (0)