Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ec8b29b
Use a constant for chart types
adamwoodnz Sep 17, 2025
cf32104
Add getElementStyles method with color and lineStyles properties
adamwoodnz Sep 17, 2025
f0db3cd
Add changelog
adamwoodnz Sep 17, 2025
01ed5cc
Revert adding chartType
adamwoodnz Sep 17, 2025
8bcfbf4
Revert adding chart type constant
adamwoodnz Sep 17, 2025
cc34adc
Remove resolveGroupColor from context value
adamwoodnz Sep 17, 2025
f26958b
Allow for data options stroke color
adamwoodnz Sep 17, 2025
648a728
Add glyph
adamwoodnz Sep 17, 2025
eb2ac14
Simplify resolveColor
adamwoodnz Sep 17, 2025
6b1fe74
Fix global context tests
adamwoodnz Sep 17, 2025
a57df8c
Use getElementStyles in useChartLegendItems
adamwoodnz Sep 17, 2025
9180049
Update legend stories to use different shapes
adamwoodnz Sep 17, 2025
204be2a
Revert removal of legend shape styles handling
adamwoodnz Sep 18, 2025
eace67a
Remove override color from pie charts
adamwoodnz Sep 18, 2025
dae2793
Simplify getItemShapeStyles return type
adamwoodnz Sep 18, 2025
92ed0da
Loosen type of legendShapeStyles
adamwoodnz Sep 18, 2025
5db07d0
Fix legend color update lag
adamwoodnz Sep 18, 2025
bf97bbe
Add tests for getElementStyles
adamwoodnz Sep 18, 2025
a28be09
Fix legend glyph rendering from theme
adamwoodnz Sep 18, 2025
7b83864
Remove comments from context types
adamwoodnz Sep 18, 2025
81a34b8
Use getElementStyles for line chart glyphs
adamwoodnz Sep 18, 2025
23af8fa
Update test suite description
adamwoodnz Sep 19, 2025
4528ea5
Update test
adamwoodnz Sep 19, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Charts: Add get element styles utility to global context
23 changes: 8 additions & 15 deletions projects/js-packages/charts/src/components/bar-chart/bar-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,14 @@ const BarChartInternal: FC< BarChartProps > = ( {
totalPoints,
} );

const { resolveGroupColor } = useGlobalChartsContext();

const getColor = useCallback(
( seriesData: SeriesData, index: number ) =>
resolveGroupColor( {
group: seriesData.group,
index,
overrideColor: seriesData.options?.stroke,
} ),
[ resolveGroupColor ]
);
const { getElementStyles } = useGlobalChartsContext();

const getBarBackground = useCallback(
( index: number ) => () =>
withPatterns
? `url(#${ getPatternId( chartId, index ) })`
: getColor( dataSorted[ index ], index ),
[ withPatterns, getColor, dataSorted, chartId ]
: getElementStyles( { data: dataSorted[ index ], index } ).color,
[ withPatterns, getElementStyles, dataSorted, chartId ]
);

