tradingview / charting-library-examples

Examples of Charting Library integrations with other libraries, frameworks and data transports
MIT License
1.33k stars 744 forks source link

React-native Integration JS API #248

Open Profit-tech opened 2 years ago

Profit-tech commented 2 years ago
Screenshot 2021-09-28 at 11 11 45 Screenshot at Sep 28 11-20-49 Screenshot at Sep 28 11-38-52 Screenshot 2021-09-28 at 11 36 40
timocov commented 2 years ago

That’s the reason why it’s impossible to send the actual data to the trade chart.

Thus I'm looking for any solutions for JS API integration with an ability to send data to a trading chart dynamically.

I don't think that it is impossible because of that. Did you try to implement datafeed so it will request a data from your app rather that requesting it from the network. I think this should work fine.

Profit-tech commented 2 years ago

The question is exactly how to implement datafeed in react-native application.

Let’s follow through the example:

As a result added files will include index.html (it describes in tradingview configuration). It displays via the WebView in my application also (your could find it at the screen above).

So. My base question is how to change the data for datafeed in dynamic (relying on user’s actions) if there is no access to index.html file from my React-native application.

makedonsky94 commented 2 years ago

Hi! Please, check the documentation of React Native WebView

In the example you can see Web-to-Native communication.

  1. Inject javascript

    injectedJavaScript={this.jsToInject}

    jsToInject

    tvWidget.onChartReady(function() {
          tvWidget.chart().onIntervalChanged().subscribe(
              null,
              function(interval) {
                  const response = { type: "onIntervalChanged", interval: interval }
                  window.ReactNativeWebView.postMessage(JSON.stringify(response));
              }
          );
    });
  2. Write a handler for that action

    onMessage={(event) => {
        const data = JSON.parse(event.nativeEvent.data)
        if (data.type == "onIntervalChanged") {
            Alert.alert(
              'onIntervalChanged',
              "Interval = " + data.interval,
              [
                { text: 'OK', onPress: () => console.log('OK Pressed') }
              ],
              { cancelable: true }
            );
        }
    }}

    Now if you want to make Native-to-Web communication you should use injectJavaScript method

    
    import React, { Component } from 'react';
    import { View } from 'react-native';
    import { WebView } from 'react-native-webview';

export default class App extends Component { render() { const run = document.body.style.backgroundColor = 'blue'; true; ;

setTimeout(() => {
  this.webref.injectJavaScript(run);
}, 3000);

return (
  <View style={{ flex: 1 }}>
    <WebView
      ref={(r) => (this.webref = r)}
      source={{
        uri: 'https://github.com/react-native-webview/react-native-webview',
      }}
    />
  </View>
);

} }

Shai-E commented 2 years ago

Did anyone come up with a working solution? Is there anyone who successfully integrated the charts in react native and made it look good on mobile?

ronbansal commented 2 years ago

Any updates on this? I am stuck in the same position

Xplosive06 commented 1 year ago

The same here, don't know what to do...

wakie92 commented 1 year ago

Same here......