【echarts图标底部标签legend展示数量及字符长度限制】

echarts图标底部标签legend数量显示限制

当界面底部的标签项太多时。。。是不是很头大

在这里插入图片描述

如何限制

其实很简单, 就是对legend中的data属性做特殊的处理就行了, 代码如下, 关键代码在getCharts方法中,已经在代码中做了标注

<template>
  <div :id="chartBoxItem.idName" class="doughnutChar" />
</template>

<script>
export default {
  name: 'DoughnutChar',
  components: {},
  props: {
    chartBoxItem: {
      type: Object,
      default: () => {
      return {
          chartName: '测试数据',
          data: [{ name: '数据1', value: 11 }, { name: '数据2', value: 22 }, { name: '数据3', value: 33 }, { name: '数据4', value: 11 }, { name: '数据5', value: 11 }, { name: '数据6', value: 33 }, { name: '数据7', value: 11 }, { name: '数据8', value: 22 }, { name: '数据9', value: 55 }],
          idName: 'test'
        }}
    }
  },
  data() {
    return {}
  },
  computed: {},
  watch: {
    chartBoxItem: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val && val.idName && val.chartName) {
          this.$nextTick(() => {
            this.getChart()
          })
        }
      }
    }
  },
  created() {},
  mounted() {
  },
  // 组件方法
  methods: {
    getChart() {
      const _that = this
      // 设置底部标签展示的内容的数量
      const text_data = this.chartBoxItem.data.map(item => item.name).slice(0, 5)
      const myEChart = this.$echarts.init(document.getElementById(this.chartBoxItem.idName))
      myEChart ? myEChart.clear() : ''
      const option = {
        legend: {
          bottom: '0',
          left: 'center',
          // 控制底部标签样式大小
          // itemGap: 15,
          // itemWidth: 9,
          // itemHeight: 10,
          textStyle: {
            color: 'rgb(187,192,200)'
          },
          // 标签长度太长做如下限制
          formatter: function(name) {
            return (name.length > 14 ? (name.slice(0, 14) + '...') : name)
          },
          // 底部标签展示的内容
          data: text_data
      },
        title: [
          {
            text: this.chartBoxItem.chartName,
            textAlign: 'center',
            left: '30%',
            top: 10,
            textStyle: {
              fontSize: 12,
              color: '#fff'
            }
          }
        ],
        series: [
          {
            name: '测试数据',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '12',
                fontWeight: 'bold',
                color: '#7d818d'

              }
            },
            labelLine: {
              show: false
            },
            data: this.chartBoxItem.data,
            bottom: '10%'
          }
        ]
      }
      myEChart.setOption(option)
      myEChart.on('click', function(params) {
      // ... 省略部分无关代码
      })
    }
  }
}
</script>

    <style  lang='scss'>
     .doughnutChar{
        width: 25%;
        height:100%;
      }
    </style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值