Skip to content

feat(component): add number formatting to single value and tooltips - #169

Closed
alfredo1996 wants to merge 3 commits into
devfrom
feat/issue-138-number-formatting
Closed

feat(component): add number formatting to single value and tooltips#169
alfredo1996 wants to merge 3 commits into
devfrom
feat/issue-138-number-formatting

Conversation

@alfredo1996

@alfredo1996 alfredo1996 commented Mar 22, 2026

Copy link
Copy Markdown
Owner

Summary

Add shared number formatting utilities (formatNumber, buildTooltipFormatter) to chart-utils.ts and wire them into the single-value chart. This creates the foundation for consistent tooltip formatting across all ECharts chart types.

Changes

  • New: formatNumber() — decimal places (0-6), comma/compact/percent, prefix/suffix
  • New: buildTooltipFormatter() — ECharts tooltip formatter factory using formatNumber
  • Updated: SingleValueChart — uses shared formatter, new decimalPlaces prop
  • Updated: chart-options-schema.tsdecimalPlaces option added to single-value, bar, line, pie
  • New tests: 22 tests (18 for formatNumber/buildTooltipFormatter, 4 for decimalPlaces on single-value)

Test plan

  • cd component && npm test — 68 suites, 1031 tests passing
  • cd app && npm test — 72 suites, 1260 tests passing
  • E2E — 200+ passed (3 pre-existing flaky failures)
  • npm run build — no type errors
  • npm run lint — clean

Closes #138

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a new decimalPlaces configuration option for bar, line, pie, and single value charts to control decimal precision in displayed values.
    • Enhanced tooltip formatting with consistent, customizable number display across chart types.

Add shared formatNumber() and buildTooltipFormatter() to chart-utils.ts:
- Decimal places config (0-6, -1 for automatic)
- Comma/compact/percent formatting
- Prefix/suffix support
- Consistent tooltip formatting across all ECharts chart types

Add decimalPlaces option to single-value, bar, line, and pie chart schemas.
Update SingleValueChart to use the shared formatter.

Closes #138

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@alfredo1996 alfredo1996 added enhancement New feature or request pkg:component UI component library area:charts Chart rendering labels Mar 22, 2026
@coderabbitai

coderabbitai Bot commented Mar 22, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@alfredo1996 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 17 minutes and 46 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3a6e2afd-04da-431c-810a-f89fdbd49d8f

📥 Commits

Reviewing files that changed from the base of the PR and between 14313d5 and 691453d.

📒 Files selected for processing (6)
  • component/src/charts/__tests__/base-chart.test.tsx
  • component/src/charts/__tests__/format-number.test.ts
  • component/src/charts/base-chart.tsx
  • component/src/charts/chart-utils.ts
  • component/src/components/composed/chart-options-schema.ts
  • component/vitest.setup.ts

Walkthrough

Introduced number formatting utilities (formatNumber, buildTooltipFormatter) in chart-utils.ts to support decimal place precision, prefix/suffix, and compact notation across charts. Added decimalPlaces configuration option to SingleValueChart and ECharts tooltips. Implemented comprehensive test coverage for formatting functions and component integration.

Changes

Cohort / File(s) Summary
Formatting Utilities
component/src/charts/chart-utils.ts
Added formatNumber function supporting plain, comma, compact, and percent formats with configurable decimal places, prefix, and suffix. Added buildTooltipFormatter to generate ECharts tooltip formatters with normalized param handling and consistent number formatting.
SingleValueChart Integration
component/src/charts/single-value-chart.tsx
Refactored numeric formatting to use centralized formatNumber utility. Removed local applyNumberFormat logic. Added decimalPlaces prop to SingleValueChartProps for configurable decimal precision.
Chart Options Schema
component/src/components/composed/chart-options-schema.ts
Added shared tooltipFormatOptions constant containing decimalPlaces configuration. Integrated decimalPlaces option into bar, line, pie, and single-value chart option registries.
Format Number Tests
component/src/charts/__tests__/format-number.test.ts
Added 97 lines of test coverage for formatNumber across all format types, decimal rounding/padding, prefix/suffix combinations, edge cases (zero, negatives, non-numeric strings). Tests also validate buildTooltipFormatter output for single and array tooltip params.
SingleValueChart Tests
component/src/charts/__tests__/single-value-chart.test.tsx
Added 22 lines of test coverage verifying decimalPlaces rounding, zero-padding, composition with numberFormat="comma", and automatic formatting behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title accurately summarizes the main change: adding number formatting support to single-value charts and tooltips across the component.
Linked Issues check ✅ Passed All acceptance criteria from issue #138 are met: decimal places config (0–6), prefix/suffix support, compact notation toggle, and shared buildTooltipFormatter in chart-utils.ts.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #138 requirements: number formatting utilities, single-value chart updates, tooltip formatter, and schema additions for affected chart types.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/issue-138-number-formatting

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
component/src/charts/chart-utils.ts (1)

32-41: Hardcoded en-US locale for number formatting.

The locale is hardcoded to "en-US" for comma formatting and "en" for compact notation. This works for the current use case but may cause issues for international users expecting their locale's conventions.

