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,

            itemHeight: 4,

            itemWidth: 8,

            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%"],

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue2中封装echarts饼图可以使用chartPan.vue组件。在该组件中,可以通过设置chart-type属性为"pie"来指定使用饼图。同时,可以设置chart-height属性来定义饼图的高度。在组件中,可以使用slot来自定义右侧的内容,比如添加刷新按钮和放大按钮。在代码中,可以通过this.$refs来引用chartPan组件,并调用lineInit方法来初始化饼图的数据。具体的代码示例如下: ```html <chartPan ref="pie" chart-type="pie" chart-height="200px"> <div slot="right"> <i class="iconfont icon-color icon-shuaxin" @click="refreshPieChart" /> <i class="iconfont icon-color icon-fangda" style="margin-left: 12px" @click="zoomInPieChart" /> </div> </chartPan> ``` ```javascript // 在组件中引用chartPan组件,并初始化饼图数据 this.$refs.pie ? this.$refs.pie.lineInit(chartDate, chartData, '(%)', this.tab) : ''; ``` 以上代码中的chartDate和chartData分别表示饼图的日期和数据。通过调用lineInit方法,可以将这些数据传递给chartPan组件,从而实现饼图封装和展示。 #### 引用[.reference_title] - *1* *2* [vue2之echarts封装 折线图,饼图,大图](https://blog.csdn.net/weixin_47409897/article/details/130371366)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值