rCharts Highcharts向工具提示添加额外数据

时间:2015-08-19 10:11:29

标签: r highcharts rcharts

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

这是我的数据框架。

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))

我使用hPlot中的rCharts函数来创建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,它解释了将额外数据添加到系列中。看起来很直接,但我无法让它发挥作用。我使用toJSON中的jsonlitetoJSONArray中的RJSONIO尝试了各种各样的事情。但是,它拒绝工作。这是问题的第一部分。

第二部分是使用格式化程序在数据进入后实际显示工具提示。 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发布了同样的问题,但没有完整答案。

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

因此,事实证明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

enter image description here

在上面使用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

enter image description here