echarts 实现legend添加icon

2 篇文章 0 订阅

实现的最终效果
在这里插入图片描述
高中低是icon图,后面的数字是根据接口动态变化,可切换图例查看不同图例的数据

一 文档位置

在这里插入图片描述

二 关键代码部分

在这里插入图片描述
注意事项:

  1. 图片需要放在public文件夹下面,不然获取不到
  2. 后台返回的legend名称需要跟data里面的数据对应起来

三 图例名称动态展示
在这里插入图片描述
根据后台返回的数据来进行sum求和
在这里插入图片描述

四 整体代码

1 区分type是因为页面上有两个堆叠图,并且是不同的图例,就使用了type来进行区分

import { color } from '../../hooks/color';
import {sum} from 'lodash';
export function useBarStack(series = [], xaxis,type = 'left') {
  const legend = [];
  const seriesList = [];
  const sumList = getSum(series,type);
  series &&
    series.forEach((v, i) => {
      legend.push(v.name);
      seriesList.push({
        stack: 'total',
        barWidth: 4,
        name: v.name,
        data: v.data,
        type: 'bar',
        showSymbol: false,
        itemStyle: {
          normal: {
            label: {
              show: false,
              position: 'top',
              color: '#fff'
            },
            color: color.quoatBar[i]
          }
        }
      });
    });
  const dataleft = [
    {
      name: '高',
      textStyle: {
        fontSize: 25,
        fontWeight: 'bold',
        color: 'red',
        fontFamily: 'monospace'
      },
      icon: 'image://img/charts/quoat/gao.png'
    },
    {
      name: '中',
      textStyle: {
        fontSize: 25,
        fontWeight: 'bold',
        color: '#FAA81A',
        fontFamily: 'monospace'
      },
      icon: 'image://img/charts/quoat/zhong.png'
    },
    {
      name: '低',
      textStyle: {
        fontSize: 25,
        fontWeight: 'bold',
        color: '#22A7E4',
        fontFamily: 'monospace'
      },
      icon: 'image://img/charts/quoat/di.png'
    }
  ];
  const dataright = [
    {
      name: '特大',
      textStyle: {
        fontSize: 25,
        fontWeight: 'bold',
        color: 'red',
        fontFamily: 'monospace'
      },
      icon: 'image://img/charts/quoat/td.png'
    },
    {
      name: '重大',
      textStyle: {
        fontSize: 25,
        fontWeight: 'bold',
        color: '#FAA81A',
        fontFamily: 'monospace'
      },
      icon: 'image://img/charts/quoat/zd.png'
    },
    {
      name: '较大',
      textStyle: {
        fontSize: 25,
        fontWeight: 'bold',
        color: '#22A7E4',
        fontFamily: 'monospace'
      },
      icon: 'image://img/charts/quoat/jd.png'
    },
    {
      name: '一般',
      textStyle: {
        fontSize: 25,
        fontWeight: 'bold',
        color: '#47967A',
        fontFamily: 'monospace'
      },
      icon: 'image://img/charts/quoat/yb.png'
    }
  ];
  // 基于危险等级的告警响应时长
  const barBase = {
    legend: {
      show: true,
      orient: 'horizontal',
      itemWidth: type == 'left' ? 23 : 40,
      itemHeight: type == 'left' ? 23 : 23,
      left: 0,
      itemGap: type == 'left' ? 32 : 28,
      // top: -10,
      data: type == 'left' ? dataleft : dataright,
      formatter: function (v) {
        console.log(sumList,v,2323)
        const l = sumList.filter(i=>{
          return i.name == v;
        })
        return l[0].sum;
      }
    },

  const optBar = {
    ...barBase,
    legend: {
      ...barBase.legend
      // data: legend
    },

    series: seriesList
  };

  return optBar;
}

const getSum = (series,type) => {
  const listLeft = [
    {name:'高',sum:0},
    {name:'中',sum:0},
    {name:'低',sum:0},
  ]
  const listRight = [
    {name:'特大',sum:0},
    {name:'重大',sum:0},
    {name:'较大',sum:0},
    {name:'一般',sum:0},
  ]
  let list = [];
  if (type == 'left') {
    list = listLeft;
  }else {
    list = listRight;
  }
  list.forEach((v,i,arr)=>{
    series.forEach((v2)=> {
      if (v.name == v2.name) {
        arr[i].sum = sum(v2.data);
      }
    })
  })
  return list
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,legendEcharts中的一个组件,用于展示图例。根据引用,在配置legend时,可以通过设置icon属性来指定图例的形状,可选的形状包括circle、rect、roundRect、triangle、diamond、pin、arrow和none。同时,可以通过设置itemGap属性来调整图例之间的间距。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [echarts legend 排列问题](https://download.csdn.net/download/qq_36437172/12420436)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [echarts 实现legend添加icon](https://blog.csdn.net/weixin_44876807/article/details/126306952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [echarts图例(legend)显示设置位置,样式,内容(百分比,水平,竖直显示,icon样式)](https://blog.csdn.net/xia_tian666/article/details/119890996)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值