如何在 echarts tooltip formatter 中按组提取 y 轴值

问题描述 投票:0回答:0

我想在自定义格式化函数中引用每个组的 y 轴值:

library(echarts4r)

# Example data
data <- data.frame(
  category = c("A", "B", "C", "D", "E"),
  value1 = c(10, 15, 20, 25, 30),
  value2 = c(12, 18, 22, 28, 35)
)

# define formatter function
formatter <- htmlwidgets::JS(
  "
   function(params) {
      var tooltipContent = '<b>Category:</b> ' + params[0].axisValue + '<br/>';
  
      params.forEach(function(item) {
        var groupName = item.seriesName;
        var groupValue = 1;
        var groupColor = item.color;
        
        tooltipContent += '<div style=\"display: inline-block; width: 10px; height: 10px; margin-right: 5px; background-color: ' + groupColor + '\"></div>' +
                          '<b>' + groupName + ':</b> ' + groupValue + '<br/>';
      });
      
      return tooltipContent;
    }
  "
)

# Create the echart
data |>
  e_charts(category) |>
  e_line(value1, name = "Group 1") |>
  e_line(value2, name = "Group 2") |>
  e_y_axis(name = "Value") |>
  e_x_axis(name = "Category") |>
  e_tooltip(trigger = "axis", axisPointer = list(type = "cross"), formatter = formatter)

特别是,我如何替换

var groupValue = 1;
以引用每个组的 y 轴值?

r echarts echarts4r
© www.soinside.com 2019 - 2024. All rights reserved.