vue-echarts 中双y轴点击legend 控制 对应的轴和name显示和隐藏

1、在v-chart 中 定义legend 点击事件

  <v-chart

    :options="options"

    :auto-resize="true"

    style="width: 100%; height: 100%"

    @legendselectchanged="legendselectchanged"

  />

2、定义lengend 事件,通过params 参数来判断y轴们的具体显示和隐藏值,通过setOptions去给设置

    legendselectchanged(params) {

      const legendShow = params.selected

      const arr = []

      // 参数为 '苹果数量':true/false  '李子数量':true/false

      for (const key in legendShow) {

        arr.push(legendShow[key])

      }

      this.setOption({ yAxis1: arr[0], yAxis2: arr[1] })

    },

3、 给对象的轴设置显示与隐藏,其中yAxis 中通过show属性控制显示与隐藏,其中yAxis中的name和 legend 中 data 和 selected 需要设置

this.setOptions(yAxis1,yAxis2){

  const options ={                 

         grid: {  },

         legend: {

             data: ['苹果数量', '李子数量'],

             selected: {

                 苹果数量: yAxis1,

                  李子数量: yAxis2

              }

        },

        xAxis: {

          axisLine: {

            show: true,

          },

          data: ['第一季度', '第二季度', '第三季度', '第四季度']

        },

        yAxis: [

          {

            show: yAxis1,

            name: '苹果数量',

             ......................

          },

          {

            name: '李子数量',

            show: yAxis2,

             ....................

            }

        ],

        series: [

          {

            name: '苹果数量',

            data: [7080, 2040, 10000, 6090],

            zlevel: 999,

           .................................

          },

          {

            name: '李子数量',

            data: [4080, 6040, 10000, 4090],

            yAxisIndex: 1,

            zlevel: 1,

            ................................

          }

        ]

      }

}

4、效果图

  • 26
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,你需要安装 EchartsVue3,可以通过以下命令进行安装: ``` npm install echarts vue@next ``` 然后,在你的 Vue3 项目中,创建一个组件,例如 `DoubleLineChart.vue`,并在模板中添加一个 div 元素,用于渲染 Echarts 图表: ```html <template> <div ref="chart" style="height: 500px"></div> </template> ``` 在组件中,你需要引入 Echarts 和定义图表的配置选项: ```typescript import { defineComponent } from 'vue'; import * as echarts from 'echarts'; export default defineComponent({ name: 'DoubleLineChart', props: { data: { type: Object, required: true, }, }, mounted() { const chart = echarts.init(this.$refs.chart as HTMLDivElement); const option = { // 配置选项 }; chart.setOption(option); }, }); ``` 在配置选项中,你需要定义两个 y ,分别对应两条折线: ```typescript const option = { tooltip: { trigger: 'axis', }, legend: { data: ['销量', '访问量'], }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true, }, xAxis: { type: 'category', boundaryGap: false, data: this.data.xData, }, yAxis: [ { type: 'value', name: '销量', axisLabel: { formatter: '{value} 件', }, }, { type: 'value', name: '访问量', axisLabel: { formatter: '{value} 次', }, }, ], series: [ { name: '销量', type: 'line', yAxisIndex: 0, data: this.data.y1Data, }, { name: '访问量', type: 'line', yAxisIndex: 1, data: this.data.y2Data, }, ], }; ``` 其中,`data` 属性是一个对象,包含了 x 和两个 y 的数据: ```typescript export default defineComponent({ name: 'DoubleLineChart', props: { data: { type: Object, required: true, }, }, mounted() { const chart = echarts.init(this.$refs.chart as HTMLDivElement); const option = { tooltip: { trigger: 'axis', }, legend: { data: ['销量', '访问量'], }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true, }, xAxis: { type: 'category', boundaryGap: false, data: this.data.xData, }, yAxis: [ { type: 'value', name: '销量', axisLabel: { formatter: '{value} 件', }, }, { type: 'value', name: '访问量', axisLabel: { formatter: '{value} 次', }, }, ], series: [ { name: '销量', type: 'line', yAxisIndex: 0, data: this.data.y1Data, }, { name: '访问量', type: 'line', yAxisIndex: 1, data: this.data.y2Data, }, ], }; chart.setOption(option); }, }); ``` 最后,在父组件中,你可以使用 `DoubleLineChart` 组件,并传递数据给它: ```html <template> <div> <double-line-chart :data="chartData" /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import DoubleLineChart from '@/components/DoubleLineChart.vue'; export default defineComponent({ name: 'App', components: { DoubleLineChart, }, data() { return { chartData: { xData: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'], y1Data: [120, 200, 150, 80, 70, 110, 130], y2Data: [220, 180, 290, 130, 220, 200, 150], }, }; }, }); </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值