Skip to content

feat(react-charts): allow a function for cartesian tickFormat#36369

Open
flanakin wants to merge 3 commits into
microsoft:masterfrom
flanakin:feat/react-charts-function-tickformat
Open

feat(react-charts): allow a function for cartesian tickFormat#36369
flanakin wants to merge 3 commits into
microsoft:masterfrom
flanakin:feat/react-charts-function-tickformat

Conversation

@flanakin

@flanakin flanakin commented Jul 5, 2026

Copy link
Copy Markdown

Summary

Widens the cartesian tickFormat prop from string to string | ((value: number | Date) => string).

Motivation: A d3-format string cannot express two common value-axis needs:

  • A literal % — d3's % type multiplies by 100, double-scaling an already-0–100 axis (e.g. a 100%-stacked bar); there is no escape slot, so ~s%, %%, \% all throw invalid format.
  • App/i18n formatting — routing ticks through an app's own currency/number formatter so axis ticks match tooltip/callout values; d3's $ only emits its locale symbol.

HorizontalBarChartWithAxis (whose value axis is the x-axis) is most affected.

Change: When a function is passed, it is called directly with the tick value instead of going through d3Format. String behavior is unchanged — fully backward compatible.

Files changed

  • CartesianChart.types.tstickFormat type widened on CartesianChartProps and ModifiedCartesianChartProps.tickParams
  • utilities.tsITickParams.tickFormat type updated; createNumericXAxis and createDateXAxis branch on typeof tickFormat
  • UtilityUnitTests.test.ts — two new tests covering function tickFormat for numeric and date axes
  • HorizontalBarChartWithAxisFunctionTickFormat.stories.tsx — new Storybook story demonstrating literal % on a 0–100 axis
  • react-charts.api.md — API surface updated to match

Test plan

  • yarn nx run react-charts:test — 152 passed, 10 skipped (timezone-gated), 0 failed
  • New story verified visually in Storybook: x-axis renders 0%100% correctly
  • Existing string tickFormat behavior unchanged (covered by existing snapshot tests)
  • yarn nx run react-charts:lint
  • yarn nx run react-charts:type-check

Checklist

  • Change file added (change/@fluentui-react-charts-function-tickformat.json, type: minor)
  • Public API docs updated (react-charts.api.md)
  • Tests added for new behavior
  • Backward compatible — string path unchanged

🤖 Generated with Claude Code

The cartesian `tickFormat` prop is a d3-format string, applied as
`d3Format(tickFormat)(value)`. A string cannot express two common value-axis
needs:

- A literal `%` — d3's `%` type multiplies by 100 (`format('%')(50)` → `5000%`),
  double-scaling an already-0–100 axis (e.g. a 100%-stacked bar); there is no
  literal/escape slot, so `~s%`, `%%`, `\%`, `'%'` all throw `invalid format`.
- App/i18n formatting — routing ticks through the app's own currency/number
  formatter so axis ticks match tooltip/callout values; d3's `$` only emits its
  locale symbol.

`HorizontalBarChartWithAxis`, whose value axis is the x-axis, is most affected.

Widen `tickFormat` to `string | ((value: number | Date) => string)` and branch in
`createNumericXAxis` and `createDateXAxis`. Backward compatible: a string keeps
the exact current d3 behavior; only a function is new.
Copilot AI review requested due to automatic review settings July 5, 2026 08:16
@flanakin flanakin requested a review from a team as a code owner July 5, 2026 08:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 extends the @fluentui/react-charts cartesian tickFormat API to accept either a d3-format string or a (value: number | Date) => string callback, enabling custom/i18n formatting and literal % rendering for 0–100 numeric axes.

Changes:

  • Widened tickFormat types in the cartesian chart public props and internal tick params.
  • Updated numeric/date x-axis tick formatting to support function-based formatting.
  • Added unit tests and a new HorizontalBarChartWithAxis Storybook example demonstrating literal % ticks.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/charts/react-charts/stories/src/HorizontalBarChartWithAxis/index.stories.tsx Adds export for the new tickFormat-function story.
packages/charts/react-charts/stories/src/HorizontalBarChartWithAxis/HorizontalBarChartWithAxisFunctionTickFormat.stories.tsx New story demonstrating function tickFormat for literal % on a 0–100 axis.
packages/charts/react-charts/library/src/utilities/UtilityUnitTests.test.ts Adds tests for function-based tickFormat on numeric and date axes.
packages/charts/react-charts/library/src/utilities/utilities.ts Implements function handling for tickFormat in numeric/date axis creation.
packages/charts/react-charts/library/src/components/CommonComponents/CartesianChart.types.ts Updates public prop types/docs for tickFormat and tickParams.tickFormat.
packages/charts/react-charts/library/etc/react-charts.api.md Updates extracted API to reflect widened tickFormat types.
change/@fluentui-react-charts-function-tickformat.json Adds a minor change file for the public API enhancement.

Comment thread packages/charts/react-charts/library/src/utilities/utilities.ts
Comment thread packages/charts/react-charts/library/src/utilities/utilities.ts Outdated
…rmat

- Convert NumberValue to primitive before passing to function tickFormat
  in createNumericXAxis so consumer formatters always receive a number
- Move function tickFormat check above timeFormatLocale in createDateXAxis
  so callers can override locale formatting

Co-Authored-By: Copilot <copilot@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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