Skip to content

Conversation

annacmc
Copy link
Contributor

@annacmc annacmc commented Oct 15, 2025

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:

  • Add toggleSeriesVisibility, isSeriesVisible, and getHiddenSeries methods to GlobalChartsContext for managing series visibility state
  • Implement visibility state management in GlobalChartsProvider using a Map to track hidden series per chart
  • Add interactive prop to Legend component and LineChart (defaults to false for backward compatibility)
  • Add click and keyboard event handlers to BaseLegend with full accessibility support (ARIA attributes, focus management, Enter/Space key navigation)
  • Add CSS styles for interactive legend states including hover effects, focus outlines, and visual feedback for hidden series (reduced opacity, strikethrough)
  • Filter visible series in LineChart based on visibility state from context, preserving original indices for color stability
  • Scope: Currently implemented for LineChart only. BarChart, PieChart, and other chart types will be supported in future PRs

Test Coverage:

  • Added 18 new tests covering:
    • GlobalChartsProvider series visibility management (8 tests)
    • BaseLegend interactive functionality (7 tests)
    • LineChart interactive filtering (3 tests)
  • All 516 tests passing with 100% code coverage

Documentation:

  • Updated LineChart documentation with interactive legend section
  • Updated Legend documentation with comprehensive interactive guide
  • Added interactive prop to Legend API reference
  • Created WithInteractiveLegend Storybook story

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

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:

  1. Start Storybook: pnpm run storybook
  2. Navigate to: JS Packages → Charts → Types → Line Chart
  3. Select the "With Interactive Legend" story from the sidebar

Test the interactive features:

  • Click to toggle: Click on any legend item (e.g., "London", "Canberra") to hide/show that series
  • Visual feedback: Hidden series show reduced opacity and strikethrough text
  • Color stability: Toggle series on/off multiple times - colors remain consistent
  • Keyboard navigation:
    • Press Tab to focus legend items
    • Press Enter or Space to toggle visibility
    • Verify focus outline appears
  • Accessibility:
    • Use a screen reader to verify ARIA announcements
    • Legend items announce as buttons with "visible" or "hidden" state

Feature Requirements

Interactive legends require:

  1. GlobalChartsProvider: Chart must be wrapped in provider (already done in Storybook)
  2. chartId prop: Unique identifier for the chart (e.g., chartId="sales-chart")
  3. interactive prop: Set to true on LineChart (e.g., interactive={true})
  4. showLegend prop: Must be true to display the legend

Example:

<GlobalChartsProvider>
  <LineChart
    data={data}
    chartId="my-chart"
    showLegend={true}
    interactive={true}
  />
</GlobalChartsProvider>

Backward Compatibility Testing

Verify existing functionality still works:

  • Check other LineChart stories (Default, Single Series, etc.) - legends should be non-interactive
  • Verify that charts without interactive prop work exactly as before
  • Confirm legends without chartId work as standalone components

Current 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:

pnpm test
# All 516 tests should pass

@annacmc annacmc added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Review This PR is ready for review. labels Oct 15, 2025
@annacmc annacmc self-assigned this Oct 15, 2025
Copy link
Contributor

github-actions bot commented Oct 15, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the add/charts-54-legends-toggling-visibility-of-series branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/charts-54-legends-toggling-visibility-of-series

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@annacmc annacmc requested a review from Copilot October 15, 2025 12:20
Copy link
Contributor

@Copilot Copilot AI left a 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.

Copy link

jp-launch-control bot commented Oct 15, 2025

Code Coverage Summary

Coverage changed in 3 files.

File Coverage Δ% Δ Uncovered
projects/js-packages/charts/src/components/legend/private/base-legend.tsx 40/40 (100.00%) 0.00% 0 💚
projects/js-packages/charts/src/components/line-chart/line-chart.tsx 119/127 (93.70%) 0.31% 0 💚
projects/js-packages/charts/src/providers/chart-context/global-charts-provider.tsx 72/72 (100.00%) 0.00% 0 💚

Full summary · PHP report · JS report

@kangzj
Copy link
Contributor

kangzj commented Oct 15, 2025

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>?

@annacmc
Copy link
Contributor Author

annacmc commented Oct 17, 2025

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>?

Good point, thanks @Jasper - going to get this out for one chart type first.

@annacmc annacmc force-pushed the add/charts-54-legends-toggling-visibility-of-series branch from 645fb70 to 346a4fb Compare October 17, 2025 10:51
@annacmc annacmc force-pushed the add/charts-54-legends-toggling-visibility-of-series branch from 346a4fb to b4b45c1 Compare October 17, 2025 11:25
@annacmc annacmc force-pushed the add/charts-54-legends-toggling-visibility-of-series branch from b4b45c1 to 4cfde90 Compare October 17, 2025 11:33
@annacmc annacmc changed the title Charts: Add interactive legend for toggling series visibility Charts: Add interactive legend for toggling series visibility in LineCharts Oct 17, 2025
@annacmc annacmc requested a review from Copilot October 17, 2025 12:40
Copy link
Contributor

@Copilot Copilot AI left a 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.

@annacmc annacmc changed the title Charts: Add interactive legend for toggling series visibility in LineCharts Charts: Add interactive legend for toggling series visibility in LineChart Oct 20, 2025
@annacmc annacmc marked this pull request as ready for review October 20, 2025 03:12
@annacmc annacmc requested review from a team and kangzj October 20, 2025 03:12
renderTooltip = renderDefaultTooltip,
withStartGlyphs = false,
withEndGlyphs = false,
interactive = false,
Copy link
Contributor

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?

Copy link
Contributor Author

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 :)

Copy link
Contributor

@kangzj kangzj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good! I left some nitpicks inline, but generally looks good to ship and iterate 🚀

As an improvement point, we should properly show an empty state when all of the legends are off:

Image

// 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 } ) );
Copy link
Contributor

@kangzj kangzj Oct 20, 2025

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( {
Copy link
Contributor

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;
}

@annacmc annacmc merged commit 2a6056a into trunk Oct 20, 2025
87 checks passed
@annacmc annacmc deleted the add/charts-54-legends-toggling-visibility-of-series branch October 20, 2025 05:31
@github-actions github-actions bot removed [Status] In Progress [Status] Needs Review This PR is ready for review. labels Oct 20, 2025
@annacmc
Copy link
Contributor Author

annacmc commented Oct 20, 2025

This looks pretty good! I left some nitpicks inline, but generally looks good to ship and iterate 🚀

Thanks for the feedback @kangzj really helpful! I've gone ahead and shipped this, then addressed all your comments in a followup PR here: #45545

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[JS Package] Charts RNA [Tests] Includes Tests [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants