2016-03-08 43 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)。 –