Question

This example showed how to add a tooltip using javascript to rPlot: rPlot tooltip problems This example showed how to add a click event to an hPlot (highcharts): https://github.com/ramnathv/rCharts/blob/master/inst/libraries/highcharts/examples.R

I would like to get rPlot to do a similar on.click event as hPlot, but haven't been able to figure out the proper way to assign it using rPlot/polycharts.

Polychart Example (successfully applies tooltip):

require(rCharts)
set.seed(1)
test1 <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, data = test1, 
       type = 'point',
      point = list(events = list(click = "#!function(item){ alert( 'x: ' + item.x + 
       ' y: ' + item.y + ' id: ' + item.id); }!#")),
       tooltip = "#!function(item){ return 'x: ' + item.x + 
       ' y: ' + item.y + ' id: ' + item.id }!#")
p

HighCharts Example (successfully creates alarm popup):

 require(rCharts)
 a <- hPlot(freq ~ Exer, data = plyr::count(MASS::survey, c('Sex','Exer')), type = 'bar', group = 'Sex', group.na = 'NA\'s')
a$plotOptions(bar = list(cursor = 'pointer', point = list(events = list(click = "#! function() { alert ('Category: '+ this.category +', value: '+ this.y); } !#"))))
a

Below is my current code that plots but does not trigger the clicking event:

require(rCharts)
set.seed(1)
test1 <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, data = test1, 
       type = 'point',
       point = list(events = list(click = "#! function() {alert('testMessagePleaseWork');} !#")),
       tooltip = "#!function(item){ return 'x: ' + item.x + ' y: ' + item.y + ' id: ' + item.id }!#")
p

Currently using rCharts v0.4.2: Package: rCharts Type: Package Title: Interactive Charts using Polycharts.js Version: 0.4.2 Date: 2013-04-09

Was it helpful?

Solution

Every javascript charting library has its own mechanism to handle things, including click events. So in general trying to copy the approach from one library to the other will not work. Fortunately, polychart has a mechanism for supporting click handlers. Here is a minimal example. I am essentially adding a javascript snippet using afterScript that adds the handler to the chart. The documentation in polycharts for interaction handlers is very thin, so to do anything more meaningful you will have to dive into their source code or look at their examples.

require(rCharts)
set.seed(1)
test1 <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, 
  data = test1, 
  type = 'point',
  tooltip = "#!function(item){ return 'x: ' + item.x + ' y: ' + item.y + ' id: ' + item.id }!#"
)
p$set(dom = 'chart1')
p$setTemplate(afterScript = "
  <script>
   graph_chart1.addHandler(function(type, e){
      var data = e.evtData
      if (type === 'click'){
        alert('You clicked on' + data.x.in[0] + ',' + data.y.in[0])
      }
   })
  </script>    
")

To make this work, you will need to install the dev branch of rCharts

install.packages('base64enc') # dependency
devtools::install_github("ramnathv/rCharts@dev")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top