mrjoh3 / c3

c3 HTMLWidget Ploting
39 stars 51 forks source link

How to add Onclick event to chart? #6

Closed mishaborys closed 6 years ago

mishaborys commented 6 years ago

I want to add onclick event to chart in Shiny App. I`v read this article with c3 examples and try to do some simple app with print in console when clicked on chart. But in my case, onclick I have this error in console. Could you, please, say what I do wrong?

    library(c3)
    library(shiny)

    shinyApp(
        ui = fluidPage(
            c3Output("c3test")
        ),
        server = function(input, output, session) {
          output$c3test <- renderC3({
            pie.chart <- data.frame(sugar=20,fat=45,salt=10) %>% 
              c3(onclick="function(d,element) {
                 console.log(d.id);
          }") %>% 
                    c3_donut()
        })
        }
    )
mrjoh3 commented 6 years ago

you are very close. You just need to wrap your js function in JS. Have a look at Issue #4 had where another user had the same same problem.

c3(data, onclick = htmlwidgets::JS('function(d, element){console.log(d.id)}'))
mrjoh3 commented 6 years ago

commit e2bea1c20b56ac7affad10a42fe52d3fe8615dd6 added an example to the readme page and to the c3() functions documentation

mishaborys commented 6 years ago

Thanks a lot for fast feedback. It really help me.