@@ -89,38 +89,45 @@ const InlineChartComponent = ({ node, updateAttributes, deleteNode }: any) => {
8989 return { mean } ;
9090 } ;
9191
92- // Sanitized numeric data specifically for Scatter plots
93- const sanitizedScatterData = useMemo ( ( ) => {
94- if ( type !== 'scatter' || ! data ) return data ;
95- return data . map ( ( d : any ) => {
96- const sanitized : any = { ...d } ;
97- // Force X axis to number
98- sanitized [ xAxis ] = Number ( d [ xAxis ] ) ;
99- // Force all active Y axes to numbers
100- activeYAxes . forEach ( ( y : string ) => {
101- sanitized [ y ] = Number ( d [ y ] ) ;
102- } ) ;
103- return sanitized ;
104- } ) . filter ( ( d : any ) => ! isNaN ( d [ xAxis ] ) ) ;
105- } , [ data , type , xAxis , activeYAxes ] ) ;
92+ // Detect if X-axis column is primarily numeric
93+ const isXNumeric = useMemo ( ( ) => {
94+ if ( ! data || data . length === 0 ) return false ;
95+ // Sample a few values to see if they are numbers
96+ const samples = data . slice ( 0 , 5 ) . map ( ( d : any ) => d [ xAxis ] ) ;
97+ return samples . every ( ( v : any ) => ! isNaN ( parseFloat ( v ) ) && isFinite ( v ) ) ;
98+ } , [ data , xAxis ] ) ;
10699
107- // Augmented data with trend lines
100+ // Unified augmented data (Sanitization + Trend Lines)
108101 const augmentedData = useMemo ( ( ) => {
109- const sourceData = type === 'scatter' ? sanitizedScatterData : data ;
110- if ( ! showTrendLine || ! sourceData ) return sourceData ;
102+ if ( ! data ) return [ ] ;
111103
112- let result = [ ... sourceData ] ;
113- activeYAxes . forEach ( ( y : string ) => {
114- const trend = getRegressionLine ( y ) ;
115- if ( trend ) {
116- result = result . map ( ( d : any , i : number ) => ( {
117- ... d ,
118- ... trend [ i ]
119- } ) ) ;
104+ // 1. Initial sanitization: ensure numeric Y values and potentially numeric X values
105+ let result = data . map ( ( d : any ) => {
106+ const sanitized : any = { ... d } ;
107+ activeYAxes . forEach ( ( y : string ) => {
108+ sanitized [ y ] = parseFloat ( d [ y ] ) ;
109+ } ) ;
110+ if ( isXNumeric ) {
111+ sanitized [ xAxis ] = parseFloat ( d [ xAxis ] ) ;
120112 }
113+ return sanitized ;
121114 } ) ;
115+
116+ // 2. Add Trend Lines if needed
117+ if ( showTrendLine ) {
118+ activeYAxes . forEach ( ( y : string ) => {
119+ const trend = getRegressionLine ( y ) ;
120+ if ( trend ) {
121+ result = result . map ( ( d : any , i : number ) => ( {
122+ ...d ,
123+ ...trend [ i ]
124+ } ) ) ;
125+ }
126+ } ) ;
127+ }
128+
122129 return result ;
123- } , [ data , sanitizedScatterData , activeYAxes , showTrendLine , type ] ) ;
130+ } , [ data , activeYAxes , showTrendLine , type , xAxis , isXNumeric ] ) ;
124131
125132 return (
126133 < NodeViewWrapper ref = { chartRef } className = "inline-chart-wrapper my-8 group relative" >
@@ -336,7 +343,17 @@ const InlineChartComponent = ({ node, updateAttributes, deleteNode }: any) => {
336343 ) : type === 'scatter' ? (
337344 < ScatterChart width = { 1000 } height = { 400 } margin = { { top : 20 , right : 40 , left : 10 , bottom : 20 } } >
338345 < CartesianGrid strokeDasharray = "3 3" stroke = { gridColor } />
339- < XAxis type = "number" dataKey = { xAxis } name = { xAxis } stroke = { textColor } fontSize = { 14 } tickLine = { false } axisLine = { false } tick = { { fill : textColor } } domain = { [ 'auto' , 'auto' ] } />
346+ < XAxis
347+ type = { isXNumeric ? "number" : "category" }
348+ dataKey = { xAxis }
349+ name = { xAxis }
350+ stroke = { textColor }
351+ fontSize = { 14 }
352+ tickLine = { false }
353+ axisLine = { false }
354+ tick = { { fill : textColor } }
355+ domain = { isXNumeric ? [ 'auto' , 'auto' ] : undefined }
356+ />
340357 < YAxis type = "number" stroke = { textColor } fontSize = { 14 } tickLine = { false } axisLine = { false } tick = { { fill : textColor } } domain = { [ 'auto' , 'auto' ] } />
341358 < ZAxis type = "number" range = { [ 64 , 64 ] } />
342359 { activeYAxes . length > 1 && (
@@ -347,17 +364,17 @@ const InlineChartComponent = ({ node, updateAttributes, deleteNode }: any) => {
347364 < Scatter
348365 key = { y }
349366 name = { y }
350- data = { augmentedData . map ( ( p : any ) => ( { ... p , [ xAxis ] : Number ( p [ xAxis ] ) } ) ) } // Ensure X is numeric for scatter
351- dataKey = { y } // EXPLICIT Y KEY
367+ data = { augmentedData }
368+ dataKey = { y }
352369 fill = { getSeriesColor ( index ) }
353370 isAnimationActive = { false }
354371 />
355372 { showTrendLine && (
356373 < Scatter
357374 key = { `${ y } _trend` }
358375 name = { `${ y } Trend` }
359- data = { augmentedData . map ( ( p : any ) => ( { ... p , [ xAxis ] : Number ( p [ xAxis ] ) } ) ) }
360- dataKey = { `${ y } _trend` } // USE CALCULATED TREND KEY
376+ data = { augmentedData }
377+ dataKey = { `${ y } _trend` }
361378 fill = { getSeriesColor ( index ) }
362379 isAnimationActive = { false }
363380 line = { { stroke : getSeriesColor ( index ) , strokeWidth : 2 , strokeDasharray : '5 5' } }
@@ -455,14 +472,15 @@ const InlineChartComponent = ({ node, updateAttributes, deleteNode }: any) => {
455472 < ScatterChart margin = { { top : 10 , right : 10 , left : - 20 , bottom : 0 } } >
456473 < CartesianGrid strokeDasharray = "3 3" stroke = { gridColor } />
457474 < XAxis
458- type = "number"
475+ type = { isXNumeric ? "number" : "category" }
459476 dataKey = { xAxis }
460477 name = { xAxis }
461478 stroke = { textColor }
462479 fontSize = { 10 }
463480 tickLine = { false }
464481 axisLine = { false }
465482 tick = { { fill : textColor } }
483+ domain = { isXNumeric ? [ 'auto' , 'auto' ] : undefined }
466484 />
467485 < YAxis
468486 type = "number"
@@ -471,6 +489,7 @@ const InlineChartComponent = ({ node, updateAttributes, deleteNode }: any) => {
471489 tickLine = { false }
472490 axisLine = { false }
473491 tick = { { fill : textColor } }
492+ domain = { [ 'auto' , 'auto' ] }
474493 />
475494 < ZAxis type = "number" range = { [ 40 , 40 ] } />
476495 < Tooltip
@@ -498,15 +517,15 @@ const InlineChartComponent = ({ node, updateAttributes, deleteNode }: any) => {
498517 key = { y }
499518 name = { y }
500519 data = { augmentedData }
501- dataKey = { y } // EXPLICIT Y KEY
520+ dataKey = { y }
502521 fill = { getSeriesColor ( index ) }
503522 />
504523 { showTrendLine && (
505524 < Scatter
506525 key = { `${ y } _trend` }
507526 name = { `${ y } Trend` }
508527 data = { augmentedData }
509- dataKey = { `${ y } _trend` } // USE CALCULATED TREND KEY
528+ dataKey = { `${ y } _trend` }
510529 fill = { getSeriesColor ( index ) }
511530 line = { { stroke : getSeriesColor ( index ) , strokeWidth : 2 , strokeDasharray : '5 5' } }
512531 shape = { ( ) => null }
0 commit comments