diff --git a/inst/examples-input/category.R b/inst/examples-input/category.R index c0dbcee..8ef45eb 100644 --- a/inst/examples-input/category.R +++ b/inst/examples-input/category.R @@ -17,6 +17,17 @@ ui <- fluidPage( apexchartOutput("chart2"), verbatimTextOutput("result2") ) + ), + fluidRow( + column( + width = 6, + tags$b("Pie selection"), + apexchartOutput("chart3"), + verbatimTextOutput("result3") + ), + column( + width = 6 + ) ) ) @@ -46,6 +57,18 @@ server <- function(input, output, session) { input$month_click_mult }) + output$chart3 <- renderApexchart({ + data.frame( + answer = c("Yes", "No"), + n = c(254, 238) + ) %>% + apex(type = "pie", mapping = aes(x = answer, y = n)) %>% + set_input_category("click_pie") + }) + output$result3 <- renderPrint({ + input$click_pie + }) + } shinyApp(ui, server) \ No newline at end of file diff --git a/inst/htmlwidgets/apexcharter.js b/inst/htmlwidgets/apexcharter.js index aef6389..661cd1b 100644 --- a/inst/htmlwidgets/apexcharter.js +++ b/inst/htmlwidgets/apexcharter.js @@ -36,10 +36,19 @@ HTMLWidgets.widget({ } if (x.input.hasOwnProperty("category")) { ax_opts.chart.events.dataPointSelection = function(event, chartContext, opts) { - var data = opts.w.config.series[opts.seriesIndex].data; - var selected = opts.selectedDataPoints[0].map(function(index) { - return data[index].x; - }); + var typeLabels = ["pie", "radialBar", "donut"]; + var selected; + if (typeLabels.indexOf(opts.w.config.chart.type) > -1) { + var labels = opts.w.config.labels; + selected = opts.selectedDataPoints[0].map(function(index) { + return labels[index]; + }); + } else { + var data = opts.w.config.series[opts.seriesIndex].data; + selected = opts.selectedDataPoints[0].map(function(index) { + return data[index].x; + }); + } Shiny.setInputValue( x.input.category.inputId, selected