Skip to content

Conversation

adamwoodnz
Copy link
Contributor

@adamwoodnz adamwoodnz commented Sep 23, 2025

Fixes CHARTS-107: Generate extra colors for charts from primary color

Proposed changes:

  • Adds color generation to the global chart context so that if a color is required for an index greater than the number of colors in the theme, a color will be provided which is complementary to the existing colors. Ensures generated colors are within the hue range of the existing colors, but sufficiently different from them, and have sufficient contrast for accessibility.
  • Adds a story with 8 required colors and supporting sample data
  • Adds tests for the chart context changes and new color utils

Screenshots

In both cases the first 5 colors are from theme.colors

Jetpack Woo
Screenshot 2025-09-23 at 5 05 34 PM Screenshot 2025-09-23 at 3 36 28 PM

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

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Testing instructions:

@adamwoodnz adamwoodnz self-assigned this Sep 23, 2025
Copy link
Contributor

github-actions bot commented Sep 23, 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 charts-107-generate-extra-colors-for-charts-from-primary-color branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack charts-107-generate-extra-colors-for-charts-from-primary-color

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

github-actions bot commented Sep 23, 2025

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!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Sep 23, 2025
Copy link

jp-launch-control bot commented Sep 23, 2025

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/js-packages/charts/src/providers/chart-context/global-charts-provider.tsx 52/52 (100.00%) 0.00% 0 💚
projects/js-packages/charts/src/utils/color-utils.ts 49/49 (100.00%) 0.00% 0 💚

1 file is newly checked for coverage.

File Coverage
projects/js-packages/charts/src/providers/chart-context/private/get-chart-color.ts 44/51 (86.27%) 💚

Full summary · PHP report · JS report

Comment on lines -31 to +37
datum={ sampleData[ 1 ].data[ sampleData[ 1 ].data.length - 10 ] }
datum={ sampleData[ 1 ].data[ 1 ] }
title="Another notable event"
subtitle="This is another notable event"
{ ...( annotationArgs?.[ 1 ] || {} ) }
/>
<LineChart.Annotation
datum={ sampleData[ 2 ].data[ sampleData[ 2 ].data.length - 51 ] }
datum={ sampleData[ 2 ].data[ 7 ] }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When the sample data was updated sometime recently these indexes broke. Fixing here as I'm touching the sample data again.

@adamwoodnz adamwoodnz added [Type] Feature Development of a new feature and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Sep 23, 2025
@adamwoodnz adamwoodnz force-pushed the charts-107-generate-extra-colors-for-charts-from-primary-color branch from 866c1b2 to dd36607 Compare September 23, 2025 03:31
@adamwoodnz adamwoodnz requested a review from Copilot September 23, 2025 03:44
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

This PR adds automatic color generation for charts when the number of data series exceeds the available theme colors. Instead of cycling through the existing palette, the system now generates complementary colors using the golden ratio and perceptual distance algorithms to ensure good visual distinction and accessibility.

  • Implements color generation algorithms using HSL color space with distance calculations
  • Adds performance optimizations through color caching in the chart context
  • Extends sample data and stories to demonstrate charts with 8+ series

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
projects/js-packages/charts/src/utils/color-utils.ts Adds hexToHsl and getColorDistance functions for color conversion and perceptual distance calculation
projects/js-packages/charts/src/utils/test/color-utils.test.ts Comprehensive test suite for new color utility functions
projects/js-packages/charts/src/providers/chart-context/private/get-chart-color.ts Core color generation logic using golden ratio and distance algorithms
projects/js-packages/charts/src/providers/chart-context/global-charts-provider.tsx Integrates color caching and generation into the chart context
projects/js-packages/charts/src/providers/chart-context/test/chart-context.test.tsx Updates tests to verify color generation behavior instead of cycling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

for ( let attempt = 0; attempt < maxAttempts; attempt++ ) {
let hue = ( ( index - colors.length + attempt * 0.1 ) * goldenRatio * 360 ) % 360;

// If we have existing colors, constrain new colors to their hue range
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hue range for Jetpack is wide (lots of different colors), whereas hue range is small for Woo (all blue/purple/pink). Consider this when generating.

- Introduced constants for golden ratio, minimum color distance, and maximum generation attempts to improve readability and maintainability.
- Added detailed comments to clarify the purpose of each constant and the overall color generation process.
@adamwoodnz adamwoodnz marked this pull request as ready for review September 23, 2025 04:48
@adamwoodnz adamwoodnz requested a review from a team September 23, 2025 04:48
@adamwoodnz adamwoodnz added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Sep 24, 2025
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.

Looks good to me!

// Process all colors once and cache the results
if ( Array.isArray( colors ) ) {
for ( const color of colors ) {
if ( color && typeof color === 'string' && color.startsWith( '#' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens for named colors, like red, dodgerblue etc? if we don't not support them then we should probably be clear in the documentation.

@kangzj
Copy link
Contributor

kangzj commented Sep 25, 2025

@adamwoodnz just dumping some thoughts on next steps here - I don't think there would be a lot of use cases where we need more series than 10, so it's definitely not a priority right now.

We could use combinations of color, glyph and pattern to multiply the choice of series / bar styles. For example, if we have 5 colors, 4 glyphs and 3 patterns, we'd have 5x4x3=60 combinations! It'll be impossible for consumers to exceed the limit 😄

@kangzj kangzj merged commit 51e30a0 into trunk Sep 25, 2025
114 of 116 checks passed
@kangzj kangzj deleted the charts-107-generate-extra-colors-for-charts-from-primary-color branch September 25, 2025 04:28
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants