【问题标题】:Series highlighting legend in dygraphs package for R/Shiny系列突出显示 R/Shiny 的 dygraphs 包中的图例
【发布时间】:2016-03-08 14:47:32
【问题描述】:

我正在使用 dygraphs 包在 R 中编写一个 Shiny 应用程序。

该应用程序具有交互式绘图功能,该绘图使用dyHighlight() 函数突出显示所选系列。但是,由于数据涉及 1000 列,因此图例显示 1000 个系列。

这是一个只有 2 列的最小代码示例:

ui <- fluidPage(

  # This CSS code should solve the problem, but somehow does not.
  # To test, replace CSS in tags$style(...) with this code
  #.dygraph-legend  { display: none; }
  #.dygraph-legend .highlight { display: inline; }

  # CSS to highlight selected series in legend.
  # Does not solve problem, but is best alternative for now.
  tags$style("
              .highlight {
               border: 2px solid black;
               background-color: #B0B0B0;
              }

             "), 

  sidebarLayout(

    sidebarPanel(),

    mainPanel(dygraphOutput("plot"))

  )

)

server <- function(input, output) {

  set.seed(123)
  data <- matrix(rnorm(12), ncol = 2)
  data <- ts(data)

  # Workaround for what might be a bug
  # Reference: https://stackoverflow.com/questions/28305610/use-dygraph-for-r-to-plot-xts-time-series-by-year-only
  data <- cbind(as.xts(data[,1]), as.xts(data[,2]))

  colnames(data) <- c("Series 1", "Series 2")
  #print(data) # Uncomment to view data frame

  output$plot <- renderDygraph({

    dygraph(data) %>%

      # Highlighting series
      dyHighlight(data, 
                  highlightCircleSize = 2, 
                  highlightSeriesBackgroundAlpha = .5, 
                  highlightSeriesOpts = list(strokeWidth = 2))

  })

}

shinyApp(ui = ui, server = server)

有没有办法调整 R/Shiny 中的图例,以便只显示突出显示的系列(而不是同一时间点的所有系列)?
以下链接中较低的 2 个图准确显示了应该实现的目标:http://dygraphs.com/gallery/#g/highlighted-series

这已经在stackoverflow上被质疑过几次,但还没有回答或不起作用(+我不想死帖):
Is there a way to highlight closest series in R dygraphs?
Highlight Closest Series - but only show X/Y of highlighted series?
@ 987654324@

无论如何,dyHighlight() 似乎没有支持此功能的内置参数,因此可能需要 JavaScript 和 CSS。

搜索互联网使我从以下链接找到highlightSeriesOptshighlightCallbackunhighlightCallbackhttp://dygraphs.com/options.html

但是如何在 R 中使用这些选项?

【问题讨论】:

  • 您对可重现示例问题的尝试会很有用
  • 你应该有一个 timeseries 对象,你的数据不是,请提供相关数据

标签: javascript css r shiny dygraphs


【解决方案1】:

这应该让你开始:

https://rstudio.github.io/dygraphs/gallery-css-styling.html

http://dygraphs.com/css.html

图例具有 .dygraph-legend 类。使用 highlightSeriesOpts 时,所选系列会获得一个 .highlight 类。这可用于在图例中仅显示单个系列。

所以你必须创建 CSS 文件并将其添加到图形中:https://rstudio.github.io/dygraphs/gallery-css-styling.html

这应该可以工作,但由于某种原因.dygraph-legend 正在接管并且没有显示图例。

 .dygraph-legend  { display: none; }

.highlight {
  display: inline;
}

替代方案:

.dygraph-legend  { display: all; }

.highlight {
  display: inline;
  background-color: #B0B0B0;
}

将其保存为 *.css 文件:

dygraph(data)%>% dyHighlight() %>% dyCSS("path to css")

这并不能解决问题,但会为所选系列添加亮点:

【讨论】:

  • 非常感谢您的帮助,JavK。建议的 CSS 代码运行良好。 (它现在已包含在 OP 中。)但就像你说的那样,不幸的是它并没有解决问题。奇怪的是 .dygraph-legend {display: none;} 会覆盖 .highlight {display: inline;},即使在使用 !important 之后也是如此。
  • 问题已经解决了:)。显然,需要&gt; span 才能使其工作(请参阅及时组合的答案:stackoverflow.com/questions/35943583/…)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多