-
Notifications
You must be signed in to change notification settings - Fork 834
Charts: Add interactive legend for toggling series visibility in LineChart #45506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Charts: Add interactive legend for toggling series visibility in LineChart #45506
Conversation
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds interactive legend support to toggle series visibility across charts. Key changes introduce visibility state management in the global charts context, wire up interactive legend behaviors with full keyboard/ARIA support, and update chart components to respect hidden series while preserving color consistency via original series indices.
- Add series visibility API to GlobalChartsContext (toggle/isVisible/getHiddenSeries) and implement state via Map<chartId, Set>
- Update LineChart, BarChart, and PieChart to filter hidden series and preserve original indices for consistent colors
- Make Legend items interactive (click and keyboard), with new styles and Storybook story; pass chartId automatically from context
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 8 comments.
Show a summary per file
File | Description |
---|---|
projects/js-packages/charts/src/stories/legend-config.tsx | Adds a Storybook argType for enabling interactive legends |
projects/js-packages/charts/src/providers/chart-context/types.ts | Extends GlobalChartsContextValue with methods for series visibility |
projects/js-packages/charts/src/providers/chart-context/global-charts-provider.tsx | Implements visibility state and exposes toggle/isVisible/getHiddenSeries |
projects/js-packages/charts/src/components/pie-chart/pie-chart.tsx | Filters hidden series and preserves original indices for colors |
projects/js-packages/charts/src/components/line-chart/line-chart.tsx | Filters hidden series; uses original indices to keep colors/IDs stable |
projects/js-packages/charts/src/components/legend/types.ts | Adds interactive and chartId props to BaseLegend/Legend props |
projects/js-packages/charts/src/components/legend/stories/index.stories.tsx | Adds InteractiveLegend story demonstrating toggling |
projects/js-packages/charts/src/components/legend/private/base-legend.tsx | Adds interactive behaviors, handlers, ARIA, and styling hooks |
projects/js-packages/charts/src/components/legend/private/base-legend.module.scss | Adds styles for interactive and inactive legend states |
projects/js-packages/charts/src/components/legend/legend.tsx | Passes chartId from context to BaseLegend |
projects/js-packages/charts/src/components/bar-chart/bar-chart.tsx | Filters hidden series; maintains original index for color/pattern consistency |
projects/js-packages/charts/changelog/add-charts-54-legends-toggling-visibility-of-series | Adds changelog entry |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
projects/js-packages/charts/src/components/legend/private/base-legend.tsx
Show resolved
Hide resolved
projects/js-packages/charts/src/components/legend/private/base-legend.module.scss
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/providers/chart-context/global-charts-provider.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/components/line-chart/line-chart.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/components/bar-chart/bar-chart.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/components/pie-chart/pie-chart.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/components/legend/private/base-legend.tsx
Outdated
Show resolved
Hide resolved
Code Coverage SummaryCoverage changed in 3 files.
|
I think it'd be faster if we add this for one chart type. If it works well, we then could easily apply to other charts. What do you think>? |
projects/js-packages/charts/src/components/bar-chart/bar-chart.tsx
Outdated
Show resolved
Hide resolved
Good point, thanks @Jasper - going to get this out for one chart type first. |
645fb70
to
346a4fb
Compare
346a4fb
to
b4b45c1
Compare
b4b45c1
to
4cfde90
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
projects/js-packages/charts/src/components/line-chart/line-chart.tsx
Outdated
Show resolved
Hide resolved
projects/js-packages/charts/src/components/pie-chart/pie-chart.tsx
Outdated
Show resolved
Hide resolved
renderTooltip = renderDefaultTooltip, | ||
withStartGlyphs = false, | ||
withEndGlyphs = false, | ||
interactive = false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better if we rename it to something like legendInteractive
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah - I was just considering that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Filter out hidden series when using interactive legends, preserving original index | ||
const visibleData = useMemo( () => { | ||
if ( ! chartId || ! interactive ) { | ||
return dataSorted.map( ( series, index ) => ( { series, originalIndex: index } ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using originalIndex
, I wonder whether it might be easier if we add an isVisbile
property to series. And in the loop starting L452, if a series is not visible, then we return null to skip it. With that, we wouldn't have to mess with originalIndex in there.
const visibleData = useMemo( () => {
if ( ! chartId || ! interactive ) {
return dataSorted;
}
return dataSorted
.map( ( series, index ) => ( { series, isVisible: isSeriesVisible( chartId, series.label ) } ) );
}, [ dataSorted, chartId, isSeriesVisible, interactive ] );
|
||
{ dataSorted.map( ( seriesData, index ) => { | ||
{ visibleData.map( ( { series: seriesData, originalIndex } ) => { | ||
const { color, lineStyles, glyph } = getElementStyles( { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if( !seriesData.isVisible ) {
return null;
}
Proposed changes:
This PR adds interactive legend support to LineChart only, allowing users to toggle series visibility by clicking or using keyboard navigation.
Implementation Details:
toggleSeriesVisibility
,isSeriesVisible
, andgetHiddenSeries
methods to GlobalChartsContext for managing series visibility stateinteractive
prop to Legend component and LineChart (defaults to false for backward compatibility)Test Coverage:
Documentation:
interactive
prop to Legend API referenceWithInteractiveLegend
Storybook storyOther information:
Does this pull request change what data or activity we track or use?
No, it does not.
Testing instructions:
Quick Start - Storybook Demo
Find the interactive legend story:
pnpm run storybook
Test the interactive features:
Feature Requirements
Interactive legends require:
chartId="sales-chart"
)true
on LineChart (e.g.,interactive={true}
)true
to display the legendExample:
Backward Compatibility Testing
Verify existing functionality still works:
interactive
prop work exactly as beforechartId
work as standalone componentsCurrent Scope
✅ Supported: LineChart with interactive legends
❌ Not Yet: BarChart, PieChart, BarListChart, LeaderboardChart (will be added in future PRs)
Code Coverage
Run tests to verify coverage: