Highchart 在 R 中的提示框

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

Highchart Tooltip in R

问题

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

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

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

我使用以下highchart代码:

  1. highchart() %>%
  2. hc_chart(type = "bubble") %>%
  3. hc_add_series(
  4. data = initial_tumors$count,
  5. name = "Initial tumors",
  6. type = "bubble",
  7. color = "purple",
  8. dataLabels = list(enabled = TRUE, format = "{point.y}")
  9. ) %>%
  10. hc_add_series(
  11. data = modified_recurring_tumors$count,
  12. name = "Recurring tumors",
  13. type = "spline",
  14. color = "red",
  15. dataLabels = list(enabled = TRUE, format = "{point.y}")
  16. ) %>%
  17. hc_tooltip(
  18. shared = FALSE,
  19. formatter = JS(
  20. "function() {
  21. var tumorCategory;
  22. if (this.series.name === 'Initial tumors' || this.series.name === 'Initial tumor categories') {
  23. tumorCategory = this.point.y;
  24. } else if (this.series.name === 'Recurring tumors' || this.series.name === 'Recurring tumor categories') {
  25. tumorCategory = this.point.y;
  26. }
  27. var tooltip = '<b>' + this.series.name + '</b><br> : ' + tumorCategory + '<br>Count: ' + this.y;
  28. return tooltip;
  29. }"
  30. )
  31. ) %>%
  32. hc_title(text = "Initial vs Recurring Tumors")

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

如何更正它?

英文:

I Have two datasets (initial_tumors and modified_recurring_tumors).

dput:

  1. initial_tumors:
  2. structure(
  3. list(
  4. number = c(1L, 3L, 2L, 4L, 5L, 6L, 8L),
  5. count = c(145L,
  6. 47L, 38L, 25L, 16L, 14L, 9L),
  7. tumor_category = c(
  8. &quot;1 Tumor&quot;,
  9. &quot;3 Tumors&quot;,
  10. &quot;2 Tumors&quot;,
  11. &quot;4 Tumors&quot;,
  12. &quot;5 Tumors&quot;,
  13. &quot;6 Tumors&quot;,
  14. &quot;8 or more Tumors&quot;
  15. )
  16. ),
  17. row.names = c(NA,-7L),
  18. class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;)
  19. )
  20. data2:
  21. modified_recurring_tumors:
  22. structure(
  23. list(
  24. recur = c(NA, 5L, 1L),
  25. count = c(111L, 46L, 45L),
  26. tumor_category = c(&quot;8 or more Tumors&quot;, &quot;5 Tumors&quot;, &quot;1 Tumor&quot;)
  27. ),
  28. row.names = c(NA,-3L),
  29. class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;)
  30. )

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:

  1. highchart() %&gt;%
  2. hc_chart(type = &quot;bubble&quot;) %&gt;%
  3. hc_add_series(
  4. data = initial_tumors$count,
  5. name = &quot;Initial tumors&quot;,
  6. type = &quot;bubble&quot;,
  7. color = &quot;purple&quot;,
  8. dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  9. ) %&gt;%
  10. hc_add_series(
  11. data = modified_recurring_tumors$count,
  12. name = &quot;Recurring tumors&quot;,
  13. type = &quot;spline&quot;,
  14. color = &quot;red&quot;,
  15. dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  16. ) %&gt;%
  17. hc_tooltip(
  18. shared = FALSE,
  19. formatter = JS(
  20. &quot;function() {
  21. var tumorCategory;
  22. if (this.series.name === &#39;Initial tumors&#39; || this.series.name === &#39;Initial tumor categories&#39;) {
  23. tumorCategory = this.point.y;
  24. } else if (this.series.name === &#39;Recurring tumors&#39; || this.series.name === &#39;Recurring tumor categories&#39;) {
  25. tumorCategory = this.point.y;
  26. }
  27. 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;
  28. return tooltip;
  29. }&quot;
  30. )
  31. ) %&gt;%
  32. 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:

  1. 当您使用 `hc_add_series` 时,只传递了列值;如果您传递数据框,您可以访问其他列的数据。使用 `hcaes`,您可以定义 y 轴值(计数)。在格式化器中,您可以使用 `this.point.tumor_category` 访问类别,以及 `this.point.count` 访问计数。
  2. library(tidyverse)
  3. library(highcharter)
  4. highchart() %>%
  5. hc_chart(type = "bubble") %>%
  6. hc_add_series(
  7. data = initial_tumors,
  8. hcaes(y = count),
  9. name = "Initial tumors",
  10. type = "bubble",
  11. color = "purple",
  12. dataLabels = list(enabled = TRUE, format = "{point.y}")
  13. ) %>%
  14. hc_add_series(
  15. data = modified_recurring_tumors,
  16. hcaes(y = count),
  17. name = "Recurring tumors",
  18. type = "spline",
  19. color = "red",
  20. dataLabels = list(enabled = TRUE, format = "{point.y}")
  21. ) %>%
  22. hc_tooltip(
  23. shared = FALSE,
  24. formatter = JS(
  25. "function() {
  26. var tooltip = '<b>' + this.series.name + '</b>' +
  27. '<br>Tumor category: ' + this.point.tumor_category +
  28. '<br>Count: ' + this.point.count;
  29. return tooltip;
  30. }"
  31. )
  32. ) %>%
  33. 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.

  1. library(tidyverse)
  2. library(highcharter)
  3. highchart() %&gt;%
  4. hc_chart(type = &quot;bubble&quot;) %&gt;%
  5. hc_add_series(
  6. data = initial_tumors,
  7. hcaes(y = count),
  8. name = &quot;Initial tumors&quot;,
  9. type = &quot;bubble&quot;,
  10. color = &quot;purple&quot;,
  11. dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  12. ) %&gt;%
  13. hc_add_series(
  14. data = modified_recurring_tumors,
  15. hcaes(y = count),
  16. name = &quot;Recurring tumors&quot;,
  17. type = &quot;spline&quot;,
  18. color = &quot;red&quot;,
  19. dataLabels = list(enabled = TRUE, format = &quot;{point.y}&quot;)
  20. ) %&gt;%
  21. hc_tooltip(
  22. shared = FALSE,
  23. formatter = JS(
  24. &quot;function() {
  25. var tooltip = &#39;&lt;b&gt;&#39; + this.series.name + &#39;&lt;/b&gt;&#39; +
  26. &#39;&lt;br&gt;Tumor category: &#39; + this.point.tumor_category +
  27. &#39;&lt;br&gt;Count: &#39; + this.point.count;
  28. return tooltip;
  29. }&quot;
  30. )
  31. ) %&gt;%
  32. 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:

确定