Skip to content

Commit 19c2628

Browse files
committed
make sure dimensions are ints, add some logging in case need it for future
1 parent eac9533 commit 19c2628

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/components/ChartUPlot.tsx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ export interface UPlotProps {
313313
injectedJavaScript?: string;
314314
/** Additional props for the WebView */
315315
webviewProps?: any;
316+
/** name for the WebView component to force remount if needed */
317+
name?: string;
316318
}
317319

318320
const ChartUPlot = forwardRef<any, UPlotProps>(
@@ -326,6 +328,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
326328
onMessage,
327329
onLoad = null,
328330
injectedJavaScript = '',
331+
name = '',
329332
...webviewProps
330333
},
331334
ref,
@@ -339,17 +342,17 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
339342
const initialized = useRef<boolean>(false);
340343
const containerRef = useRef<any>(null);
341344
const dimensionsRef = useRef({
342-
containerWidth: options?.width || style?.width || height,
343-
containerHeight: options?.height || style?.height || width,
345+
containerWidth: Math.round(options?.width || style?.width || height),
346+
containerHeight: Math.round(options?.height || style?.height || width),
344347
});
345348

346349
const bgColor = style?.backgroundColor || 'transparent';
347350

348351
const handleLayout = useCallback((event) => {
349352
const { width, height } = event.nativeEvent.layout;
350353
dimensionsRef.current = {
351-
containerWidth: width,
352-
containerHeight: height,
354+
containerWidth: Math.round(width),
355+
containerHeight: Math.round(height),
353356
};
354357
}, []);
355358

@@ -361,7 +364,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
361364
width: options?.width || base?.width || '100%',
362365
height: options?.height || base?.height || '100%',
363366
};
364-
}, [style, options?.width, options?.height]);
367+
}, [style?.width, style?.height, options?.width, options?.height]);
365368

366369
// memoized onLoadEnd handler for native WebView
367370
const handleLoadEnd = useCallback((): void => {
@@ -401,6 +404,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
401404
// memoized ref callback for both web container and native WebView
402405
const setWebRef = useCallback(
403406
(r: any) => {
407+
// console.log(`setWebRef | name=${name}`);
408+
404409
const prevContainer = containerRef.current;
405410
const prevWeb = webref.current;
406411
const shouldReinit = Boolean(prevContainer && r && r !== prevWeb);
@@ -470,6 +475,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
470475
useEffect(() => {
471476
// update uplot height and width if options change
472477

478+
// console.log(
479+
// `useEffect | dimensionsRef change | name=${name}, w=${dimensionsRef.current.containerWidth}, h=${dimensionsRef.current.containerHeight}`,
480+
// );
481+
473482
if (isWeb) {
474483
uplotInstance.current?.setSize({
475484
width: dimensionsRef.current.containerWidth,
@@ -498,6 +507,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
498507
// eslint-disable-next-line @typescript-eslint/no-explicit-any
499508
const createChart = useCallback(
500509
(opts: any, data: number[][] | null = null, bgColor?: string): void => {
510+
// console.log(
511+
// `createChart | name=${name} | initialized=${initialized.current}`,
512+
// );
513+
501514
if (initialized.current) {
502515
return;
503516
}
@@ -534,8 +547,6 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
534547
UTIL_FUNCTIONS,
535548
);
536549

537-
console.log('createChartStr', createChartStr);
538-
539550
webref.current.injectJavaScript(createChartStr);
540551
}
541552
initialized.current = true;
@@ -550,6 +561,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
550561
* @param {Object} newOptions - The new options to set for the chart.
551562
*/
552563
const updateOptions = useCallback((newOptions: any): void => {
564+
// console.log(`updateOptions | name=${name}`);
565+
553566
destroy(true); // keep data
554567
createChart(newOptions);
555568
}, []);
@@ -561,6 +574,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
561574
* @param {number[][]} newData - The new data to set for the chart.
562575
*/
563576
const setData = useCallback((newData: number[][]): void => {
577+
// console.log(`setData | name=${name}, newData length=${newData?.length}`);
578+
564579
// Keep canonical copy (plain arrays)
565580
const plain = toPlainArrays(newData as any[]);
566581
dataRef.current = plain as number[][];
@@ -590,6 +605,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
590605
* Append a new data point across all series: [x, y1, y2, ...]
591606
*/
592607
const pushData = useCallback((item: number[]): void => {
608+
// console.log(`pushData | name=${name}, item length=${item?.length}`);
609+
593610
// Update canonical copy locally first
594611
if (!dataRef.current || dataRef.current.length !== item.length) {
595612
dataRef.current = [];
@@ -637,6 +654,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
637654
*/
638655
const sliceSeries = useCallback(
639656
(axis: number, min: number, max: number): void => {
657+
// console.log(
658+
// `sliceSeries | name=${name}, axis=${axis}, min=${min}, max=${max}`,
659+
// );
660+
640661
// Update canonical dataRef by slicing locally
641662
dataRef.current = _sliceSeries(dataRef.current, axis, min, max);
642663

@@ -670,6 +691,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
670691

671692
// function to call setScale
672693
const setScale = useCallback((axis: string, options: any): void => {
694+
// console.log(
695+
// `setScale | name=${name}, axis=${axis}, options=${JSON.stringify(options)}`,
696+
// );
697+
673698
if (isWeb) {
674699
uplotInstance.current?.setScale(axis, options);
675700
} else {
@@ -692,6 +717,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
692717
// if web, sets the variable to window.[name]
693718
// if native, sets the variable to window.[name] via webref.current.injectJavaScript
694719
const setVariable = useCallback((name: string, value: any): void => {
720+
// console.log(
721+
// `setVariable | name=${name}, name=${name}, value=${JSON.stringify(value)}`,
722+
// );
723+
695724
variablesRef.current[name] = value;
696725

697726
if (isWeb) {
@@ -716,6 +745,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
716745

717746
// function to call setSize
718747
const setSize = useCallback((width: number, height: number): void => {
748+
// console.log(`setSize | name=${name}, width=${width}, height=${height}`);
749+
719750
if (isWeb) {
720751
uplotInstance.current?.setSize(width, height);
721752
} else {
@@ -737,6 +768,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
737768

738769
// function to call destroy, also clears the data
739770
const destroy = useCallback((keepData: boolean = false): void => {
771+
// console.log(`destroy | name=${name}, keepData=${keepData}`);
772+
740773
if (isWeb) {
741774
uplotInstance.current?.destroy();
742775
if (!keepData) {
@@ -768,6 +801,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
768801
// destroy, clear data, and reinitialize the chart when the component unmounts
769802
const reset = useCallback(
770803
(opts: any, data: number[][], bgColor?: string): void => {
804+
// console.log(`reset | name=${name}`);
771805
destroy();
772806
createChart(opts, data, bgColor);
773807
},
@@ -798,6 +832,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
798832
reset,
799833
}));
800834

835+
// console.log(`render | name=${name}`);
836+
801837
if (Platform.OS === 'web') {
802838
return (
803839
<View
@@ -812,12 +848,14 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
812848
{...webviewProps}
813849
originWhitelist={['*']}
814850
source={html}
851+
allowingReadAccessToURLs={true}
815852
style={memoizedContainerStyle}
816853
scrollEnabled={false}
817854
onLoadEnd={handleLoadEnd}
818855
ref={setWebRef}
819856
onLayout={handleLayout}
820857
javaScriptEnabled={true}
858+
// injectedJavaScript={injectedJavaScriptWithFunctions}
821859
injectedJavaScriptBeforeContentLoaded={
822860
injectedJavaScriptWithFunctions
823861
}

0 commit comments

Comments
 (0)