echarts自定义legend方法

思路:在配置legend里面设置一个selected,名字和series里面的name对应,name名字是必须的,legend里面的data是可配置或不配置都可以,否则legend无效,不显示,通过true和false控制显示或不显示。配置本身的legend设置不显示。通过自己写的checkbox,可自定义样式,来控制legend
如下:

data(){
  return:{
    //折线图例
    theChart:null,
    //折线图配置项
    option : {
     legend: {
     show:false,//是否显示图例,如果要自定样式,则false
     data:['line1','line2'],//series的name,也可不配置
      selected:{
        line1:false,//可设置变量控制check1
        line2:true,//可设置变量控制check2 }
      },
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: 'line1',//名字需要和legend对应得上
          data: [80, 32, 91, 93, 129, 130, 120],
          type: 'line',
          smooth: true
        }   ,
        {
          name: 'line2',
          data: [24, 32, 41, 61, 21, 31, 53],
          type: 'line',
          smooth: true
        }
      ]
    }
  }
}
 
methods:{
  changeLegend(params){
    //初始化图例配置项,
     let that=this;
    let theOption=that.option;
    //更新图例配置,或是改变check1,check2的变量,或是自定义组件绑定theOption.legend.selected.line1
    if(params=='line1'){
       theOption.legend.selected.line1=!theOption.legend.selected.line1
    }
    if(params=='line2'){
       theOption.legend.selected.line2=!theOption..legend.selected.line2
    }
    //绑定配置项,第二个参数true是为了清除series切换时上次数据的缓存
    this.theChart.setOption(theOption,true);
    this.theChart.resize();//更新页面数据
  },
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值