Skip to content

Commit 651c0bb

Browse files
LG-5588 Fix array field updates in chart options merge (#3351)
* feat: correctly merge array-fields (choose full override, no recursive) * docs
1 parent fbe44ca commit 651c0bb

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

.changeset/calm-buckets-admire.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@lg-charts/core': patch
3+
---
4+
5+
Ensure array fields like `labels` in `<XAxis>` are updated as expected in the chart options.
6+
This update changes the recursive merge function in the EChart options utility to treat arrays as atoms: instead of merging their elements, it now replaces arrays entirely. Previously, arrays were being merged like objects, resulting in arrays becoming plain objects with numeric keys. With this fix, when merging, any existing array field (such as `labels` on `<XAxis>`) is now overwritten by the new array rather than partially merged.

charts/core/src/Echart/utils/updateUtils.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,28 @@ describe('@lg-charts/core/Chart/hooks/updateUtils', () => {
5353
test('updateOptions should merge chart options non-destructively', () => {
5454
const currentOptions: Partial<EChartOptions> = {
5555
xAxis: {
56+
type: 'category',
5657
show: true,
58+
data: ['series1', 'series2', 'series3'],
5759
splitLine: {
5860
show: true,
5961
},
6062
},
6163
};
6264
const updatedOptions = updateOptions(currentOptions, {
6365
xAxis: {
66+
type: 'category',
6467
show: false, // This should only update the show property and not other properties
68+
data: ['series4', 'series5'],
6569
},
6670
grid: {
6771
show: true,
6872
},
6973
});
7074
// @ts-ignore: Property 'show' does not exist on type 'Arrayable<AriaOption>'.
7175
expect(updatedOptions?.xAxis?.show).toBe(false);
76+
// @ts-ignore: Property 'data' does not exist on type 'Arrayable<XAXisOption>'.
77+
expect(updatedOptions?.xAxis?.data).toEqual(['series4', 'series5']);
7278
// @ts-ignore: Property 'show' does not exist on type 'Arrayable<AriaOption>'.
7379
expect(updatedOptions?.xAxis?.splitLine?.show).toBe(true);
7480
// @ts-ignore: Property 'show' does not exist on type 'Arrayable<GridOption>'.

charts/core/src/Echart/utils/updateUtils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isPlainObject } from 'lodash';
2+
13
import { EChartOptions, EChartSeriesOption } from '../Echart.types';
24

35
export function addSeries(
@@ -46,10 +48,7 @@ function recursiveMerge(
4648
const updatedObj = { ...target };
4749

4850
for (const key in source) {
49-
if (
50-
typeof source[key] === 'object' &&
51-
typeof updatedObj[key] === 'object'
52-
) {
51+
if (isPlainObject(source[key]) && isPlainObject(updatedObj[key])) {
5352
// Recursively update nested objects
5453
updatedObj[key] = recursiveMerge(updatedObj[key], source[key]);
5554
} else {

0 commit comments

Comments
 (0)