我想提取高位图表“股票”图表上的可视化效果的选定日期on click
。
将这些日期保存在变量中,以便在其他位置访问。 如何访问这些日期?
highchart(type = "stock") %>%
hc_add_series(df, hcaes(DateTime, Count),type = "column") %>%
hc_xAxis(type = "datetime",
dateTimeLabelFormats = list(day = '%d of %b'))
样本数据:
DateTime Count
<dttm> <int>
1 2019-01-01 00:00:00 5
2 2019-01-01 01:00:00 2
3 2019-01-01 02:00:00 1
4 2019-01-01 03:00:00 2
5 2019-01-01 04:00:00 1
6 2019-01-01 05:00:00 1
答案 0 :(得分:2)
不能完全确定最小和最大日期是什么意思。单击后您希望发生什么?
创建一个获取并存储值的 JavaScript 点击事件。
click_event <- JS("
function(event) {
// Get value of clicked column
var clickedValue = event.point.name;
// Store it in input$bar_clicked
Shiny.onInputChange('bar_clicked', clickedValue);
}
")
并将click事件添加到Highchart对象hc_plotOptions(column = list(events = list(click = click_event)))
中。 (基于Highcharts API documentation)
此后,您将可以在Shiny中的任何地方使用input$bar_clicked
变量。
library(tibble)
library(highcharter)
library(shiny)
df <- tribble(
~DateTime, ~Count,
"2019-01-01 00:00:00", 5,
"2019-01-01 01:00:00", 2,
"2019-01-01 02:00:00", 1,
"2019-01-01 03:00:00", 2,
"2019-01-01 04:00:00", 1,
"2019-01-01 05:00:00", 1
)
click_event <- JS("
function(event) {
// Get value of clicked column
var clickedValue = event.point.name;
// Store it in input$bar_clicked
Shiny.onInputChange('bar_clicked', clickedValue);
}
")
ui <- fluidPage(
highchartOutput("hc"),
verbatimTextOutput("txt")
)
server <- function(input, output, session) {
output$hc <- renderHighchart( {
highchart(type = "stock") %>%
hc_add_series(df, hcaes(DateTime, Count),type = "column") %>%
hc_xAxis(type = "datetime",
dateTimeLabelFormats = list(day = '%d of %b')) %>%
hc_plotOptions(column = list(events = list(click = click_event)))
})
# Print the variable
output$txt <- renderPrint( {
input$bar_clicked
})
}
shinyApp(ui, server)