【问题标题】:rCharts::rPlot on click eventrCharts::rPlot 点击事件
【发布时间】:2014-04-18 04:43:40
【问题描述】:

这个例子展示了如何使用 javascript 向 rPlot 添加工具提示:rPlot tooltip problems 这个例子展示了如何将点击事件添加到 hPlot(highcharts): https://github.com/ramnathv/rCharts/blob/master/inst/libraries/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

【问题讨论】:

    标签: javascript r highcharts rcharts polychart


    【解决方案1】:

    每个 javascript 图表库都有自己的处理机制,包括点击事件。所以一般来说,试图将方法从一个库复制到另一个库是行不通的。幸运的是,polychart 具有支持点击处理程序的机制。这是一个最小的例子。我本质上是使用afterScript 添加一个javascript sn-p,它将处理程序添加到图表中。用于交互处理程序的 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>    
    ")
    

    要完成这项工作,您需要安装 rChartsdev 分支

    install.packages('base64enc') # dependency
    devtools::install_github("ramnathv/rCharts@dev")
    

    【讨论】:

    • 发现这个解决方案似乎不适用于我正在开发的闪亮应用程序。看起来“afterScript”从未被执行(尝试过console.log)。正在渲染绘图(带有工具提示),但没有点击事件。有什么线索还是我应该提交一份完整的报告?
    • 可以在github上提交完整的报告吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2023-03-27
    • 1970-01-01
    • 2012-12-07
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多