highcharts - 捕获取消选择类别的事件
问题描述
我正在使用 highcharter 库。我想根据特定类别的饼图(甜甜圈)图的选择来拆分(或过滤)其他图表和表格。我下面的代码工作正常。
期望的结果 -当用户在选择后再次点击饼图时,它应该删除过滤。下面的代码捕获最后点击的类别,它与当前选择使用curre()
和lstre()
反应值相匹配。
问题当用户点击饼图两次以上时,最后点击的类别与当前选择的类别匹配,因此它不执行任何过滤。
我试过hc_add_event_point(event = "unselect")
了,它不会让用户选择特定类别的馅饼超过两次。
library("shiny")
library("highcharter")
library(dplyr)
ui <- shinyUI(
fluidPage(
column(width = 8, highchartOutput("hcontainer", height = "500px")),
column(width = 4, textOutput("text")),
column(width = 4, dataTableOutput('temptable')))
)
server <- function(input, output) {
a <- data.frame(b = LETTERS[1:5], c = 11:15)
aa <- data.frame(b = LETTERS[1:5])
output$hcontainer <- renderHighchart({
canvasClickFunction <- JS("function(event) {Shiny.setInputValue('canvasClicked', [this.name, event.point.name, Math.random()]);}")
legendClickFunction <- JS("function(event) {Shiny.setInputValue('legendClicked', this.name);}")
highchart() %>%
hc_chart(type="pie") %>%
hc_add_series_labels_values(labels = a$b, values = a$c,
innerSize = '60%',
allowPointSelect= TRUE,
slicedOffset = 20,
states = list(
select = list(
color= NULL,
borderWidth = 5,
borderColor = '#ccc'
))) %>%
hc_plotOptions(series = list(
events = list(click = canvasClickFunction,
legendItemClick = legendClickFunction))) %>%
hc_add_event_point(event = "unselect")
})
makeReactiveBinding("outputText")
rv <- reactiveValues(lstval=0,curval=0)
observeEvent(input$canvasClicked[2], {
rv$lstval <- rv$curval;
rv$curval <- input$canvasClicked[2]}
)
curre <- reactive({req(input$canvasClicked[2]); input$canvasClicked[2]; rv$curval})
lstre <- reactive({req(input$canvasClicked[2]); input$canvasClicked[2]; rv$lstval})
observeEvent(input$canvasClicked, {
outputText <<- paste0("You clicked on series ", input$canvasClicked[1], " and the bar you clicked was from category ", input$canvasClicked[2],
input$plot_hc_unselect, ".")
})
observeEvent(input$legendClicked, {
outputText <<- paste0("You clicked into the legend and selected series ", input$legendClicked, ".")
})
output$text <- renderText({
outputText
})
output$temptable <- renderDataTable(
if (length(input$canvasClicked[2])>0) {
if (curre()!=lstre())
aa %>% filter(b==input$canvasClicked[2])
else {
aa
}
}
else {aa}
)
}
shinyApp(ui, server)
解决方案
推荐阅读
- java - Javafx Textfield 设置对象的名称
- android - 为什么在添加依赖项后从导入行重定向时 http.dart 文件显示为空,而且 http.get(url) 方法未定义
- python - 除了中间部分,如何左右切片列表?
- python - 将 JSON/字典转换为数据框
- laravel - Laravel - Eloquent sync() 向每一行插入相同的值
- visual-studio-2017 - 是
和 标签应该适用于 C# 代码片段? - php - laravel 中的数组合并
- asp.net-core - Add Web API To .Net Core Console Application
- c# - String to 7-bit ASCII
- python - 我想知道如何计算图像中颜色的百分比