Skip to content

Commit d803b78

Browse files
committed
readme
1 parent 701e482 commit d803b78

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

README.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,22 @@ Margin for the title and legend will be subtracted from the final width and heig
8686
8787
### Functions
8888
89-
The uPlot options can include custom functions for things like drawing and formatting tick labels. You'll need to duplicate each function: one as a normal javascript function and another as a string for the webview. Pass any functions to the `functions` prop of `ChartUPlot`, which will be injected into the webview and can be used in the uPlot options.
89+
If you have custom functions within your uPlot `options`, have them defined elsewhere; do not use inline functions. Then to make use of them on iOS and Android version, wrap all functions into a single string to pass them to the ChartUPlot's `injectedJavaScript` prop.
9090
9191
```javascript
9292
// for web version of uPlot
93-
function formatLabel(value) {
94-
return value.toFixed(2);
93+
function format_value(self, ticks) {
94+
return ticks.map((val) => {
95+
return 2;
96+
});
9597
}
9698

9799
// for iOS and Android version
98-
const formatLabelString = `
99-
function formatLabel(value) {
100-
return value.toFixed(2);
100+
const injectedJavaScript = `
101+
function format_value(self, ticks) {
102+
return ticks.map((val) => {
103+
return 2;
104+
});
101105
}
102106
`;
103107

@@ -107,32 +111,28 @@ const options = {
107111
scales: { x: { time: false } },
108112
series: [{ label: 'X' }, { label: 'Value', stroke: 'blue', width: 2 }],
109113
axes: [
110-
{
111-
scale: 'x',
112-
values: formatLabel,
113-
},
114-
{},
115-
],
114+
{
115+
scale: 'x',
116+
values: format_value,
117+
},
118+
{},
119+
],
116120
};
117121

118-
<ChartUPlot
119-
options={options}
120-
functions={[formatLabelString]}
121-
<!-- ... other props -->
122-
/>
122+
<ChartUPlot options={options} injectedJavaScript={injectedJavaScript} />;
123123
```
124124
125125
#### Important!
126126
127127
Any dependencies in each function must be:
128128
129-
- One of the other functions passed to the `functions` prop.
130-
- A global variable that is defined in the webview.
129+
- One of the other functions passed to the `injectedJavaScript` prop.
130+
- Anything globally available within a WebView.
131131
- A function from the uPlot library that is available in the webview.
132132
133133
#### Why is it done this way?
134134
135-
There may be a better way to do this, so please open an issue if you have a suggestion. Passing javascript functions to the webview is tricky, so the current solution is to pass them as strings into WebView's `injectedJavaScript` prop. This means if you're also supporting web, you need to duplicate each function to support both web and iOS/Android. If you didn't do this and instead tried doing something like `function.toString()` in an iOS build, it does not return the javascript source code.
135+
There may be a better way to do this, so please open an issue if you have a suggestion. Passing javascript functions to the webview is tricky, so the current solution is to pass them as strings into WebView's `injectedJavaScript` prop. This means if you're also supporting web, you need to duplicate each function to support both web and iOS/Android. You could argue that you can try something like, `function.toString()`, but in iOS build `toString()` does not actually return the javascript source code, so it won't work.
136136

137137
## Demo app
138138

@@ -152,6 +152,10 @@ npx expo run:ios
152152
eas build --profile development --platform ios --local
153153
```
154154

155+
## Tips
156+
157+
1. Don't create inline props to the `ChartUPlot` component, otherwise uPlot instance may get out of sync with the rendered component. Instead, use a `useMemo` hook to memoize the options and data.
158+
155159
## Contributing
156160

157161
If you would like to contribute, please open an issue or a pull request. Contributions are welcome!

0 commit comments

Comments
 (0)