echarts饼图封装

news/2025/2/22 15:46:35/

1. 组件

<template>

  <div

    :id="id"

    class="main"

    :style="{ width: width, height: height }"

    :ref="id"

  ></div>

</template>

<script>

import * as echarts from "echarts";

export default {

  name: "roseChart",

  data() {

    return {

      myEchart: null,

    };

  },

  props: {

    width: {

      type: String,

      default: "100%",

    },

    height: {

      type: String,

      default: "100%",

    },

    // 数据(数组)

    typeAnalysisData: {

      type: Array,

      default: () => [],

    },

    id: {

      type: String,

      default: "roseChart",

    },

    // 是否显示提示(方框)

    legendShow: {

      type: Boolean,

      default: true,

    },

    // 上面距离

    legendTop: {

      type: String,

      default: "auto",

    },

    // 左面距离

    legendLeft: {

      type: String,

      default: "auto",

    },

    // 排列方式

    legendOrient: {

      type: String,

      default: "vertical",

    },

    // 字体颜色(方框)

    legendColor: {

      type: String,

      default: "#8493c3",

    },

    // 字体大小(方框)

    legendFontSize: {

      type: String,

      default: "16px",

    },

    titleFontSize: {

      type: Number,

      default: 34,

    },

    titleColor: {

      type: String,

      default: "#8493c3",

    },

    // 饼图的半径

    radius: {

      type: Array,

      default: () => [],

    },

    // 文本标签

    labelShow: {

      type: Boolean,

      default: true,

    },

    labelPosition: {

      type: String,

      default: "center",

    },

    // 文本字体大小

    labelFontSize: {

      type: Number,

      default: 14,

    },

    // 文本字体行高

    lineHeight: {

      type: Number,

      default: 15,

    },

    // 文本字体颜色

    labelColor: {

      type: String,

      default: "#8493c3",

    },

    activeFontSize: {

      type: Number,

      default: 15,

    },

    activeColor: {

      type: String,

      default: "#8493c3",

    },

    // 中心

    center: {

      type: Array,

      default: () => [],

    },

    // 标题

    titleShow: {

      type: Boolean,

      default: true,

    },

    // 主标题

    text: {

      type: String,

      default: "主标题",

    },

    // 副标题

    subtext: {

      type: String,

      default: "副标题",

    },

    // 标题位置(上下)

    titleTop: {

      type: String,

      default: "center",

    },

    // 标题位置(左右)

    titleLeft: {

      type: String,

      default: "center",

    },

  },

  methods: {

    drawChart() {

      let timer = null;

      timer = setTimeout(() => {

        if (

          this.myEchart != null &&

          this.myEchart != "" &&

          this.myEchart != undefined

        ) {

          this.myEchart.dispose(); //销毁

        }

        if (!this.$refs[this.id]) return;

        this.myEchart = echarts.init(this.$refs[this.id]);

        let option = {

          title: {

            show: this.titleShow,

            // x: "44%", //X坐标

            // y: "35%", //Y坐标

            top: this.titleTop,

            left: this.titleLeft,

            text: this.text, //主标题

            subtext: this.subtext, //副标题

            textStyle: {

              //标题样式

              fontSize: 20,

              fontWeight: "bolder",

              color: "rgb(113, 116, 123)",

              // formatter: "",

              // marginTop: this.marginTop,

              // marginLeft: this.marginLeft,

              // transfrom: "translate(-50%,-50%)",

            },

            subtextStyle: {

              //副标题样式

              fontSize: 14,

              fontWeight: "bolder",

              color: "rgb(113, 116, 123)",

              // transform: "translate(-50%,-50%)",

              // marginTop: this.marginTop,

              // marginLeft: this.marginLeft,

            },

          },

          legend: {

            show: this.legendShow,

            orient: this.legendOrient,

            top: this.legendTop,

            left: this.legendLeft,

            textStyle: {

              color: this.legendColor,

              fontSize: this.legendFontSize,

            },

          },

          series: [

            {

              name: "Nightingale Chart",

              type: "pie",

              radius: this.radius,

              center: this.center,

              // roseType: "area",

              itemStyle: {

                normal: {

                  color: function (colors) {

                    var colorList = [

                      "rgb(250, 133, 133)",

                      "rgb(108, 200, 121)",

                    ];

                    return colorList[colors.dataIndex];

                  },

                  borderRadius: 0,

                },

              },

              label: {

                show: this.labelShow,

                alignTo: "edge",

                formatter: "{name|{b}}\n{time|{d} %}",

                minMargin: 5,

                edgeDistance: 10,

                lineHeight: this.lineHeight,

                rich: {

                  time: {

                    fontSize: this.labelFontSize,

                    color: this.labelColor,

                  },

                  name: {

                    fontSize: this.labelFontSize,

                    color: this.labelColor,

                  },

                },

              },

              labelLine: {

                length: 15,

                length2: 0,

                maxSurfaceAngle: 80,

              },

              // label: {

              //   normal: {

              //     show: true,

              //     textStyle: {

              //       color: this.labelColor,

              //       fontSize: this.labelFontSize,

              //     },

              //     formatter: "{per|{d}%}",

              //     rich: {

              //     }

              //   },

              // },

              data: this.typeAnalysisData,

            },

          ],

        };

        this.myEchart.setOption(option);

      }, 500);

    },

  },

  mounted() {

    this.drawChart();

  },

  watch: {

    typeAnalysisData: {

      handler(newName, oldName) {

        this.$nextTick(() => {

          this.drawChart();

          window.addEventListener("resize", this.drawChart);

        });

      },

      deep: true,

    },

  },

  destroyed() {

    window.removeEventListener("resize", this.drawChart);

  },

};

