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>
  

效果展示

相关推荐
REDcker9 分钟前
浏览器端Web程序性能分析与优化实战 DevTools指标与工程清单
开发语言·前端·javascript·vue·ecmascript·php·js
随遇丿而安2 小时前
第2周:`EditText` 不只是输入框,它是 Android 输入体验的第一道门
android
我命由我123452 小时前
Kotlin 开发 - lateinit 关键字
android·java·开发语言·kotlin·android studio·android-studio·android runtime
Linsk2 小时前
Java和JavaScript的关系真是雷峰和雷峰塔的关系吗?
java·javascript·oracle
一起搞IT吧2 小时前
Android性能系列专题理论之十:systrace/perfetto相关指标知识点细节含义总结
android·嵌入式硬件·智能手机·性能优化
当时只道寻常2 小时前
浏览器文本复制到剪贴板:企业级最佳实践
javascript
Alice-YUE3 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
是上好佳佳佳呀4 小时前
【前端(十一)】JavaScript 语法基础笔记(多语言对比)
前端·javascript·笔记
莎士比亚的文学花园4 小时前
Linux驱动开发(3)——设备树
开发语言·javascript·ecmascript
01漫游者5 小时前
JavaScript函数与对象增强知识
开发语言·javascript·ecmascript