Echarts的formatter函数用法

tooltip是提示框组件 formatter是提示框内容

下面是错误的代码,在对右上角4个按钮进行切换时,会报错,因为formatter内返回的参数点击之后,数据会减少。
在这里插入图片描述

 tooltip: {
    
    
          // 悬停样式
          trigger: "axis",
          padding: [10, 22],
          backgroundColor: "rgba(66, 119, 108, 0.8)", //0.8
          borderColor: "rgba(66, 119, 108, 0.8)",
          textStyle: {
    
    
            color: "#fff",
          },
          formatter: (val) => {
    
    
          return `
      		 <div>${
      
      val[0] ? val[0].name : "-"}</div>
     		 <div>xx告警 :${
      
      val[0] ? val[0].value : "-"}</div>
 			 <div>心率告警 :${
      
      val[1] ? val[1].value : "-"}</div>
      		 <div>xxx告警 :${
      
      val[2] ? val[2].value : "-"}</div>
         	 <div>体温告警 :${
      
      val[3] ? val[3].value : "-"}</div>`
          ;
          },
        },

所以将代码改良了下,便利val这个list,进行判断,在加上去最后return出去。

   tooltip: {
    
    
          formatter: (val) => {
    
    
            var res = `<div>${
      
      val[0] ? val[0].name : "-"}</div>`;
            val.forEach((item) => {
    
    
              if (item.seriesName == "SOS告警") {
    
    
                res += `<div>SOS告警 :${
      
      item.value ? item.value : "-"}</div>`;
              } else if (item.seriesName == "心率告警") {
    
    
                res += `<div>心率告警 :${
      
      item.value ? item.value : "-"}</div>`;
              } else if (item.seriesName == "血氧告警") {
    
    
                res += `<div>血氧告警 :${
      
      item.value ? item.value : "-"}</div>`;
              } else if (item.seriesName == "体温告警") {
    
    
                res += `<div>体温告警 :${
      
      item.value ? item.value : "-"}</div>`;
              }
            });
            return res;
          },
        },

猜你喜欢

转载自blog.csdn.net/qq_38594056/article/details/118572431
今日推荐