echarts自定义legend图例

效果如下
在这里插入图片描述
左边饼图大小通过series属性里面的 radius: [“61%”, “76%”],来调整大小,图例在legend里面调整

    // 成本分析
    changeTabType(val) {
      this.tabType = val;
      let bgColor = "#fff";
      let title = "总计";
      let color = ["#f7c122", "#6395fa", "#62daab", "#657798"];
      let echartData = [
        {
          name: "电费",
          value: "3720",
        },
        {
          name: "药剂费",
          value: "2920",
        },
        {
          name: "设备维修费",
          value: "2200",
        },
        {
          name: "其他",
          value: "1420",
        },
      ];
      this.getChart1(bgColor, title, color, echartData);
    },
    // 成本分析
    getChart1(bgColor, title, color, echartData) {
      let formatNumber = function (num) {
        let reg = /(?=(\B)(\d{3})+$)/g;
        return num.toString().replace(reg, ",");
      };
      let total = echartData.reduce((a, b) => {
        return a + b.value * 1;
      }, 0);

      let option = {
        color: color,
        tooltip: {
          trigger: "item",
        },

        legend: {
          orient: "vertical",
          icon: "circle",
          itemWidth: 8, // 图例项宽度
          left: "55%",
          itemGap: 14, // 调整图例项之间的间距
          align: "left",
          top: "middle",
          symbolKeepAspect: false,
          textStyle: {
            color: "#333",
            rich: {
              a: {
                verticalAlign: "right",
                align: "left",
                width: 85,
                fontSize: 14,
              },
              b: {
                align: "left",
                fontSize: 12,
                color: "#ff5722",
              },
              df: {
                align: "left",
                fontSize: 12,
                color: "#f7c122",
              },
              yjf: {
                align: "left",
                fontSize: 12,
                color: "#6395fa",
              },
              sbwxf: {
                align: "left",
                fontSize: 12,
                color: "#62daab",
              },
              qt: {
                align: "left",
                fontSize: 12,
                color: "#657798",
              },
            },
          },
          formatter: (name) => {
            let total = 0;
            let target;
            let className = "b";
            for (let i = 0, l = echartData.length; i < l; i++) {
              total += Number(echartData[i].value);
              if (echartData[i].name == name) {
                target = echartData[i].value;
              }
              if (name == "电费") {
                className = "df";
              }
              if (name == "药剂费") {
                className = "yjf";
              }
              if (name == "设备维修费") {
                className = "sbwxf";
              }
              if (name == "其他") {
                className = "qt";
              }
            }
            let str =
              "{a|" +
              name +
              "}{" +
              className +
              "| " +
              target +
              "/" +
              ((target / total) * 100).toFixed(2) +
              "%}";
            return str;
          },
        },
        title: [
          {
            text: "{name|" + title + "}\n{val|" + formatNumber(total) + "}",
            top: "center",
            left: "17%",
            textStyle: {
              rich: {
                name: {
                  fontSize: 18,
                  fontWeight: "normal",
                  color: "#686f78",
                  padding: [8, 35],
                },
                val: {
                  fontSize: 28,
                  fontWeight: "bold",
                  color: "#333333",
                },
              },
            },
          },
        ],

        series: [
          {
            type: "pie",
            radius: ["61%", "76%"],
            center: ["28%", "50%"],
            data: echartData,
            hoverAnimation: false,
            itemStyle: {
              normal: {
                borderColor: bgColor,
                borderWidth: 1,
              },
            },
            labelLine: {
              normal: {
                length: 20,
                length2: 120,
                lineStyle: {
                  color: "#e6e6e6",
                },
              },
            },
            label: {
              show: false,
            },
          },
        ],
      };
      this.charOption = option;
      window.addEventListener("resize", this.listenResize);
    },

----------------------------分割-----------------------------------------
调整图表的时候标题跟着一起动(始终居中) ,标题放在series里

