【React】echarts-for-react 的使用

文章目录

  • 安装依赖
$ npm install --save echarts-for-react

# `echarts` 是 `echarts-for-react`的对等依赖,您可以使用自己的版本安装echarts。
$ npm install --save echarts
  • 示例展示

在这里插入图片描述

  • 示例代码:
import ReactECharts from 'echarts-for-react';
import {
    useEffect,  useState } from 'react';

export default function () {
   
  const [names, setNames] = useState([]);
  const [topCounts, setTopCounts] = useState([]);
  // 配置对象
  const getTopViewOption = () => ({
   
    title: {
   
      subtext: '成员读取次数',
    },
    tooltip: {
   },
    legend: {
   
      borderWidth: 1,
      borderColor: 'rgb(229, 231, 235)',
    },
    yAxis: {
   
      data: names,
    },
    grid: {
   
      left: 110,
    },
    xAxis: {
   },
    series: [
      {
   
        type: 'bar',
        data: topCounts,
      },
    ],
  });
  function transferListDataToChart(list) {
   
    const xArrays = [];
    const yArrays = [];
    list.forEach((item) => {
   
      xArrays.push(item.query_user);
      yArrays.push(item.count);
    });
    setNames(xArrays);
    setTopCounts(yArrays);
  }
  useEffect(() => {
   
    async function fetchData() {
   
      try {
   
        const mock = [
          {
   
            query_user: 'zhangsan',
            count: 7,
          },
          {
   
            query_user: 'lisi',
            count: 6,
          },
          {
   
            query_user: 'wangwu',
            count: 9,
          },
        ];
        transferListDataToChart(mock);
      } catch (err) {
   
        transferListDataToChart([]);
        console.log(err);
      }
    }
    fetchData();
  }, []);
  return (
    <div style={
   {
    width: '100%' }}>
      <ReactECharts option={
   getTopViewOption()} />
    </div>
  );
}

相关推荐

  1. React-hook-form-mui (一):基本使用

    2023-12-27 14:24:02       53 阅读
  2. react useMemo使用

    2023-12-27 14:24:02       54 阅读
  3. react(2) - react-redux基本使用

    2023-12-27 14:24:02       59 阅读
  4. reactreact 使用 Context 简单示例

    2023-12-27 14:24:02       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 14:24:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 14:24:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 14:24:02       87 阅读
  4. Python语言-面向对象

    2023-12-27 14:24:02       96 阅读

热门阅读

  1. 结合el-upload修改支持上传图片、视频并预览

    2023-12-27 14:24:02       67 阅读
  2. Node.js中处理特殊字符的文件名,安全稳妥的方案

    2023-12-27 14:24:02       61 阅读
  3. 腾讯云国外服务器价格表免费公网IP地址

    2023-12-27 14:24:02       70 阅读
  4. MySQL5.7服务器状态变量参考

    2023-12-27 14:24:02       61 阅读