</script>

<style lang="scss" scoped>

</style>

2.内容

(1)标签

<rose-chart

                      :typeAnalysisData="dataList"

                      :color="color"

                      :labelShow="false"

                      :radius="radius"

                      :center="center"

                      legendLeft="right"

                      legendTop="middle"

                    ></rose-chart>

(2)数据

// 饼状图

      dataList: [

        { value: 1048, name: "异常设备" },

        { value: 735, name: "正常设备" },

      ],

      color: ["#3c4a73", "#00a0e9", "#090", "#f00", "#f00"],

      radius: ["50%", "60%"],

      center: ["50%", "50%"],


http://www.ppmy.cn/news/25513.html

相关文章

哈希表题目:矩阵置零

文章目录题目标题和出处难度题目描述要求示例数据范围进阶解法一思路和算法代码复杂度分析解法二思路和算法代码复杂度分析解法三思路和算法代码复杂度分析题目 标题和出处 标题&#xff1a;矩阵置零 出处&#xff1a;73. 矩阵置零 难度 3 级 题目描述 要求 给定一个 m…

防火墙有关iptables的知识点

基本概念 什么是防火墙 在计算中&#xff0c;防火墙是基于预定安全规则来监视和控制传入和传出网络流量的网络安全系统。该计算机流入流出的所有网络通信均要经过此防火墙。防火墙对流经它的网络通信进行扫描&#xff0c;这样能够过滤掉一些攻击&#xff0c;以免其在目标计算机…

FPGA时序约束与分析 --- 时序约束概述

本系列参考文献 — FPGA时序与约束分析-吴厚航 FPGA从综合到实现需要的过程如下&#xff1a;synth_design -> opt_design -> place-design -> phys_opt_design -> route_design 1、时序约束的理解 2、时序约束的基本路径 3、时序约束的步骤 4、时序约束的主要方法…

postman使用简介

1、介绍 postman是一款功能强大的网页调试和模拟发送HTTP请求的Chrome插件&#xff0c;支持几乎所有类型的HTTP请求 2、下载及安装 官方文档&#xff1a;https://www.getpostman.com/docs/v6/ chrome插件&#xff1a;chrome浏览器应用商店直接搜索添加即可&#xff08;需墙&…

CSDN 编辑器 Marddown 语法备忘

原文链接&#xff1a;https://blog.csdn.net/blogdevteam/article/details/103478461 本文对其二次加工&#xff0c;增加渲染样式、补充例程、添加未收录的常用语法。 CSDN Markdown 编辑器遵循 CommonMark spec 语法规范。 快捷键 撤销&#xff1a;Ctrl/Command Z 重做&…

IP协议详解

目录 前言&#xff1a; IP协议 提出问题 解决方案 地址管理 子网掩码 路由选择 小结&#xff1a; 前言&#xff1a; IP协议作为网络层知名协议。当数据经过传输层使用TCP或者UDP对数据进行封装&#xff0c;然后当数据到达网络层&#xff0c;基于TCP或UDP数据包继续进行…

SHA1详解

目录 一、介绍 二、与MD5的区别 1、对强行攻击的安全性 2、对密码分析的安全性 3、速度 三、应用 1、文件指纹 2、Git中标识对象 四、算法原理 1、填充消息 2、消息处理 3、数据运算 &#xff08;1&#xff09;链接变量 &#xff08;2&#xff09;步函数 一、介绍…

区块链知识系列 - 系统学习EVM(四)-zkEVM

区块链知识系列 - 系统学习EVM(一) 区块链知识系列 - 系统学习EVM(二) 区块链知识系列 - 系统学习EVM(三) 今天我们来聊聊 zkEVM、EVM 兼容性 和 Rollup 是什么&#xff1f; 1. 什么是 Rollup rollup顾名思义&#xff0c;就是把一堆交易卷&#xff08;rollup&#xff09;起来…