@@ -9,7 +9,8 @@ import { WebView } from 'react-native-webview';
99
1010var isWeb = Platform . OS === 'web' ;
1111
12- const html = require ( './uplot.html' ) ;
12+ // const html = require('./uplot.html');
13+ import html from './uplot.html' ;
1314import 'uplot/dist/uPlot.min.css' ;
1415
1516var uPlot : any = null ;
@@ -33,29 +34,62 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
3334 let webref : any = useRef ( null ) ;
3435 const uplotInstance = useRef < any > ( null ) ;
3536 const dataRef = useRef ( data ) ;
37+ const initialized = useRef ( false ) ;
38+
39+ const guardAndCreateJS = `
40+ (function() {
41+ if (window.__CHART_CREATED__) return;
42+ window.__CHART_CREATED__ = true;
43+
44+ // stash your data and options on window
45+ window._data = ${ JSON . stringify ( data ) } ;
46+ window._opts = ${ JSON . stringify ( {
47+ ...options ,
48+ width : style ?. width || width ,
49+ height : style ?. height || height ,
50+ } ) } ;
51+
52+ // now actually construct uPlot
53+ window._chart = new uPlot(window._opts, window._data, document.getElementById('chart'));
54+ })();
55+ true;
56+ ` ;
3657
3758 // eslint-disable-next-line @typescript-eslint/no-explicit-any
3859 const createChart = useCallback ( ( opts : any , data : number [ ] [ ] ) : void => {
3960 // Subtracting height of u-title and u-legend and some more?
4061 opts . height = style ?. height || 200 ;
4162 opts . width = style ?. width || width ;
4263
64+ if ( initialized . current ) {
65+ // If already initialized, just set the data
66+ // setData(data);
67+ return ;
68+ }
69+
4370 if ( isWeb ) {
4471 uplotInstance . current = new uPlot ( opts , data , webref ) ;
4572 } else {
4673 webref ?. injectJavaScript (
47- `let data = ${ JSON . stringify ( data ) } ; let uplot = new uPlot(${ JSON . stringify ( opts ) } , data, document.getElementById("chart"));true;` ,
74+ `
75+ window._data = ${ JSON . stringify ( data ) } ;
76+ window._opts = ${ JSON . stringify ( opts ) } ;
77+ window._chart = new uPlot(window._opts, window._data, document.getElementById("chart"));
78+ true;` ,
4879 ) ;
4980 }
81+ initialized . current = true ;
5082 } , [ ] ) ;
5183
5284 const setData = useCallback ( ( newData : number [ ] [ ] ) : void => {
5385 if ( isWeb ) {
5486 uplotInstance . current ?. setData ( newData ) ;
5587 } else {
56- webref ?. injectJavaScript (
57- `uplot.setData(${ JSON . stringify ( newData ) } );true;` ,
58- ) ;
88+ webref ?. injectJavaScript ( `
89+ window._data = ${ JSON . stringify ( newData ) } ;
90+ window._chart.setData(window._data);
91+ true;
92+ ` ) ;
5993 }
6094 } , [ ] ) ;
6195
@@ -72,10 +106,18 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
72106 } else {
73107 webref ?. injectJavaScript ( `
74108 var item = ${ JSON . stringify ( item ) } ;
109+
110+ if (!window._data) {
111+ window._data = [];
112+ for (let i = 0; i < item.length; i++) {
113+ window._data.push([]);
114+ }
115+ }
116+
75117 for (let i = 0; i < item.length; i++) {
76- data [i].push(item[i]);
118+ window._data [i].push(item[i]);
77119 }
78- uplot. setData(data );true;` ) ;
120+ window._chart. setData(window._data );true;` ) ;
79121 }
80122 } , [ ] ) ;
81123
@@ -84,9 +126,9 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
84126 if ( isWeb ) {
85127 uplotInstance . current ?. setScale ( axis , options ) ;
86128 } else {
87- webref ?. injectJavaScript (
88- `uplot. setScale(${ JSON . stringify ( axis ) } , ${ JSON . stringify ( options ) } );true;` ,
89- ) ;
129+ webref ?. injectJavaScript ( `
130+ window._chart. setScale(${ JSON . stringify ( axis ) } , ${ JSON . stringify ( options ) } );true;
131+ ` ) ;
90132 }
91133 } , [ ] ) ;
92134
@@ -96,7 +138,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
96138 uplotInstance . current ?. setSize ( width , height ) ;
97139 } else {
98140 webref ?. injectJavaScript (
99- `uplot .setSize(${ JSON . stringify ( width ) } , ${ JSON . stringify ( height ) } );true;` ,
141+ `window._chart .setSize(${ JSON . stringify ( width ) } , ${ JSON . stringify ( height ) } );true;` ,
100142 ) ;
101143 }
102144 } , [ ] ) ;
@@ -106,7 +148,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
106148 if ( isWeb ) {
107149 uplotInstance . current ?. destroy ( ) ;
108150 } else {
109- webref ?. injectJavaScript ( 'uplot .destroy();true;' ) ;
151+ webref ?. injectJavaScript ( 'window._chart .destroy();true;' ) ;
110152 }
111153 } , [ ] ) ;
112154
@@ -145,22 +187,16 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
145187 } }
146188 scrollEnabled = { false }
147189 onLoadEnd = { ( ) : void => {
148- console . log ( 'WebView loaded' ) ;
149-
150- createChart ( options , data ) ;
190+ webref . injectJavaScript ( guardAndCreateJS ) ;
151191 } }
152- ref = { ( r ) : any => {
192+ ref = { ( r ) => {
153193 webref = r ;
154- if ( r ) {
155- console . log ( 'WebView ref set' ) ;
156-
157- createChart ( options , data ) ;
158- }
159194 } }
195+ javaScriptEnabled = { true }
160196 />
161197 ) ;
162198 }
163199 } ,
164200) ;
165201
166- export default ChartUPlot ;
202+ export default React . memo ( ChartUPlot ) ;
0 commit comments