Skip to content

Conversation

@adamwoodnz
Copy link
Contributor

@adamwoodnz adamwoodnz commented Dec 16, 2025

Fixes CHARTS-150: Add support for custom tooltip rendering

Proposed changes:

This PR enhances the GeoChart component to use Google Charts native data format, providing maximum flexibility for consumers to customize tooltip content, formatted values, and more.

Key Changes:

  • Updated GeoData type to use Google Charts native types (GoogleDataTableColumn[], GoogleDataTableRow[]) instead of custom simplified types
  • Exported Google Charts types directly from the package (GoogleDataTableColumn, GoogleDataTableRow, GoogleDataTableColumnRoleType) following the same pattern as visx types
  • Updated all examples and documentation to use full country names (e.g., "United States") instead of ISO codes, as Google Charts supports both formats
  • Added comprehensive story examples demonstrating:
    • Custom text tooltips
    • HTML tooltips with rich formatting
    • Formatted values (display format separate from actual value)
    • Complex tooltip styling
  • Enhanced documentation with detailed examples of all supported features
  • Updated API reference to reflect the new flexible data format

Screenshots

Default Custom Complex Custom
Screenshot 2025-12-17 at 11 54 47 AM Screenshot 2025-12-17 at 11 55 00 AM Screenshot 2025-12-17 at 11 55 12 AM

Benefits:

  • Maximum flexibility - Consumers can now use all Google Charts features including custom tooltips, formatted values, and cell properties
  • Better developer experience - Full TypeScript support with Google Charts types
  • Backward compatibility - Still supports both full country names and ISO codes
  • Improved documentation - Comprehensive examples showing real-world use cases

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?

No, this PR only updates the data format for the GeoChart component. No tracking or data collection changes.

Testing instructions:

Basic Usage

  1. Check out this branch
  2. Navigate to projects/js-packages/charts
  3. Run pnpm install and pnpm storybook
  4. Open Storybook and navigate to "JS Packages/Charts Library/Charts/Geo Chart"
  5. Verify the Default story displays a world map with sample data

Custom Tooltips

  1. In Storybook, view the "With Custom Tooltip" story
  2. Hover over countries on the map
  3. Verify HTML tooltips appear with formatted content

Formatted Values

  1. View the "With Formatted Values" story
  2. Hover over countries
  3. Verify tooltips show formatted values (e.g., "$1.23M" instead of 1234567)

Text Tooltips

  1. View the "With Text Tooltips" story
  2. Verify custom text tooltips appear with percentage information

Complex Tooltips

  1. View the "With Complex Tooltips" story
  2. Verify rich HTML tooltips with emoji flags, bold text, and styled content

TypeScript Usage

import { GeoChart, type GeoData, GoogleDataTableColumnRoleType } from '@automattic/charts';

// Basic usage
const basicData: GeoData = [
  ['Country', 'Value'],
  ['United States', 100],
  ['Canada', 50],
];

// With custom tooltips
const tooltipData: GeoData = [
  ['Country', 'Value', { type: 'string', role: 'tooltip', p: { html: true } }],
  ['United States', 100, '<b>USA</b><br/>100 visitors'],
];

<GeoChart data={tooltipData} width={800} height={500} />

Verify Type Checking

cd projects/js-packages/charts
pnpm typecheck

All types should compile without errors.

- Updated GeoData type to align with Google Charts format, allowing for flexible data representation.
- Refactored GeoChartProps to accept data in the new format, supporting both full country names and ISO 3166-1 alpha-2 codes.
- Improved documentation and examples to reflect the new data structure and advanced features like custom tooltips and formatted values.
- Updated tests to validate the new data handling and tooltip functionalities.
@github-actions
Copy link
Contributor

github-actions bot commented Dec 16, 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-150-add-support-for-custom-tooltip-rendering branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack charts-150-add-support-for-custom-tooltip-rendering

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

github-actions bot commented Dec 16, 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 Dec 16, 2025
@adamwoodnz adamwoodnz self-assigned this Dec 16, 2025
@adamwoodnz adamwoodnz marked this pull request as draft December 16, 2025 21:23
@adamwoodnz adamwoodnz added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] In Progress 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 Dec 16, 2025
…redundant comments. Simplified the description of country identifiers while retaining essential information for users.
@jp-launch-control
Copy link

jp-launch-control bot commented Dec 16, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/js-packages/charts/src/charts/geo-chart/geo-chart.tsx 22/22 (100.00%) 0.00% 0 💚

Full summary · PHP report · JS report

…ted tooltip options to conditionally include HTML support based on data structure, improving user experience for interactive charts.
- Updated test to expect isHtml: false by default
- Added test for HTML tooltip detection (isHtml: true)
- Added test for text-only tooltips (isHtml: false)
- Updated story column names for consistency (Value → Orders/Views)
@adamwoodnz adamwoodnz requested a review from Copilot December 16, 2025 22:57
@adamwoodnz adamwoodnz marked this pull request as ready for review December 16, 2025 22:57
@adamwoodnz adamwoodnz added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Dec 16, 2025
Copy link
Contributor

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 modernizes the GeoChart component to use Google Charts' native data format instead of a simplified custom format, enabling support for custom tooltips (both text and HTML), formatted values, and other advanced Google Charts features. The change is a breaking API change that improves flexibility and developer experience with comprehensive TypeScript support.

