reactnative实现图表功能

来个需求,需要图形统计,以前写vue图形统计都是用echarts,时间紧迫,我就想echarts 有没有reactnative的插件?结果还真有。
在这里插入图片描述

安装

yarn add @wuba/react-native-echarts echarts
yarn add react-native-svg
import React, { useRef, useEffect,useState } from 'react';
import * as echarts from 'echarts/core';
import {View,Text,Image,Dimensions,TouchableOpacity} from 'react-native';

import { BarChart } from 'echarts/charts';
import {Toast,ActivityIndicator } from '@ant-design/react-native'
import {
    ToolboxComponent,
    LegendComponent,
    TooltipComponent,
    DataZoomComponent,
  } from 'echarts/components';
  
import { SVGRenderer, SvgChart } from '@wuba/react-native-echarts';

// echarts.use([SVGRenderer, LineChart, BarChart]);
echarts.use([
    SVGRenderer,
    BarChart,
    ToolboxComponent,
    TooltipComponent,
    LegendComponent,
    DataZoomComponent,
  ]);
  function getOption(ylist:[],onlinList:[],offList:[],userList:[]) {
    const option = {
      title: {
        text: ''
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      // legend: {},
      grid: {
        left: '3%',
        right: '4%',
        bottom: '2%',
        top: 5,
        containLabel: true
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: '#FFCE82',
          width: 0.5,
        },
      },
      xAxis: {

        axisLabel: { // 隐藏刻度线
          show: false
      },
      
      },
      yAxis: {
        type: 'category',
        data: ylist,
        axisTick: { // 隐藏刻度线
          show: false
      },
      },
      series: [
        {
          name: '线上',
          type: 'bar',
          data: onlinList,
          itemStyle: {
            color: '#369AFF', // 设置线的颜色为红色
            width: 3,  
          },
          barGap: 0,
      // barCategoryGap: 0,
        },
        {
          name: '线下',
          type: 'bar',
          data: offList,
          itemStyle: {
            color: '#53D475', // 设置线的颜色为红色
            width: 3,  
          },
          barGap: 0,
      // barCategoryGap: 0,
        },
        {
          name: '用户',
          type: 'bar',
          data: userList,
          itemStyle: {
            color: '#F1DF69', // 设置线的颜色为红色
            width: 3,  
          },
          barGap: 0,
      // barCategoryGap: 0,
        }
      ]
    };
    return option
  }
export default function Revenuestatistics() {
    const svgRef = useRef<any>(null);
    const [day,setDay] = useState()
    const [loding,setloding]  = useState(false)
  useEffect(() => {
    setloding(true)
    getData().then((res:any)=>{
      setloding(false)
  
      const ylist = []
      const onlinList = []
      const offList = []
      const userList = []

      res.data.vos.forEach((v:any)=>{
        ylist.push(v.addressName)
        onlinList.push(v.onlineNum)
        offList.push(v.offlineNum)
        userList.push(v.customerNum)
      })
      const option = getOption(ylist,onlinList,offList,userList)
      let chart = echarts.init(svgRef.current, 'light', {
        renderer: 'svg',
        width: Dimensions.get('window').width,
        height: Dimensions.get('window').height-200,
      });
      chart.setOption(option);
    return () => chart?.dispose();
    }).catch((err:any)=>{
      setloding(false)
    })
    

    
  }, []);

  return <View>
       <View style={{flexDirection:'row'}}>
       <View style={{flexDirection:'row',alignItems:'center',paddingHorizontal:10}}>
        <Image  style={{width:18,height:18,marginRight:10}}  source={require('../../assets/images/Chart4.png')}   />
        <Text style={{color:'#171717',fontSize:16,fontWeight:'bold'}}>用户分布</Text>
        </View>
       </View>
       <View style = {{flexDirection:'row',padding:10}}>
            <View style = {{flexDirection:'row',alignItems:'center',}}>
              {/* 小圆球 */}
              <View style={{width:8,height:8,backgroundColor:'#0084FF',borderRadius:5}}></View>
              <Text style={{marginLeft:8}}>线上商家</Text>
            </View>
            <View style = {{flexDirection:'row',alignItems:'center',marginHorizontal:10}}>
              {/* 小圆球 */}
              <View style={{width:8,height:8,backgroundColor:'#53D475',borderRadius:5}}></View>
              <Text style={{marginLeft:8}} >线下商家</Text>
            </View>
            <View style = {{flexDirection:'row',alignItems:'center'}}>
              {/* 小圆球 */}
              <View style={{width:8,height:8,backgroundColor:'#F1DF69',borderRadius:5}}></View>
              <Text style={{marginLeft:8}} >用户</Text>
            </View>
        </View>
      
        {loding ?<ActivityIndicator/> : <SvgChart ref={svgRef} />}
    </View> 
}
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值