Echart图组件封装

需求

        在前端的工作中难免要遇到用各种图形去展示数据,这时一般都会涉及到echarts图,那么为了方便使用,我们可以封装一个简单的echart组件去满足一些基本的需求。

实现

import React, { Component } from 'react'
const echarts = require('echarts');

export default class Chart extends Component {
    constructor(props){
        super(props)
    }
    
    componentDidMount(){
        this.loadTeamAppEcharts()
    }
    loadTeamAppEcharts = () => {
        const {keys, svalue, cname} = this.props
        const echartsId = '#'+ keys +'clusterInfo'
        // 1.创建实例对象
        const myEcharts1 = echarts.init(document.querySelector(echartsId));
        // 2. options配置项
        var datas = {
            value: 80,
            title: "健康度",
            type: 1,
            radiusType: 1,
          };
          var fontColor = "#fff";
          var seriesName = "";
          let noramlSize = 16;
          let state = "";
          let center = ["50%", "70%"];
          let wqradius, nqradius, kdradius;
      
          wqradius = "100%";
          nqradius = "100%";
          kdradius = "100%";
      
        
      
          let wqColor = svalue > 80 ? "#f5222d" : 'rgba(80, 152, 237,0.9)';
          let nqColor = [
            [
              datas.value / 100,
              new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: svalue > 80 ? "#f5222d" : "#3d54c4",
                },
                {
                  offset: 0.5,
                  color: svalue > 80 ? "#f5222d" : "#3d54c4",
                },
                {
                  offset: 1,
                  color: svalue > 80 ? "#f5222d" : "#3d54c4",
                },
              ]),
            ],
            [1, "rgb(199, 222, 239)"],
          ];
        const option = {
            backgroundColor:"#fff",
            title: {
              //分配率文字配置
              show: true,
              x: "left",
              bottom: "-3%",
              left: "14%",
              text: cname,
              textStyle: {
                fontWeight: "500",
                fontSize: 12,
                color: svalue > 80 ? "#f5222d" : "#79828f",
              },
            },
            tooltip: {
                show: false
            },
            series: [
              {
                name: "刻度文字",
                type: "gauge",
                radius: "100%", //仪表盘大小
                center: ["50%", "74%"],
                startAngle: 180,
                endAngle: 0,
                z: 2,
                splitNumber: 5,
                min: 0,
                max: 100,
                axisTick: {
                  show: false,
                  lineStyle: {
                    color: "#0af", //刻度线
                    width: 1, //刻度线宽度
                  },
                  length: 3, //刻度线长度
                  splitNumber: 1, //刻度线分割比例
                },
                splitLine: {
                  show: false,
                },
                axisLine: {
                  lineStyle: {
                    width: 25,
                    opacity: 0,
                  },
                },
                axisLabel: {
                  distance: -34, //外层文字位置
                  fontSize: 10, //文字大小
                  color: svalue > 80 ? "#f5222d" : "#3d54c4", //颜色
                },
                pointer: {
                  show: true,
                  width: 2, //指针
                  length: "70%",
                },
                itemStyle: {
                  normal: {
                    //color: "#0af",//wqColor
                    color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color:  svalue > 80 ? "#f5222d" : "#3d54c4",
                      },
                      {
                        offset: 0.5,
                        color:  svalue > 80 ? "#f5222d" : "#3d54c4",
                      },
                      {
                        offset: 1,
                        color:  svalue > 80 ? "#f5222d" : "#3d54c4",
                      },
                    ]),
                  },
                },
                detail: {
                  show: true,
                  offsetCenter: [24, "39%"], //显示数值的位置
                  color:  svalue > 80 ? "#f5222d" : "#3d54c4",
                  fontSize: 20,
                  rich: {
                    value: {
                      fontSize: 20,
                      lineHeight: 10,
                      color:  svalue > 80 ? "#f5222d" : "#3d54c4",
                      fontWeight: "700",
                    },
                    company: {
                      fontSize: 10,
                      lineHeight: 20,
                      color:  svalue > 80 ? "#f5222d" : "#3d54c4",
                    },
                  },
                  valueAnimation: true,
                  formatter: "{value}%",
                },
                data: [
                  {
                    value: svalue,
                  },
                ],
              },
              {
                name: "内层盘",
                type: "gauge",
                z: 2,
                radius: "85%",
                startAngle: 180,
                endAngle: 0,
                center: ["49%", "70%"],
                axisLine: {
                  lineStyle: {
                    color: nqColor,
                    width: 10,
                    opacity: 1,
                  },
                },
                splitNumber: 5,
                min: 0,
                max: 100,
                axisTick: {
                  show: false,
                },
                splitLine: {
                  show: true,
                  length: 4,
                  lineStyle: {
                    color:  svalue > 80 ? "#f5222d" : "#3d54c4",
                    width: 1,
                    shadowBlur: 0.5,
                    opacity: 0.9,
                    shadowOffsetX:0,
                    shadowOffsetY: 0,
                  },
                },
                itemStyle: {
                  //指针阴影
                  shadowBlur: 10,
                  shadowColor:  svalue > 80 ? "#f5222d" : "rgba(0, 103, 255, 0.2)",
                  shadowOffsetX: 0,
                  shadowOffsetY: 8,
                },
                axisLabel: {
                  show: false,
                },
                pointer: {
                  show: false,
                },
      
                detail: {
                  show: false,
                },
              },
            ],
        };
        // 3. 配置项和数据给实例化对象
        myEcharts1.setOption(option);
        // 4. 当我们浏览器缩放的时候,图表也等比例缩放
        window.addEventListener('resize', function () {
          // 让我们的图表调用 resize这个方法
          myEcharts1.resize();
        });
      };
  render() {
    const {keys, swidth, sheight} = this.props
    return (
        <div id={keys + 'clusterInfo'} style={{width: swidth,height: sheight}} />
    )
  }
}

