React引入Echart水球图

在搭建React项目时候,遇到了Echart官方文档中没有的水球图,此时该如何配置并将它显示到项目中呢?

目录

一、拓展网站

二、安装

三、React中引入

1、在components文件夹下新建一个组件

2、在组件中引入

3、使用水波球组件


一、拓展网站

echarts图表集

"ECharts图表集" 网站页面。这个网站是一个ECharts的demo集和社区,用户可以在这里分享和查看各种ECharts的可视化作品。

我们的需要的水球图就在里面,搜索并在手机上看一分钟广告。我们终于拿到了水球图!

二、安装

复制代码
npm install echarts
npm install echarts-liquidfill

echarts-liquidfill 是一个专门为 Apache ECharts 设计的扩展插件,用于创建水球图(Liquid Fill Chart)。这种图表通常用于以百分比形式展示数据,并具有动态波纹效果。

官网:

https://www.npmjs.com/package/echarts-liquidfill

三、React中引入

1、在components文件夹下新建一个组件

2、在组件中引入

javascript 复制代码
import React from "react";
import * as echarts from "echarts";
import "echarts-liquidfill";
1;
class LiquidFill extends React.Component {
  componentDidMount() {
    this.chart = echarts.init(this.echartsReactNode);
    this.chart.setOption(this.getOptions());
  }

  componentDidUpdate() {
    this.chart.setOption(this.getOptions());
  }

  getOptions = () => {
    const Pie = () => {
      let dataArr = [];
      for (var i = 0; i < 150; i++) {
        if (i % 2 === 0) {
          dataArr.push({
            name: (i + 1).toString(),
            value: 50,
            itemStyle: {
              normal: {
                color: "#00AFFF",
                borderWidth: 0,
                borderColor: "rgba(0,0,0,0)",
              },
            },
          });
        } else {
          dataArr.push({
            name: (i + 1).toString(),
            value: 100,
            itemStyle: {
              normal: {
                color: "rgba(0,0,0,0)",
                borderWidth: 0,
                borderColor: "rgba(0,0,0,0)",
              },
            },
          });
        }
      }
      return dataArr;
    };

    const option = {
      backgroundColor: "#081736",
      series: [
        {
          type: "liquidFill",
          radius: "70%",
          center: ["50%", "50%"],
          data: [0.6, { value: 0.6, direction: "left" }],
          backgroundStyle: {
            borderWidth: 1,
            color: "rgba(62, 208, 255, 1)",
          },
          amplitude: "6%",
          color: [
            {
              type: "linear",
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                { offset: 1, color: "#6CDEFC" },
                { offset: 0, color: "#429BF7" },
              ],
              globalCoord: false,
            },
          ],
          label: {
            normal: {
              formatter: 0.6 * 100 + "\n{d|%}",
              rich: {
                d: {
                  fontSize: 36,
                },
              },
              textStyle: {
                fontSize: 48,
                color: "#fff",
              },
            },
          },
          outline: {
            show: false,
          },
        },
        // ... 其他 series 配置
        {
          type: "pie",
          zlevel: 0,
          silent: true,
          radius: ["78%", "80%"],
          z: 1,
          label: {
            normal: {
              show: false,
            },
          },
          labelLine: {
            normal: {
              show: false,
            },
          },
          data: Pie(),
        },
      ],
    };

    return option;
  };

  render() {
    return (
      <div
        ref={(node) => (this.echartsReactNode = node)}
        style={{ width: "100%", height: "400px" }}
      />
    );
  }
}

export default LiquidFill;

3、使用水波球组件

javascript 复制代码
import LiquidFill from "../../../components/LiquidFill";
function MedicalEquipmentStatistics() {
  return (
    <div className="App">
      <LiquidFill />
    </div>
  );
}

export default MedicalEquipmentStatistics;

完成效果(没显示的重启一下项目):

相关推荐
benchmark_cc4 分钟前
Python 量化系统如何设计统一的数据访问接口:从策略解耦到数据层架构
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
const_qiu17 分钟前
第四章 00:开篇 —— 从「会聊天」到「记得住、会干活、能协作」
java·开发语言·人工智能·python
Wang's Blog22 分钟前
Java 项目实战: 交友项目 - 个人资料管理的更新接口实现与坑
java·开发语言·交友
CoderYanger1 小时前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)
java·开发语言·前端·spring·面试·java-ee·mvc
估值探索者1 小时前
【Python量化选股流水线 #04】选股器好不好用?「昨日候选→今日表现」回看验收
开发语言·python·股票数据·股票数据api接口
szp20051 小时前
Cache API 会丢掉 URL 里的 #:我的 9 个模型分块一直写在同一条缓存上
javascript·浏览器
keyipatience2 小时前
TypeScript学习(1)
前端·javascript·学习·typescript·html·css3
爱喝热水的呀哈喽2 小时前
HEJIAN RESEARCH
开发语言·matlab
白露与泡影2 小时前
后端 Java 面试核心八股汇总|高频面试题 + 解析,覆盖基础到进阶
java·开发语言·面试
xifangge20252 小时前
基于 JavaScript 实现 CNC 数控切削参数与螺纹公差纯前端核算引擎
开发语言·前端·javascript