Echart-react封装echart图表(React版)

思路:
外层只需要传入options即可进行渲染操作

import { useState, useEffect , useRef} from 'react';
import * as echarts from 'echarts';
import { generateMixed } from '../../utils/common';
import './index.css';


export default function EchartInit(props: any) {
  // 传入options配置项
  const { options } = props;
  

  const [echartID, setEchartID] = useState<string>('kop');
  const echartRef = useRef(null);
  let echart = null;
  useEffect(() => {
    setEchartID(generateMixed(32));

  },[])
  useEffect(() => {
    // 生成32位随机码当dom元素ID
    let dom = document.getElementById(echartID);
    // 实例化echart对象
    echart = echarts.init(dom as HTMLElement);
    // 渲染
    echart && echart.setOption(options)
  }, [options])

  return (
    <div id={echartID} ref={echartRef} className='echart-content'></div>
  )
}

index.css


.echart-content {
  height: 100%;
  width: 100%;
}

生成随机码


export function generateMixed(n: number) {
  var chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
  var res = "";
  for (var i = 0; i < n; i++) {
    var id = Math.floor(Math.random() * 62);
    res += chars[id];
  }
  return res;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值