React-native 安裝echarts

效果圖.png

以上就是集成完成的效果圖。在沒(méi)成功之前,集成就展示了一些莫名的html代碼、

1.問(wèn)題解決,參考文章

問(wèn)題:


image.png

參考文章感謝小伙伴
github鏈接地址 native-charts

集成步驟1:

安裝npm install native-echarts --save 或者 yarn add native-echarts

步驟2:

yarn add react-native-webview
react-native link react-native-webview
新版本需要react-native-webview,所以要添加他并link一下。

步驟3:

在node_modules\native-echarts\src\index.js和
node_modules\native-echarts\src\components\Echarts\index.js
中將import { WebView,View } from 'react-native';這里的 WebView 去掉.
添加下面的
import { WebView } from "react-native-webview";

步驟4:
image.png

在node_modules\native-echarts\src\components\Echarts找到tpl.html,復(fù)制他到android的asserts


image.png
步驟5(復(fù)制即可)

更改node_modules\native-echarts\src\components\Echarts下index.js文件代碼
(修改的內(nèi)容有注釋新增)


image.png
import React, { Component } from 'react';
import {  View, StyleSheet, Platform } from 'react-native';
import  {WebView} from "react-native-webview";
import renderChart from './renderChart';
import echarts from './echarts.min';
//以下為新增
const  iosPlatform=Platform.OS==="ios"?'true':'false'
export default class App extends Component {

  constructor(props) {
    super(props);
    this.setNewOption = this.setNewOption.bind(this);
  }


  componentWillReceiveProps(nextProps) {
    if(nextProps.option !== this.props.option) {
      this.refs.chart.reload();
    }
  }

  setNewOption(option) {
    this.refs.chart.postMessage(JSON.stringify(option));
  }

  render() {
    return (
      <View style={{flex: 1, height: this.props.height || 400,}}>
        <WebView
          ref="chart"
          scrollEnabled = {false}
          injectedJavaScript = {renderChart(this.props)}
          style={{
            height: this.props.height || 400,
            backgroundColor: this.props.backgroundColor || 'transparent'
          }}
          scalesPageToFit={Platform.OS !== 'ios'}
          originWhitelist={['*']}
         //以下source有新增
          source={iosPlatform==="true"?require('./tpl.html'):{uri:'file:///android_asset/tpl.html'}}
          onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null}
        />
      </View>
    );
  }
}

注意:運(yùn)行android時(shí)提示FileProvider找不到問(wèn)題,

把a(bǔ)ndroidx等修改成
import android.support.v4.content.FileProvider;
其他地方的一些修改
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;

另外:如果刪除了node_modules,重新進(jìn)行 npm install 的話,就需要重新進(jìn)行步驟3,步驟5的操作
這樣就能運(yùn)行了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容