将值添加到rCharts hPlot工具提示 [英] Add values to rCharts hPlot tooltip

查看:141
本文介绍了将值添加到rCharts hPlot工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过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作为标题。现在我还想要将名称值添加到工具提示中。然而,我不知道这是如何完成的。

解决方案

几年后,我有一个答案。
$ b

看起来像 hPlot()这些包装函数即使使用简单的自定义格式化函数也不支持其他工具提示变量。

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

#使用hPlot
h1 <-hPlot(x =x,y =y,data = df,type =scatter,group =z)
h1 $ tooltip(borderWidth = 0, Y:'+ this.point。,然后按照下面的步骤进行操作:followPointer = TRUE,followTouchMove = TRUE,shared = FALSE,
formatter =#!function(){return'X:'+ this.point.x +'< br> '+ this.point.name;}!#)
h1



这是因为数组变量是用 names = T h1 $ series ... 行中。请参阅 str(h1)



这种解决了工具提示问题,但可能还有其他问题命名数组。例如,它在闪亮的应用程序环境中破坏了事物。为什么 hPlot()不能使用指定的数组。


I would like to add some extra values to the standard Highcharts tooltip via rCharts. Example code:

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

This generates a tooltip with the x and y values. And the series name z as title. Now I also want the to add the name values to the tooltip. However I have no idea how this is done.

解决方案

After several years, I have an answer.

It seems like these wrapper functions like hPlot() does not support additional tooltip variables even with a simple custom formatter function. See working example below based on the dataset from the question.

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

Tooltips do not work in the above example because the variables in the array are not named. See 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

This works because the array variables are named using names=T in the line starting h1$series.... See str(h1).

This sort of solves the tooltip issue, but there might be other problems with the named arrays. For example, it breaks things in a shiny-app environment. There must be a reason why hPlot() does not use the named arrays.

这篇关于将值添加到rCharts hPlot工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