Highchart 在 R 中的提示框

huangapple go评论98阅读模式
英文:

Highchart Tooltip in R

问题

我正在使用highchart绘制“初始肿瘤”与“复发肿瘤”的图表。我使用工具提示,以便在鼠标悬停时显示tumor_category的详细信息,例如:

初始肿瘤:
4个肿瘤
计数:16

复发肿瘤:
4个肿瘤
计数:18

我使用以下highchart代码:

highchart() %>%
  hc_chart(type = "bubble") %>%
  hc_add_series(
    data = initial_tumors$count,
    name = "Initial tumors",
    type = "bubble",
    color = "purple",
    dataLabels = list(enabled = TRUE, format = "{point.y}")
  ) %>%
  hc_add_series(
    data = modified_recurring_tumors$count,
    name = "Recurring tumors",
    type = "spline",
    color = "red",
    dataLabels = list(enabled = TRUE, format = "{point.y}")
  ) %>%
  hc_tooltip(
    shared = FALSE,
    formatter = JS(
      "function() {
      var tumorCategory;
      if (this.series.name === 'Initial tumors' || this.series.name === 'Initial tumor categories') {
        tumorCategory = this.point.y;
      } else if (this.series.name === 'Recurring tumors' || this.series.name === 'Recurring tumor categories') {
        tumorCategory = this.point.y;
      }
      var tooltip = '<b>' + this.series.name + '</b><br>   : ' + tumorCategory + '<br>Count: ' + this.y;
      return tooltip;
    }"
    )
  )  %>%
  hc_title(text = "Initial vs Recurring Tumors")

但是当我将鼠标悬停在图表上时,它不显示tumor_category,而是显示计数。

如何更正它?

英文:

I Have two datasets (initial_tumors and modified_recurring_tumors).

dput:

initial_tumors:

structure(
  list(
    number = c(1L, 3L, 2L, 4L, 5L, 6L, 8L),
    count = c(145L,
              47L, 38L, 25L, 16L, 14L, 9L),
    tumor_category = c(
      &quot;1 Tumor&quot;,
      &quot;3 Tumors&quot;,
      &quot;2 Tumors&quot;,
      &quot;4 Tumors&quot;,
      &quot;5 Tumors&quot;,
      &quot;6 Tumors&quot;,
      &quot;8 or more Tumors&quot;
    )
  ),
  row.names = c(NA,-7L),
  class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;)
)

data2:
modified_recurring_tumors:

structure(
  list(
    recur = c(NA, 5L, 1L),
    count = c(111L, 46L, 45L),
    tumor_category = c(&quot;8 or more Tumors&quot;, &quot;5 Tumors&quot;, &quot;1 Tumor&quot;)
  ),
  row.names = c(NA,-3L),
  class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;)
)

I am plotting a graph using highchart for Initial Tumors vs Recurrent Tumors. I am using tooltip so that when mouse is hovered it shows the tumor_category details such as:

Initial Tumors:
4 Tumors
Count:16

Recurrent Tumors:
4 Tumors
Count:18

Am using following highchart code:

highchart() %&gt;%
  hc_chart(type = &quot;bubble&quot;) %&gt;%
  hc_add_series(
    data = initial_tumors$count,
    name = &quot;Initial tumors&quot;,
    type = &quot;bubble&quot;,
    color = &quot;purple&quot;,
    dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  ) %&gt;%
  hc_add_series(
    data = modified_recurring_tumors$count,
    name = &quot;Recurring tumors&quot;,
    type = &quot;spline&quot;,
    color = &quot;red&quot;,
    dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  ) %&gt;%
  hc_tooltip(
    shared = FALSE,
    formatter = JS(
      &quot;function() {
      var tumorCategory;
      if (this.series.name === &#39;Initial tumors&#39; || this.series.name === &#39;Initial tumor categories&#39;) {
        tumorCategory = this.point.y;
      } else if (this.series.name === &#39;Recurring tumors&#39; || this.series.name === &#39;Recurring tumor categories&#39;) {
        tumorCategory = this.point.y;
      }
      var tooltip = &#39;&lt;b&gt;&#39; + this.series.name + &#39;&lt;/b&gt;&lt;br&gt;   : &#39; + tumorCategory + &#39;&lt;br&gt;Count: &#39; + this.y;
      return tooltip;
    }&quot;
    )
  )  %&gt;%
  hc_title(text = &quot;Initial vs Recurring Tumors&quot;)

But when I hover the mouse over the graph, it is not showing the tumor_category but is showing count.

How to correct it?Highchart 在 R 中的提示框

答案1

得分: 2

Sure, here is the translated code:

当您使用 `hc_add_series` 时,只传递了列值;如果您传递数据框,您可以访问其他列的数据。使用 `hcaes`,您可以定义 y 轴值(计数)。在格式化器中,您可以使用 `this.point.tumor_category` 访问类别,以及 `this.point.count` 访问计数。

    library(tidyverse)
    library(highcharter)
      
    highchart() %>%
      hc_chart(type = "bubble") %>%
      hc_add_series(
        data = initial_tumors,
        hcaes(y = count),
        name = "Initial tumors",
        type = "bubble",
        color = "purple",
        dataLabels = list(enabled = TRUE, format = "{point.y}")
      ) %>%
      hc_add_series(
        data = modified_recurring_tumors,
        hcaes(y = count),
        name = "Recurring tumors",
        type = "spline",
        color = "red",
        dataLabels = list(enabled = TRUE, format = "{point.y}")
      ) %>%
      hc_tooltip(
        shared = FALSE,
        formatter = JS(
          "function() {
            var tooltip = '<b>' + this.series.name + '</b>' + 
                          '<br>Tumor category: ' + this.point.tumor_category + 
                          '<br>Count: ' + this.point.count;
            return tooltip;
          }"
        )
      ) %>%
      hc_title(text = "Initial vs Recurring Tumors")

Please note that I have translated the code portion as requested, excluding the code itself.

英文:

When you use hc_add_series you only passed in the column values; if you instead pass in the data.frame you can access other column data. Using hcaes you can define your y-axis value (count). In your formatter, you can use this.point.tumor_category to access the category, and this.point.count to access the count.

library(tidyverse)
library(highcharter)
  
highchart() %&gt;%
  hc_chart(type = &quot;bubble&quot;) %&gt;%
  hc_add_series(
    data = initial_tumors,
    hcaes(y = count),
    name = &quot;Initial tumors&quot;,
    type = &quot;bubble&quot;,
    color = &quot;purple&quot;,
    dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  ) %&gt;%
  hc_add_series(
    data = modified_recurring_tumors,
    hcaes(y = count),
    name = &quot;Recurring tumors&quot;,
    type = &quot;spline&quot;,
    color = &quot;red&quot;,
    dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  ) %&gt;%
  hc_tooltip(
    shared = FALSE,
    formatter = JS(
      &quot;function() {
        var tooltip = &#39;&lt;b&gt;&#39; + this.series.name + &#39;&lt;/b&gt;&#39; + 
                      &#39;&lt;br&gt;Tumor category: &#39; + this.point.tumor_category + 
                      &#39;&lt;br&gt;Count: &#39; + this.point.count;
        return tooltip;
      }&quot;
    )
  ) %&gt;%
  hc_title(text = &quot;Initial vs Recurring Tumors&quot;)

Highchart 在 R 中的提示框

huangapple
  • 本文由 发表于 2023年6月12日 02:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定