【问题标题】:Add Unique Links to all Data Points in Graph with rCharts使用 rCharts 为图形中的所有数据点添加唯一链接
【发布时间】:2014-06-25 13:03:28
【问题描述】:

我正在使用 rCharts 创建一个散点图,显示我随时间计算的评分。我有关于每个单独数据点(评级)的更多信息,并希望图表上的每个数据点链接到一个唯一页面,其中包含有关该特定数据点的更多信息。

例如:我希望能够将鼠标悬停在图表上的第一个数据点上并单击它以转到提供有关该评级或数据点的更多信息的特定页面 (http://www.example.com/info?id=1)。每个数据点都有一个我想链接到的唯一 id 和唯一 url。

这是我用来生成图表的代码

library(rCharts)
age <- c(1:20)
tall <- seq(0.5, 1.90, length = 20)
name <- paste(letters[1:20], 1:20, sep = "")
df <- data.frame(age = age, tall = tall, name = name)
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 ){ 
  var d = e.series.values[e.pointIndex];
  return 'x: ' + x + '  y: ' + y + ' name: ' + d.name
} !#")
n1

【问题讨论】:

  • 我知道它可以与 D3 一起使用,但我不是 javascript 程序员!对不起
  • 是的,有可能。我会尝试想出一种方法来攻击它。

标签: r rcharts


【解决方案1】:

现在绝对应该将其视为一种 hack,但它确实有效。我们在这里面临的导致我们需要此 hack 的问题是标准 rCharts 模板中的 draw 函数没有为我们提供为 nvd3 添加代码位的地方,并且 nvd3 的 afterScript 不在我们的 @987654323 范围内@so 在图表呈现之前被调用。此外,nvd3 工具提示只是 html,但在此处提供单击链接的问题是触发了鼠标悬停,并且工具提示在我们单击它之前就消失了(有趣的技巧但没用)。因此,在这个 hack 中,我们将劫持工具提示内容函数来指定一个点击事件函数。

我试图澄清 cmets,但如果这些都没有意义,请告诉我。我当然不会因为支持而成为职业:),所以我还没有建立起那种技能。

  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

希望对你有帮助。

【讨论】:

  • 非常感谢你,这正是我想要的!再次感谢您的明确解释:)
  • 太好了,如果我能以其他方式提供帮助,请告诉我。与往常一样,如果可能的话,我们喜欢看到 rCharts 的创作。 n1$publish()
  • 很高兴,我将创建一个 github 页面并发布它(这将是一个闪亮的应用程序)我会随时通知您!
猜你喜欢
  • 2012-08-19
  • 2023-02-06
  • 1970-01-01
  • 2012-01-01
  • 2016-11-21
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多