Echarts的formatter函数用法

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

下面是错误的代码,在对右上角4个按钮进行切换时,会报错,因为formatter内返回的参数点击之后,数据会减少。
Echarts的formatter函数用法_第1张图片

 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 `
      		 
${val[0] ? val[0].name : "-"}
xx告警 :${val[0] ? val[0].value : "-"}
心率告警 :${val[1] ? val[1].value : "-"}
xxx告警 :${val[2] ? val[2].value : "-"}
体温告警 :${val[3] ? val[3].value : "-"}
`
; }, },

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

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

你可能感兴趣的:(vue,echarts)