const renderDefaultTooltip = useCallback(
Expand Down Expand Up @@ -341,12 +331,15 @@ const BarChartInternal: FC< BarChartProps > = ( {
<>
<defs data-testid="bar-chart-patterns">
{ dataSorted.map( ( seriesData, index ) =>
renderPattern( index, getColor( seriesData, index ) )
renderPattern( index, getElementStyles( { data: seriesData, index } ).color )
) }
</defs>
<style>
{ dataSorted.map( ( seriesData, index ) =>
createPatternBorderStyle( index, getColor( seriesData, index ) )
createPatternBorderStyle(
index,
getElementStyles( { data: seriesData, index } ).color
)
) }
</style>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function useLeaderboardLegendItems( {
};
} ): BaseLegendItem[] {
const { leaderboardChart: leaderboardChartSettings } = useGlobalChartsTheme();
const { resolveGroupColor } = useGlobalChartsContext();
const { getElementStyles } = useGlobalChartsContext();

return useMemo( () => {
if ( ! data || data.length === 0 ) {
Expand All @@ -46,7 +46,7 @@ export function useLeaderboardLegendItems( {
const items: BaseLegendItem[] = [];

// Add current period legend item
const resolvedPrimaryColor = resolveGroupColor( {
const { color: resolvedPrimaryColor } = getElementStyles( {
index: 0,
overrideColor: primaryColor || leaderboardChartSettings.primaryColor,
} );
Expand All @@ -55,13 +55,11 @@ export function useLeaderboardLegendItems( {
label: legendLabels?.primary || __( 'Current period', 'jetpack-charts' ),
value: '',
color: resolvedPrimaryColor,
index: 0,
overrideColor: primaryColor,
} );

// Add comparison period legend item if comparison is enabled and overlay label is not enabled
if ( withComparison && ! withOverlayLabel ) {
const resolvedSecondaryColor = resolveGroupColor( {
const { color: resolvedSecondaryColor } = getElementStyles( {
index: 1,
overrideColor: secondaryColor || leaderboardChartSettings.secondaryColor,
} );
Expand All @@ -70,8 +68,6 @@ export function useLeaderboardLegendItems( {
label: legendLabels?.comparison || __( 'Previous period', 'jetpack-charts' ),
value: '',
color: resolvedSecondaryColor,
index: 1,
overrideColor: secondaryColor,
} );
}

Expand All @@ -83,7 +79,7 @@ export function useLeaderboardLegendItems( {
withComparison,
legendLabels,
leaderboardChartSettings,
resolveGroupColor,
getElementStyles,
withOverlayLabel,
] );
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ const LeaderboardChartInternal: FC< LeaderboardChartProps > = ( {
secondaryColor: settingsSecondaryColor,
deltaColors,
} = leaderboardChartSettings;
const { resolveGroupColor } = useGlobalChartsContext();
const resolvedPrimaryColor = resolveGroupColor( {
const { getElementStyles } = useGlobalChartsContext();
const { color: resolvedPrimaryColor } = getElementStyles( {
index: 0,
overrideColor: primaryColor || settingsPrimaryColor,
} );
const resolvedSecondaryColor = resolveGroupColor( {
const { color: resolvedSecondaryColor } = getElementStyles( {
index: 1,
overrideColor: secondaryColor || settingsSecondaryColor,
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ describe( 'useLeaderboardLegendItems', () => {
label: 'Current period',
value: '',
color: expect.any( String ),
index: 0,
overrideColor: undefined,
} );
} );

Expand All @@ -106,17 +104,13 @@ describe( 'useLeaderboardLegendItems', () => {
label: 'Current period',
value: '',
color: expect.any( String ),
index: 0,
overrideColor: undefined,
} );

// Previous period item
expect( result.current[ 1 ] ).toEqual( {
label: 'Previous period',
value: '',
color: expect.any( String ),
index: 1,
overrideColor: undefined,
} );
} );
} );
Expand All @@ -143,8 +137,6 @@ describe( 'useLeaderboardLegendItems', () => {
// Note: The actual color will be resolved by the context, but we can check structure
expect( result.current[ 0 ].color ).toBeTruthy();
expect( result.current[ 1 ].color ).toBeTruthy();
expect( result.current[ 0 ].overrideColor ).toBeUndefined();
expect( result.current[ 1 ].overrideColor ).toBeUndefined();
} );

it( 'should use custom primary color override', () => {
Expand All @@ -161,7 +153,7 @@ describe( 'useLeaderboardLegendItems', () => {
{ wrapper }
);

expect( result.current[ 0 ].overrideColor ).toBe( customPrimary );
expect( result.current[ 0 ].color ).toBe( customPrimary );
} );

it( 'should use custom secondary color override', () => {
Expand All @@ -180,8 +172,8 @@ describe( 'useLeaderboardLegendItems', () => {
{ wrapper }
);

expect( result.current[ 0 ].overrideColor ).toBe( customPrimary );
expect( result.current[ 1 ].overrideColor ).toBe( customSecondary );
expect( result.current[ 0 ].color ).toBe( customPrimary );
expect( result.current[ 1 ].color ).toBe( customSecondary );
} );

it( 'should use both custom colors with comparison', () => {
Expand All @@ -201,8 +193,8 @@ describe( 'useLeaderboardLegendItems', () => {
);

expect( result.current ).toHaveLength( 2 );
expect( result.current[ 0 ].overrideColor ).toBe( customPrimary );
expect( result.current[ 1 ].overrideColor ).toBe( customSecondary );
expect( result.current[ 0 ].color ).toBe( customPrimary );
expect( result.current[ 1 ].color ).toBe( customSecondary );
} );
} );

Expand Down Expand Up @@ -576,7 +568,7 @@ describe( 'useLeaderboardLegendItems', () => {
} );

describe( 'Index and structure validation', () => {
it( 'should have correct indices for items', () => {
it( 'should have correct number of items with comparison', () => {
const wrapper = createWrapper();
const { result } = renderHook(
() =>
Expand All @@ -588,8 +580,9 @@ describe( 'useLeaderboardLegendItems', () => {
{ wrapper }
);

expect( result.current[ 0 ].index ).toBe( 0 );
expect( result.current[ 1 ].index ).toBe( 1 );
expect( result.current ).toHaveLength( 2 );
expect( result.current[ 0 ].label ).toBe( 'Current period' );
expect( result.current[ 1 ].label ).toBe( 'Previous period' );
} );

it( 'should have empty value strings for all items', () => {
Expand Down
Loading
Loading