Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

r - Hyperlink bar chart in Highcharter

I'm having trouble recreating 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 about recreating Javascript that I tried to follow. Here's what tried so far. It doesn't show any of the bars.

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)
  )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Andrew,

You have some (2) errors replicating the example:

  1. If you check carefully the example you gave. The point argument lives at the same depth of cursor in the series argument.
  2. You didn't add the data in the correct way (like the vignette show).

A fixed version of your code is:

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)
        )
      )
  )

And a better version to add the data would be:

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))

Hope its helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...