Consider parameterizing locale in NumberFormatConfig if i18n is a future requirement.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@component/src/charts/chart-utils.ts` around lines 32 - 41, The formatting
branches for "comma" and "compact" are using hardcoded locales ("en-US" and
"en"); update NumberFormatConfig to accept an optional locale string (e.g.,
locale?: string) and use that value when constructing
toLocaleString/Intl.NumberFormat, falling back to a sensible default (like
navigator.language or "en-US") when locale is undefined; adjust the "comma" case
and the "compact" case to reference the new config.locale instead of the
hardcoded strings so formatting respects the caller's locale preference.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@component/src/charts/chart-utils.ts`:
- Around line 79-83: The current items.map callback builds a label using
`${p.marker ?? ""} ${p.seriesName ?? ""}: <b>${val}</b>` which can produce "
undefined: <b>…</b>" or an extra colon/space when seriesName is missing; update
the template in the items.map (the arrow callback that assigns lines) to only
include the colon and seriesName when p.seriesName is present (e.g.
conditionally prepend `${p.seriesName}: ` or omit it), and ensure spacing around
p.marker (p.marker) is trimmed or added only when needed so the final string
never shows "undefined" or stray punctuation while still using formatNumber(val,
tooltipConfig) for value formatting.

In `@component/src/components/composed/chart-options-schema.ts`:
- Around line 373-375: The tooltip decimalPlaces option is currently inert
because tooltipFormatOptions (which includes decimalPlaces) is spread into the
chart option registries but the chart components (bar-chart.tsx, line-chart.tsx,
pie-chart.tsx) use hardcoded tooltip configs; either remove tooltipFormatOptions
from the bar/line/pie arrays or wire the formatter: call
buildTooltipFormatter(...) inside each component and replace the hardcoded
tooltip config with one that uses the returned formatter (ensure you
import/accept decimalPlaces from props and pass it to buildTooltipFormatter in
bar-chart.tsx, line-chart.tsx, and pie-chart.tsx so decimalPlaces controls
tooltip output).

---

Nitpick comments:
In `@component/src/charts/chart-utils.ts`:
- Around line 32-41: The formatting branches for "comma" and "compact" are using
hardcoded locales ("en-US" and "en"); update NumberFormatConfig to accept an
optional locale string (e.g., locale?: string) and use that value when
constructing toLocaleString/Intl.NumberFormat, falling back to a sensible
default (like navigator.language or "en-US") when locale is undefined; adjust
the "comma" case and the "compact" case to reference the new config.locale
instead of the hardcoded strings so formatting respects the caller's locale
preference.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 507d458d-743c-4a0e-8b75-f5e184ba13c8

📥 Commits

Reviewing files that changed from the base of the PR and between 59e78c2 and 14313d5.

📒 Files selected for processing (5)
  • component/src/charts/__tests__/format-number.test.ts
  • component/src/charts/__tests__/single-value-chart.test.tsx
  • component/src/charts/chart-utils.ts
  • component/src/charts/single-value-chart.tsx
  • component/src/components/composed/chart-options-schema.ts

Comment thread component/src/charts/chart-utils.ts
Comment thread component/src/components/composed/chart-options-schema.ts Outdated
alfredorubin96 and others added 2 commits March 23, 2026 01:37
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ions

- Fix TypeScript compilation error (unused NumberFormatConfig import)
- Conditionally include seriesName in tooltip to avoid "undefined:" display
- Remove inert decimalPlaces option from bar/line/pie chart schemas
  (only used in single-value where it's actually wired up)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

alfredo1996 pushed a commit that referenced this pull request Mar 24, 2026
…els, markLine, pie donut)

Merges PRs #169, #170, #171, #173, #174 into a single release branch.
Resolves merge conflicts in chart-utils.ts, bar-chart.tsx, line-chart.tsx,
and chart-options-schema.ts.

Includes:
- Number formatting for single-value and tooltips (#169)
- DataZoom support for bar and line charts (#170)
- Auto-rotate and truncate axis labels (#171)
- Reference lines (markLine) for bar and line charts (#173)
- Donut center text and Top-N grouping for pie chart (#174)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@alfredo1996

Copy link
Copy Markdown
Owner Author

Superseded by consolidated PR #185 (release/chart-improvements)

@alfredo1996
alfredo1996 deleted the feat/issue-138-number-formatting branch March 29, 2026 22:22
alfredo1996 pushed a commit that referenced this pull request May 10, 2026
…els, markLine, pie donut)

Merges PRs #169, #170, #171, #173, #174 into a single release branch.
Resolves merge conflicts in chart-utils.ts, bar-chart.tsx, line-chart.tsx,
and chart-options-schema.ts.

Includes:
- Number formatting for single-value and tooltips (#169)
- DataZoom support for bar and line charts (#170)
- Auto-rotate and truncate axis labels (#171)
- Reference lines (markLine) for bar and line charts (#173)
- Donut center text and Top-N grouping for pie chart (#174)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:charts Chart rendering enhancement New feature or request pkg:component UI component library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(component): add number formatting to single value and tooltips

2 participants