Skip to content

Commit 849d568

Browse files
Copilothotlong
andcommitted
fix: ensure chart data is always an array to prevent r.slice crash
Add Array.isArray() guards at three layers: - ObjectChart.tsx: validate finalData before passing to ChartRenderer - ChartRenderer.tsx: validate schema.data in useMemo adapter - AdvancedChartImpl.tsx: validate data prop before passing to Recharts When boundData (from useDataScope) or schema.data is a non-array truthy value (e.g. an object), Recharts internally calls .slice() on it, causing "r.slice is not a function" runtime error. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent c45d73a commit 849d568

3 files changed

Lines changed: 5 additions & 3 deletions

File tree

packages/plugin-charts/src/AdvancedChartImpl.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ export interface AdvancedChartImplProps {
8484
*/
8585
export default function AdvancedChartImpl({
8686
chartType = 'bar',
87-
data = [],
87+
data: rawData = [],
8888
config = {},
8989
xAxisKey = 'name',
9090
series = [],
9191
className = '',
9292
}: AdvancedChartImplProps) {
93+
const data = Array.isArray(rawData) ? rawData : [];
9394
const [isMobile, setIsMobile] = React.useState(false);
9495

9596
React.useEffect(() => {

packages/plugin-charts/src/ChartRenderer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const ChartRenderer: React.FC<ChartRendererProps> = ({ schema }) => {
8989

9090
return {
9191
chartType: schema.chartType,
92-
data: schema.data,
92+
data: Array.isArray(schema.data) ? schema.data : [],
9393
config,
9494
xAxisKey,
9595
series,

packages/plugin-charts/src/ObjectChart.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ export const ObjectChart = (props: any) => {
9898
return () => { isMounted = false; };
9999
}, [schema.objectName, dataSource, boundData, schema.data, schema.filter, schema.aggregate]);
100100

101-
const finalData = boundData || schema.data || fetchedData || [];
101+
const rawData = boundData || schema.data || fetchedData;
102+
const finalData = Array.isArray(rawData) ? rawData : [];
102103

103104
// Merge data if not provided in schema
104105
const finalSchema = {

0 commit comments

Comments
 (0)