swidth

设置的宽

sheight

设置的高

keys

唯一的key值

svalue

设置的内容

cname

设置的文字

        当然 如果想封装的更为灵活一些,你可以将 option也作为参数传入该组件。这样就能实现不同的图标了。这里我就不过多赘述。

 使用

 {/* chart图 */}
                            <div className={styles.chartsStyle}>

                                <div>
                                    <Charts keys={'upcpu' + `${1}`} svalue={cpuUsed} cname="CPU" swidth='200px' sheight='120px' />
                                </div>
                                <div>
                                    <Charts keys={'upcpu' + `${2}`} svalue={Number(memoryUsed) == 0 ? 0 : Number(memoryUsed)} cname={formatMessage({id:'enterpriseColony.mgt.cluster.memory'})} swidth='200px' sheight='120px' />
                                </div>
                                <div>
                                    <Charts keys={'upcpu' + `${3}`} svalue={Number(diskUsed) == 0 ? 0 : Number(diskUsed)} cname={formatMessage({id:'enterpriseColony.mgt.cluster.store'})} swidth='200px' sheight='120px' />
                                </div>
                                <div>
                                    <p>
                                        {node_ready == {} ? 0 : node_ready || 0}
                                        <span>
                                            /{all_nodes || 0}
                                        </span>
                                    </p>
                                </div>
                                <div>
                                    <p>
                                        {services_status && services_status.running || 0}
                                    </p>
                                </div>
                            </div>
                        </>

 

 

其他 

echarts配置项使用手册:Documentation - Apache ECharts

echarts官网:Examples - Apache ECharts

MCChart官网:MCChart 

isqqw官网: https://www.isqqw.com/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
微信小程序支持使用第三方库来创建表件,其中包括E。下面是一些步骤制作一个ECharts组件: 1. 首,在小程序项目的根目录中创建一个名`echarts`的文件夹,用于放ECharts相关的文件。 2. 下载E库的最新版本,并将其解压`echarts`文件夹中。 3 在小程序项目的根录中创建一个名`echarts.js`的文件,用于封装ECharts的功能。 ```javascript const echarts = require('./echarts/echarts.min.js'); Component({ properties: { // 在这里定义你的组件属性 }, data: { // 在这里定义你的组件内部数据 }, methods: { // 在这里定义你的组件方法 initChart() { // 在这里初始化你的ECharts实例并绘制表 } }, lifetimes: { attached() { this.initChart(); } } }) ``` 在上述代码中,我们首先引入了ECharts库的入口文件`echarts.min.js`,然后在`attached`生命周期函数中调用`initChart`方法来初始化ECharts实例并绘制表。 4. 在需要使用ECharts的页面中引入你刚刚创建的组件。 ```xml <import src="../echarts/echarts.wxml" /> <view> <echarts></echarts> </view> ``` 在上述代码中,我们首先使用`import`标签引入了你刚刚创建的组件的`wxml`模板文件,然后在页面中使用`echarts`标签来使用这个组件。 5. 在页面的`js`文件中,对组件进行配置和数据传递。 ```javascript Page({ data: { chartOption: { // 在这里定义表的配置项和数据 } }, onReady() { this.echartsComponent = this.selectComponent('#echarts'); this.echartsComponent.setOption(this.data.chartOption); } }) ``` 在上述代码中,我们首先通过`selectComponent`方法获取到组件实例,然后使用`setOption`方法将表的配置项和数据传递给组件。 通过上述步骤,你就可以在微信小程序中制作ECharts组件了。记得要根据ECharts的官方文档来设置表的配置项和数据,以满足你的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大聪明码农徐

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

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

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

打赏作者

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

抵扣说明:

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

余额充值