1

此示例展示了如何使用 javascript 向 rPlot 添加工具提示:rPlot 工具提示问题 此示例展示了如何将点击事件添加到 hPlot(highcharts): https ://github.com/ramnathv/rCharts/blob/master/inst/库/highcharts/examples.R

我想让 rPlot 执行与 hPlot 类似的 on.click 事件,但无法找出使用 rPlot/polycharts 分配它的正确方法。

Polychart 示例(成功应用工具提示):

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 示例(成功创建警报弹出窗口):

 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

下面是我当前绘制但不触发点击事件的代码:

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

当前使用 rCharts v0.4.2: 包:rCharts 类型:包标题:使用 Polycharts.js 的交互式图表版本:0.4.2 日期:2013-04-09

4

1 回答 1

2

每个 javascript 图表库都有自己的处理机制,包括点击事件。所以一般来说,试图将方法从一个库复制到另一个库是行不通的。幸运的是,polychart有一种支持点击处理程序的机制。这是一个最小的例子。我实际上是在添加一个 javascript 片段,使用afterScript它将处理程序添加到图表中。polycharts 中用于交互处理程序的文档非常薄,因此要做任何更有意义的事情,您必须深入研究它们的源代码或查看它们的示例。

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>    
")

要完成这项工作,您需要dev安装rCharts

install.packages('base64enc') # dependency
devtools::install_github("ramnathv/rCharts@dev")
于 2014-04-18T14:21:50.820 回答