【问题标题】:Is there a way to add a click event to open a url in a highcharter plot without shiny?有没有办法添加一个点击事件以在没有闪亮的情况下在 highcharter 图中打开一个 url?
【发布时间】:2019-08-09 15:02:51
【问题描述】:

我正在尝试使用 highcharter 制作地图,其中可以单击点以访问其关联的 url。我创建了以下我正在做的简化版本(我正在做一个地图气泡图)来说明我的代码。我究竟做错了什么?注意:我也在代码的 javascript 部分尝试了 this.point.options.url。

library(dplyr)
library(highcharter)

data("USArrests", package = "datasets")
USArrests = mutate(USArrests, "woe-name" = rownames(USArrests))
USArrests[["url"]] = c("https://www.google.com/", "https://www.wikipedia.org/")

hcmap(map = "countries/us/us-all", data = USArrests,
      joinBy = "woe-name", value = "UrbanPop", name = "Urban Population",
  allowPointSelect = TRUE) %>%
  hc_plotOptions(
    point = list(
      events = list(
        click = JS("function() {
                        window.open(point.url);
                      }"
        )
      )
    )
  )

【问题讨论】:

    标签: r highcharts r-highcharter


    【解决方案1】:

    您的代码有 2 个问题:

    1. 地图图块是map 而不是point,因此,您的选项文件应该将事件处理程序链接到map 而不是point

    2. 要访问url你必须在你的JS函数中使用一个参数,你最终可以通过它访问url。

    hcmap(map = "countries/us/us-all", data = USArrests,
          joinBy = "woe-name", value = "UrbanPop", name = "Urban Population",
          allowPointSelect = TRUE) %>%
      hc_plotOptions(
        map = list(
          events = list(
            click = JS("function(self) { 
                          window.open(self.point.url);
                       }"
            )
          )
        )
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2015-06-22
      • 2022-11-16
      • 2019-01-18
      • 1970-01-01
      相关资源
      最近更新 更多