Rshiny高级图中的工具提示内容

时间:2016-09-03 16:06:18

标签: r highcharts shiny tooltip

下午好,所有:),

我在Rshiny图表的高图中有一个工具提示有点问题。我在一个简化的例子中重现了这个问题,需要'高图'和'闪亮'的图书馆。这是代码:

library("shiny")
library("highcharter")

data(citytemp)

ui <- fluidPage(
    h1("Highcharter EXAMPLE"),
    fluidRow(
        column(width = 8,
               highchartOutput("hcontainer",height = "500px")
        )
    )
)

server = function(input, output) {
    data = data[,c("month","tokyo","new_york")]
    output$hcontainer <- renderHighchart({
      hc <-  highchart() %>% 
            hc_chart(type = "line") %>% 
            hc_title(text = "Monthly Average Temperature for TOKYO") %>% 
            hc_subtitle(text = "Source: WorldClimate.com") %>% 
            hc_xAxis(categories = c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) %>% 
            hc_yAxis(title = list(text = "Temperature (C)")) %>% 
          hc_tooltip(pointFormat = '<span style="color:{series.color}">As for NY: </span>:
                       <b>{point.percentage:.1f}%</b> ({point.y:,.0f} millions)<br/>',
                     followPointer=TRUE,
                     shared = TRUE)%>% 
            hc_plotOptions(line = list(
                dataLabels = list(enabled = TRUE),
                enableMouseTracking = FALSE)
            ) %>% 
            hc_series(
                list(
                    name = "Tokyo",
                    data = data$tokyo))      
        hc

    })

}

shinyApp(ui = ui, server = server)

我的工具提示有问题,我无法理解为什么它不起作用?它在启动时不会出现在应用程序中。 另外,我希望工具提示包含来自另一个系列(这里是纽约)的数据 - 这是可行的还是工具提示只能引用图表上的线? 非常感谢您的帮助 ! 祝一切顺利, madzia

1 个答案:

答案 0 :(得分:2)

在示例中将绘图选项enableMouseTracking设置为TRUE时,会出现工具提示。

我还使用javascript编辑了工具提示,以便可以访问共享数据,根据这篇文章:https://stackoverflow.com/a/19315076

这对你有帮助吗? :)

library("shiny")
library("highcharter")

data(citytemp)

ui <- fluidPage(
  h1("Highcharter EXAMPLE"),
  fluidRow(
    column(width = 8,
           highchartOutput("hcontainer",height = "500px")
    )
  )
)

server <- function(input, output) {
  data <- citytemp[,c("month","tokyo","new_york")]
  output$hcontainer <- renderHighchart({
    hc <-  highchart() %>% 
      hc_chart(type = "line") %>% 
      hc_title(text = "Monthly Average Temperature for TOKYO") %>% 
      hc_subtitle(text = "Source: WorldClimate.com") %>% 
      hc_xAxis(categories = c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                              'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) %>% 
      hc_yAxis(title = list(text = "Temperature (C)")) %>% 
      hc_series(
        list(
          name = "Tokyo",
          data = data$tokyo),
        list(
          name = "New York",
          data = data$new_york)
        )   %>%
      hc_tooltip(
formatter = JS("function() {

    var s = [];

    $.each(this.points, function(i, point) {
        s.push('<span style=\"color:' + point.series.color + ';font-weight:bold;\">' + point.series.name + ' : ' + point.y + '<span>');
    });

    if (this.points.length === 2) {
        s.push('<span>Second point is ' + Math.round((this.points[1].y / this.points[0].y) * 100) + '% of first point.</span>');

    }

    return s.join('<br/>');

}"),
                followPointer=TRUE,
                 shared = TRUE) %>% 
      hc_plotOptions(line = list(
        dataLabels = list(enabled = TRUE),
        enableMouseTracking = TRUE
        )
      )
    return(hc)
  })
} 

shinyApp(ui = ui, server = server)