echars 图例太多, legend 翻页显示,图例过多处理,格式化图例显示

echars legend过多 翻页显示,格式化图例

最终效果展示
可参考官网实例:
链接: https://www.echartsjs.com/examples/zh/editor.html?c=pie-legend.

图片:
核心代码参考
显示翻页
只需要 在legend 加上 type:‘scroll’ 属性,
一定要绑定data,
一开始我没有在legend绑定data,
都可以正常显示不用翻页 不绑定data是没有问题的,
但是 加上翻页就要绑定data!!!

只要加翻页不用 格式化图例显示的
按照官方的 例子copy过去改下数据源就可以!

  legend: {           
              type: 'scroll', // 显示分页            
              orient: 'vertical',       
              right: 10,           
              top: 20,          
              bottom: 20,           
              data: ld, // 显示分页要加       
              selectedMode: false, // 关闭图例的选择模式  
                 // 格式化图例            
              formatter: function(name) {       
                             if(!arr[name]) {             
                                       return name;         
                             }     
return '{title|' + name + '} \t{value|' + '\t|\t\t' + ((arr[name].value / sum) * 100).toFixed(2) + '%\t' + '} \t{title|' + arr[name].value + ' 个' + '}';               
                    },   
           // 格式化图例          
           textStyle: {     

                   rich: {            
                         title: {    
                                  fontSize: 14,             
                                  lineHeight: 22,         
                                  color: '#595959',       
                                  width: 80          
     },          
                     value: {             
                              fontSize: 14,              
                              lineHeight: 22,              
                              color: 'rgba(0,0,0,0.45)',         
                              width: 30,          
                              align: 'center'      
                               }                
                          }               
                   }        
      },

数据格式

series:
在这里插入图片描述
legend:
在这里插入图片描述
全部代码参考

      let ld = this.legendData;    
      let sum = this.sumCount;       
      let arr = [];      
      for (let i = 0; i < this.dataPie.length; i++) {    
    arr[this.dataPie[i]['name']] = this.dataPie[i];     
           }        
    let that = this;     
    that.per = echarts.init(that.$refs.proPer);        
        let option = {            
            title: {             
               text: '问题类型占比',         
               left: '43%',         
               top: 20              
                     },               
           tooltip: {                
                trigger: 'item',       
                formatter: '{a} <br/>{b}: {c} ({d}%)'                },    
           legend: {      
                type: 'scroll', // 显示分页       
                orient: 'vertical',       
                right: 10,           
                top: 20,        
                bottom: 20,          
                data: ld, // 显示分页要加          
                selectedMode: false, // 关闭图例的选择模式 
                // 格式化图例         
         formatter: function(name) {          
            if(!arr[name]) {     
                return name;          
          }                      
      return '{title|' + name + '} \t{value|' + '\t|\t\t' + ((arr[name].value / sum) * 100).toFixed(2) + '%\t' + '} \t{title|' + arr[name].value + ' 个' + '}';          
                },            
      // 格式化图例           
         textStyle: {             
             rich: {                         
                title: {           
                 fontSize: 14,               
                lineHeight: 22,         
                 color: '#595959',             
               width: 80                         
                  },      
                    value: {   
                     fontSize: 14,  
                 lineHeight: 22,         
               color: 'rgba(0,0,0,0.45)',  
             width: 30,                            
                 align: 'center'             
                                }              
                           }          
                     }            
             },            
         color: ['#8543E0', '#1890FF', '#13C2C2', '#2FC25B', '#FACC14', '#F04864'],          
         series: [            
              {                
                 name: '问题类型占比',    
                 type: 'pie',   
                 center: ['30%', '55%'],           
                 radius: ['53%', '70%'],   
                 avoidLabelOverlap: false,       
         label: {                     
             normal: {            
                 show: false,        
                 position: 'center'         
             },              
             emphasis: {   
                 show: true,     
         textStyle: {         
                 fontSize: '30',                  
                 fontWeight: 'bold'              
                 }                     
           }                 
       },                    
              
      labelLine: {             
         normal: {                           
                 show: false          
                }                  
          },      
     itemStyle: {                    
                 shadowOffsetX: 0,               
                 borderColor: '#fff',         
                 borderWidth: 5             
         },            
                 data: this.dataPie            
               }                 
          ]   
      };
    let index = 0;          
    if (ld.length > 8) {       
          option.legend.top = '0%';       
          option.legend.itemGap = 3;        
     } else {     
          option.legend.top = '25%';    
       }  
 
 that.per.setOption(option);    
 that.per.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: 0 }); // 设置默认选中高亮部分         
   that.per.on('mouseover', function(e) {    
     if (e.dataIndex != index) {   
        that.per.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: index });           
            }         
       });       
   that.per.on('mouseout', function(e) {       
      index = e.dataIndex;   
      that.per.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: e.dataIndex 
         });    
   });
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值