Skip to content

Commit 03a3bd9

Browse files
committed
android fixes
1 parent ec5eedb commit 03a3bd9

2 files changed

Lines changed: 57 additions & 17 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uplot-react-native",
3-
"version": "0.1.12",
3+
"version": "0.1.13",
44
"description": "React Native wrapper for uPlot on web, iOS, and Android",
55
"homepage": "https://github.com/murphycj/uplot-react-native",
66
"bugs": {

src/components/ChartUPlot.tsx

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,16 @@ function getCreateChartString(
197197
options: any,
198198
bgColor: string = 'transparent',
199199
injectedJavaScript: string = '',
200+
name: string = 'uPlot',
200201
): string {
201202
// Prepare data assignment only if data is not null
202203
const dataAssignment =
203204
data !== null ? `window._data = ${JSON.stringify(data)};` : '';
204205

206+
// console.log(
207+
// `getCreateChartString | name=${name}, data length=${data ? data[0]?.length : 'null'}`,
208+
// );
209+
205210
const chartCreatedCheck =
206211
data !== null ? `if (window.__CHART_CREATED__) return;` : ``;
207212

@@ -225,7 +230,35 @@ function getCreateChartString(
225230
if (window._chart) {
226231
try { window._chart.destroy(); } catch (e) {}
227232
}
228-
233+
234+
// CLEAR ANY EXISTING CONTENT to avoid stacking multiple charts
235+
// try {
236+
// chartEl.innerHTML = '';
237+
// console.log('Cleared existing chart content');
238+
// } catch (e) { /* ignore */ }
239+
240+
// remove any left-over uPlot DOM elements and reset the container
241+
try {
242+
document.querySelectorAll('.uplot').forEach(function(n){
243+
if (n && n.parentNode) {
244+
console.log('Removing leftover uPlot element');
245+
n.parentNode.removeChild(n);
246+
}
247+
});
248+
249+
var _existing = document.getElementById('chart');
250+
if (_existing && _existing.parentNode) {
251+
var _fresh = document.createElement('div');
252+
_fresh.id = 'chart';
253+
_existing.parentNode.replaceChild(_fresh, _existing);
254+
console.log('Replaced chart container with fresh div');
255+
} else {
256+
console.warn('Chart container not found for replacement');
257+
}
258+
} catch (e) {
259+
console.error('Error cleaning up existing chart elements', e);
260+
}
261+
229262
// get element by id and make sure it exists
230263
var chartEl = document.getElementById('chart');
231264
if (!chartEl) {
@@ -236,9 +269,11 @@ function getCreateChartString(
236269
// create the chart
237270
238271
requestAnimationFrame(() => {
272+
// console.log('Creating uPlot chart...');
273+
// console.log(['${name}', window._data.length, window._data[0].length]);
239274
window._chart = new uPlot(window._opts, window._data, chartEl);
240275
window.__CHART_CREATED__ = true;
241-
console.log('uPlot chart created (after rAF)');
276+
// console.log('uPlot chart created (after rAF)');
242277
});
243278
244279
@@ -352,6 +387,9 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
352387
const dataRef = useRef<number[][]>(
353388
toPlainArrays((data || []) as any[]) as number[][],
354389
);
390+
// console.log(
391+
// `ChartUPlot | name=${name}, dataRef length=${dataRef.current ? dataRef.current[0]?.length : 'null'}`,
392+
// );
355393
const variablesRef = useRef<{ [key: string]: any }>({});
356394
const initialized = useRef<boolean>(false);
357395
const containerRef = useRef<any>(null);
@@ -448,7 +486,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
448486

449487
// Native WebView: detect reinitialization and restore variables/data
450488
if (shouldReinit) {
451-
console.log('Reinitializing WebView chart');
489+
// console.log('Reinitializing WebView chart');
452490

453491
initialized.current = false;
454492
destroy(true);
@@ -472,16 +510,6 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
472510
[options, data, bgColor],
473511
);
474512

475-
// useEffect(() => {
476-
// console.log('useffect [ref]', ref);
477-
478-
// if (Platform.OS === 'web') {
479-
// console.log('useffect [ref] - success');
480-
// handleLoadEnd();
481-
// return;
482-
// }
483-
// }, [ref]);
484-
485513
// Keep canonical copy of incoming prop `data` (convert typed arrays to plain arrays).
486514
// Also mirror to window._data for native platforms when webref is available.
487515
useEffect(() => {
@@ -545,7 +573,11 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
545573
force: boolean = false,
546574
): void => {
547575
// console.log(
548-
// `createChart | name=${name} | initialized=${initialized.current}, timeMs=${Date.now()}, w=${dimensionsRef.current.containerWidth}, h=${dimensionsRef.current.containerHeight}`,
576+
// `createChart | name=${name} | initialized=${initialized.current}, timeMs=${Date.now()}`,
577+
// );
578+
// print out length of data and dataRef
579+
// console.log(
580+
// `createChart | data name=${name} length=${data ? data[0]?.length : 'null'}, dataRef length=${dataRef.current ? dataRef.current[0]?.length : 'null'}`,
549581
// );
550582

551583
if (initialized.current && !force) {
@@ -582,6 +614,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
582614
optsFinal,
583615
bgColor,
584616
UTIL_FUNCTIONS,
617+
name,
585618
);
586619

587620
webref.current.injectJavaScript(createChartStr);
@@ -614,7 +647,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
614647
*/
615648
const setData = useCallback((newData: number[][]): void => {
616649
if (!isWeb && !loadedRef.current) return;
617-
// console.log(`setData | name=${name}, newData length=${newData?.length}`);
650+
var n = newData[0].length;
651+
// console.log(`setData | name=${name}, newData length=${n}`);
618652

619653
// Keep canonical copy (plain arrays)
620654
const plain = toPlainArrays(newData as any[]);
@@ -633,12 +667,17 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
633667
const body = `
634668
window._data = ${JSON.stringify(dataRef.current)};
635669
if (window._chart) {
670+
// console.log('setData | name=${name}, data length=${n}');
671+
// console.log(window._data[0].length);
636672
window._chart.setData(window._data);
637673
} else {
638674
console.error('setData | Chart not initialized');
639675
}
640676
`;
641-
webref.current.injectJavaScript(runWhenReady(body));
677+
// console.log('setData | calling webref.current.injectJavaScript');
678+
679+
// webref.current.injectJavaScript(runWhenReady(body));
680+
webref.current.injectJavaScript(body);
642681
}, []);
643682

644683
/**
@@ -843,6 +882,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
843882
console.error('destroy | could not destroy chart');
844883
}
845884
885+
window._chart = null;
846886
window.__CHART_CREATED__ = false;
847887
848888
// clear queued ops to avoid stale calls after destroy

0 commit comments

Comments
 (0)