Key Changes:

  • Updated GeoData type from Record<string, number> to Google Charts native format [GoogleDataTableColumn[], ...GoogleDataTableRow[]]
  • Added support for custom HTML tooltips, text tooltips, and formatted values with automatic HTML tooltip detection
  • Exported Google Charts types (GoogleDataTableColumn, GoogleDataTableRow, GoogleDataTableColumnRoleType) directly from the package
  • Updated all examples, tests, and documentation to use full country names instead of ISO codes (though both are still supported)

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/types.ts Updated GeoData type to use Google Charts native format with comprehensive JSDoc examples
src/stories/sample-data/index.ts Converted sample data from object format to array format with full country names
src/index.ts Exported Google Charts types for consumer use
src/charts/geo-chart/types.ts Updated prop documentation to reflect new data format
src/charts/geo-chart/test/geo-chart.test.tsx Updated all tests for new data format and added tests for HTML tooltip detection
src/charts/geo-chart/stories/index.stories.tsx Added comprehensive story examples showcasing custom tooltips, formatted values, and complex HTML tooltips
src/charts/geo-chart/stories/index.docs.mdx Enhanced documentation with detailed examples of advanced features and full country name usage
src/charts/geo-chart/stories/index.api.mdx Updated API reference with new type definitions and comprehensive examples
src/charts/geo-chart/geo-chart.tsx Removed data transformation logic and added HTML tooltip detection to configure Google Charts options
changelog/charts-150-add-support-for-custom-tooltip-rendering Added changelog entry documenting the breaking change

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Contributor

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 10 out of 10 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

adamwoodnz and others added 3 commits December 17, 2025 13:25
…yCountry for improved accuracy in visual representation.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Updated test for data passing to Google Charts to ensure it reflects complex data structures, including formatted values and tooltips.
- Removed outdated tests that checked for simpler data formats, focusing on validating the integrity of the data as provided.
- Improved clarity in test descriptions to better reflect the functionality being tested.
@adamwoodnz adamwoodnz requested review from a team December 17, 2025 00:45
- Introduced useMemo for hasHtmlTooltips and options to enhance performance by memoizing values based on dependencies.
- Improved readability and efficiency of the GeoChart component by reducing unnecessary recalculations.
| Prop | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| `data` | `Record<string, number>` | - | **Required.** Record mapping country ISO 3166-1 alpha-2 codes (e.g., 'US', 'CA', 'GB') to numeric values |
| `data` | `GeoData` | - | **Required.** Data in Google Charts format. First row contains column headers, subsequent rows contain data. Countries can be identified by full name (e.g., 'United States') or ISO codes (e.g., 'US'). Full names are recommended. |
Copy link
Contributor

Choose a reason for hiding this comment

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

Full names are recommended

Is this correct? I took a look at https://developers.google.com/chart/interactive/docs/gallery/geochart#data-format and didn't find the narrative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Full names are recommended for display purposes, maybe the comment should be updated?

From that document:

A country name as a string (for example, "England"), or an uppercase ISO-3166-1 alpha-2 code or its English text equivalent (for example, "GB" or "United Kingdom").

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 regions / states in this case? I imagined there would be a separate string for display 🤔

Copy link
Contributor

@kangzj kangzj Dec 17, 2025

Choose a reason for hiding this comment

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

Full names are recommended for display purposes

If the tooltip could be cusomized, then using country / region code doesn't feel like an issue or at least using full names shouldn't be recommended imho. Country names are longer and harder to use I think and sometimes might not be easy to be accurate especially if we want to do filtering etc later. And I think we wanted to add support for regions / states as well, which made it even harder.

Copy link
Contributor Author

@adamwoodnz adamwoodnz Dec 17, 2025

Choose a reason for hiding this comment

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

Here's the beginnings of the PR for states so you can see what will be involved: #46332

It's possible to use ISO codes and still display the country name as the tooltip title, but the full country name would also need to be included in the data like this:

Screenshot 2025-12-17 at 4 03 21 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see the Jetpack Stats data is like this, with both included:

[
  {
    "label": "Australia",
    "countryCode": "AU",
    "countryFull": "Australia",
    "value": 14,
    "region": "053"
  },
  {
    "label": "New Zealand",
    "countryCode": "NZ",
    "countryFull": "New Zealand",
    "value": 1,
    "region": "053"
  }
]

data.length > 0 &&
data[ 0 ].some(
col =>
typeof col === 'object' &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to use optional chaining to simply the conditions here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No unfortunately, due to TS. Claude's explanation:

The verbose version is required because GoogleDataTableColumn can be either a string or an object. While optional chaining (?.) helps with null/undefined checks, TypeScript still needs explicit type narrowing to distinguish between strings and objects in a union type.

The issue: The data format allows columns to be defined as either:
Simple strings: 'Country'
Objects with metadata: { type: 'string', role: 'tooltip', p: { html: true } }

Why optional chaining alone doesn't work: When TypeScript sees a union type (string | object), it can't safely assume properties exist even with ?. because strings don't have those properties at all. We need the typeof check and in operator to properly narrow the type.

This is the correct TypeScript pattern for type-safe property access on union types.

legend: 'none',
keepAspectRatio: true,
};
const options: GoogleChartOptions = useMemo(
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

…t types to emphasize the use of full names for better tooltip readability.
@adamwoodnz adamwoodnz requested a review from kangzj December 18, 2025 04:13
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.

Happy to start with this and see what we can improve on :shipit:

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

Labels

[JS Package] Charts RNA [Status] In Progress [Status] Needs Review This PR is ready for review. [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.

3 participants