【问题标题】:"e_tooltip(formatter = )" how to display the interval instead of the average of the histogram column?\"e_tooltip(formatter = )\" 如何显示间隔而不是直方图列的平均值?
【发布时间】:2022-12-17 15:32:54
【问题描述】:

我有一个直方图,需要查看的不是直方图列的平均值(图片上的2.9),而是它的间隔(2.8-3.0)。怎么做?

示例代码:

iris %>% setDT() %>% 
    e_charts() %>%
    e_histogram(
        Sepal.Width,
        name = "histogram") %>%
    e_density(
        Sepal.Width,
        name = "density",
        y_index = 1) %>%
    # e_tooltip(trigger = "axis")
    e_tooltip(trigger = "axis")

我看到的和我想看到的:

【问题讨论】:

    标签: javascript r echarts echarts4r


    【解决方案1】:

    为了添加此信息,我编写了一个 JS 函数,用于调用 e_tooltip。只有一个元素是我无法动态获取的——数据的行数。

    在 JS 中,您看到数字 32,这是您必须在此代码中更新的唯一信息(e_ 函数中的数据除外)。

    假设:

    • 调用直方图时未指定binwidth(echarts4r计算的bin宽度)
    • 情节的第一个系列是直方图
    • 情节的第二个系列是密度

    使用行数和此函数来创建您要查找的格式化工具提示。

    ttFrm <- function(rowCt) { # use row count to call function
      htmlwidgets::JS(paste0("
        function(data) {
          console.log(data);
          h = data[0];            /* histogram */
          d = data[1];            /* density */
    
          bc = h.value[1];        /* bin count */
          ds = d.value[1];        /* density */
    
          /* bin width = count in bin / count in dataset / density */
          br = bc/", rowCt, "/ds/2;        /* bin span divided by two */
          bL = h.axisValue - br;  /* bin low */
          bH = h.axisValue + br;  /* bin high */
          
          return(
            '<b>' + bL + ' - ' + bH + '</b><br /><span style="color:' +
            h.color + ';">●</span> ' + h.seriesName + 
            '<b style="padding-left: 2em;">' + 
            bc + '</b><br /><span style="color:' +
            d.color + ';">●</span> ' + d.seriesName + 
            '<b style="padding-left: 2em;">' + 
            ds + '</b><br/>')
        }"))
    }
    
    

    以下是如何应用此格式的示例。 (对该图的调用来自 echarts4r 示例图。)

    mtcars |>
      e_charts(elementId = 'chart') |>
      e_histogram(mpg, name = "histogram") |>
      e_density(mpg, areaStyle = list(opacity = .4),
                smooth = TRUE, name = "density", y_index = 1) |>
      e_tooltip(
        trigger = "axis", confine = T, 
        textStyles = list(overflow = "breakall", width = 50),
        formatter = ttFrm(nrow(mtcars)))     # <<---- I'm new!!
    

    这是另一个例子。在这里,我将数据换成了 iris 数据集。

    iris |>
      e_charts(elementId = 'chart') |>
      e_histogram(Sepal.Width, name = "histogram") |>
      e_density(Sepal.Width, areaStyle = list(opacity = .4),
                smooth = TRUE, name = "density", y_index = 1) |>
      e_tooltip(
        trigger = "axis", confine = T, digits = 3,
        textStyles = list(overflow = "breakall", width = 50),
        formatter = ttFrm(nrow(iris)))
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-18
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      相关资源
      最近更新 更多