ReactNative使用图表

安装native-echarts

npm install native-echarts

修改

 安装好打开node_modules找到native-echarts

安装react-native-webview

npm install react-native-webview

native-echarts里修改native-echarts/src/components/Echarts/index,把从react-native里引入的WebView删了换成:

import WebView from 'react-native-webview',

因为react-native把WebView移出去了现在用第三方包

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

export default class App extends Component {

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

    // 预防过渡渲染
    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,false))
      }
  }

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

然后把native-echarts/src/components/Echarts/tpl.html,把这个tpl.html复制一份出来,在android/app/src/main,在main文件下创建一个assets文件夹,把刚才的tpl.html复制到assets里面

简单使用:

import React from "react";
import { View } from "react-native";
import Echarts from 'native-echarts';

export default ()=> {

  const getEcharts =() => {
      let option = {
          legend: {
            top: 'bottom'
          },
           tooltip: {
            trigger: 'item'
          },
          toolbox: {
            show: true,
            feature: {
              mark: { show: true },
              dataView: { show: true, readOnly: false },
              restore: { show: true },
              saveAsImage: { show: true }
            }
          },
          series: [
            {
              name: 'Nightingale Chart',
              type: 'pie',
              radius: ['38%', '50%'],
               padAngle: 3,
              itemStyle: {
                borderRadius: 1
              },
                emphasis: {
                label: {
                  show: true,
                  fontSize: 40,
                  fontWeight: 'bold'
                }
              },
              labelLine: {
                show: true
              },
              data: [
                { value: 40, name: 'rose 1' },
                { value: 38, name: 'rose 2' },
                { value: 32, name: 'rose 3' },
                { value: 30, name: 'rose 4' },
                { value: 28, name: 'rose 5' },
                { value: 26, name: 'rose 6' },
                { value: 22, name: 'rose 7' },
                { value: 18, name: 'rose 8' }
              ]
            }
          ]
        };
  
        return option;
   }
  
  return (
     <View>
          <Echarts option={getEcharts()} height={300} />
     </View>
  )
  
  }

注意:之前我用的时候图表没渲染出来,把html给显示在页面上了,解决就是我上面的那个native-echarts/src/components/Echarts/index里的修改,把WebView里的source那个内容换成:

 source={{uri: 'file:///android_asset/tpl.html'}}
最后保存重新运行一下就ok了,图表显示

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值