@@ -307,7 +307,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
307307 */
308308 const setData = useCallback ( ( newData : number [ ] [ ] ) : void => {
309309 if ( isWeb ) {
310- uplotInstance . current ?. setData ( newData ) ;
310+ dataRef . current = newData ;
311+ uplotInstance . current ?. setData ( dataRef . current ) ;
311312 } else {
312313 if ( ! webref ?. current ) {
313314 console . error ( 'WebView reference is not set' ) ;
@@ -338,6 +339,13 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
338339 */
339340 const pushData = useCallback ( ( item : number [ ] ) : void => {
340341 if ( isWeb ) {
342+ if ( ! dataRef . current || dataRef . current . length !== item . length ) {
343+ dataRef . current = [ ] ;
344+ for ( let i = 0 ; i < item . length ; i ++ ) {
345+ dataRef . current . push ( [ ] ) ;
346+ }
347+ }
348+
341349 for ( let i = 0 ; i < item . length ; i ++ ) {
342350 dataRef . current [ i ] . push ( item [ i ] ) ;
343351 }
@@ -352,7 +360,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
352360 webref . current . injectJavaScript ( `
353361 var item = ${ JSON . stringify ( item ) } ;
354362
355- if (! window._data) {
363+ if (window._data === undefined || window._data == null || window._data.length !== item.length ) {
356364 window._data = [];
357365 for (let i = 0; i < item.length; i++) {
358366 window._data.push([]);
@@ -493,30 +501,35 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
493501 const destroy = useCallback ( ( ) : void => {
494502 if ( isWeb ) {
495503 uplotInstance . current ?. destroy ( ) ;
504+ dataRef . current = [ ] ;
496505 } else {
497506 if ( ! webref ?. current ) {
498507 console . error ( 'WebView reference is not set' ) ;
499508 return ;
500509 }
501510
502511 webref . current . injectJavaScript ( `
512+ window._data = []
503513
504- // destroy data
505- if (window._data) {
506- window._data = [];
507- }
508-
509- // destroy chart
510514 if (window._chart) {
511- window._chart.destroy();true;
512- } else {
513- console.error('Chart not initialized');
515+ window._chart.destroy();
516+ window.__CHART_CREATED__ = false;
514517 }
515518 true;
516519 ` ) ;
517520 }
521+ initialized . current = false ;
518522 } , [ ] ) ;
519523
524+ // destroy, clear data, and reinitialize the chart when the component unmounts
525+ const reset = useCallback (
526+ ( opts : any , data : number [ ] [ ] , bgColor ?: string ) : void => {
527+ destroy ( ) ;
528+ createChart ( opts , data , bgColor ) ;
529+ } ,
530+ [ ] ,
531+ ) ;
532+
520533 useImperativeHandle ( ref , ( ) => ( {
521534 createChart,
522535 updateOptions,
@@ -527,6 +540,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
527540 setVariable,
528541 setSize,
529542 destroy,
543+ reset,
530544 } ) ) ;
531545
532546 if ( Platform . OS === 'web' ) {
0 commit comments