此示例演示了如何使用javascript向rPlot:rPlot tooltip problems添加工具提示--本例演示了如何向hPlot (高图):https://github.com/ramnathv/rCharts/blob/master/inst/libraries/highcharts/examples.R添加单击事件
我想让rPlot做一个类似于hPlot的on.click事件,但是还没有找到使用rPlot/Poly图表分配它的正确方法。
缩略图示例(成功应用工具提示):
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 }!#")
pHighCharts示例(成功创建警报弹出):
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: Package: rCharts类型:包标题:使用Polycharts.js版本的交互式图表: 0.4.2日期: 2013-04-09
发布于 2014-04-18 14:21:50
每个javascript图表库都有自己的机制来处理事情,包括单击事件。因此,尝试将方法从一个库复制到另一个库通常是行不通的。幸运的是,polychart有一种支持单击处理程序的机制。下面是一个很小的例子。实际上,我使用afterScript添加了一个javascript片段,它将处理程序添加到图表中。用于交互处理程序的多图表文档非常薄,因此要做更有意义的事情,您必须深入了解它们的源代码或查看它们的示例。
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>
")要做到这一点,您需要安装rCharts的rCharts分支。
install.packages('base64enc') # dependency
devtools::install_github("ramnathv/rCharts@dev")https://stackoverflow.com/questions/23147487
复制相似问题