VUE 封装一个echart组件

子组件

<template>
  <div :id="id" ref="echarts" :data="data"></div>
</template>
<script>
import echarts from "echarts";
export default {
  props: ["id", "data"],
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    data: {
      handler(newValue, oldValue) {
        if (newValue) {
        this.drawLineGraph(this.id, newValue);
        }else{
        this.drawLineGraph(this.id, oldValue);
        }
},
      deep: true,
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.drawLineGraph(this.id, this.data);
    });
  },
  methods: {
    drawLineGraph(id, data) {
      // eslint-disable-next-line no-unused-vars
      let _this = this;
      this.chart = echarts.init(this.$refs.echarts);
      this.chart.setOption(data, true,true);
      window.addEventListener("resize", function () {
        _this.chart.resize();
      });
    },
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.clear();
    }
  },
  components: {},
};
</script>

<style scoped>
</style>

父组件

 <linegraph
              :id="'ss'"
              :data="optionPies"
              style="width: 500px; height: 300px"
            ></linegraph>
// 导入 子组件
import linegraph from "./Echart";
// 引用子组件
components: {  linegraph },

// 数据源
      this.optionPies = {
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          left: "center",
          bottom: "6",
          data: ["微信支付笔数", "对公支付笔数"],
        },
        color: ["#2FCBCB", "#34A0FF"],
        series: [
          {
            name: "支付笔数",
            type: "pie",
            radius: ["30%", 95],
            center: ["50%", "38%"],
            label: {
              normal: {
                show: false,
              },
            },
            data: this.wxListtotal,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
          },
        ],
      };

 

上一篇:Spire.XLS:一款Excel处理神器


下一篇:helm-实际操作