The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix horizontal bar chart left axis labels always truncated to 10 characters (#25544)
Thomas Vaudescal73017bc74a67
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/constants/CommonChartConstants.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/constants/CommonChartConstants.tsindex b2d306c8a17..96157cc0244 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/constants/CommonChartConstants.ts+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/constants/CommonChartConstants.ts@@ -13,7 +13,6 @@ export const COMMON_CHART_CONSTANTS = {ROTATED_TICK_LABEL_WIDTH_ESTIMATE_RATIO: 0.5,TICK_LABEL_LINE_HEIGHT_RATIO: 1.4,TICK_ROTATION_ANGLE: -45,- TICK_PADDING_ALLOWANCE: 10,MIN_TICK_LABEL_LENGTH: 5,POSITIVE_RANGE_PADDING_RATIO: 0.1,MINIMUM_POSITIVE_RANGE_PADDING: 1,diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/__tests__/getBarChartLayout.test.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/__tests__/getBarChartLayout.test.tsindex dbdd1d2de6b..24a11180cd8 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/__tests__/getBarChartLayout.test.ts+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/__tests__/getBarChartLayout.test.ts@@ -1,6 +1,8 @@import { TEXT_MARGIN_LIMITS } from '@/page-layout/widgets/graph/constants/TextMarginLimits';import { getBarChartLayout } from '@/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartLayout';import { type ChartAxisTheme } from '@/page-layout/widgets/graph/types/ChartAxisTheme';+import { computeLeftAxisTitleWidth } from '@/page-layout/widgets/graph/utils/computeLeftAxisTitleWidth';+import { computeMaxLabelLengthForMargin } from '@/page-layout/widgets/graph/utils/computeMaxLabelLengthForMargin';import { type GraphValueFormatOptions } from '@/page-layout/widgets/graph/utils/graphFormatters';import { type BarChartDatum } from '@/page-layout/widgets/graph/graph-widget-bar-chart/types/BarChartDatum';import { BarChartLayout } from '~/generated-metadata/graphql';@@ -125,6 +127,81 @@ describe('getBarChartLayout', () => {);});+ it('widens the left margin to fit long category labels instead of truncating them to the minimum', () => {+ const longLabel = 'Achraf Benjelloun';++ const result = getBarChartLayout({+ ...baseParams,+ layout: BarChartLayout.HORIZONTAL,+ xAxisLabel: undefined,+ yAxisLabel: undefined,+ data: [{ category: longLabel, value: 10 }],+ });++ expect(result.margins.left).toBeGreaterThan(75);+ expect(result.margins.left).toBeLessThanOrEqual(+ TEXT_MARGIN_LIMITS.max.left,+ );+ expect(result.axisLeftConfiguration.format(longLabel)).toBe(longLabel);+ });++ it('caps the left margin and still truncates labels that exceed the maximum margin', () => {+ const veryLongLabel = 'A'.repeat(40);++ const result = getBarChartLayout({+ ...baseParams,+ layout: BarChartLayout.HORIZONTAL,+ xAxisLabel: undefined,+ yAxisLabel: undefined,+ data: [{ category: veryLongLabel, value: 10 }],+ });++ expect(result.margins.left).toBe(TEXT_MARGIN_LIMITS.max.left);+ expect(result.axisLeftConfiguration.format(veryLongLabel)).toMatch(+ /\.\.\.$/,+ );+ expect(+ result.axisLeftConfiguration.format(veryLongLabel).length,+ ).toBeLessThan(veryLongLabel.length);+ });++ it('keeps capped labels clear of the Y-axis title sharing the left margin', () => {+ const veryLongLabel = 'A'.repeat(40);++ const buildLayout = (yAxisLabel: string | undefined) =>+ getBarChartLayout({+ ...baseParams,+ layout: BarChartLayout.HORIZONTAL,+ xAxisLabel: undefined,+ yAxisLabel,+ data: [{ category: veryLongLabel, value: 10 }],+ });++ const withoutTitle = buildLayout(undefined);+ const withTitle = buildLayout('Company');++ expect(withTitle.margins.left).toBe(TEXT_MARGIN_LIMITS.max.left);++ const renderedWithoutTitle =+ withoutTitle.axisLeftConfiguration.format(veryLongLabel);+ const renderedWithTitle =+ withTitle.axisLeftConfiguration.format(veryLongLabel);++ expect(renderedWithTitle.length).toBeLessThan(+ renderedWithoutTitle.length,+ );+ expect(+ computeMaxLabelLengthForMargin({+ marginSize: withTitle.margins.left,+ axisFontSize: 11,+ axisTitleWidth: computeLeftAxisTitleWidth({+ yAxisLabel: 'Company',+ legendFontSize: 11,+ }),+ }),+ ).toBe(renderedWithTitle.length);+ });+it('keeps explicit range bounds as value domain', () => {const result = getBarChartLayout({...baseParams,diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartLayout.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartLayout.tsindex 61ba43345bd..04a15c66a86 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartLayout.ts+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartLayout.ts@@ -9,6 +9,7 @@ import {import { truncateTickLabel } from '@/page-layout/widgets/graph/graph-widget-bar-chart/utils/truncateTickLabel';import { type ChartAxisTheme } from '@/page-layout/widgets/graph/types/ChartAxisTheme';import { type ChartMargins } from '@/page-layout/widgets/graph/types/ChartMargins';+import { computeLeftAxisTitleWidth } from '@/page-layout/widgets/graph/utils/computeLeftAxisTitleWidth';import { computeBottomLegendOffsetFromText } from '@/page-layout/widgets/graph/utils/computeBottomLegendOffsetFromText';import { computeChartMargins } from '@/page-layout/widgets/graph/utils/computeChartMargins';import { computeValueTickValues } from '@/page-layout/widgets/graph/utils/computeValueTickValues';@@ -81,12 +82,7 @@ const resolveMarginInputs = ({? tickResult.tickValues.map((value) =>formatGraphValue(value, formatOptions),)- : tickConfiguration.categoryTickValues.map((value) =>- truncateTickLabel(- String(value),- tickConfiguration.maxLeftAxisTickLabelLength,- ),- );+ : tickConfiguration.categoryTickValues.map((value) => String(value));return { bottomTickLabels, leftTickLabels };};@@ -133,6 +129,10 @@ export const getBarChartLayout = ({rightTickLabels = [],}: GetBarChartLayoutParams): BarChartLayoutResult => {const { tickFontSize, legendFontSize } = resolveAxisFontSizes(axisTheme);+ const leftAxisTitleWidth = computeLeftAxisTitleWidth({+ yAxisLabel,+ legendFontSize,+ });const { margins, tickConfiguration, valueTickResult, bottomLegendOffset } =computeChartMargins({@@ -150,6 +150,7 @@ export const getBarChartLayout = ({axisFontSize: tickFontSize,layout,margins: currentMargins,+ leftAxisTitleWidth,}),computeValueTickValues: (currentTickConfiguration) =>computeValueTickValues({diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartTickConfig.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartTickConfig.tsindex bba0f9a6376..f90ad53fbdf 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartTickConfig.ts+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graph-widget-bar-chart/utils/getBarChartTickConfig.ts@@ -23,6 +23,7 @@ export const getBarChartTickConfig = ({axisFontSize,layout,margins,+ leftAxisTitleWidth,}: {width: number;height: number;@@ -31,6 +32,7 @@ export const getBarChartTickConfig = ({axisFontSize: number;layout: BarChartLayout;margins: ChartMargins;+ leftAxisTitleWidth: number;}): BarChartTickConfig => {const clampValueTickCount = (tickCount: number) =>Math.min(@@ -76,6 +78,7 @@ export const getBarChartTickConfig = ({const maxLeftAxisTickLabelLength = computeMaxLabelLengthForMargin({marginSize: margins.left,axisFontSize,+ axisTitleWidth: leftAxisTitleWidth,});return {diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/__tests__/computeMaxLabelLengthForMargin.test.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/__tests__/computeMaxLabelLengthForMargin.test.tsindex 050b0e6f805..baca0997d89 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/__tests__/computeMaxLabelLengthForMargin.test.ts+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/__tests__/computeMaxLabelLengthForMargin.test.ts@@ -1,4 +1,5 @@import { COMMON_CHART_CONSTANTS } from '@/page-layout/widgets/graph/constants/CommonChartConstants';+import { TEXT_MARGIN_EXTRAS } from '@/page-layout/widgets/graph/constants/TextMarginExtras';import { computeMaxLabelLengthForMargin } from '@/page-layout/widgets/graph/utils/computeMaxLabelLengthForMargin';describe('computeMaxLabelLengthForMargin', () => {@@ -10,7 +11,10 @@ describe('computeMaxLabelLengthForMargin', () => {const expectedCharacterWidth =11 * COMMON_CHART_CONSTANTS.HORIZONTAL_LABEL_CHARACTER_WIDTH_RATIO;- const availableWidth = 100 - COMMON_CHART_CONSTANTS.TICK_PADDING_ALLOWANCE;+ const availableWidth =+ 100 -+ (COMMON_CHART_CONSTANTS.TICK_PADDING ++ TEXT_MARGIN_EXTRAS.tickPaddingExtra);const expectedLength = Math.floor(availableWidth / expectedCharacterWidth);expect(result).toBe(expectedLength);diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/computeLeftAxisTitleWidth.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/computeLeftAxisTitleWidth.tsnew file mode 100644index 00000000000..1093c017e6f--- /dev/null+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/computeLeftAxisTitleWidth.ts@@ -0,0 +1,13 @@+import { TEXT_MARGIN_EXTRAS } from '@/page-layout/widgets/graph/constants/TextMarginExtras';+import { isNonEmptyString } from '@sniptt/guards';++export const computeLeftAxisTitleWidth = ({+ yAxisLabel,+ legendFontSize,+}: {+ yAxisLabel?: string;+ legendFontSize: number;+}): number =>+ isNonEmptyString(yAxisLabel)+ ? legendFontSize + TEXT_MARGIN_EXTRAS.tickPaddingExtra+ : 0;diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/computeMaxLabelLengthForMargin.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/computeMaxLabelLengthForMargin.tsindex 5f1e22f9200..8e78a637845 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/computeMaxLabelLengthForMargin.ts+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/computeMaxLabelLengthForMargin.ts@@ -1,17 +1,22 @@import { COMMON_CHART_CONSTANTS } from '@/page-layout/widgets/graph/constants/CommonChartConstants';+import { TEXT_MARGIN_EXTRAS } from '@/page-layout/widgets/graph/constants/TextMarginExtras';export const computeMaxLabelLengthForMargin = ({marginSize,axisFontSize,+ axisTitleWidth = 0,}: {marginSize: number;axisFontSize: number;+ axisTitleWidth?: number;}): number => {const characterWidth =axisFontSize *COMMON_CHART_CONSTANTS.HORIZONTAL_LABEL_CHARACTER_WIDTH_RATIO;const availableWidth =- marginSize - COMMON_CHART_CONSTANTS.TICK_PADDING_ALLOWANCE;+ marginSize -+ axisTitleWidth -+ (COMMON_CHART_CONSTANTS.TICK_PADDING + TEXT_MARGIN_EXTRAS.tickPaddingExtra);const calculatedLength = Math.max(COMMON_CHART_CONSTANTS.TICK_MINIMUM_NUMBER_OF_DISPLAYED_CHARACTERS,Math.floor(availableWidth / characterWidth),diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/getChartMarginsFromText.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/getChartMarginsFromText.tsindex 5242656016c..97657cc7ba6 100644--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/getChartMarginsFromText.ts+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/getChartMarginsFromText.ts@@ -2,6 +2,7 @@ import { COMMON_CHART_CONSTANTS } from '@/page-layout/widgets/graph/constants/Coimport { TEXT_MARGIN_EXTRAS } from '@/page-layout/widgets/graph/constants/TextMarginExtras';import { TEXT_MARGIN_LIMITS } from '@/page-layout/widgets/graph/constants/TextMarginLimits';import { type ChartMargins } from '@/page-layout/widgets/graph/types/ChartMargins';+import { computeLeftAxisTitleWidth } from '@/page-layout/widgets/graph/utils/computeLeftAxisTitleWidth';import { estimateLineHeight } from '@/page-layout/widgets/graph/utils/estimateLineHeight';import { estimateRotatedHeight } from '@/page-layout/widgets/graph/utils/estimateRotatedHeight';import { getMaxLabelLength } from '@/page-layout/widgets/graph/utils/getMaxLabelLength';@@ -86,9 +87,10 @@ export const getChartMarginsFromText = ({COMMON_CHART_CONSTANTS.TICK_PADDING +TEXT_MARGIN_EXTRAS.tickPaddingExtra: 0;- const leftLabelBlock = yAxisLabel- ? normalizedLegendFontSize + TEXT_MARGIN_EXTRAS.tickPaddingExtra- : 0;+ const leftLabelBlock = computeLeftAxisTitleWidth({+ yAxisLabel,+ legendFontSize: normalizedLegendFontSize,+ });const left = clamp(Math.ceil(leftTicksBlock + leftLabelBlock),TEXT_MARGIN_LIMITS.min.left,