-
Notifications
You must be signed in to change notification settings - Fork 0
feat(component): auto-rotate and truncate axis labels #171
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { buildCategoryAxisLabel } from "../chart-utils"; | ||
|
|
||
| describe("buildCategoryAxisLabel", () => { | ||
| it("returns default config for small category count", () => { | ||
| const result = buildCategoryAxisLabel(5); | ||
| expect(result.rotate).toBe(0); | ||
| expect(result.formatter).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("rotates labels at 30° when categories >= 8", () => { | ||
| const result = buildCategoryAxisLabel(8); | ||
| expect(result.rotate).toBe(30); | ||
| }); | ||
|
|
||
| it("rotates labels at 45° when categories >= 15", () => { | ||
| const result = buildCategoryAxisLabel(15); | ||
| expect(result.rotate).toBe(45); | ||
| }); | ||
|
|
||
| it("truncates labels longer than 15 chars with ellipsis", () => { | ||
| const result = buildCategoryAxisLabel(10); | ||
| expect(result.formatter).toBeDefined(); | ||
| const fmt = result.formatter as (value: string) => string; | ||
| expect(fmt("Short")).toBe("Short"); | ||
| expect(fmt("This is a very long label text")).toBe("This is a very\u2026"); | ||
| }); | ||
|
|
||
| it("respects custom maxLength", () => { | ||
| const result = buildCategoryAxisLabel(10, { maxLabelLength: 8 }); | ||
| const fmt = result.formatter as (value: string) => string; | ||
| expect(fmt("12345678")).toBe("12345678"); | ||
| expect(fmt("123456789")).toBe("1234567\u2026"); | ||
| }); | ||
|
|
||
| it("respects rotation override", () => { | ||
| const result = buildCategoryAxisLabel(100, { rotateOverride: 60 }); | ||
| expect(result.rotate).toBe(60); | ||
| }); | ||
|
|
||
| it("returns rotate 0 with override of 0", () => { | ||
| const result = buildCategoryAxisLabel(20, { rotateOverride: 0 }); | ||
| expect(result.rotate).toBe(0); | ||
| }); | ||
|
|
||
| it("always includes tooltip config for full text", () => { | ||
| const result = buildCategoryAxisLabel(10); | ||
| expect(result.tooltip).toEqual({ show: true }); | ||
| }); | ||
|
|
||
| it("returns show: false when compact is true", () => { | ||
| const result = buildCategoryAxisLabel(10, { compact: true }); | ||
| expect(result.show).toBe(false); | ||
| }); | ||
|
|
||
| it("normalizes -1 sentinel to automatic rotation", () => { | ||
| // -1 is the "automatic" sentinel from the UI; it should fall through | ||
| // to the category-count heuristic, not produce rotate: -1 | ||
| const few = buildCategoryAxisLabel(5, { rotateOverride: -1 }); | ||
| expect(few.rotate).toBe(0); | ||
|
|
||
| const medium = buildCategoryAxisLabel(10, { rotateOverride: -1 }); | ||
| expect(medium.rotate).toBe(30); | ||
|
|
||
| const many = buildCategoryAxisLabel(20, { rotateOverride: -1 }); | ||
| expect(many.rotate).toBe(45); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { | |||||||||||||||||
| resolveShowLegend, | ||||||||||||||||||
| buildCompactGrid, | ||||||||||||||||||
| resolveItemColor, | ||||||||||||||||||
| buildCategoryAxisLabel, | ||||||||||||||||||
| } from "./chart-utils"; | ||||||||||||||||||
| import { parseColorThresholds } from "./color-threshold"; | ||||||||||||||||||
| import type { StylingRule } from "./styling-rule"; | ||||||||||||||||||
|
|
@@ -34,6 +35,8 @@ export interface BarChartProps extends Omit<BaseChartProps, "options"> { | |||||||||||||||||
| xAxisLabel?: string; | ||||||||||||||||||
| /** Y-axis name label */ | ||||||||||||||||||
| yAxisLabel?: string; | ||||||||||||||||||
| /** Override axis label rotation angle (0-90). Omit for automatic. */ | ||||||||||||||||||
| axisLabelRotation?: number; | ||||||||||||||||||
| /** @deprecated Use stylingRules instead. JSON string of thresholds for per-bar coloring */ | ||||||||||||||||||
| colorThresholds?: string; | ||||||||||||||||||
| /** Rule-based styling rules */ | ||||||||||||||||||
|
|
@@ -62,6 +65,7 @@ function BarChart({ | |||||||||||||||||
| showGridLines = true, | ||||||||||||||||||
| xAxisLabel, | ||||||||||||||||||
| yAxisLabel, | ||||||||||||||||||
| axisLabelRotation, | ||||||||||||||||||
| colorThresholds, | ||||||||||||||||||
| stylingRules, | ||||||||||||||||||
| paramValues, | ||||||||||||||||||
|
|
@@ -80,13 +84,20 @@ function BarChart({ | |||||||||||||||||
| const effectiveBarWidth = barWidth > 0 ? barWidth : undefined; | ||||||||||||||||||
| const thresholds = stylingRules ? [] : parseColorThresholds(colorThresholds ?? ""); | ||||||||||||||||||
|
|
||||||||||||||||||
| const categoryLabels = data.map((d) => d.label); | ||||||||||||||||||
| const axisLabelConfig = buildCategoryAxisLabel(categoryLabels.length, { | ||||||||||||||||||
| compact, | ||||||||||||||||||
| rotateOverride: axisLabelRotation, | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+88
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sentinel value When Normalize 🐛 Proposed fix const axisLabelConfig = buildCategoryAxisLabel(categoryLabels.length, {
compact,
- rotateOverride: axisLabelRotation,
+ rotateOverride: axisLabelRotation === -1 ? undefined : axisLabelRotation,
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| const categoryAxis = { | ||||||||||||||||||
| type: "category" as const, | ||||||||||||||||||
| data: data.map((d) => d.label), | ||||||||||||||||||
| axisLabel: { show: !compact }, | ||||||||||||||||||
| data: categoryLabels, | ||||||||||||||||||
| axisLabel: axisLabelConfig, | ||||||||||||||||||
| axisPointer: { type: "shadow" as const }, | ||||||||||||||||||
| name: compact ? undefined : (isHorizontal ? yAxisLabel : xAxisLabel), | ||||||||||||||||||
| nameLocation: "middle" as const, | ||||||||||||||||||
| nameGap: 30, | ||||||||||||||||||
| nameGap: axisLabelConfig.rotate > 0 ? 50 : 30, | ||||||||||||||||||
| }; | ||||||||||||||||||
| const valueAxis = { | ||||||||||||||||||
| type: "value" as const, | ||||||||||||||||||
|
|
@@ -123,7 +134,7 @@ function BarChart({ | |||||||||||||||||
| emphasis: seriesKeys.length > 1 ? { focus: "series" as const } : {}, | ||||||||||||||||||
| })), | ||||||||||||||||||
| }; | ||||||||||||||||||
| }, [data, orientation, stacked, showValues, showLegend, barWidth, barGap, showGridLines, xAxisLabel, yAxisLabel, colorThresholds, stylingRules, paramValues, compact, hideLegend]); | ||||||||||||||||||
| }, [data, orientation, stacked, showValues, showLegend, barWidth, barGap, showGridLines, xAxisLabel, yAxisLabel, axisLabelRotation, colorThresholds, stylingRules, paramValues, compact, hideLegend]); | ||||||||||||||||||
|
|
||||||||||||||||||
| return ( | ||||||||||||||||||
| <div ref={containerRef} className="h-full w-full"> | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.