在R中连接2个图例高图

时间:2019-05-13 12:14:04

标签: r shinydashboard r-highcharter

我想关联两个here的高图表图例

我正在使用R,因此必须使用shinyjs软件包。我尝试过的是在jsCode中。

我的问题是,如果我将hc_plotOptions(series = list(events = list(legendItemClick = jsCode)))放在output $ plot1中(它可以使用javascript部分),什么也没出现

不含上一句话的代码:

library('shiny')
library('shinydashboard')
library('highcharter')
library("shinyWidgets")
library('shinyjs')

data_plot <- data.frame(categories = c("A", "B", "C", "D"),
                        serie1 = c(1563, 1458, 205, 695),
                        serie2 = c(562, 258, 17, 115))


jsCode <- JS("legendItemClick: function (event) {
                        var XYZ = $('#plot1').highcharts(),
                            series = XYZ.get(this.options.id); 

                        if (series) {
                            if (this.visible) {
                                series.hide();
                            } else {
                                series.show();
                            }}}")

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    extendShinyjs(text = jsCode),
    highchartOutput(outputId = "plot1"), 
    highchartOutput(outputId = "plot2")
  )
)

server <- function(input, output, session){

  output$plot1 <- renderHighchart({
    highchart() %>%
      hc_chart(type = 'bar') %>%
      hc_add_series(data = data_plot$serie1,name = 'Serie 1',id = "serie1") %>%
      hc_add_series(data = data_plot$serie2,name = 'Serie 2',id = "serie2") %>%
      hc_xAxis(categories = data_plot$categories,title = list(text = 'Categories')) %>%
      hc_plotOptions(bar = list(stacking = 'normal'))})

   output$plot2 <- renderHighchart({
    highchart() %>%
      hc_chart(type = 'bar') %>%
      hc_add_series(data = data_plot$serie1,name = 'Serie 1',id = "serie1",showInLegend = FALSE) %>%
      hc_add_series(data = data_plot$serie2,name = 'Serie 2',id = "serie2",showInLegend = FALSE) %>%
      hc_xAxis(categories = data_plot$categories,title = list(text = 'Categories')) %>%
      hc_plotOptions(bar = list(stacking = 'normal'))})

}

shinyApp(ui = ui, server = server)

0 个答案:

没有答案