Highcharter 中的超链接条形图

Hyperlink bar chart in Highcharter

我在重新创建 this answer in R with Highcharter to make the bars in a bar chart into clickable URLs. Here is the Javascript code from the answer. Highcharter has a vignette 时遇到了问题,关于重新创建 Javascript 我试图遵循。这是到目前为止的尝试。它不显示任何条。

library(tidyverse)
library(highcharter)

highchart() %>%
  hc_chart(type = "column") %>%
  hc_title(text = "Click points to go to URL") %>%
  hc_xAxis(type = "category") %>%
  hc_plotOptions(series = list(cursor = "pointer"),
                 point =
                   list(events = list(
                     click = JS(
                       "function () {
                       location.href = 'https://en.wikipedia.org/wiki/' +
                       this.options.key;
                       }"
                     )
                     ))) %>%
  hc_series(
    list(name = "USA", key = "United_States", y = 29.9),
    list(name = "Canada", key = "Canada", y = 71.5),
    list(name = "Mexico", key = "Mexico", y = 106.4)
  )

安德鲁,

您在复制示例时有一些 (2) 个错误:

  1. 如果你仔细检查你给出的例子。 point 参数与 series 参数中的 cursor 处于同一深度。
  2. 您没有以正确的方式添加数据(如小插图展示)。

您的代码的固定版本是:

highchart() %>%
  hc_chart(type = "column") %>%
  hc_title(text = "Click points to go to URL") %>%
  hc_xAxis(type = "category") %>%
  hc_plotOptions(
    series = list(
      cursor = "pointer",
      point = list(
        events = list(
          click = JS( "function () { location.href = 'https://en.wikipedia.org/wiki/' + this.options.key; }")
          )
        )
      )
    ) %>%
  hc_series(
    list(
      data = list(
        list(name = "USA", key = "United_States", y = 29.9),
        list(name = "Canada", key = "Canada", y = 71.5),
        list(name = "Mexico", key = "Mexico", y = 106.4)
        )
      )
  )

添加数据的更好版本是:

dat <- data.frame(
  country = c("USA", "Canada", "Mexico"),
  url = c("United_States", "Canada", "Mexico"),
  value = c(29.9, 71.5, 106.4)
)

highchart() %>%
  hc_xAxis(type = "category") %>%
  hc_plotOptions(
    series = list(
      cursor = "pointer",
      point = list(
        events = list(
          click = JS( "function () { location.href = 'https://en.wikipedia.org/wiki/' + this.options.key; }")
          )
        )
      )
    ) %>%
  hc_add_series(data = dat, type = "column", mapping = hcaes(name = country, key = url, y = value))

希望对您有所帮助