// 表4
    getChart1(bgColor, title, color, echartData, data) {
      let formatNumber = function (num) {
        let reg = /(?=(\B)(\d{3})+$)/g;
        return num.toString().replace(reg, ",");
      };
      let total = echartData.reduce((a, b) => {
        return a + b.value * 1;
      }, 0);

      let option = {
        color: color,
        tooltip: {
          trigger: "item",
        },
        grid: {
          left: 0,
          right: "10%",
        },
        legend: {
          orient: "vertical",
          icon: "circle",
          itemWidth: 8, // 图例项宽度
          left: "57%",
          itemGap: 18, // 调整图例项之间的间距
          align: "left",
          top: "middle",
          symbolKeepAspect: false,
          textStyle: {
            color: "#333",
            align: "left",
            fontSize: 12,
            rich: {
              a: {
                verticalAlign: "right",
                align: "left",
                width: 85,
                fontSize: 14,
              },
            },
          },
          formatter: (name) => {
            let total = 0;
            let itemRatio = "";
            let className = "";
            let target;
            for (let i = 0, l = echartData.length; i < l; i++) {
              total += Number(echartData[i].value);
              if (echartData[i].name == name) {
                target = echartData[i].value;
                itemRatio = echartData[i].itemRatio;
                className = `color${i}`;
              }
            }
            let str =
              "{a|" +
              name +
              "}{" +
              className +
              "| " +
              target +
              "/" +
              itemRatio +
              "}";
            return str;
          },
        },

        series: [
          {
            type: "pie",
            radius: ["61%", "76%"],
            center: ["24%", "50%"],
            data: echartData,
            avoidLabelOverlap: false,
            hoverAnimation: false,
            labelLine: {
              normal: {
                length: 20,
                length2: 120,
                lineStyle: {
                  color: "#e6e6e6",
                },
              },
            },
            label: {
              show: false,
            },
            tooltip: {
              valueFormatter: function (value) {
                for (let i = 0, l = echartData.length; i < l; i++) {
                  return value + echartData[i].itemUnit;
                }
              },
            },
          },
          // 标题放在这里 
          {
            z: 100,
            type: "gauge",
            radius: "-50%",
            center: ["24%", "50%"], // 需和type: 'pie'中的center一致
            // 配置中间的数字的样式
            detail: {
              // 调节数字位置
              offsetCenter: [-1, 20],
              fontSize: 28,
              fontWeight: "bold",
              color: "#333333",
            },
            pointer: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            // 中间的字和数字 数据
            data: [
              {
                name: "总计",
                value: formatNumber(total),
                title: {
                  // 配置“总计”的样式
                  show: true,
                  fontSize: 18,
                  fontWeight: "normal",
                  color: "#686f78",
                  offsetCenter: ["0", "-10"],
                },
              },
            ],
          },
        ],
      };
      //使用rich,来动态展示legend.title 以不同的颜色描边
      for (let i = 0; i < echartData.length; i++) {
        let key = `color${i}`;

        option.legend.textStyle.rich[key] = {
          color: echartData[i].itemColor,
          align: "left",
          fontSize: 12,
        };
      }

      this.charOption = option;
      window.addEventListener("resize", this.listenResize);
    },
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts中,可以通过自定义legend来实现对图例的样式进行修改。通常情况下,可以使用官方提供的模板加载出样式,然后根据需求进行自定义修改。为了自定义legend的样式,可以使用legend.icon参数进行设置。通过设置legend.icon为自定义的图标,可以实现对图例的样式进行个性化的定制。此外,还可以使用图片链接或者base64图片来作为legend.icon的样式,以实现更多样式的自定义。另外,也可以使用矢量路径来设置legend.icon的样式,通过创建矢量路径并导出,可以实现更加灵活和多样化的图例样式。总结来说,Echarts提供了多种方式来自定义legend的样式,包括使用模板加载样式、自定义图标、使用图片链接或base64图片以及使用矢量路径来实现个性化的图例样式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [echarts自定义legend样式的详细图文教程](https://blog.csdn.net/snow_living/article/details/127791432)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [echarts 图例legend icon)图标自定义的几种方式](https://blog.csdn.net/rudy_zhou/article/details/111474179)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值