【问题标题】:Add values to rCharts hPlot tooltip将值添加到 rCharts hPlot 工具提示
【发布时间】:2013-08-06 17:36:11
【问题描述】:

我想通过 rCharts 为标准的 Highcharts 工具提示添加一些额外的值。示例代码:

require(rCharts)
df <- data.frame(x = c(1:5), y = c(5:1), 
             z = c("A", "B", "C", "D", "E"),
             name = c("K", "L", "M", "N", "O"))
h1 <- hPlot(x = "x", y = "y", data = df, type = "scatter", group = "z")

这会生成一个带有 x 和 y 值的工具提示。并将系列名称 z 作为标题。现在我还希望将名称值添加到工具提示中。但是我不知道这是怎么做到的。

【问题讨论】:

    标签: r highcharts rcharts


    【解决方案1】:

    rCharts 是一个很棒的软件包。但它仍然没有很好的记录(也许我错过了这一点)。我认为您需要为 tooltip 属性重新定义新的 JS 函数。 任何 JS 文字都需要包裹在 #!和 !# 。这是一个开始,但它并没有像我想象的那样工作(我认为是一个好的开始):

    h1$tooltip( formatter = "#! function() { return 'x: '     + this.point.x + 
                                                    'y: '    + this.point.y  + 
                                                    'name: '  + this.point.group; } !#")
    

    【讨论】:

    • 确实这是一个好的开始。我刚刚找到了他们网站的 highcharts 工具提示格式化程序文档。
    • @jeroen81 你的意思是某处有文档吗?如果您成功获得更好的结果,您可以添加一个链接或添加您的答案吗?
    • @pfuhlert 好的..但是你能给我更多的背景信息吗...我回答这个问题已经很长时间了...
    • 那么,真的有人知道如何将name 添加到工具提示吗?一个来源指出使用多维数组作为序列。但在这里,xyz 等都是作为一维向量传递的。
    【解决方案2】:

    几年后,我有了答案。

    看起来像hPlot() 这样的包装函数不支持额外的工具提示变量,即使使用简单的自定义格式化函数也是如此。请参阅下面基于问题数据集的工作示例。

    require(rCharts)
    # create data frame
    df <- data.frame(x = c(1:5), y = c(5:1), 
                     z = c("A", "B", "C", "D", "E"),
                     name = c("K", "L", "M", "N", "O"))
    
    # Plot using hPlot() approach
    h1 <- hPlot(x = "x", y = "y", data = df, type = "scatter", group = "z")
    h1$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
               formatter = "#! function(){return 'X: ' + this.point.x + '<br>Y: ' + this.point.y + '<br>Z: ' + this.point.z + '<br>Name: ' + this.point.name;} !#")
    h1
    

    工具提示在上面的示例中不起作用,因为数组中的变量没有命名。见str(h1)

    # Plot using manual build
    h1 <- rCharts:::Highcharts$new()
    dlev <- levels(factor(as.character(df$z)))
    for(i in 1:length(dlev))
    {
      h1$series(data = toJSONArray2(df[df$z==dlev[i],,drop=F], json = F,names=T), name = dlev[i],type = c("scatter"), marker = list(radius = 3))
    }
    h1$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
               formatter = "#! function(){return 'X: ' + this.point.x + '<br>Y: ' + this.point.y + '<br>Z: ' + this.point.z + '<br>Name: ' + this.point.name;} !#")
    h1
    

    之所以有效,是因为数组变量在以h1$series... 开头的行中使用names=T 命名。见str(h1)

    这种方法解决了工具提示问题,但命名数组可能存在其他问题。例如,它破坏了闪亮应用环境中的东西。 hPlot() 不使用命名数组一定是有原因的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2012-11-02
      • 1970-01-01
      • 2023-03-18
      • 2013-07-05
      • 2021-12-13
      相关资源
      最近更新 更多