echarts使用案例

1.配置legend icon 根据点击事件动态更换样式

html 复制代码
<template>
    <div ref="chart" style="width: 600px; height: 400px;"></div>
  </template>
  
  <script>
  import * as echarts from 'echarts';
  
  export default {
    name: 'EchartsExample',
    data() {
      return {
        chart: null,
        legendIcons: {
          selected: 'path://M512 0a512 512 0 1 0 512 512A512 512 0 0 0 512 0z m299.264 382.208L481.28 712.192a51.2 51.2 0 0 1-70.656 0L212.992 514.56a51.2 51.2 0 0 1 70.656-69.888l162.048 162.048 294.656-295.168a49.664 49.664 0 0 1 48.384-13.056 51.2 51.2 0 0 1 22.528 83.712z',
          unselected: 'path://M512 64c60.5 0 119.2 11.8 174.4 35.2 53.3 22.6 101.3 54.9 142.4 96 41.2 41.2 73.5 89.1 96 142.4C948.2 392.8 960 451.5 960 512s-11.8 119.2-35.2 174.4c-22.6 53.3-54.9 101.3-96 142.4-41.2 41.2-89.1 73.5-142.4 96C631.2 948.2 572.5 960 512 960s-119.2-11.8-174.4-35.2c-53.3-22.6-101.3-54.9-142.4-96-41.2-41.2-73.5-89.1-96-142.4C75.8 631.2 64 572.5 64 512s11.8-119.2 35.2-174.4c22.6-53.3 54.9-101.3 96-142.4s89.1-73.5 142.4-96C392.8 75.8 451.5 64 512 64m0-64C229.2 0 0 229.2 0 512s229.2 512 512 512 512-229.2 512-512S794.8 0 512 0z',
        },
        legendSelected: {
          'Category1': true,
          'Category2': false,
          'Category3': false,
          'Category4': false,
          'Category5': false,
        }
      };
    },
    mounted() {
      this.initChart();
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$refs.chart);
        this.setOption();
        this.chart.on('legendselectchanged', this.updateLegendIcons);
      },
      setOption() {
        const option = {
          title: {
            text: 'ECharts Example'
          },
          tooltip: {
            trigger: 'axis',
          },
          legend: {
            bottom:0,
            data: [
              {
                name: 'Category1',
                icon: this.legendIcons.selected
              },
              {
                name: 'Category2',
                icon: this.legendIcons.unselected
              },
              {
                name: 'Category3',
                icon: this.legendIcons.unselected
              },
              {
                name: 'Category4',
                icon: this.legendIcons.unselected
              },
              {
                name: 'Category5',
                icon: this.legendIcons.unselected
              }
            ],
            selected: this.legendSelected,
          },
          xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          },
          yAxis: {
            type: 'value'
          },
          series: [
          {
            name: 'Category1',
            type: 'bar',
            data: [120, 200, 150, 80, 70, 110, 130]
          }, 
          {
            name: 'Category2',
            type: 'bar',
            data: [60, 100, 90, 130, 140, 120, 160]
          },
          {
            name: 'Category3',
            type: 'bar',
            data: [60, 100, 90, 130, 140, 120, 160]
          },
          {
            name: 'Category4',
            type: 'bar',
            data: [60, 100, 90, 130, 140, 120, 160]
          },
          {
            name: 'Category5',
            type: 'bar',
            data: [60, 100, 90, 130, 140, 120, 160]
          }
          ]
        };
  
        this.chart.setOption(option);
      },
      updateLegendIcons(params) {
        this.legendSelected = params.selected;
        const legendData = [
          {
            name: 'Category1',
            icon: this.legendSelected['Category1'] ? this.legendIcons.selected : this.legendIcons.unselected
          },
          {
            name: 'Category2',
            icon: this.legendSelected['Category2'] ? this.legendIcons.selected : this.legendIcons.unselected
          },
          {
            name: 'Category3',
            icon: this.legendSelected['Category3'] ? this.legendIcons.selected : this.legendIcons.unselected
          },
          {
            name: 'Category4',
            icon: this.legendSelected['Category4'] ? this.legendIcons.selected : this.legendIcons.unselected
          },
          {
            name: 'Category5',
            icon: this.legendSelected['Category5'] ? this.legendIcons.selected : this.legendIcons.unselected
          }
        ];
        this.chart.setOption({
          legend: {
            data: legendData
          }
        });
      }
    }
  };
  </script>
  
  <style scoped>
  </style>
  

效果展示

相关推荐
qq_406176142 小时前
深入浅出 Pinia:Vue3 时代的状态管理新选择
javascript·vue.js·ecmascript
谢白羽2 小时前
vllm实践
android·vllm
电子云与长程纠缠2 小时前
Godot学习03 - 实例化、层级访问、Export
android·学习·godot
德育处主任Pro3 小时前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
毕设源码-朱学姐3 小时前
【开题答辩全过程】以 基于Android的便民系统的设计与实现为例,包含答辩的问题和答案
android
鬼蛟3 小时前
Spring————事务
android·java·spring
叫我一声阿雷吧3 小时前
JS 入门通关手册(23):JS 异步编程:回调函数与异步本质
javascript·es6·前端面试·回调函数·回调地狱·js异步编程·异步本质
zayzy3 小时前
前端八股总结
开发语言·前端·javascript
今天减肥吗3 小时前
前端面试题
开发语言·前端·javascript
小J听不清4 小时前
CSS 外边距(margin)全解析:取值规则 + 实战用法
前端·javascript·css·html·css3