Skip to content

Charts: Add Woo Analytics Leaderboard chart component #44299

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

Merged
merged 15 commits into from
Jul 21, 2025

Conversation

dognose24
Copy link
Contributor

@dognose24 dognose24 commented Jul 14, 2025

Related to CHARTS-34.

Proposed changes:

  • Migrate the LeaderboardChart component from the Woo Analytics repo to the Chart Library for reuse.
Woo Analytics Chart Library
截圖 2025-07-14 晚上10 09 29 截圖 2025-07-14 晚上10 16 32

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

pejTkB-27k-p2.

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

Testing instructions:

  • cd /path-to/jetpack/projects/js-packages/storybook && pnpm run storybook:dev.
  • Go to JS Packages > Charts > Types > Leaderboard Chart for stories.
  • Ensure the Leaderboard Chart usage is aligned with the existing Woo Analytics ones.

Copy link
Contributor

github-actions bot commented Jul 14, 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 or WordPress.com Site Helper), and enable the update/add_woo_leaderboard_chart_component branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack update/add_woo_leaderboard_chart_component
bin/jetpack-downloader test jetpack-mu-wpcom-plugin update/add_woo_leaderboard_chart_component

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

@github-actions github-actions bot added [JS Package] Charts [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. [Status] In Progress [Tests] Includes Tests Docs RNA labels Jul 14, 2025
Copy link
Contributor

github-actions bot commented Jul 14, 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!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: August 5, 2025
    • Code freeze: August 4, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Backup plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Boost plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Search plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Social plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Starter Plugin plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Protect plugin:

  • Next scheduled release: September 1, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Videopress plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@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 Jul 14, 2025
@dognose24 dognose24 requested a review from Copilot July 14, 2025 14:17
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 introduces a new LeaderboardChart component for Woo Analytics, complete with data utilities, formatting, tests, Storybook stories, and documentation.

  • Adds LeaderboardChart, its utilities (buildLeaderboardData, calculateDelta, formatMetricValue), and related types to the charts package.
  • Implements comprehensive tests, sample data, and Storybook stories for the new component.
  • Updates individual plugin changelogs to note inclusion of the new chart component.

Reviewed Changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
projects/plugins/*/changelog/update-add_woo_leaderboard_chart_component Changelog entries for each plugin
projects/js-packages/charts/src/index.ts Export new component, utilities, and types
projects/js-packages/charts/src/components/leaderboard-chart/utils.ts Data processing helpers (calculateDelta, etc.)
projects/js-packages/charts/src/components/leaderboard-chart/leaderboard-chart.tsx Core component implementation and JSDoc
projects/js-packages/charts/src/components/leaderboard-chart/test/leaderboard-chart.test.tsx Jest tests for component and utils
projects/js-packages/charts/src/components/leaderboard-chart/format-metric-value.ts Number formatting utility
projects/js-packages/charts/src/components/leaderboard-chart/README.md Component documentation and usage guide
projects/js-packages/charts/changelog/update-add_woo_leaderboard_chart_component Main changelog entry for charts package
projects/js-packages/charts/LEADERBOARD_INTEGRATION.md Integration guide for consuming applications
Comments suppressed due to low confidence (3)

projects/js-packages/charts/src/components/leaderboard-chart/leaderboard-chart.tsx:28

  • The JSDoc for currentShare states “% of the current value,” but the code computes it relative to the maximum value across all entries. Update the comment to clarify it’s “% of the max data value.”
	 * Width of current bar, as % of the current value

projects/js-packages/charts/src/components/leaderboard-chart/leaderboard-chart.tsx:33

  • Similar to currentShare, the previousShare comment should reflect that it’s computed as a percentage of the max data value, not the current value.
	 * Width of previous bar, as % of the current value

projects/js-packages/charts/src/components/leaderboard-chart/test/leaderboard-chart.test.tsx:104

  • This test only checks that entries are absent for empty data. It should also assert that the empty state message ("No data available") is rendered.
	it( 'handles empty data', () => {

Copy link

jp-launch-control bot commented Jul 14, 2025

Code Coverage Summary

3 files are newly checked for coverage.

File Coverage
projects/js-packages/charts/src/components/shared/format-metric-value.ts 8/13 (61.54%) 💚
projects/js-packages/charts/src/components/leaderboard-chart/leaderboard-chart.tsx 13/13 (100.00%) 💚
projects/js-packages/charts/src/components/leaderboard-chart/index.tsx 0/0 (—%) 🤷

Full summary · PHP report · JS report

@dognose24 dognose24 self-assigned this Jul 15, 2025
@dognose24 dognose24 added [Type] Feature Development of a new feature [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Status] In Progress labels Jul 16, 2025
@dognose24 dognose24 force-pushed the update/add_woo_leaderboard_chart_component branch from c01035e to 911fa7a Compare July 16, 2025 08:34
@dognose24 dognose24 marked this pull request as ready for review July 16, 2025 08:36
@dognose24 dognose24 force-pushed the update/add_woo_leaderboard_chart_component branch from faf0782 to 1f5c35f Compare July 17, 2025 06:11
@dognose24 dognose24 requested a review from chihsuan July 17, 2025 06:15
@dognose24
Copy link
Contributor Author

Thanks for the review, @chihsuan! 🙏🏼 I updated the PR, which could use another review round.

By the way, would you have preferred to use core components like ProgressBar, or is it reasonable to use plain HTML + CSS here for the progress bar? Curious about your thoughts. 🙂

const deltaColor = signColors[ colorIndex ];

return (
<Fragment key={ entry.id }>
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this Fragment instance wrapping the div here? I think we can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, we can move the key attribute to the next level. Updated in 41dcbc5.

Comment on lines +2 to +6
width: 100%;
max-width: 400px;
display: flex;
flex-direction: column;
gap: 12px;
Copy link
Contributor

Choose a reason for hiding this comment

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

If we are going to begin using HTML+CSS to compose the chart (as we do here), we should also consider using WordPress components. The regular div with these styles could be replaced with the <VStack /> component, for instance.

I'd like to know the opinion of other Chart development folks here, though.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with Damián.

I noticed that the Chart package doesn't currently use any WordPress components, perhaps we should check with @kangzj to confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The regular div with these styles could be replaced with the component, for instance.

Yes, I struggled with that, but I turned the draft into a PR for more solid feedback. We should reuse the core component as much as possible to avoid design deviations.

I noticed that the Chart package doesn't currently use any WordPress components

That might deserve a confirmation about whether we plan to introduce more bundle sizes for the library. 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably try to use as less dependencies as possible; however Core components are used in almost all of our projects, and if consumers are not using HTML based charts, they could be shook off by their bundlers, so I think it's fine to add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds thoughtful and reasonable! 💯

Comment on lines +17 to +25
/**
* Value of the entry
*/
currentValue: number;

/**
* Value of the entry in the previous period
*/
previousValue: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation is limited to handling only two entries per item. Is that correct
Do you think we should consider handling more than two entries?

This is an example of a BarChart rendering three entries per item, for instance.

Image

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, it's the current implementation. I was aiming first to migrate the component to allow for more variation in the future.

Do we already have use cases or a design for the leaderboard chart with more than two series?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should keep it as a migration PR but not one to add more functionality. We could look into this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thansk, @kangzj! I agreed with it. 🙂

Copy link
Contributor

@retrofox retrofox left a comment

Choose a reason for hiding this comment

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

Thanks @dognose24 for this work. My feedback/suggestions.

I think we should iterate on the Chart API to make it simpler and more flexible. Several of the current props (delta, share, etc.) are derived values that the chart can compute internally based on the input data. Moving that logic inside the chart would help keep the data shape minimal and semantic.

Another point is the assumption of having exactly two data points per entry (current and previous). That limits the use case. Ideally, the chart should support rendering two, three, or more values per item. For example, to compare multiple time periods or targets.

Here's a rough example of how the data could look with three samples per entry:

{
  id: 'google',
  label: 'Google',
  series: [
    { key: '2024', label: 'YTD 2024', value: 324000 },
    { key: '2023', label: 'YTD 2023', value: 296000 },
    { key: 'target', label: 'Target 2024', value: 400000 },
  ]
}

And a possible type definition:

type LeaderboardSeries = {
  key: string;       // unique identifier, used for color mapping or legends
  label?: string;    // human-friendly label
  value: number;
};

type LeaderboardEntry = {
  id: string;        // unique identifier per row/item
  label: string;     // display label for the row
  series: LeaderboardSeries[];
};

I know this implementation is based on the Analytics one, but I think this chart could benefit from a more general and scalable data structure.

Please take it as a suggestion 🙇‍♂️

@dognose24
Copy link
Contributor Author

dognose24 commented Jul 17, 2025

@retrofox Thanks for the feedback! 👍🏼

Several of the current props (delta, share, etc.) are derived values that the chart can compute internally based on the input data. Moving that logic inside the chart would help keep the data shape minimal and semantic.

Indeed, we just need to restore the buildLeaderboardData function and consider a more comprehensive data structure.

Here's a rough example of how the data could look with three samples per entry:

The data structure would be extensible for multiple items in a row. However, introducing more than two items in a row would mean the display format might differ from the current one we use in Analytics. We might need to show each value for every progress bar inside the row and remove the delta part, wouldn't we?

If we want multiple bars without comparison, the data could be independent for each row, like

[
  {
    id: 'google-2024',
    label: 'Google YTD 2024',
    currentValue: 12500,
    currentShare: 100
  },
  {
    id: 'google-2023',
    label: 'Google YTD 2023',
    currentValue: 8750,
    currentShare: 70
  }
]
截圖 2025-07-18 凌晨1 23 21

Does it make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice docs! Could we consider integrating these into Storybook? There is an approach discussed on CHARTS-44: Improve Storybook Docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Integrated in 2a55a2a.

@dognose24
Copy link
Contributor Author

the Chart package doesn't currently use any WordPress components

I have included @wordpress/components to use the core ProgressBar component as an alternative, which looks good for now!

截圖 2025-07-18 晚上11 34 56


### Basic Usage

\`\`\`typescript
Copy link
Contributor

@retrofox retrofox Jul 18, 2025

Choose a reason for hiding this comment

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

Do we need to scape the backslash chars? Is it a trick to not break the markdown format in the AI app? 😅
Ignore me please :-)

className,
style,
} ) => {
const signColors = [ '#D63638', '#757575', '#008A20' ];
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we try to handle this from the ThemeProvider component? If so, I think we can add a ToDo block for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in 7ee47b7.

kangzj
kangzj previously approved these changes Jul 21, 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.

Let's ship and iterate 🚀

@dognose24 dognose24 merged commit 0c8cfdc into trunk Jul 21, 2025
85 checks passed
@dognose24 dognose24 deleted the update/add_woo_leaderboard_chart_component branch July 21, 2025 12:38
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs [JS Package] Charts [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Protect A plugin with features to protect a site: brute force protection, security scanning, and a WAF. [Plugin] Search A plugin to add an instant search modal to your site to help visitors find content faster. [Plugin] Social Issues about the Jetpack Social plugin [Plugin] Starter Plugin [Plugin] VideoPress A standalone plugin to add high-quality VideoPress videos to your site. RNA [Tests] Includes Tests [Type] Feature Development of a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants