charting-library-examples icon indicating copy to clipboard operation
charting-library-examples copied to clipboard

React-native Integration JS API

Open Profit-tech opened this issue 3 years ago • 7 comments

  • I’ve successfully integrated an example for React-native.
Screenshot 2021-09-28 at 11 11 45
  • The global problem is I am not able to integrate the JS API. According to the example, we have an included index.html file which is out of my React-native application. That’s the reason why it’s impossible to send the actual data to the trade chart.
Screenshot at Sep 28 11-20-49
  • In the case of including the library into my application.
Screenshot at Sep 28 11-38-52
  • I am getting a page loading error.
Screenshot 2021-09-28 at 11 36 40
  • Also I’d like to notice that connecting via JS API in web version of React-js was successful.
  • Thus I'm looking for any solutions for JS API integration with an ability to send data to a trading chart dynamically.

Profit-tech avatar Sep 28 '21 08:09 Profit-tech

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.

timocov avatar Sep 28 '21 11:09 timocov

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

Let’s follow through the example:

  • Open Example.xcodeproj in Xcode.
  • Make a right click on AppName and select "Add Files to "AppName"".
  • Copy all files from https://github.com/tradingview/charting_library/.

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.

Profit-tech avatar Sep 28 '21 13:09 Profit-tech

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>
   );
 }
}

makedonsky94 avatar Sep 29 '21 12:09 makedonsky94

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?

Shai-E avatar Nov 10 '21 11:11 Shai-E

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

ronbansal avatar May 26 '22 18:05 ronbansal

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

Xplosive06 avatar Aug 08 '22 14:08 Xplosive06

Same here......

wakie92 avatar Aug 11 '22 06:08 wakie92