2016-03-08 47 views
1

我正在使用dygraphs软件包编程R中的Shiny应用程序。R/Shiny dygraphs软件包中突出显示的图例

该应用程序提供了一个交互式图表,该图表使用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中的图例/闪亮,以便只突出显示系列(而不是所有系列在同一时间点)显示?
低2个地块下面的链接节目究竟应该怎样实现:http://dygraphs.com/gallery/#g/highlighted-series

这已经质疑计算器几次,但尚未答复或不工作(+我不想死灵帖子):
Is there a way to highlight closest series in R dygraphs?
Highlight Closest Series - but only show X/Y of highlighted series?
100 labels in separate div with highlighted series in a box

总之,dyHighlight()似乎并不具备支持此功能,所以JavaScript和CSS很可能需要一个内置的说法。

搜索互联网使我highlightSeriesOpts,从以下链接highlightCallbackunhighlightCallbackhttp://dygraphs.com/options.html

但你如何使用R这些选项?

+1

您在与重复的例子,这个问题的尝试将是有益的 –

+0

你应该有一个时间序列对象,你数据不是,请提供相关数据 –

+0

完成,请参阅OP :) –

回答

1

这应该让你开始:

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

http://dygraphs.com/css.html

传说有.dygraph,传说级。当使用highlightSeriesOpts时,所选系列'获得.highlight类。这可以用来在图例中只显示一个系列。

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

这应该工作,但由于某种原因.dygraph-legend接管并显示没有传说。

.dygraph-legend { display: none; } 

.highlight { 
    display: inline; 
} 

Altenative:

.dygraph-legend { display: all; } 

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

保存为*。css文件:

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

这简化版,解决问题,但增加了一大亮点,以选定系列: enter image description here

+0

非常感谢您的帮助,JavK。建议的CSS代码很好地工作。 (现在它已经包含在OP中)但正如你所说的那样,不幸的是这并不能解决问题。奇怪的是.dygraph-legend {display:none;}会覆盖.highlight {display:inline;},即使在使用!important之后也是如此。 –

+1

问题已解决:)。显然,需要使用'> span'才能使其工作(请参阅及时配件答案:http://stackoverflow.com/questions/35943583/plain-dygraphs-javascript-options-in-r-shiny)。 –