echarts使用饼图之自定义饼图图例的排列方式,水平,垂直,两端排列

在这里插入图片描述
1,引入饼图
这一步大多数应该都会,官网有例子,直接cv就可以用,简单提一下吧

<template>
  <div class="box">
    <div id="mycircle"></div>
  </div>
</template>
<script>
export default {
  data(){
    return{
    	option: {
			tooltip:{},
			legend:{},
			series:[],
			......
		}
    }
  },
  methods:{
	async init(){
	  var myChart = this.$echarts.init(document.getElementById('mycircle'));
      myChart.setOption(this.option);
      myChart.clear();
      await getIndex7DayException().then(data => {
		// data返回值格式就是[{name: '', value: ''}],有且只能是name和value这两个key,其他都不行
		// 如果返回值是其他的,那就需要转化一下了
        data.map(v =>{
          let keymap = {count: 'value',areaName: 'name'}  // 将count字段转成value字段,areaName字段转化成name字段
          Object.keys(v).map(k =>{
            let newKey = keymap[k]
            if (newKey) {
              v[newKey] = v[k]
              delete v[k]
            }
          })
        })
        this.option.series[0].data = data;
        myChart.setOption(this.option);
        window.addEventListener("resize", () => { myChart.resize() });
	  }
	}
  },
  mounted(){
    this.init()
  }
}
</script>

到这为止,图表就可以正常显示了,下面开始自定义图例的排列方式
1.水平顶部(底部)排列
top: 0
在这里插入图片描述
bottom: 0
在这里插入图片描述

legend:{
 top: '0',
 //bottom: '0'
 textStyle: { color: '#fff'},
}
            

2,垂直左右排列
垂直属性: orient: ‘vertical’,
left: 0
在这里插入图片描述
right: 0
在这里插入图片描述

legend:{
  left: '0',
  // right: '0'
  textStyle: {  color: '#fff'},
  orient: 'vertical',
}

3,两端排列
顶部一半,底部一半
在这里插入图片描述

        legend: [
          {
            top: '0',
            textStyle: {  color: '#fff', padding: [11,0,11,0] },
            data: [],
          },
          {
            bottom: '0',
            textStyle: {  color: '#fff', padding: [11,0,11,0] },
            data: [],
          },
        ],

        init(){
        	  this.option.legend[0].data = legendName.slice(0,5)
     	      this.option.legend[1].data = legendName.slice(-5)
        }

左边一半,右边一半
在这里插入图片描述

        legend: [
          {
            x: 'left',
            textStyle: {  color: '#fff', padding: [11,0,11,0] },
            orient: 'vertical',
            data: [],
          },
          {
            x: 'right',
            textStyle: {  color: '#fff', padding: [11,0,11,0] },
            orient: 'vertical',
            data: [],
          },
        ],

   init(){
        	  this.option.legend[0].data = legendName.slice(0,5)
     	      this.option.legend[1].data = legendName.slice(-5)
        }

4,图例后面拼接占比百分比
每个图例后面拼接该部分占整体的百分比
1,上下左右排列+占比百分比
在这里插入图片描述

        legend:{
            x: 'left',
            textStyle: {  color: '#fff',  },
            orient: 'vertical',
            data: [],
            formatter: e =>{
              var data = this.option.series[0].data;
              var total = 0;
              var val = 0;
              data.forEach(el => {
                total += el.value;
                if(e == el.name) val = el.value
              });
              return `${e}  ${((val / total)*100).toFixed(2)}%`
            }
          },

this.option.legend.data = legendName

2.两端排列+占比百分比
在这里插入图片描述

        legend: [
          {
            x: 'left',
            textStyle: {  color: '#fff', padding: [11,0,11,0] },
            orient: 'vertical',
            data: [],
            formatter: e =>{
              var data = this.option.series[0].data;
              var total = 0;
              var val = 0;
              data.slice(0,5).forEach(el => {
                total += el.value;
                if(e == el.name) val = el.value
              });
              return `${e}  ${((val / total)*100).toFixed(2)}%`
            }
          },
          {
            x: 'right',
            textStyle: {  color: '#fff', padding: [11,0,11,0] },
            orient: 'vertical',
            data: [],
            formatter: e =>{
              var data = this.option.series[0].data;
              var total = 0;
              var val = 0;
              data.forEach(el => {
                total += el.value;
                if(e == el.name) val = el.value
              });
              return `${e}  ${((val / total)*100).toFixed(2)}%`
            }
          },
        ],

		this.option.legend[0].data = legendName.slice(0,5)
        this.option.legend[1].data = legendName.slice(-5)

记录一下
📢没了,结束了,是不是很简单呐,如有错误,欢迎留言.如有问题,不吝赐教。
📢如果此篇博文对您有帮助,还请动动小手点赞 👍 收藏 ⭐留言 📝呐~,谢谢 ~ ~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

杨同学*

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

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

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

打赏作者

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

抵扣说明:

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

余额充值