查看: 23|回复: 0

ECharts柱状图自定义lenged 图例图标使用vue项目中assets下的的图片

[复制链接]

2

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2009-3-30
发表于 2025-5-12 09:34:00 | 显示全部楼层 |阅读模式
<template>
  <div>
    <div ref="barChart" style="width: 800px; height: 600px;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import iconA from '@/assets/mhImg/iconTest.png';
// import iconB from '@/assets/icons/icon-b.png';
// import iconC from '@/assets/icons/icon-c.png';
export default {
  data() {
    return {
      types: ['A', 'B', 'C', 'D', 'E', 'F'],
      values: [20, 30, 15, 40, 25, 35],
      iconA: iconA,
      // icons: [iconA, iconB, iconC] // 图片路径数组
    };
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chartDom = this.$refs.barChart;
      const myChart = echarts.init(chartDom);

      const option = {
        // 自定义图例配置
        legend: {
          data: this.types.map((type, index) => ({
            name: type,
            icon: type=='A' ? `image://${iconA}` : ''
          }))
        },
        // // 配置图例,循环使用 assets 图片
        // legend: {
        //   data: this.types.map((type, index) => ({
        //     name: type,
        //     icon: `image://${this.icons[index]}` // 关键:使用 image:// 前缀
        //   }))
        // },
        xAxis: {
          type: 'category',
          data: this.types
        },
        yAxis: {
          type: 'value'
        },
        series: this.types.map((type, index) => ({
          name: type,
          type: 'bar',
          data: [this.values[index]]
        }))
      };

      myChart.setOption(option);
    }
  }
};
</script>

 

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部