-
Notifications
You must be signed in to change notification settings - Fork 0
feat(component): add number formatting to single value and tooltips #169
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { formatNumber, buildTooltipFormatter } from "../chart-utils"; | ||
|
|
||
| describe("formatNumber", () => { | ||
| it("returns plain number by default", () => { | ||
| expect(formatNumber(1234)).toBe("1234"); | ||
| }); | ||
|
|
||
| it("respects decimalPlaces", () => { | ||
| expect(formatNumber(3.14159, { decimalPlaces: 2 })).toBe("3.14"); | ||
| }); | ||
|
|
||
| it("pads with zeros when decimalPlaces exceeds precision", () => { | ||
| expect(formatNumber(5, { decimalPlaces: 2 })).toBe("5.00"); | ||
| }); | ||
|
|
||
| it("applies comma formatting", () => { | ||
| expect(formatNumber(1234567, { numberFormat: "comma" })).toBe("1,234,567"); | ||
| }); | ||
|
|
||
| it("applies comma formatting with decimalPlaces", () => { | ||
| expect(formatNumber(1234567.891, { numberFormat: "comma", decimalPlaces: 2 })).toBe("1,234,567.89"); | ||
| }); | ||
|
|
||
| it("applies compact notation", () => { | ||
| const result = formatNumber(1500000, { numberFormat: "compact" }); | ||
| expect(result).toMatch(/1\.5M/i); | ||
| }); | ||
|
|
||
| it("applies compact notation with decimalPlaces", () => { | ||
| const result = formatNumber(1234, { numberFormat: "compact", decimalPlaces: 1 }); | ||
| expect(result).toMatch(/1\.2K/i); | ||
| }); | ||
|
|
||
| it("applies percent format", () => { | ||
| expect(formatNumber(75, { numberFormat: "percent" })).toBe("75%"); | ||
| }); | ||
|
|
||
| it("applies percent format with decimalPlaces", () => { | ||
| expect(formatNumber(75.678, { numberFormat: "percent", decimalPlaces: 1 })).toBe("75.7%"); | ||
| }); | ||
|
|
||
| it("adds prefix", () => { | ||
| expect(formatNumber(100, { prefix: "$" })).toBe("$100"); | ||
| }); | ||
|
|
||
| it("adds suffix", () => { | ||
| expect(formatNumber(100, { suffix: " items" })).toBe("100 items"); | ||
| }); | ||
|
|
||
| it("combines prefix, suffix, decimalPlaces, and comma", () => { | ||
| expect(formatNumber(9876.5, { prefix: "$", suffix: "M", numberFormat: "comma", decimalPlaces: 1 })).toBe("$9,876.5M"); | ||
| }); | ||
|
|
||
| it("handles zero", () => { | ||
| expect(formatNumber(0, { decimalPlaces: 2 })).toBe("0.00"); | ||
| }); | ||
|
|
||
| it("handles negative numbers", () => { | ||
| expect(formatNumber(-42.567, { decimalPlaces: 1 })).toBe("-42.6"); | ||
| }); | ||
|
|
||
| it("returns string values unchanged", () => { | ||
| expect(formatNumber("N/A" as unknown as number)).toBe("N/A"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildTooltipFormatter", () => { | ||
| it("returns a function", () => { | ||
| const formatter = buildTooltipFormatter({}); | ||
| expect(typeof formatter).toBe("function"); | ||
| }); | ||
|
|
||
| it("formats a single value with config", () => { | ||
| const formatter = buildTooltipFormatter({ decimalPlaces: 1, prefix: "$" }); | ||
| // ECharts tooltip params shape for axis trigger | ||
| const result = formatter({ | ||
| seriesName: "Revenue", | ||
| value: 1234.56, | ||
| name: "Jan", | ||
| marker: '<span style="color:#3b82f6">●</span>', | ||
| }); | ||
| expect(result).toContain("$1,234.6"); | ||
| expect(result).toContain("Revenue"); | ||
| }); | ||
|
|
||
| it("handles array params (axis trigger with multiple series)", () => { | ||
| const formatter = buildTooltipFormatter({ decimalPlaces: 0 }); | ||
| const result = formatter([ | ||
| { seriesName: "A", value: 100.7, name: "Jan", marker: "●" }, | ||
| { seriesName: "B", value: 200.3, name: "Jan", marker: "●" }, | ||
| ]); | ||
| expect(result).toContain("101"); | ||
| expect(result).toContain("200"); | ||
| }); | ||
|
|
||
| it("omits seriesName label when seriesName is undefined", () => { | ||
| const formatter = buildTooltipFormatter({}); | ||
| const result = formatter({ value: 42, name: "Jan" }); | ||
| expect(result).not.toContain("undefined"); | ||
| expect(result).toContain("<b>"); | ||
| }); | ||
|
|
||
| it("omits seriesName label when seriesName is empty string", () => { | ||
| const formatter = buildTooltipFormatter({}); | ||
| const result = formatter({ seriesName: "", value: 42, name: "Jan" }); | ||
| expect(result).not.toContain(": <b>"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.