Skip to content

The open alternative to Salesforce, designed for AI.

Custom data point labels for line chart (#15793)

nitineab123f999aa
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/components/GraphDataLabel.tsx b/packages/twenty-front/src/modules/page-layout/widgets/graph/components/GraphDataLabel.tsx
new file mode 100644
index 00000000000..a73c3304d74
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/components/GraphDataLabel.tsx
@@ -0,0 +1,40 @@
+import { type GraphLabelData } from '@/page-layout/widgets/graph/types/GraphLabelData';
+import { calculateGraphLabelStyles } from '@/page-layout/widgets/graph/utils/calculateGraphLabelStyles';
+import { useTheme } from '@emotion/react';
+import { animated } from '@react-spring/web';
+import { isDefined } from 'twenty-shared/utils';
+
+type GraphDataLabelProps = {
+ label: GraphLabelData;
+ formatValue?: (value: number) => string;
+ offset: number;
+ isVerticalLayout: boolean;
+};
+
+export const GraphDataLabel = ({
+ label,
+ formatValue,
+ offset,
+ isVerticalLayout,
+}: GraphDataLabelProps) => {
+ const theme = useTheme();
+ const styles = calculateGraphLabelStyles(label, offset, isVerticalLayout);
+
+ return (
+ <animated.text
+ key={label.key}
+ x={styles.x}
+ y={styles.y}
+ textAnchor={styles.textAnchor}
+ dominantBaseline={styles.dominantBaseline}
+ style={{
+ fill: theme.font.color.light,
+ fontSize: 11,
+ fontWeight: theme.font.weight.medium,
+ transform: styles.transformOffset,
+ }}
+ >
+ {isDefined(formatValue) ? formatValue(label.value) : label.value}
+ </animated.text>
+ );
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/components/__stories__/GraphWidgetLineChart.stories.tsx b/packages/twenty-front/src/modules/page-layout/widgets/graph/components/__stories__/GraphWidgetLineChart.stories.tsx
index 38b1743e0f5..b2dbb7ff442 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/components/__stories__/GraphWidgetLineChart.stories.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/components/__stories__/GraphWidgetLineChart.stories.tsx
@@ -45,6 +45,10 @@ const meta: Meta<typeof GraphWidgetLineChart> = {
enablePointLabel: {
control: 'boolean',
},
+ groupMode: {
+ control: 'select',
+ options: ['stacked', undefined],
+ },
xAxisLabel: {
control: 'text',
},
@@ -79,6 +83,7 @@ const renderChart = (args: ChartArgs) => (
showLegend={args.showLegend}
showGrid={args.showGrid}
enablePointLabel={args.enablePointLabel}
+ groupMode={args.groupMode}
xAxisLabel={args.xAxisLabel}
yAxisLabel={args.yAxisLabel}
rangeMin={args.rangeMin}
@@ -182,6 +187,43 @@ export const StackedArea: Story = {
yAxisLabel: 'Revenue',
displayType: 'shortNumber',
prefix: '$',
+ groupMode: 'stacked',
+ },
+};
+
+export const StackedWithNegativeValues: Story = {
+ render: renderChart,
+ args: {
+ id: 'line-chart-stacked-negative',
+ data: [
+ {
+ id: 'series-positive',
+ label: 'Positive Series',
+ color: 'blue',
+ data: [
+ { x: 'Jan', y: 10 },
+ { x: 'Feb', y: -5 },
+ { x: 'Mar', y: 15 },
+ ],
+ },
+ {
+ id: 'series-negative',
+ label: 'Negative Series',
+ color: 'red',
+ data: [
+ { x: 'Jan', y: 5 },
+ { x: 'Feb', y: -10 },
+ { x: 'Mar', y: 5 },
+ ],
+ },
+ ],
+ showLegend: true,
+ showGrid: true,
+ xAxisLabel: 'Month',
+ yAxisLabel: 'Net Value',
+ displayType: 'number',
+ enablePointLabel: true,
+ groupMode: 'stacked',
},
};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/CustomTotalsLayer.tsx b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/CustomTotalsLayer.tsx
index 695566f2f20..c4bdf50bf32 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/CustomTotalsLayer.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/CustomTotalsLayer.tsx
@@ -1,9 +1,11 @@
+import { GraphDataLabel } from '@/page-layout/widgets/graph/components/GraphDataLabel';
import { type BarChartDataItem } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartDataItem';
+import { type BarChartLabelData } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartLabelData';
import { BarChartLayout } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartLayout';
-import { useTheme } from '@emotion/react';
-import { type BarCustomLayerProps, type ComputedBarDatum } from '@nivo/bar';
-import { animated } from '@react-spring/web';
-import { isDefined } from 'twenty-shared/utils';
+import { computeBarChartGroupedLabels } from '@/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartGroupedLabels';
+import { computeBarChartStackedLabels } from '@/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartStackedLabels';
+import { type GraphLabelData } from '@/page-layout/widgets/graph/types/GraphLabelData';
+import { type BarCustomLayerProps } from '@nivo/bar';
type CustomTotalsLayerProps = Pick<
BarCustomLayerProps<BarChartDataItem>,
@@ -14,123 +16,19 @@ type CustomTotalsLayerProps = Pick<
layout?: BarChartLayout;
groupMode?: 'grouped' | 'stacked';
omitNullValues?: boolean;
+ showValues: boolean;
};
-type LabelData = {
- key: string;
- value: number;
- verticalX: number;
- verticalY: number;
- horizontalX: number;
- horizontalY: number;
- isNegative: boolean;
-};
-
-const computeGroupedLabels = (
- bars: readonly ComputedBarDatum<BarChartDataItem>[],
-): LabelData[] => {
- return bars.map((bar) => {
- const value = Number(bar.data.value);
- const isNegative = value < 0;
- const centerX = bar.x + bar.width / 2;
- const centerY = bar.y + bar.height / 2;
-
- return {
- key: `value-${bar.data.id}-${bar.data.indexValue}`,
- value,
- verticalX: centerX,
- verticalY: isNegative ? bar.y + bar.height : bar.y,
- horizontalX: isNegative ? bar.x : bar.x + bar.width,
- horizontalY: centerY,
- isNegative,
- };
- });
-};
-
-const computeStackedLabels = (
- bars: readonly ComputedBarDatum<BarChartDataItem>[],
-): LabelData[] => {
- const groupTotals = new Map<
- string,
- {
- total: number;
- maxY: number;
- maxX: number;
- bars: ComputedBarDatum<BarChartDataItem>[];
- }
- >();
-
- for (const bar of bars) {
- const groupKey = String(bar.data.indexValue);
- const existingGroup = groupTotals.get(groupKey);
-
- if (isDefined(existingGroup)) {
- existingGroup.total += Number(bar.data.value);
- existingGroup.maxY = Math.min(existingGroup.maxY, bar.y);
- existingGroup.maxX = Math.max(existingGroup.maxX, bar.x + bar.width);
- existingGroup.bars.push(bar);
- } else {
- groupTotals.set(groupKey, {
- total: Number(bar.data.value),
- maxY: bar.y,
- maxX: bar.x + bar.width,
- bars: [bar],
- });
- }
- }
-
- return Array.from(groupTotals.entries()).map(
- ([groupKey, { total, maxY, maxX, bars: groupBars }]) => {
- const centerX =
- groupBars.reduce((acc, bar) => acc + bar.x + bar.width / 2, 0) /
- groupBars.length;
- const centerY =
- groupBars.reduce((acc, bar) => acc + bar.y + bar.height / 2, 0) /
- groupBars.length;
-
- return {
- key: `total-${groupKey}`,
- value: total,
- verticalX: centerX,
- verticalY: maxY,
- horizontalX: maxX,
- horizontalY: centerY,
- isNegative: false,
- };
- },
- );
-};
-
-const getLabelStyles = (
- label: LabelData,
- isVertical: boolean,
- offset: number,
-) => {
- const offsetSign = isVertical
- ? label.isNegative
- ? 1
- : -1
- : label.isNegative
- ? -1
- : 1;
-
- const axis = isVertical ? 'Y' : 'X';
- const transformOffset = `translate${axis}(${offsetSign * offset}px)`;
-
- const textAnchor = isVertical ? 'middle' : label.isNegative ? 'end' : 'start';
-
- const dominantBaseline = isVertical
- ? label.isNegative
- ? 'hanging'
- : 'auto'
- : 'central';
-
+const convertToGraphLabelData = (
+ barChartLabel: BarChartLabelData,
+ isVerticalLayout: boolean,
+): GraphLabelData => {
return {
- x: isVertical ? label.verticalX : label.horizontalX,
- y: isVertical ? label.verticalY : label.horizontalY,
- textAnchor,
- dominantBaseline,
- transformOffset,
+ key: barChartLabel.key,
+ value: barChartLabel.value,
+ x: isVerticalLayout ? barChartLabel.verticalX : barChartLabel.horizontalX,
+ y: isVerticalLayout ? barChartLabel.verticalY : barChartLabel.horizontalY,
+ shouldRenderBelow: barChartLabel.shouldRenderBelow,
};
};
@@ -141,40 +39,39 @@ export const CustomTotalsLayer = ({
layout = BarChartLayout.VERTICAL,
groupMode = 'grouped',
omitNullValues = false,
+ showValues,
}: CustomTotalsLayerProps) => {
- const theme = useTheme();
- const isVertical = layout === BarChartLayout.VERTICAL;
+ if (!showValues) {
+ return null;
+ }
+
+ const isVerticalLayout = layout === BarChartLayout.VERTICAL;
- const labels =
+ const barChartLabels =
groupMode === 'stacked'
- ? computeStackedLabels(bars)
- : computeGroupedLabels(bars);
+ ? computeBarChartStackedLabels(bars)
+ : computeBarChartGroupedLabels(bars);
- const labelsToRender = labels.filter(
- (label) => !omitNullValues || label.value !== 0,
- );
+ const barChartLabelsToRender = omitNullValues
+ ? barChartLabels.filter((label) => label.value !== 0)
+ : barChartLabels;
return (
<>
- {labelsToRender.map((label) => {
- const styles = getLabelStyles(label, isVertical, offset);
+ {barChartLabelsToRender.map((barChartLabel) => {
+ const graphLabel = convertToGraphLabelData(
+ barChartLabel,
+ isVerticalLayout,
+ );
return (
- <animated.text
- key={label.key}
- x={styles.x}
- y={styles.y}
- textAnchor={styles.textAnchor}
- dominantBaseline={styles.dominantBaseline}
- style={{
- fill: theme.font.color.light,
- fontSize: 11,
- fontWeight: theme.font.weight.medium,
- transform: styles.transformOffset,
- }}
- >
- {isDefined(formatValue) ? formatValue(label.value) : label.value}
- </animated.text>
+ <GraphDataLabel
+ key={graphLabel.key}
+ label={graphLabel}
+ formatValue={formatValue}
+ offset={offset}
+ isVerticalLayout={isVerticalLayout}
+ />
);
})}
</>
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChart.tsx b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChart.tsx
index 370ead6f884..4365e82e615 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChart.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChart.tsx
@@ -186,18 +186,15 @@ export const GraphWidgetBarChart = ({
}: {
bars: readonly ComputedBarDatum<BarChartDataItem>[];
}) => (
- <>
- {showValues && (
- <CustomTotalsLayer
- bars={bars}
- formatValue={(value) => formatGraphValue(value, formatOptions)}
- offset={theme.spacingMultiplicator * 2}
- layout={layout}
- groupMode={groupMode}
- omitNullValues={omitNullValues}
- />
- )}
- </>
+ <CustomTotalsLayer
+ bars={bars}
+ formatValue={(value) => formatGraphValue(value, formatOptions)}
+ offset={theme.spacingMultiplicator * 2}
+ layout={layout}
+ groupMode={groupMode}
+ omitNullValues={omitNullValues}
+ showValues={showValues}
+ />
);
const calculatedValueRange =
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChartRenderer.tsx b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChartRenderer.tsx
index 6cb3a058281..1d66d200994 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChartRenderer.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/components/GraphWidgetBarChartRenderer.tsx
@@ -48,7 +48,6 @@ export const GraphWidgetBarChartRenderer = ({
configuration.groupMode,
hasGroupByOnSecondaryAxis,
);
-
const chartFilterKey = generateChartAggregateFilterKey(
configuration.rangeMin,
configuration.rangeMax,
@@ -60,29 +59,27 @@ export const GraphWidgetBarChartRenderer = ({
}
return (
- <>
+ <Suspense fallback={<ChartSkeletonLoader />}>
<GraphWidgetChartHasTooManyGroupsEffect
hasTooManyGroups={hasTooManyGroups}
/>
- <Suspense fallback={<ChartSkeletonLoader />}>
- <GraphWidgetBarChart
- key={chartFilterKey}
- data={data}
- series={series}
- indexBy={indexBy}
- keys={keys}
- xAxisLabel={xAxisLabel}
- yAxisLabel={yAxisLabel}
- showValues={showDataLabels}
- layout={layout}
- groupMode={groupMode}
- id={widget.id}
- displayType="shortNumber"
- rangeMin={configuration.rangeMin ?? undefined}
- rangeMax={configuration.rangeMax ?? undefined}
- omitNullValues={configuration.omitNullValues ?? false}
- />
- </Suspense>
- </>
+ <GraphWidgetBarChart
+ key={chartFilterKey}
+ data={data}
+ series={series}
+ indexBy={indexBy}
+ keys={keys}
+ xAxisLabel={xAxisLabel}
+ yAxisLabel={yAxisLabel}
+ showValues={showDataLabels}
+ layout={layout}
+ groupMode={groupMode}
+ id={widget.id}
+ displayType="shortNumber"
+ rangeMin={configuration.rangeMin ?? undefined}
+ rangeMax={configuration.rangeMax ?? undefined}
+ omitNullValues={configuration.omitNullValues ?? false}
+ />
+ </Suspense>
);
};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartLabelData.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartLabelData.ts
new file mode 100644
index 00000000000..6c940b31b14
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartLabelData.ts
@@ -0,0 +1,9 @@
+export type BarChartLabelData = {
+ key: string;
+ value: number;
+ verticalX: number;
+ verticalY: number;
+ horizontalX: number;
+ horizontalY: number;
+ shouldRenderBelow: boolean;
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/__tests__/computeBarChartStackedLabels.test.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/__tests__/computeBarChartStackedLabels.test.ts
new file mode 100644
index 00000000000..5ae9eebc2bc
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/__tests__/computeBarChartStackedLabels.test.ts
@@ -0,0 +1,258 @@
+import { type BarChartDataItem } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartDataItem';
+import { type ComputedBarDatum } from '@nivo/bar';
+import { computeBarChartStackedLabels } from '../computeBarChartStackedLabels';
+
+const createMockBar = (
+ id: string,
+ indexValue: string | number,
+ value: number,
+ x: number,
+ y: number,
+ width: number,
+ height: number,
+): ComputedBarDatum<BarChartDataItem> =>
+ ({
+ id,
+ key: id,
+ index: 0,
+ data: { id, indexValue, value },
+ x,
+ y,
+ absX: x,
+ absY: y,
+ width,
+ height,
+ color: 'blue',
+ label: String(indexValue),
+ formattedValue: String(value),
+ }) as unknown as ComputedBarDatum<BarChartDataItem>;
+
+describe('computeBarChartStackedLabels (essential cases)', () => {
+ it('returns total for single index with all positive values', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', 100, 50, 200, 30, 100),
+ createMockBar('series2', 'Jan', 50, 50, 100, 30, 100),
+ createMockBar('series3', 'Jan', 25, 50, 50, 30, 50),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe('total-Jan');
+ expect(result[0].value).toBe(175);
+ expect(result[0].verticalX).toBeCloseTo(65, 1);
+ expect(result[0].verticalY).toBe(50);
+ expect(result[0].horizontalX).toBe(80);
+ expect(result[0].horizontalY).toBeCloseTo(158.33, 1);
+ expect(result[0].shouldRenderBelow).toBe(false);
+ });
+
+ it('returns total for single index with all negative values', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', -100, 50, 400, 30, 100),
+ createMockBar('series2', 'Jan', -50, 50, 500, 30, 100),
+ createMockBar('series3', 'Jan', -25, 50, 575, 30, 50),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe('total-Jan');
+ expect(result[0].value).toBe(-175);
+ expect(result[0].verticalX).toBeCloseTo(65, 1);
+ expect(result[0].verticalY).toBe(625);
+ expect(result[0].horizontalX).toBe(80);
+ expect(result[0].horizontalY).toBeCloseTo(533.33, 1);
+ expect(result[0].shouldRenderBelow).toBe(true);
+ });
+
+ it('returns total for single index with mixed positive/negative (net positive)', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', 100, 50, 200, 30, 100),
+ createMockBar('series2', 'Jan', -30, 50, 370, 30, 70),
+ createMockBar('series3', 'Jan', 20, 50, 250, 30, 50),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe('total-Jan');
+ expect(result[0].value).toBe(90);
+ expect(result[0].verticalX).toBeCloseTo(65, 1);
+ expect(result[0].verticalY).toBe(200);
+ expect(result[0].horizontalX).toBe(80);
+ expect(result[0].horizontalY).toBeCloseTo(310, 1);
+ expect(result[0].shouldRenderBelow).toBe(false);
+ });
+
+ it('returns total for single index with mixed positive/negative (net negative)', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', -100, 50, 400, 30, 100),
+ createMockBar('series2', 'Jan', 30, 50, 270, 30, 70),
+ createMockBar('series3', 'Jan', 20, 50, 280, 30, 50),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe('total-Jan');
+ expect(result[0].value).toBe(-50);
+ expect(result[0].verticalX).toBeCloseTo(65, 1);
+ expect(result[0].verticalY).toBe(500);
+ expect(result[0].horizontalX).toBe(80);
+ expect(result[0].horizontalY).toBeCloseTo(353.33, 1);
+ expect(result[0].shouldRenderBelow).toBe(true);
+ });
+
+ it('returns multiple totals for multiple indices', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', 100, 50, 200, 30, 100),
+ createMockBar('series2', 'Jan', 50, 50, 100, 30, 100),
+ createMockBar('series1', 'Feb', 75, 150, 225, 30, 75),
+ createMockBar('series2', 'Feb', 25, 150, 275, 30, 50),
+ createMockBar('series1', 'Mar', -50, 250, 400, 30, 50),
+ createMockBar('series2', 'Mar', -30, 250, 430, 30, 30),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toHaveLength(3);
+ expect(result).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ key: 'total-Jan',
+ value: 150,
+ shouldRenderBelow: false,
+ }),
+ expect.objectContaining({
+ key: 'total-Feb',
+ value: 100,
+ shouldRenderBelow: false,
+ }),
+ expect.objectContaining({
+ key: 'total-Mar',
+ value: -80,
+ shouldRenderBelow: true,
+ }),
+ ]),
+ );
+ });
+
+ it('handles single bar per index (no stacking)', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', 100, 50, 200, 30, 100),
+ createMockBar('series1', 'Feb', 150, 150, 150, 30, 150),
+ createMockBar('series1', 'Mar', -50, 250, 400, 30, 50),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: 100,
+ verticalX: 65,
+ verticalY: 200,
+ horizontalX: 80,
+ horizontalY: 250,
+ shouldRenderBelow: false,
+ },
+ {
+ key: 'total-Feb',
+ value: 150,
+ verticalX: 165,
+ verticalY: 150,
+ horizontalX: 180,
+ horizontalY: 225,
+ shouldRenderBelow: false,
+ },
+ {
+ key: 'total-Mar',
+ value: -50,
+ verticalX: 265,
+ verticalY: 450,
+ horizontalX: 280,
+ horizontalY: 425,
+ shouldRenderBelow: true,
+ },
+ ]);
+ });
+
+ it('handles zero values correctly', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', 100, 50, 200, 30, 100),
+ createMockBar('series2', 'Jan', 0, 50, 300, 30, 0),
+ createMockBar('series3', 'Jan', -100, 50, 400, 30, 100),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].key).toBe('total-Jan');
+ expect(result[0].value).toBe(0);
+ expect(result[0].verticalX).toBeCloseTo(65, 1);
+ expect(result[0].verticalY).toBe(200);
+ expect(result[0].horizontalX).toBe(80);
+ expect(result[0].horizontalY).toBeCloseTo(333.33, 1);
+ expect(result[0].shouldRenderBelow).toBe(false);
+ });
+
+ it('handles empty bars array', () => {
+ const result = computeBarChartStackedLabels([]);
+ expect(result).toEqual([]);
+ });
+
+ it('handles numeric index values (not just strings)', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 2024, 100, 50, 200, 30, 100),
+ createMockBar('series2', 2024, 50, 50, 100, 30, 100),
+ createMockBar('series1', 2025, 75, 150, 225, 30, 75),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result).toHaveLength(2);
+ expect(result[0].key).toBe('total-2024');
+ expect(result[0].value).toBe(150);
+ expect(result[0].horizontalY).toBeCloseTo(200, 1);
+ expect(result[1].key).toBe('total-2025');
+ expect(result[1].value).toBe(75);
+ expect(result[1].horizontalY).toBeCloseTo(262.5, 1);
+ });
+
+ it('tracks minimum Y position correctly for positive totals', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', 50, 100, 250, 30, 50),
+ createMockBar('series2', 'Jan', 100, 100, 150, 30, 100),
+ createMockBar('series3', 'Jan', 25, 100, 325, 30, 25),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result[0].verticalY).toBe(150);
+ });
+
+ it('tracks maximum bottom Y position correctly for negative totals', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', -50, 100, 350, 30, 50),
+ createMockBar('series2', 'Jan', -100, 100, 400, 30, 100),
+ createMockBar('series3', 'Jan', -25, 100, 325, 30, 25),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result[0].verticalY).toBe(500);
+ });
+
+ it('calculates center positions correctly', () => {
+ const bars: ComputedBarDatum<BarChartDataItem>[] = [
+ createMockBar('series1', 'Jan', 100, 50, 200, 40, 100),
+ createMockBar('series2', 'Jan', 50, 60, 150, 20, 50),
+ ];
+
+ const result = computeBarChartStackedLabels(bars);
+
+ expect(result[0].verticalX).toBe((70 + 70) / 2); // (50+20, 60+10) avg
+ expect(result[0].horizontalX).toBe(90); // max(50+40, 60+20) = 90
+ });
+});
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartGroupedLabels.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartGroupedLabels.ts
new file mode 100644
index 00000000000..da7a6e2b3a6
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartGroupedLabels.ts
@@ -0,0 +1,24 @@
+import { type BarChartDataItem } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartDataItem';
+import { type BarChartLabelData } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartLabelData';
+import { type ComputedBarDatum } from '@nivo/bar';
+
+export const computeBarChartGroupedLabels = (
+ bars: readonly ComputedBarDatum<BarChartDataItem>[],
+): BarChartLabelData[] => {
+ return bars.map((bar) => {
+ const value = Number(bar.data.value);
+ const shouldRenderBelow = value < 0;
+ const centerX = bar.x + bar.width / 2;
+ const centerY = bar.y + bar.height / 2;
+
+ return {
+ key: `value-${bar.data.id}-${bar.data.indexValue}`,
+ value,
+ verticalX: centerX,
+ verticalY: shouldRenderBelow ? bar.y + bar.height : bar.y,
+ horizontalX: shouldRenderBelow ? bar.x : bar.x + bar.width,
+ horizontalY: centerY,
+ shouldRenderBelow,
+ };
+ });
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartStackedLabels.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartStackedLabels.ts
new file mode 100644
index 00000000000..57a1c47c094
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetBarChart/utils/computeBarChartStackedLabels.ts
@@ -0,0 +1,88 @@
+import { type BarChartDataItem } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartDataItem';
+import { type BarChartLabelData } from '@/page-layout/widgets/graph/graphWidgetBarChart/types/BarChartLabelData';
+import { type ComputedBarDatum } from '@nivo/bar';
+import { isDefined } from 'twenty-shared/utils';
+
+export const computeBarChartStackedLabels = (
+ bars: readonly ComputedBarDatum<BarChartDataItem>[],
+): BarChartLabelData[] => {
+ const stackData = new Map<
+ string,
+ {
+ total: number;
+ minimumYPosition: number;
+ maximumBottomYPosition: number;
+ maximumXPosition: number;
+ bars: ComputedBarDatum<BarChartDataItem>[];
+ }
+ >();
+
+ for (const bar of bars) {
+ const groupKey = String(bar.data.indexValue);
+ const value = Number(bar.data.value);
+ const barTopY = bar.y;
+ const barBottomY = bar.y + bar.height;
+ const barRightX = bar.x + bar.width;
+ const existingGroup = stackData.get(groupKey);
+
+ if (isDefined(existingGroup)) {
+ existingGroup.total += value;
+ existingGroup.minimumYPosition = Math.min(
+ existingGroup.minimumYPosition,
+ barTopY,
+ );
+ existingGroup.maximumBottomYPosition = Math.max(
+ existingGroup.maximumBottomYPosition,
+ barBottomY,
+ );
+ existingGroup.maximumXPosition = Math.max(
+ existingGroup.maximumXPosition,
+ barRightX,
+ );
+ existingGroup.bars.push(bar);
+ } else {
+ stackData.set(groupKey, {
+ total: value,
+ minimumYPosition: barTopY,
+ maximumBottomYPosition: barBottomY,
+ maximumXPosition: barRightX,
+ bars: [bar],
+ });
+ }
+ }
+
+ const groupedEntries = Array.from(stackData.entries());
+
+ const labels: BarChartLabelData[] = groupedEntries.map(
+ ([
+ groupKey,
+ {
+ total,
+ minimumYPosition,
+ maximumBottomYPosition,
+ maximumXPosition,
+ bars: groupBars,
+ },
+ ]) => {
+ const isNegativeTotal = total < 0;
+ const centerX =
+ groupBars.reduce((acc, bar) => acc + bar.x + bar.width / 2, 0) /
+ groupBars.length;
+ const centerY =
+ groupBars.reduce((acc, bar) => acc + bar.y + bar.height / 2, 0) /
+ groupBars.length;
+
+ return {
+ key: `total-${groupKey}`,
+ value: total,
+ verticalX: centerX,
+ verticalY: isNegativeTotal ? maximumBottomYPosition : minimumYPosition,
+ horizontalX: maximumXPosition,
+ horizontalY: centerY,
+ shouldRenderBelow: isNegativeTotal,
+ };
+ },
+ );
+
+ return labels;
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/components/CustomPointLabelsLayer.tsx b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/components/CustomPointLabelsLayer.tsx
new file mode 100644
index 00000000000..296217532dd
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/components/CustomPointLabelsLayer.tsx
@@ -0,0 +1,51 @@
+import { GraphDataLabel } from '@/page-layout/widgets/graph/components/GraphDataLabel';
+import { computeLineChartGroupedLabels } from '@/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartGroupedLabels';
+import { computeLineChartStackedLabels } from '@/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartStackedLabels';
+import { type LineCustomSvgLayerProps, type LineSeries } from '@nivo/line';
+
+type CustomPointLabelsLayerProps = Pick<
+ LineCustomSvgLayerProps<LineSeries>,
+ 'points'
+> & {
+ formatValue?: (value: number) => string;
+ offset?: number;
+ groupMode?: 'stacked';
+ omitNullValues?: boolean;
+ enablePointLabel: boolean;
+};
+
+export const CustomPointLabelsLayer = ({
+ points,
+ formatValue,
+ offset = 0,
+ groupMode,
+ omitNullValues = false,
+ enablePointLabel,
+}: CustomPointLabelsLayerProps) => {
+ if (!enablePointLabel) {
+ return null;
+ }
+
+ const lineChartLabels =
+ groupMode === 'stacked'
+ ? computeLineChartStackedLabels(points)
+ : computeLineChartGroupedLabels(points);
+
+ const lineChartLabelsToRender = omitNullValues
+ ? lineChartLabels.filter((label) => label.value !== 0)
+ : lineChartLabels;
+
+ return (
+ <>
+ {lineChartLabelsToRender.map((lineChartLabel) => (
+ <GraphDataLabel
+ key={lineChartLabel.key}
+ label={lineChartLabel}
+ formatValue={formatValue}
+ offset={offset}
+ isVerticalLayout={true}
+ />
+ ))}
+ </>
+ );
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/components/GraphWidgetLineChart.tsx b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/components/GraphWidgetLineChart.tsx
index 780e635ac63..43b5b799763 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/components/GraphWidgetLineChart.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/components/GraphWidgetLineChart.tsx
@@ -5,6 +5,7 @@ import {
CustomCrosshairLayer,
type SliceHoverData,
} from '@/page-layout/widgets/graph/graphWidgetLineChart/components/CustomCrosshairLayer';
+import { CustomPointLabelsLayer } from '@/page-layout/widgets/graph/graphWidgetLineChart/components/CustomPointLabelsLayer';
import { GraphLineChartTooltip } from '@/page-layout/widgets/graph/graphWidgetLineChart/components/GraphLineChartTooltip';
import { LINE_CHART_MARGIN_BOTTOM } from '@/page-layout/widgets/graph/graphWidgetLineChart/constants/LineChartMarginBottom';
import { LINE_CHART_MARGIN_LEFT } from '@/page-layout/widgets/graph/graphWidgetLineChart/constants/LineChartMarginLeft';
@@ -37,6 +38,7 @@ import { useCallback, useId, useRef, useState } from 'react';
import { useDebouncedCallback } from 'use-debounce';
type CrosshairLayerProps = LineCustomSvgLayerProps<LineSeries>;
+type PointLabelsLayerProps = LineCustomSvgLayerProps<LineSeries>;
type GraphWidgetLineChartProps = {
data: LineChartSeries[];
@@ -172,6 +174,17 @@ export const GraphWidgetLineChart = ({
[dataMap, debouncedHideTooltip, setActiveLineTooltip, setCrosshairX],
);
+ const PointLabelsLayer = (layerProps: PointLabelsLayerProps) => (
+ <CustomPointLabelsLayer
+ points={layerProps.points}
+ formatValue={(value) => formatGraphValue(value, formatOptions)}
+ offset={theme.spacingMultiplicator * 2}
+ groupMode={groupMode}
+ omitNullValues={_omitNullValues}
+ enablePointLabel={enablePointLabel}
+ />
+ );
+
const CrosshairLayer = (layerProps: CrosshairLayerProps) => (
<CustomCrosshairLayer
key="custom-crosshair-layer"
@@ -182,6 +195,7 @@ export const GraphWidgetLineChart = ({
onRectLeave={() => debouncedHideTooltip()}
/>
);
+
const axisBottomConfig = getLineChartAxisBottomConfig(
xAxisLabel,
chartWidth,
@@ -222,16 +236,13 @@ export const GraphWidgetLineChart = ({
clamp: true,
}}
curve={'monotoneX'}
- lineWidth={2}
+ lineWidth={1}
enableArea={enableArea}
areaBaselineValue={0}
enablePoints={true}
pointSize={0}
- enablePointLabel={enablePointLabel}
+ enablePointLabel={false}
pointBorderWidth={0}
- pointLabel={(point) =>
- formatGraphValue(Number(point.data.y), formatOptions)
- }
colors={colors}
areaBlendMode={'normal'}
defs={defs}
@@ -253,6 +264,7 @@ export const GraphWidgetLineChart = ({
'lines',
CrosshairLayer,
'points',
+ PointLabelsLayer,
'legends',
]}
useMesh={true}
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/__tests__/computeLineChartStackedLabels.test.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/__tests__/computeLineChartStackedLabels.test.ts
new file mode 100644
index 00000000000..a20798bd61b
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/__tests__/computeLineChartStackedLabels.test.ts
@@ -0,0 +1,258 @@
+import { type LineSeries, type Point } from '@nivo/line';
+import { computeLineChartStackedLabels } from '../computeLineChartStackedLabels';
+
+const createMockPoint = (
+ seriesId: string,
+ dataX: string | number,
+ dataY: number,
+ pixelX: number,
+ pixelY: number,
+): Point<LineSeries> =>
+ ({
+ seriesId,
+ data: { x: dataX, y: dataY },
+ x: pixelX,
+ y: pixelY,
+ }) as Point<LineSeries>;
+
+describe('computeLineChartStackedLabels (essential cases)', () => {
+ it('returns total for single x-value with all positive values', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', 100, 50, 200),
+ createMockPoint('series2', 'Jan', 50, 50, 250),
+ createMockPoint('series3', 'Jan', 25, 50, 275),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: 175,
+ x: 50,
+ y: 200,
+ shouldRenderBelow: false,
+ },
+ ]);
+ });
+
+ it('returns total for single x-value with all negative values', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', -100, 50, 400),
+ createMockPoint('series2', 'Jan', -50, 50, 350),
+ createMockPoint('series3', 'Jan', -25, 50, 325),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: -175,
+ x: 50,
+ y: 400,
+ shouldRenderBelow: true,
+ },
+ ]);
+ });
+
+ it('returns total for single x-value with mixed positive/negative (net positive)', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', 100, 50, 200),
+ createMockPoint('series2', 'Jan', -30, 50, 270),
+ createMockPoint('series3', 'Jan', 20, 50, 230),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: 90,
+ x: 50,
+ y: 200,
+ shouldRenderBelow: false,
+ },
+ ]);
+ });
+
+ it('returns total for single x-value with mixed positive/negative (net negative)', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', -100, 50, 400),
+ createMockPoint('series2', 'Jan', 30, 50, 270),
+ createMockPoint('series3', 'Jan', 20, 50, 280),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: -50,
+ x: 50,
+ y: 400,
+ shouldRenderBelow: true,
+ },
+ ]);
+ });
+
+ it('returns multiple totals for multiple x-values', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', 100, 50, 200),
+ createMockPoint('series2', 'Jan', 50, 50, 250),
+ createMockPoint('series1', 'Feb', 75, 150, 225),
+ createMockPoint('series2', 'Feb', 25, 150, 275),
+ createMockPoint('series1', 'Mar', -50, 250, 350),
+ createMockPoint('series2', 'Mar', -30, 250, 330),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toHaveLength(3);
+ expect(result).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ key: 'total-Jan',
+ value: 150,
+ shouldRenderBelow: false,
+ }),
+ expect.objectContaining({
+ key: 'total-Feb',
+ value: 100,
+ shouldRenderBelow: false,
+ }),
+ expect.objectContaining({
+ key: 'total-Mar',
+ value: -80,
+ shouldRenderBelow: true,
+ }),
+ ]),
+ );
+ });
+
+ it('handles single point per x-value (no stacking)', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', 100, 50, 200),
+ createMockPoint('series1', 'Feb', 150, 150, 150),
+ createMockPoint('series1', 'Mar', -50, 250, 350),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: 100,
+ x: 50,
+ y: 200,
+ shouldRenderBelow: false,
+ },
+ {
+ key: 'total-Feb',
+ value: 150,
+ x: 150,
+ y: 150,
+ shouldRenderBelow: false,
+ },
+ {
+ key: 'total-Mar',
+ value: -50,
+ x: 250,
+ y: 350,
+ shouldRenderBelow: true,
+ },
+ ]);
+ });
+
+ it('handles zero values correctly', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', 100, 50, 200),
+ createMockPoint('series2', 'Jan', 0, 50, 300),
+ createMockPoint('series3', 'Jan', -100, 50, 400),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: 0,
+ x: 50,
+ y: 200, // Net zero uses minimumYPosition (0 < 0 is false, so not negative)
+ shouldRenderBelow: false,
+ },
+ ]);
+ });
+
+ it('handles empty points array', () => {
+ const result = computeLineChartStackedLabels([]);
+ expect(result).toEqual([]);
+ });
+
+ it('handles numeric x-values (not just strings)', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 2024, 100, 50, 200),
+ createMockPoint('series2', 2024, 50, 50, 250),
+ createMockPoint('series1', 2025, 75, 150, 225),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-2024',
+ value: 150,
+ x: 50,
+ y: 200,
+ shouldRenderBelow: false,
+ },
+ {
+ key: 'total-2025',
+ value: 75,
+ x: 150,
+ y: 225,
+ shouldRenderBelow: false,
+ },
+ ]);
+ });
+
+ it('tracks minimum and maximum Y positions correctly across multiple series', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', 50, 100, 250),
+ createMockPoint('series2', 'Jan', 100, 100, 150),
+ createMockPoint('series3', 'Jan', 25, 100, 325),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: 175,
+ x: 100,
+ y: 150,
+ shouldRenderBelow: false,
+ },
+ ]);
+ });
+
+ it('uses maximum Y position for negative totals', () => {
+ const points: Point<LineSeries>[] = [
+ createMockPoint('series1', 'Jan', -50, 100, 350),
+ createMockPoint('series2', 'Jan', -100, 100, 400),
+ createMockPoint('series3', 'Jan', -25, 100, 325),
+ ];
+
+ const result = computeLineChartStackedLabels(points);
+
+ expect(result).toEqual([
+ {
+ key: 'total-Jan',
+ value: -175,
+ x: 100,
+ y: 400,
+ shouldRenderBelow: true,
+ },
+ ]);
+ });
+});
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartGroupedLabels.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartGroupedLabels.ts
new file mode 100644
index 00000000000..023ea1a995a
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartGroupedLabels.ts
@@ -0,0 +1,19 @@
+import { type GraphLabelData } from '@/page-layout/widgets/graph/types/GraphLabelData';
+import { type LineSeries, type Point } from '@nivo/line';
+
+export const computeLineChartGroupedLabels = (
+ points: readonly Point<LineSeries>[],
+): GraphLabelData[] => {
+ return points.map((point) => {
+ const value = Number(point.data.y);
+ const shouldRenderBelow = value < 0;
+
+ return {
+ key: `value-${point.seriesId}-${point.data.x}`,
+ value,
+ x: point.x,
+ y: point.y,
+ shouldRenderBelow,
+ };
+ });
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartStackedLabels.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartStackedLabels.ts
new file mode 100644
index 00000000000..141ef5c91d1
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/graphWidgetLineChart/utils/computeLineChartStackedLabels.ts
@@ -0,0 +1,62 @@
+import { type GraphLabelData } from '@/page-layout/widgets/graph/types/GraphLabelData';
+import { type LineSeries, type Point } from '@nivo/line';
+import { isDefined } from 'twenty-shared/utils';
+
+export const computeLineChartStackedLabels = (
+ points: readonly Point<LineSeries>[],
+): GraphLabelData[] => {
+ const stackData = new Map<
+ string,
+ {
+ total: number;
+ minimumYPosition: number;
+ maximumYPosition: number;
+ xPosition: number;
+ }
+ >();
+
+ for (const point of points) {
+ const groupKey = String(point.data.x);
+ const existingGroup = stackData.get(groupKey);
+
+ if (isDefined(existingGroup)) {
+ existingGroup.total += Number(point.data.y);
+ existingGroup.minimumYPosition = Math.min(
+ existingGroup.minimumYPosition,
+ point.y,
+ );
+ existingGroup.maximumYPosition = Math.max(
+ existingGroup.maximumYPosition,
+ point.y,
+ );
+ } else {
+ stackData.set(groupKey, {
+ total: Number(point.data.y),
+ minimumYPosition: point.y,
+ maximumYPosition: point.y,
+ xPosition: point.x,
+ });
+ }
+ }
+
+ const groupedEntries = Array.from(stackData.entries());
+
+ const labels: GraphLabelData[] = groupedEntries.map(
+ ([groupKey, { total, minimumYPosition, maximumYPosition, xPosition }]) => {
+ const isNegativeTotal = total < 0;
+ const labelYPosition = isNegativeTotal
+ ? maximumYPosition
+ : minimumYPosition;
+
+ return {
+ key: `total-${groupKey}`,
+ value: total,
+ x: xPosition,
+ y: labelYPosition,
+ shouldRenderBelow: isNegativeTotal,
+ };
+ },
+ );
+
+ return labels;
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/types/GraphLabelData.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/types/GraphLabelData.ts
new file mode 100644
index 00000000000..d047e942b5d
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/types/GraphLabelData.ts
@@ -0,0 +1,7 @@
+export type GraphLabelData = {
+ key: string;
+ value: number;
+ x: number;
+ y: number;
+ shouldRenderBelow: boolean;
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/calculateGraphLabelStyles.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/calculateGraphLabelStyles.ts
new file mode 100644
index 00000000000..cd510563520
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/calculateGraphLabelStyles.ts
@@ -0,0 +1,46 @@
+import { type GraphLabelData } from '@/page-layout/widgets/graph/types/GraphLabelData';
+
+type GraphLabelStyles = {
+ x: number;
+ y: number;
+ textAnchor: 'start' | 'middle' | 'end';
+ dominantBaseline: 'auto' | 'hanging' | 'central';
+ transformOffset: string;
+};
+
+export const calculateGraphLabelStyles = (
+ label: GraphLabelData,
+ offset: number,
+ isVerticalLayout: boolean,
+): GraphLabelStyles => {
+ const offsetSign = isVerticalLayout
+ ? label.shouldRenderBelow
+ ? 1
+ : -1
+ : label.shouldRenderBelow
+ ? -1
+ : 1;
+
+ const axis = isVerticalLayout ? 'Y' : 'X';
+ const transformOffset = `translate${axis}(${offsetSign * offset}px)`;
+
+ const textAnchor = isVerticalLayout
+ ? 'middle'
+ : label.shouldRenderBelow
+ ? 'end'
+ : 'start';
+
+ const dominantBaseline = isVerticalLayout
+ ? label.shouldRenderBelow
+ ? 'hanging'
+ : 'auto'
+ : 'central';
+
+ return {
+ x: label.x,
+ y: label.y,
+ textAnchor,
+ dominantBaseline,
+ transformOffset,
+ };
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/sortLineChartDataPoints.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/sortLineChartDataPoints.ts
deleted file mode 100644
index a0e1c49c20e..00000000000
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/sortLineChartDataPoints.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { type LineChartDataPoint } from '@/page-layout/widgets/graph/graphWidgetLineChart/types/LineChartDataPoint';
-import { GraphOrderBy } from '~/generated/graphql';
-
-const normalizeXValue = (x: number | string | Date): string => {
- if (x instanceof Date) {
- return x.toISOString();
- }
- return String(x);
-};
-
-export const sortLineChartDataPoints = ({
- dataPoints,
- orderBy,
-}: {
- dataPoints: LineChartDataPoint[];
- orderBy?: GraphOrderBy | null;
-}): LineChartDataPoint[] => {
- switch (orderBy) {
- case GraphOrderBy.FIELD_ASC:
- return [...dataPoints].sort((a, b) =>
- normalizeXValue(a.x).localeCompare(normalizeXValue(b.x)),
- );
- case GraphOrderBy.FIELD_DESC:
- return [...dataPoints].sort((a, b) =>
- normalizeXValue(b.x).localeCompare(normalizeXValue(a.x)),
- );
- default:
- return dataPoints;
- }
-};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformOneDimensionalGroupByToLineChartData.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformOneDimensionalGroupByToLineChartData.ts
index 27b0918c891..80baa3cbc39 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformOneDimensionalGroupByToLineChartData.ts
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformOneDimensionalGroupByToLineChartData.ts
@@ -9,7 +9,6 @@ import { type GraphColor } from '@/page-layout/widgets/graph/types/GraphColor';
import { type GroupByRawResult } from '@/page-layout/widgets/graph/types/GroupByRawResult';
import { computeAggregateValueFromGroupByResult } from '@/page-layout/widgets/graph/utils/computeAggregateValueFromGroupByResult';
import { formatDimensionValue } from '@/page-layout/widgets/graph/utils/formatDimensionValue';
-import { sortLineChartDataPoints } from '@/page-layout/widgets/graph/utils/sortLineChartDataPoints';
import { isDefined } from 'twenty-shared/utils';
import { type LineChartConfiguration } from '~/generated/graphql';
@@ -43,7 +42,7 @@ export const transformOneDimensionalGroupByToLineChartData = ({
LINE_CHART_MAXIMUM_NUMBER_OF_DATA_POINTS,
);
- const unsortedData: LineChartDataPoint[] = limitedResults
+ const data: LineChartDataPoint[] = limitedResults
.map((result) => {
const dimensionValues = result.groupByDimensionValues;
@@ -78,11 +77,6 @@ export const transformOneDimensionalGroupByToLineChartData = ({
})
.filter((point) => isDefined(point));
- const data = sortLineChartDataPoints({
- dataPoints: unsortedData,
- orderBy: configuration.primaryAxisOrderBy,
- });
-
const series: LineChartSeries[] = [
{
id: aggregateField.name,
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformTwoDimensionalGroupByToLineChartData.ts b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformTwoDimensionalGroupByToLineChartData.ts
index deecc66a372..bdb292274ca 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformTwoDimensionalGroupByToLineChartData.ts
+++ b/packages/twenty-front/src/modules/page-layout/widgets/graph/utils/transformTwoDimensionalGroupByToLineChartData.ts
@@ -8,7 +8,6 @@ import { type GraphColor } from '@/page-layout/widgets/graph/types/GraphColor';
import { type GroupByRawResult } from '@/page-layout/widgets/graph/types/GroupByRawResult';
import { computeAggregateValueFromGroupByResult } from '@/page-layout/widgets/graph/utils/computeAggregateValueFromGroupByResult';
import { formatDimensionValue } from '@/page-layout/widgets/graph/utils/formatDimensionValue';
-import { sortLineChartDataPoints } from '@/page-layout/widgets/graph/utils/sortLineChartDataPoints';
import { sortLineChartSeries } from '@/page-layout/widgets/graph/utils/sortLineChartSeries';
import { isDefined } from 'twenty-shared/utils';
import { type LineChartConfiguration } from '~/generated/graphql';
@@ -99,16 +98,11 @@ export const transformTwoDimensionalGroupByToLineChartData = ({
const unsortedSeries: LineChartSeries[] = Array.from(seriesMap.entries()).map(
([seriesKey, xToYMap]) => {
- const unsortedData: LineChartDataPoint[] = allXValues.map((xValue) => ({
+ const data: LineChartDataPoint[] = allXValues.map((xValue) => ({
x: xValue,
y: xToYMap.get(xValue) ?? 0,
}));
- const data = sortLineChartDataPoints({
- dataPoints: unsortedData,
- orderBy: configuration.primaryAxisOrderBy,
- });
-
return {
id: seriesKey,
label: seriesKey,