React项目中使用echarts实现数据可视化

本章主要记录如何在react18项目中使用echarts实现数据可视化,分享给大家,仅供参考~

 

项目背景技术栈:react + ts + vite + less + react-router + axios + antd mobile + redux + echarts

1. 项目引入echarts

npm install echarts --save

yarn add echarts

2. 项目中新建一个页面Echart.tsx

import * as Echarts from "echarts";
import { MutableRefObject, useEffect, useRef } from "react";
import style from "../styles/chart.module.less";

type EChartsOption = echarts.EChartsOption

const pieOption: EChartsOption = {
  title: {
    text: '创业统计表',
    subtext: '创业网',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: '创业统计表',
      type: 'pie',
      radius: '50%',
      data: [
        { value: 1048, name: '家电' },
        { value: 735, name: '生鲜' },
        { value: 580, name: '便利店' },
        { value: 484, name: '娱乐' },
        { value: 300, name: '文化' }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
}

function Echart() {
  const chartpie: MutableRefObject<any> = useRef(null);

  const chartInit = () => {
    const mychartpie = Echarts.init(chartpie.current);
    mychartpie.setOption(pieOption, true);

    window.onresize = () => {
      mychartpie.resize();
    };
  };

  useEffect(() => {
    chartInit();

    return () => {
      window.onresize = null;
    };
  }, []);

  return (
    <>
      <div ref={chartpie} className={style.chartbox}></div>
    </>
  );
}

export default Echart;

3. 样式文件chart.module.less

.chartbox{   

        width: 100vw;

        height: 400px;

        padding: 8px;

        margin-bottom: 10px;

        box-sizing: border-box;

}

4. 效果展示

4. 具体详情请参阅:

1). 饼图官方链接:​​​​​​​Examples - Apache ECharts

2). option中的参数说明文档:Documentation - Apache ECharts 

3). ​​​​​​​可参阅:vue3项目中使用echarts实现数据可视化​​​​​​​

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值