目前这绝对应该被认为是一种黑客攻击,但它确实有效。我们在这里面临的导致我们需要这种黑客攻击的问题是draw
标准 rCharts 模板中的函数没有为我们提供为 nvd3 添加代码位的地方,并且afterScript
因为 nvd3 不在我们的范围内draw
so 在图表渲染之前调用。另外,nvd3 工具提示只是 html,但在此处提供单击链接的问题是,鼠标悬停会被触发,并且工具提示会在我们单击它之前消失(有趣的技巧,但没有用)。因此,在这个黑客中,我们将劫持工具提示内容函数来指定单击事件函数。
我试图澄清评论,但如果这些都没有意义,请告诉我。我当然不会因为支持而从事职业:),所以我还没有建立这种技能。
library(rCharts)
age <- c(1:20)
tall <- seq(0.5, 1.90, length = 20)
name <- paste(letters[1:20], 1:20, sep = "")
#this next line is not entirely necessary if other data
#provides the part of the link address
#will also comment in the js piece below to show
#how to handle that
links <- paste0("http://example.com/",name)
df <- data.frame(age = age, tall = tall, name = name, links = links)
n1 <- nPlot(age ~ tall ,data = df, type = "scatterChart")
n1$xAxis(axisLabel = "the age")
n1$yAxis(axisLabel = "the tall", width = 50)
n1$chart(tooltipContent = "#! function(key, x, y, e ){
d3.selectAll('[class*=\"nv-path\"]').on('click',function(){
//uncomment debugger if you want to see what you have
//debugger;
window.open(d3.select(this).datum().data['point'][4].links,'_blank');
//as stated in the r code generating this
//the link address might be in the data that we already have
//window.open(
// 'http://example.com/' + d3.select(this).datum().data['point'][4].name,
// '_blank'
//);
})
//looks like the actual point is below the hover tooltip path
//if tooltips disabled we could do click on the actual points
//d3.selectAll('.nv-group circle').on('click',function(){
// debugger;
//})
var d = e.series.values[e.pointIndex];
return 'x: ' + x + ' y: ' + y + ' name: ' + d.name
} !#")
n1
我希望它有帮助。