React实现echarts高阶组件

在react项目中需要使用echarts的高阶组件,当使用echarts的时候,只需要使用高阶组件包围即可
高阶组件代码如下:

import React, { memo, useEffect, useRef } from 'react';
import * as echarts from 'echarts';

const HOCEcharts = memo((props) => {

  const element = useRef<any>(null);
  const chart = useRef<any>(null);
  const { options } = props;

  const resizeAll = () => {
    chart.current.resize()
  }

  useEffect(() => {
    setTimeout(() => {
      chart.current = echarts.init(element.current);
      window.removeEventListener('resize', resizeAll)
      if(chart.current){
        window.addEventListener('resize', resizeAll);
        chart.current.setOption(options, true)
      }
    }, 20)
  },[])

  useEffect(() => {
    window.removeEventListener('resize', resizeAll)
    if(chart.current){
      window.addEventListener('resize', resizeAll);
      chart.current.setOption(options, true)
    }
  }, [options])

  return <div ref={element} style={{width: '100%', height: '100%'}}></div>
})

export default HOCEcharts;

组件代码如下:

import React, { memo, useEffect, useRef, useState } from 'react';
import * as echarts from 'echarts';
import _ from 'lodash';
import HOCEcharts from '../../HOCEcharts/EchartsHoc';

function areEqual(prevProps: any, nextProps: any) {
  return JSON.stringify(prevProps) === JSON.stringify(nextProps);
}

const Bar: any = memo(({datasource, style, ...props}: any) => {
  const { barData, barLenged } = props;

  const option: any = {
    grid:{
      left:'5%',
      top: '10%',
      right: '5%',
      bottom: '10%'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      data: barLenged
    },
    xAxis: [
      {
        type: 'category',
        data: _.map(_.get(barData, 'data'), (item) => (_.get(item, 'days'))),
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        name: barLenged[0],
        type: 'bar',
        tooltip: {
          valueFormatter: function (value: any) {
            return value;
          }
        },
        itemStyle: {
          color: '#647bbf'
        },
        data: _.map(_.get(barData, 'data'), (item) => (_.get(item, 'current')))
      },
      {
        name: barLenged[1],
        type: 'bar',
        tooltip: {
          valueFormatter: function (value: any) {
            return value;
          }
        },
        itemStyle: {
          color: '#35c9c8'
        },
        data: _.map(_.get(barData, 'data'), (item) => (_.get(item, 'lastOPay')))
      }
    ]
  }
  
  return <HOCEcharts
    options={option}
    />;
}, areEqual)

export default Bar;

这是对柱状图的实现,可以自行实现查看效果图。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天边彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值