【问题标题】:rCharts Highcharts Add extra data to tooltiprCharts Highcharts 向工具提示添加额外数据
【发布时间】:2015-08-19 10:11:29
【问题描述】:

我有一个包含 4 列的数据框。我使用两列 (x,y) 来绘制 x 和 y。第三列(组)用于分组。而且我的第四列(猫)没有使用,但我希望它显示在工具提示中。

这是我的数据框。

library(rCharts)
df <- data.frame(x=c(1:12,1:12),
                 y=c(0.6, 0.5, 0.3, 0.3, 0.8, 0.99, 0.6, 0.5, 0.4, 0.7, 0.6, 0.8,
                         0.4, 0.5, 0.7, 0.7, 0.2, 0.01, 0.4, 0.5, 0.6, 0.3, 0.4, 0.2),
                 group=c(rep("group1",12),rep("group2",12)))

df$cat <- c(rep(c(rep("A",6),rep("B",6)),2))

我使用rCharts中的hPlot函数来创建highcharts条形图。

p <- hPlot(x = "x", y = "y", data = df, type = c("column"),group="group")
p$addParams(dom = "plot1")
p$tooltip(borderWidth=0,
           headerFormat="<span style='font-size: 10px'><b>{point.key}</b></span><br/>",
           followPointer=TRUE,
           followTouchMove=TRUE,
           shared = FALSE)

Here,它解释了将额外的数据添加到系列中。似乎直截了当,但我无法让它工作。我已经使用来自jsonlitetoJSON 和来自RJSONIOtoJSONArray 尝试了各种事情。但是,它拒绝工作。这是问题的第一部分。

第二部分是使用格式化程序在数据进入后实际显示工具提示。 highcharts reference 建议:

    tooltip: {
        formatter: function () {
            return 'The value for <b>' + this.x +
                '</b> is <b>' + this.y + '</b>';
        }
    }

如何在 R 中做到这一点?

#idea1. maybe like this?
p$tooltip(formatter=function () {
  return 'The value for <b>' + this.x +
    '</b> is <b>' + this.y + '</b>';
})

#idea2. or like this?
p$tooltip(formatter="function () {
  return 'The value for <b>' + this.x +
    '</b> is <b>' + this.y + '</b>';
}")

#idea3. or perhaps like this?
p$tooltip(formatter=function () {
  "return 'The value for <b>' + this.x +
    '</b> is <b>' + this.y + '</b>';"
})

无论如何,它们都不起作用。所以,如果有人有任何见解,我想知道。非常感谢。

PS:someone else before 发布了同样的问题,但没有完整的答案。

【问题讨论】:

标签: r highcharts rcharts


【解决方案1】:

,您好,您必须将格式化程序函数放在“#!...!#”之间,如下所示:

p$tooltip(formatter="#!function () {
  return 'The value for <b>' + this.x +
    '</b> is <b>' + this.y + '</b>';
}!#")

@Ramnath 可能在某处回答了类似的问题。

【讨论】:

    【解决方案2】:

    所以,hPlot() 不能用于在工具提示中显示其他变量。请参阅下面的工作示例:

    library(rCharts)
    
    d <- data.frame(x=c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
                    y=c(0.7,0.8,0.7,0.3,0.5,0.2,0.4,0.4,0.5,0.5,
                        0.3,0.2,0.3,0.7,0.5,0.8,0.6,0.6,0.5,0.7),
                    type=factor(c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B")),
                    pop=c("Group 1","Group 1","Group 1","Group 1","Group 1","Group 1","Group 2","Group 2","Group 2","Group 2",
                          "Group 1","Group 1","Group 1","Group 1","Group 1","Group 1","Group 2","Group 2","Group 2","Group 2"))
    
    hp <- hPlot(x = "x", y = "y", data = d, type = c("column"), group = "type")
    hp$plotOptions(column = list(stacking = "normal", pointPadding = 0, groupPadding = 0, borderWidth = 0))
    hp$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
              formatter = "#! function(){return this.point.x + '<br>' + this.point.pop + '<br>' + this.point.type + ': ' + this.point.y;} !#")
    hp
    

    在上面使用hPlot() 的示例中,附加变量type 无法在工具提示中显示,因为数组未命名。

    在下面的示例中,使用相同的数据,而不是使用hPlot(),绘图是手动构建的。此处工具提示有效,因为数组已命名。以hm$series.... 开头的行有names=T

    hm <- rCharts:::Highcharts$new()
    dlev <- levels(d$type)
    for(i in 1:length(dlev))
    {
      hm$series(data = toJSONArray2(d[d$type==dlev[i],,drop=F], json = F,names=T), name = dlev[i],type = c("column"), marker = list(radius = 3))
    }
    hm$plotOptions(column = list(stacking = "normal", pointPadding = 0, groupPadding = 0, borderWidth = 0))
    hm$tooltip(borderWidth=0, followPointer=TRUE, followTouchMove=TRUE, shared = FALSE,
              formatter = "#! function(){return this.point.x + '<br>' + this.point.pop + '<br>'+ this.point.type + ': ' + this.point.y;} !#")
    hm
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2022-07-14
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多