echarts中饼图的指示线加圆点,文字向外对齐

需求:echarts中饼图的指示线加圆点,文字向外对齐

效果图如下:

代码实现:

<template>
  <!-- <img :src="barbg" style="width: 18px; height: 100px;;"> -->
  <div :id="props.id" style=" width: 400px;"></div>
</template>

<script setup>
import * as echarts from 'echarts';
import { nextTick, onBeforeUnmount, onMounted } from 'vue';

const props = defineProps({
  id: {
    type: String,
  },
  data: {
    type: Array,
    default: () => {
      return [];
    },
  },
});
let timer = null;
let myChart = null;
// const data = reactive({
// });

onMounted(() => {
  nextTick(() => {
    // 一条数据
    myChart = echarts.init(document.getElementById(props.id));
    initEcharts1();
    window.addEventListener('resize', resizeEcharts);
  });
});


const initEcharts1 = () => {
  //职称结构取数
  
  const alignTo = 'edge';
  var colorList = [
    '#77E750',
    '#FFDF02',
    '#F67825',
    '#F93148',
    '#A4036E',
    '#821313',
  ];

  //职称结构图表
  let option = {
    // color: colorList,
    backgroundColor: '#13305D',
    tooltip: {
      trigger: 'axis',
    },
    legend: {
      show: false,
      // data:getsjjg,
      itemWidth: 6,
      itemHeight: 6,
      top: 6,
      // top: -12px
      textStyle: {
        color: '#fff',
      },
    },
    title: {
      text: '自定义',
      x: 'center',
      y: 'center',
      textStyle: {
        color: '#fff',
        fontSize: 12,
        lineHeight: 25,
        fontWeight: 400,
      },
    },
    series: [
      {
        itemStyle: {
          normal: {
            borderColor: '#13305D',
            borderWidth: 3,
            color: (params) => {
              let item = colorList[params.dataIndex];
              return item;
            },
          },
        },
        type: 'pie',
        silent: true, //取消高亮
        radius: ['45%', '62%'],
        center: ['50%', '50%'],
        labelLine: {
          // width: 4,
          // length: 10,
          // length2: 200,
          show: true,
          color: 'red',
          lineStyle: {
            color: 'red',
            width: 1,
            // type: 'dashed',
          },
        },
        label: {
          formatter: (params) => {
            console.log(params);
            // return `{a|${params.name}}\n{hr|}\n {c|${params.percent}}`;
            return `{c|${params.name}}\n{d|${params.percent}%}`;
          },
          // padding: [0, -50, 0, -50],
          alignTo: alignTo,
          edgeDistance: '5%',

          width: 50,
          rich: {
            d: {
              fontSize: 12,
              color: '#fff',
              padding: [4,0,4,0],
            },
            c: {
              fontSize: 12,
              color: '#fff',
              padding: [4,0,4,0],
            },
          },
        },
        data: props.data,
        z: -1,
      },
      {
        itemStyle: {
          normal: {
            borderColor: '#13305D',
            borderWidth: 3,
            color: (params) => {
              let item = colorList[params.dataIndex];
              return item;
            },
          },
        },
        type: 'pie',
        silent: true, //取消高亮
        radius: ['45%', '62%'],
        center: ['50%', '50%'],
        labelLine: {
          show: true,
          color: 'yellow',
          lineStyle: {
            color: 'yellow',
            width: 1,
          },
        },
        label: {
          formatter: (params) => {
            console.log(params);
            return `{i|}`;
          },
          padding: [0, -5, 0, -5],
          align: 'center',
          alignTo: alignTo,
          edgeDistance: '5%',
          rich: {
            i: {
              backgroundColor: 'red',
              borderRadius: 4,
              width: 4,
              height: 4,
              padding: [0, 0, 0, 0],
            },
          },
        },
        data:  props.data,
        z: 1,
      },
    ],
  };
  option && myChart.setOption(option);
};

const resizeEcharts = () => {
  if (timer !== null) {
    clearTimeout(timer);
  }
  timer = setTimeout(() => {
    if (myChart) {
      myChart.resize();
    }
  }, 300);
};
onBeforeUnmount(() => {
  window.removeEventListener('resize', resizeEcharts);
});
</script>

<style lang="scss" scoped></style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts,可以通过设置饼图指示线加圆点来实现。可以使用series的labelLine和label属性来控制引导线和圆点的样式。首先,需要设置labelLine的show属性为true,然后通过设置length和length2来控制引导线的长度。同时,可以通过设置label的formatter属性来自定义引导线上的文字显示格式。而圆点的颜色可以通过设置itemStyle的color属性来实现。 以下是一个示例代码,展示了如何在ECharts实现饼图的引导线加圆点效果: ```javascript let data = [ { "name": "业务1", "value": 25 }, { "name": "业务2", "value": 10 }, { "name": "业务3", "value": 15 }, { "name": "业务4", "value": 15 }, { "name": "业务5", "value": 25 }, { "name": "业务6", "value": 10 } ]; let option = { backgroundColor: '#fff', animation: true, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, labelLine: { normal: { show: true, length: 15, length2: 110, align: 'right' }, emphasis: { show: true } }, itemStyle: { normal: { label: { show: true, formatter: [ '{a|{d}% {b}}', '{b|}' ].join('\n'), rich: { a: { left: 20, padding: [0, -100, -20, -100] }, b: { height: 5, width: 5, lineHeight: 5, marginBottom: 10, padding: [0, -5], borderRadius: 5, backgroundColor: 'red' } } } } }, data: data } ] }; ``` 通过以上代码,可以实现饼图的引导线加圆点效果。其,引导线的长度可以通过调整length和length2来控制,圆点的颜色可以通过设置itemStyle的backgroundColor属性来定义。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值