用vue二次封装一个echarts

2 篇文章 0 订阅

目的:可以重复利用组件

封装组件
<template>
  <div :id="_id" class="echargraph"></div>
</template>
import Vue from "vue";
import echarts from "echarts";
Vue.prototype.$echarts = echarts;
const Echarts = require("echarts");
 name: "chart",
 props: {
    _id: String,
    _titleText: String,
    _colorArr: Array,
    xAxisData: Array,
    yAxisMax: Number,
    yAxisInterval: Number,
    seriesData: Array,
    _yText: String,
    pieRadius: [String, Array, Number]
  },
 watch: {
    seriesData(val) {
      switch (this._yText) {
        case "radar":
          this.radar(this._id, this._titleText, this.seriesData);
          break;
      
      }
    }
  },
mounted() {
    switch (this._yText) {
      case "radar":
        this.radar(this._id, this._titleText, this.seriesData);
        break;
 
    }
  },
    //  雷达图
    radar(_id, _titleText, seriesData) {
      let myChart = Echarts.init(document.getElementById(_id));
      let legendData = [];
      seriesData.map(item => {
        legendData.push(item.name);
      });
      let option = {
        title: {
          text: _titleText
        },
        color: "rgba(183,225,232,0.8)",
        tooltip: {
          backgroundColor: "rgba(255,255,255,0.5)",
          textStyle: {
            color: "black" //设置文字颜色
          }
        },
        radar: {
          name: {
            textStyle: {
              color: "#333"
              // backgroundColor: "#999",
              // borderRadius: 3,
              // padding: [3, 5]
            }
          },
          indicator: [
            { name: "知识度", max: 6500 },
            { name: "任务力", max: 16000 },
            { name: "学习力", max: 30000 },
            { name: "服务力", max: 38000 },
            { name: "情感力", max: 52000 },
            { name: "销售力", max: 25000 }
          ]
        },
        series: [
          {
            type: "radar",
            areaStyle: {
              color: "rgba(183,225,232,0.8)" // 图表背景的颜色
            },

            data: seriesData
          }
        ]
      };
      myChart.setOption(option);
    },
调用组件

 components: {
        chart
    },
      <chart
          :_id="'newAndOld'"
          :_titleText="'分布比例图'"
          :_colorArr="visitorColor"
          :pieRadius="visitorRadius"
          :seriesData="visitorData"
          :_yText="'Pie'"
          class="shadows echargraph"
        ></chart>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值