简体   繁体   English

使用 plotlyProxyInvoke 将垂直线添加到 plotly plot 而不调整 plot

[英]Adding a vertical line to plotly plot using plotlyProxyInvoke without resizing the plot

I am trying to add a vertical line to an existing plotly plot using the addTraces method.我正在尝试使用addTraces方法向现有的 plotly plot 添加一条垂直线。 I'd like to understand why the new vertical lines are added two units to the right of where the first trace lays.我想了解为什么新的垂直线在第一条迹线所在的右侧添加了两个单位。

Fixing this issue will probably solve my original problem (question title) which is avoiding the plot resizing/moving to the right upon adding the new trace.修复此问题可能会解决我原来的问题(问题标题),即在添加新跟踪时避免 plot 调整大小/向右移动。

Here's an example of what I'm trying to do:这是我正在尝试做的一个例子:

library(shiny)
library(plotly)

ui <- bootstrapPage(
  plotlyOutput("plot")
)

myvec <- rnorm(100)

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

  values <- reactiveValues(idx=1)

  output$plot <- renderPlotly({
    plot_ly(type='scatter', mode='lines') %>%
    add_trace(y=myvec[1])
  })

  plotproxy <- plotlyProxy("plot", session)

  observe({

    plotproxy %>%
      plotlyProxyInvoke("extendTraces",
        list(y=list(list(myvec[values$idx]))),
        list(1))

    if(!values$idx%%10) {
      plotproxy %>% plotlyProxyInvoke("addTraces",
        list(x=c(values$idx, values$idx),  # + 2 would "fix it"
             y=c(0,myvec[values$idx]),
             type="line", showlegend=F))
    }

  })

  observe({
    invalidateLater(1000)
    isolate({
      values$idx <- min(values$idx + 1, length(myvec))
    })
  })

}

shinyApp(ui = ui, server = server)

In short, I'd like the x axis limits to update with extendTraces only, I'm just guessing the 2 units mismatch is the problem.简而言之,我希望 x 轴限制仅使用extendTraces进行更新,我只是猜测 2 个单位不匹配是问题所在。

I've managed to solve the x-axis mismatch, however this didn't solve the autosize issue.我已经设法解决了 x 轴不匹配问题,但这并没有解决自动调整大小的问题。 The problem was I wasn't using the same type and mode.问题是我没有使用相同的类型和模式。 Now it works:现在它起作用了:

library(shiny)
library(plotly)

ui <- bootstrapPage(
  plotlyOutput("plot")
)

myvec <- rnorm(100)

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

  values <- reactiveValues(idx=1)

  output$plot <- renderPlotly({
    plot_ly(type='scatter', mode='lines') %>%  # Must match with vertical line
    add_trace(x=c(-1,0), y=myvec[1])  # use x values
  })

  plotproxy <- plotlyProxy("plot", session)

  observe({

    plotproxy %>%
      plotlyProxyInvoke("extendTraces",
        list(x=list(list(values$idx)),  # match x values
             y=list(list(myvec[values$idx]))),
        list(1))

    if(!values$idx%%10) {
      plotproxy %>% plotlyProxyInvoke("addTraces",
        list(x=c(values$idx, values$idx),  # x limits match
             y=c(0,myvec[values$idx]),
             type='scatter', mode='lines', showlegend=F))  # must match
    }

  })

  observe({
    invalidateLater(1000)
    isolate({
      values$idx <- min(values$idx + 1, length(myvec))
    })
  })

}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM