react native中使用echarts


開發平台:mac pro

node版本:v8.11.2

npm版本:6.4.1

react-native版本:0.57.8

native-echarts版本:^0.5.0
目標平台:android端收銀機,android7.0+

 

最近在使用react native進行app的開發工作,rn開發的內容會與原生app混合,在一些使用場景下遇到了一些問題

 

使用場景:每日收益與每月收益的折線圖統計

 

在pc/h5端的開發工作中,圖標類的開發使用echarts/f2等三方工具都是比較常見的了,在react native中,沒有了DOM的概念,因此在react native中使用了一些三方的圖標庫

native-echarts,github地址。

 

需要更換echarts版本的方法

native-echarts內部使用了react native中的webview進行圖表的展示,本質上只是傳入數據,通過echarts渲染出靜態的HTML文檔,然后通過webview展示出來而已。

netive-echarts內部使用的echarts版本為v3.2.3"版本,如果需要更高級的echarts版本,只需要更換src/components/Echarts/echarts.min.js文件以及tpl.html文件里的內容即可。

 

使用時遇到的問題: 在debug模式下,真機以及測試機器上圖標正常顯示,打包成android的apk文件后圖表都不顯示

解決方式:

1:找到native-echarts/src/components/Echarts/tpl.html文件,復制到android/app/src/main/assets這個目錄下面,如果文件夾不存在就新建一個即可。

2:找到文件native-echarts/src/components/Echarts/index.js,修改為一下內容

import React, { Component } from 'react';
  import { WebView, View, StyleSheet, Platform } from 'react-native';
  import renderChart from './renderChart';
  import echarts from './echarts.min';

  export default class App extends Component {

    constructor(props) {
      super(props);
    }
    

    // 預防過渡渲染
    shouldComponentUpdate(nextProps, nextState) {
      const thisProps = this.props || {}
      nextProps = nextProps || {}
      if (Object.keys(thisProps).length !== Object.keys(nextProps).length) {
        return true
      }
      for (const key in nextProps) {
        if (JSON.stringify(thisProps[key]) != JSON.stringify(nextProps[key])) {
          // console.log('props', key, thisProps[key], nextProps[key])
          return true
        }
      }
      return false
    }

    componentWillReceiveProps(nextProps) {
      if (nextProps.option !== this.props.option) {
        // 解決數據改變時頁面閃爍的問題
        this.refs.chart.injectJavaScript(renderChart(nextProps))
      }
    }

    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={{uri: 'file:///android_asset/tpl.html'}}
            onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null}
          />
        </View>
      );
    }
  }

 

可能存在的問題????

 

同時,在后續的react native版本中,webview即將從react native內部移除出去,改為三方包安裝使用。參考:

https://reactnative.cn/docs/webview/#mixedcontentmode

https://github.com/react-native-community/discussions-and-proposals/issues/6

因此,在后續新版本中使用native-echarts,可能會使用不了,因此建議fork一個穩定的版本到自己的github上,或者在后續自己采用react-native-webview + echarts的方式自由的組合版本,使用起來更加自由。

 

 

參考文檔:

https://github.com/somonus/react-native-echarts/issues/47

https://github.com/somonus/react-native-echarts/issues/32

https://github.com/somonus/react-native-echarts/issues/122

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM