当前位置: 首页 > news >正文

echarts饼图封装

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.lryc.cn/news/9142.html

相关文章:

  • Web3.0 教学基础一
  • body使用渐变色无效的原因之一:html没有设置高度
  • Python3 函数实例及演示
  • HTB打靶(Active Directory 101 Multimaster)
  • 漏洞预警|Apache Sling JCR Base 存在JNDI注入漏洞
  • 【学习笔记】DFA的构造
  • MyBatis 之二(增、删、改操作)
  • 28k入职腾讯测试岗那天,我哭了,这5个月付出的一切总算没有白费~
  • 【surfaceflinger源码分析】surfaceflinger进程的消息驱动模型
  • 「架构师」001计算机组成与体系结构
  • 既然有HTTP协议,为什么还要有RPC
  • 【新2023】华为OD机试 - 选座位(Python)
  • 数据分析与SAS学习笔记4
  • Xepor:一款针对逆向工程和安全分析的Web路由框架
  • Hadoop核心组成和生态系统简介
  • Flutter-Charts_painter大数据量绘制性能优化-数据收敛
  • 使用 GeForce Experience 更新 NVIDIA GPU 显卡驱动
  • Java泛型的<? super T>,<? extend T>的区别
  • 如何做出好看的Excel可视化图表?
  • 智能吸吹一体式方案设计特点
  • CSDN 编辑器 Marddown 语法备忘
  • 回归预测 | MATLAB实现NGO-BiLSTM北方苍鹰算法优化双向长短期记忆网络多输入单输出回归预测
  • Linux——操作系统安装
  • AFLNET lightftp项目报错解决方法
  • av 146 003
  • 干了1年“点点点”,自己辞职了,下一步是继续干测试还是转开发?
  • 国产技术迎来突破,14nm芯片横空出世,低代码也有好消息
  • 使用clickhouse-backup工具备份clickhouse数据库
  • python cartopy绘制扇形区域图/cartopy绘制北极部分区域
  • 如何设置股票接口版交易软件的指标涨跌家数?