vue+echarts封装组件

本人可能写的 不好,如有缺点希望指出,一起进步,所有组件类似写法。

/** 圆环图形 */
<template>
  <div
    class="annulusContainer"
    ref="annulusContainer"
    :optionsData="optionsData"
    :grid="grid"
  ></div>
</template>

<script>
import {
  defineComponent,
  getCurrentInstance,
  onMounted,
  reactive,
  ref,
  toRefs,
  watch,
  markRaw
} from 'vue';
export default defineComponent({
  name: 'annulus',
  props: {
    optionsData: Array,
    grid: {
      type: Object,
      default: () => ({
        left: '0',
        top: '0',
        right: '0',
        bottom: '0',
        containLabel: true
      })
    }
  },
  setup(props) {
    let { proxy } = getCurrentInstance();
    const annulusChartOptions = ref({
      backgroundColor: 'transparent',
      grid: props.grid,
      title: {
        show: true,
        text: '10%',
        x: 'center',
        y: 'center', // 通过x,y将标题(进度)定位在圆环中心
        textStyle: {
          fontSize: '20',
          color: 'white',
          fontWeight: '400',
          fontFamily: 'YouSheBiaoTiHei-Regular, YouSheBiaoTiHei',
          textShadowColor: 'rgba(255, 255, 255, 1)',
          textShadowBlur: '6',
          textShadowOffsetX: '1',
          textShadowOffsetY: '1'
        }
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: ['65%', '90%'],
          avoidLabelOverlap: false,
          hoverAnimation: false, //鼠标移入变大
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '40',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: [
            {
              value: props.optionsData[0],
              name: '',
              itemStyle: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 1,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'rgba(20, 106, 234, 0.800000011920929)' // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: 'rgba(61, 173, 255, 1)' // 100% 处的颜色
                    }
                  ],
                  global: false // 缺省为 false
                }
              }
            },
            {
              value: props.optionsData[1],
              name: '',
              itemStyle: {
                color: 'rgba(20, 120, 231, 0.3)'
              }
            }
          ]
        },
        {
          name: '外边框',
          type: 'pie',
          hoverAnimation: false, //鼠标移入变大
          center: ['50%', '50%'],
          radius: ['60%', '60%'],
          label: {
            normal: {
              show: false
            }
          },
          data: [
            {
              value: props.optionsData[0],
              name: '',
              itemStyle: {
                normal: {
                  borderWidth: 1,
                  borderColor: 'rgba(20, 120, 231, 1)'
                }
              }
            },
            {
              value: props.optionsData[1],
              name: '',
              itemStyle: {
                normal: {
                  color: 'transparent'
                }
              }
            }
          ]
        }
      ]
    });
    const annulusContainer = ref(null); // 放echarts实例
    const annulusChart = ref(null);
    // 等容器dom挂载完才能去实例化echarts
    onMounted(() => {
      annulusChart.value = proxy.$echarts.init(annulusContainer.value, 'dark');
      annulusChart.value.setOption(annulusChartOptions.value);
    });
    window.addEventListener('resize', () => {
      annulusChart.value.setOption(annulusChartOptions.value);
    });
    // 监听echarts配置,有变化就更新实例
    watch(
      () => props.optionsData,
      () => {
        annulusChartOptions.value.series[0].data[0].value = props.optionsData[0];
        annulusChartOptions.value.series[0].data[1].value = props.optionsData[1];
        annulusChartOptions.value.series[1].data[0].value = props.optionsData[0];
        annulusChartOptions.value.series[1].data[1].value = props.optionsData[1];
        annulusChart.value.setOption(annulusChartOptions.value);
      },
      { deep: true }
    );
    return {
      annulusContainer
    };
  }
});
</script>
<style lang="scss" scoped>
.annulusContainer {
  width: 100%;
  height: 100%;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lbchenxy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值