echarts 全局换肤 echarts根据系统颜色换肤,echarts自定义icon,echarts发光效果

文章展示了如何在Vue应用中使用Echarts库创建饼图,并根据自定义的json主题文件动态切换图表颜色。通过监听pieData和theme的变化,初始化或更新图表,并使用dispose方法销毁并重建Echarts实例以实现主题切换。同时,文章还详细介绍了数据处理和颜色配置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网定义颜色网址
效果
在这里插入图片描述
代码

<template>
  <div :id="id"></div>
</template>
<script>
import echarts from 'echarts';
import theme from './theme.json'; // 定义颜色的json
import { mapGetters } from 'vuex';
export default {
  props: ['id', 'pieData'],
  data() {
    return {
      charts: null,
      pieNames: [],
      imageUrl: require('@/assets/chart/chartalarm.png'), // icon图片
      color: [
        'rgba(226, 59, 65,1)',
        'rgba(201, 20, 34, 1)',
        'rgba(185, 40, 41, 1)',
        'rgba(253, 173, 56, 1)',
        'rgba(253, 195, 68, 1)',
        'rgba(251, 105, 164, 1)',
      ],
    };
  },
  watch: {
    pieData(val) {
      this.pieData.forEach((ele) => {
        this.pieNames.push(ele.name);
      });
      if (val.length > 0) {
        this.$nextTick(() => {
          if (this.theme === 'white-theme') {
            this.imageUrl = require('@/assets/chart/chartalarm2.png');
            this.initChart(this.id, theme.whitetheme);
          } else {
            this.imageUrl = require('@/assets/chart/chartalarm.png');
            this.initChart(this.id, theme.blacktheme);
          }
        });
      }
    },
    theme: {
      handler(val) {
        // 每次切换先销毁echarts实例,重新绘制echarts
        this.charts.dispose();
        if (val === 'white-theme') {
          this.$nextTick(() => {
            this.imageUrl = require('@/assets/chart/chartalarm2.png');
            this.initChart(this.id, theme.whitetheme);
          });
        } else {
          this.$nextTick(() => {
            this.imageUrl = require('@/assets/chart/chartalarm.png');
            this.initChart(this.id, theme.blacktheme);
          });
        }
      },
    },
  },
  computed: {
    ...mapGetters(['theme']),
  },
  methods: {
    initChart(id, themeType) {
      // 注册主题
      echarts.registerTheme('slef-theme', themeType);
      // 创建实例
      this.charts = echarts.init(document.getElementById(id), 'slef-theme');
      let option = {
        // animation: true, // 是否开启动画 默认为true
        // animationDuration: 500, // 动画时长 可以接受 回调函数
        // animationEasing: 'cubicInOut', // 缓动动画 动画样式方法
        // animationThreshold: 8, // 动画阈值 data 里面的元素多余8个停止动画
        color: this.color, // 色值
        // 定义icon
        graphic: {
          elements: [
            {
              type: 'image',
              style: {
                image: this.imageUrl,
                width: 130,
                height: 130,
              },
              left: 'center',
              top: 'center',
            },
          ],
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
          position: 'inside',
        },
        legend: {
          bottom: 'bottom',
          data: this.pieNames,
        },
        series: [
          {
            name: '报警分析',
            type: 'pie',
            radius: ['43%', '55%'],
            data: this.createChartData(),
          },
        ],
      };
      // true 可选。是否不跟之前设置的 option 进行合并。默认为 false。即表示合并。如果为 true,表示所有组件都会被删除,然后根据新 option 创建所有新组件。
      this.charts.setOption(option, true);
    },
    // 创建Ecahrts数据,定义颜色
    createChartData() {
      let pieChartData = this.pieData;
      let data = [];
      for (var i = 0; i < pieChartData.length; i++) {
        data.push({
          value: pieChartData[i].value,
          name: pieChartData[i].name,
          itemStyle: {
            normal: {
              // borderWidth: 5,
              shadowBlur: 30,
              // borderColor: this.color[i],
              shadowColor: this.color[i],
            },
          },
        });
      }
      return data;
    },
  },
};
</script>

theme.json 文件

{
    "blacktheme": {
        "legend": {
            "textStyle": {
                "color": "#fff"
            }
        }
    },
    "whitetheme": {
        "legend": {
            "textStyle": {
                "color": "#262626"
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值