You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly柱状图Box selection功能疑问及选中数据存储需求

Plotly Box Selection 功能说明与需求实现

一、Box Selection的核心作用

  • 作为Plotly图表内置的交互工具,允许你通过鼠标拖拽绘制矩形区域,快速选中区域内的所有柱状条(数据点)
  • 选中后,未被选中的柱状条会自动降低透明度,视觉上突出选中的目标区间,便于聚焦分析特定时段的数据
  • 默认仅提供视觉反馈,若要提取选中数据的具体信息(如数量、数值差异),需要结合R的交互函数来捕获选中事件的数据集

二、实现选中数据的统计与计算

要实现存储选中周期数量和计算选中区间的百分比差异,需要结合Shiny框架来捕获Plotly的选中事件,以下是修改后的完整代码:

library(plotly)
library(shiny)

# 定义数据结构
d <- structure(list(DateTime = c("2005-01-03 09:00:00", "2005-01-03 10:00:00", 
                                 "2005-01-03 11:00:00", "2005-01-03 12:00:00", "2005-01-03 13:00:00"
), Open = c(87.0467, 87.0395, 86.5453, 86.5382, 86.4307), High = c(87.2042, 
                                                                     87.1183, 87.061, 87.0323, 86.4379), Low = c(86.9822, 86.4451, 
                                                                                                                86.2947, 86.352, 86.1299), Close = c(87.0323, 86.5453, 86.5382, 
                                                                                                                                                     86.4379, 86.1729), Volume = c(6222000L, 15904800L, 5900800L, 
                                                                                                                                                                                  2833300L, 4529900L), Date = c("2005-01-03", "2005-01-03", "2005-01-03", 
                                                                                                                                                                                                               "2005-01-03", "2005-01-03"), Weekday = c("Monday", "Monday", 
                                                                                                                                                                                                                                                        "Monday", "Monday", "Monday"), Day = c(3L, 3L, 3L, 3L, 3L), Month = c(1, 
                                                                                                                                                                                                                                                                                                                               1, 1, 1, 1), Year = c(2005, 2005, 2005, 2005, 2005), hour = 9:13, 
minute = c(0L, 0L, 0L, 0L, 0L), HMS = c("09:00:00", "10:00:00", 
                                        "11:00:00", "12:00:00", "13:00:00"), NumericChange = c(-0.021499999999989, 
                                                                                               -0.487000000000009, -0.00709999999999411, -0.100300000000004, 
                                                                                               -0.265000000000001), PercentChange = c(-0.0246973710509926, 
                                                                                                                                    -0.55956236937322, -0.00820379616223424, -0.115902572505557, 
                                                                                                                                    -0.306578480041742)), row.names = c(NA, 5L), class = "data.frame")

# Shiny应用结构
ui <- fluidPage(
  plotlyOutput("bar_plot"),
  br(),
  verbatimTextOutput("selection_stats")
)

server <- function(input, output) {
  # 生成柱状图
  output$bar_plot <- renderPlotly({
    d %>%
      filter(Date >= Date[1] & Date <= Date[5]) %>%
      plot_ly(showlegend = FALSE) %>%
      add_trace(type = "bar",
                name = "Rate of Change", 
                x = ~DateTime,
                y = ~PercentChange,
                marker = list(color = "#7393B3", opacity = 0.8, line = list(color = "#7393B3", width = 2))) %>%
      layout(paper_bgcolor = "Black",
             plot_bgcolor = "Black", 
             font = list(color = "White"),
             yaxis = list(gridcolor = "#242424"), 
             hovermode = "x unified")
  })
  
  # 捕获选中数据并计算统计值
  output$selection_stats <- renderPrint({
    # 获取选中的事件数据
    selected <- event_data("plotly_selected")
    
    if (!is.null(selected)) {
      # 提取选中的行索引,匹配原始数据
      selected_rows <- d[selected$pointNumber + 1, ]
      
      # 计算选中周期数量
      count <- nrow(selected_rows)
      
      # 计算选中区间的百分比差异(这里取首尾的差值,可根据需求修改)
      percent_diff <- tail(selected_rows$PercentChange, 1) - head(selected_rows$PercentChange, 1)
      
      # 输出结果
      cat("选中的周期数量:", count, "\n")
      cat("选中区间首尾的百分比变化差值:", round(percent_diff, 6), "%\n")
      
      # 若需要查看所有选中数据的百分比变化,可取消注释以下代码:
      # cat("所有选中的百分比变化值:\n")
      # print(selected_rows$PercentChange)
    } else {
      cat("未选中任何数据")
    }
  })
}

# 运行Shiny应用
shinyApp(ui, server)

代码关键说明

  • 使用event_data("plotly_selected")捕获Box Selection选中的数据集,返回选中点的索引和坐标信息
  • 通过选中点的pointNumber匹配原始数据的行,提取对应的PercentChange值
  • 自定义统计逻辑:这里计算了选中周期的数量,以及选中区间首尾的百分比变化差值,你可以根据需求调整为最大值与最小值的差、总和等其他统计方式
  • Shiny的UI部分添加了文本输出框,实时显示选中数据的统计结果

内容的提问来源于stack exchange,提问作者firmo23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 19:53:15