OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix(charts): keep breakdown values in position when one is empty
Carl-Gerhard Lindesvärdcffd4f59b31e
diff --git a/packages/core/src/shared/group-by-labels.test.ts b/packages/core/src/shared/group-by-labels.test.tsnew file mode 100644index 000000000..bdd299928--- /dev/null+++ b/packages/core/src/shared/group-by-labels.test.ts@@ -0,0 +1,56 @@+import { expect, test } from 'bun:test';+import { groupByLabels } from './group-by-labels';++const DATE = '2026-09-01 00:00:00';++test('an empty breakdown value before a set one keeps its position', () => {+ const groups = groupByLabels([+ {+ label_0: 'screen_view',+ label_1: '',+ label_2: 'AU',+ count: 3,+ date: DATE,+ },+ {+ label_0: 'screen_view',+ label_1: 'AU',+ label_2: '',+ count: 5,+ date: DATE,+ },+ ]);++ expect(groups.map((group) => group.name)).toEqual([+ ['screen_view', '', 'AU'],+ ['screen_view', 'AU'],+ ]);+ expect(groups.map((group) => group.data[0]?.count)).toEqual([3, 5]);+});++test('unset trailing breakdown values are dropped from the name', () => {+ const groups = groupByLabels([+ {+ label_0: 'screen_view',+ label_1: null,+ label_2: '',+ count: 1,+ date: DATE,+ },+ ]);++ expect(groups.map((group) => group.name)).toEqual([['screen_view']]);+});++test('a fill row without an event label forms no group but pads the dates', () => {+ const groups = groupByLabels([+ { label_0: 'screen_view', label_1: 'SE', count: 2, date: DATE },+ { label_0: '', count: 0, date: '2026-09-02 00:00:00' },+ ]);++ expect(groups).toHaveLength(1);+ expect(groups[0]?.data.map((point) => point.date)).toEqual([+ DATE,+ '2026-09-02 00:00:00',+ ]);+});diff --git a/packages/core/src/shared/group-by-labels.ts b/packages/core/src/shared/group-by-labels.tsindex cc2dc9ad4..b6d3ea293 100644--- a/packages/core/src/shared/group-by-labels.ts+++ b/packages/core/src/shared/group-by-labels.ts@@ -19,20 +19,32 @@ interface GroupedResult {data: GroupedDataPoint[];}+const LABEL_PREFIX = 'label_';+const DECIMAL_RADIX = 10;++function labelIndex(key: string): number {+ return Number.parseInt(key.slice(LABEL_PREFIX.length), DECIMAL_RADIX);+}++// An unset value keeps its slot as '' while a later label is set, so name[i]+// is always label_i and the chart engine can map it back to breakdown i.+function rowLabels(row: ISerieDataItem): string[] {+ const labels = Object.entries(row)+ .filter(([key]) => key.startsWith(LABEL_PREFIX))+ .sort(([a], [b]) => labelIndex(a) - labelIndex(b))+ .map(([, value]) => (typeof value === 'string' ? value : ''));+ while (labels.length > 0 && labels.at(-1) === '') {+ labels.pop();+ }+ return labels;+}+export function groupByLabels(data: ISerieDataItem[]): GroupedResult[] {const groupedMap = new Map<string, GroupedResult>();const timestamps = new Set<string>();data.forEach((row) => {timestamps.add(row.date);- const labels = Object.keys(row)- .filter((key) => key.startsWith('label_'))- .sort((a, b) => {- const numA = Number.parseInt(a.replace('label_', ''));- const numB = Number.parseInt(b.replace('label_', ''));- return numA - numB;- })- .map((key) => (row as any)[key])- .filter((label): label is string => !!label);+ const labels = rowLabels(row);const labelKey = labels.join(':::');