Highcharter:更新股票图时保持选定的缩放
Highcharter: Keep the selected Zoom when updating a stock plot
我正在开发一个 Shiny 应用程序,允许用户用 selectInput
更新 highcharter
情节。我希望如果用户选择一个范围按钮(例如 6m),然后使用 selectInput 切换到另一个图,它会将缩放按钮选择为 6m 而不是恢复到 All.
我阅读了 实现 afterSetExtremes()
的答案以更新绘图底部的范围选择器,但我想专门更新顶部的按钮,以便用户可以看到缩放已应用。
下面是一个简单的例子:
library(shiny)
library(highcharter)
library(quantmod)
ui <- fluidPage(sidebarLayout(sidebarPanel(
selectInput("selector", "Select stock",
choices = c("GOOG", "AAPL"))
),
mainPanel(highchartOutput("plot"))))
server <- function(input, output, session) {
output$plot <- renderHighchart({
data <- getSymbols(input$selector, auto.assign = FALSE)
highchart(type = 'stock') %>%
hc_add_series(data)
})
}
shinyApp(ui, server)
感谢您的指点。
我找到了问题的答案。我的想法来自这个 highchart forum (the eighth post) and its jsfiddle answer。我从 javascript 中的选定按钮中获取值,并将其发送回 shiny 以便在更新绘图时使用它。
我分享解决方案,以备日后有人需要。
library(shiny)
library(highcharter)
library(quantmod)
ui <- fluidPage(sidebarLayout(sidebarPanel(
selectInput("selector", "Select stock",
choices = c("GOOG", "AAPL"))
),
mainPanel(highchartOutput("plot"))))
server <- function(input, output, session) {
text_to_val <- list(
"1m" = 0,
"3m" = 1,
"6m" = 2,
"YTD" = 3,
"1y" = 4,
"All" = 5
)
PRESELECTED_TEXT <- "All"
observeEvent(input$persistanceValue, {
PRESELECTED_TEXT <<- input$persistanceValue
})
output$plot <- renderHighchart({
data <- getSymbols(input$selector, auto.assign = FALSE)
highchart(type = 'stock') %>%
hc_add_series(data) %>%
hc_xAxis(events = list(
afterSetExtremes = JS(
"function(e) {
Shiny.setInputValue('persistanceValue', e.rangeSelectorButton.text);
}"
)
)) %>%
hc_rangeSelector(selected = text_to_val[[PRESELECTED_TEXT]])
})
}
shinyApp(ui, server)
我正在开发一个 Shiny 应用程序,允许用户用 selectInput
更新 highcharter
情节。我希望如果用户选择一个范围按钮(例如 6m),然后使用 selectInput 切换到另一个图,它会将缩放按钮选择为 6m 而不是恢复到 All.
我阅读了 afterSetExtremes()
的答案以更新绘图底部的范围选择器,但我想专门更新顶部的按钮,以便用户可以看到缩放已应用。
下面是一个简单的例子:
library(shiny)
library(highcharter)
library(quantmod)
ui <- fluidPage(sidebarLayout(sidebarPanel(
selectInput("selector", "Select stock",
choices = c("GOOG", "AAPL"))
),
mainPanel(highchartOutput("plot"))))
server <- function(input, output, session) {
output$plot <- renderHighchart({
data <- getSymbols(input$selector, auto.assign = FALSE)
highchart(type = 'stock') %>%
hc_add_series(data)
})
}
shinyApp(ui, server)
感谢您的指点。
我找到了问题的答案。我的想法来自这个 highchart forum (the eighth post) and its jsfiddle answer。我从 javascript 中的选定按钮中获取值,并将其发送回 shiny 以便在更新绘图时使用它。
我分享解决方案,以备日后有人需要。
library(shiny)
library(highcharter)
library(quantmod)
ui <- fluidPage(sidebarLayout(sidebarPanel(
selectInput("selector", "Select stock",
choices = c("GOOG", "AAPL"))
),
mainPanel(highchartOutput("plot"))))
server <- function(input, output, session) {
text_to_val <- list(
"1m" = 0,
"3m" = 1,
"6m" = 2,
"YTD" = 3,
"1y" = 4,
"All" = 5
)
PRESELECTED_TEXT <- "All"
observeEvent(input$persistanceValue, {
PRESELECTED_TEXT <<- input$persistanceValue
})
output$plot <- renderHighchart({
data <- getSymbols(input$selector, auto.assign = FALSE)
highchart(type = 'stock') %>%
hc_add_series(data) %>%
hc_xAxis(events = list(
afterSetExtremes = JS(
"function(e) {
Shiny.setInputValue('persistanceValue', e.rangeSelectorButton.text);
}"
)
)) %>%
hc_rangeSelector(selected = text_to_val[[PRESELECTED_TEXT]])
})
}
shinyApp(ui, server)