plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Sluggish performance when using add_annotations

Open mussonero opened this issue 5 years ago • 0 comments

Adding "add_annotations" to many points on any plot, cause slow performance. Please see the below minimal example

library(shiny)
library(plotly)
library(quantmod)

ui <- fluidPage(
  plotlyOutput("plot"),
  verbatimTextOutput("event")
)

server <- function(input, output) {

  output$plot <- renderPlotly({
    getSymbols("AAPL",src='yahoo')

    # basic example of ohlc charts
    df <- data.frame(Date=index(AAPL),coredata(AAPL))
    df <- tail(df, 500)
    p <- df %>%
      plot_ly(x = ~Date, type="candlestick",
              open = ~AAPL.Open, close = ~AAPL.Close,
              high = ~AAPL.High, low = ~AAPL.Low) %>%
      add_annotations(x = ~Date , y = ~AAPL.Open, text = "Tslow",xref = "x", yref = "y", yref = "y",ax = -30, ay = 40,
                      arrowcolor = 'rgb(0, 153, 51)', arrowsize = 2, arrowwidth = 1, arrowhead = 4)  %>%
      layout(title = "Basic Candlestick Chart",xaxis = list(rangeslider = list(visible = F)))
    p
  })

  output$event <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover on a point!" else d
  })
}

shinyApp(ui, server)

mussonero avatar Apr 17 '20 13:04 mussonero