R包googleVis中gvisTimeline的自定义工具提示

Mar*_*ley 5 r google-visualization

问题/ TL; DR

有没有人成功定制gvisTimelineR包中的工具提示内容googleVis

需要范围:

  1. 用解释性文本替换工具提示
  2. 自定义HTML工具提示,la https://google-developers.appspot.com/chart/interactive/docs/customizing_tooltip_content_875a2af27d7f8cce657119d51bedda48.frame?hl=en&redesign=true

更新:

我特别感兴趣,gvisTimeline但是在googleVis包的其他图表中有很多关于工具提示的存根问题.我正在将这些问题整理到这个问题中供我自己参考,并试图为所有人提供一个有用的资源:

细节

Google Charts文档清楚地表明,工具提示可以针对时间轴(但不是某些图表)进行自定义:https://developers.google.com/chart/interactive/docs/gallery/timelinehttps://developers.google.com/ chart/interactive/docs/customizing_tooltip_content.

角色晕影 - https://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html - 此处突出显示Shiny - googlevis:gvisPieChart的工具提示显示如何为许多图表自定义工具提示googlevis包但不包括gvisTimeline.

检查gvisgithub上的文件(https://github.com/mages/googleVis/blob/master/R/gvis.R)表明包含的任何变量tooltip都将发送到Google Chart API.盲目地我试图将工具提示包括在一个gvisTimeline情节中,如下所示,但无济于事:

datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
                    Name=c("Washington", "Adams", "Jefferson",
                           "Adams", "Jefferson", "Burr"),
                    start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
                                          "1801-02-03"),2)),
                    end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
                                        "1809-02-03"),2)),
                    Position.html.tooltip=c(rep("cats",6)))

Timeline <- gvisTimeline(data=datTL, 
                         rowlabel="Name",
                         barlabel="Position",
                         start="start", 
                         end="end")
plot(Timeline)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

sho*_*aco 3

我在绊倒https://github.com/mages/googleVis/issues/34后发现了这一点

以下是实现这一目标的方法。它基本上包括对 gVisTimeline 调用的修改,以将工具提示移交给函数的生成。

  1. 将原始包中的 gvis.R 保存并源到您的工作文件夹(可在https://cran.r-project.org/web/packages/googleVis/index.html获取)
  2. 将此修改后的版本保存并来源到同一文件夹(它将参数“tooltips”添加到函数 gvisCheckTimelineData 和 gvisTimeline):

    gvisTimeline <- function(data, rowlabel="", barlabel="", tooltip="", start="",end="", options=list(), chartid){
      my.type <- "Timeline"
      dataName <- deparse(substitute(data))
      my.options <- list(gvis=modifyList(list(width=600, height=200),options), dataName=dataName,data=list(rowlabel=rowlabel, barlabel=barlabel,tooltip=tooltip, start=start, end=end,allowed=c("number", "string", "date", "datetime"))
      )
      checked.data <- gvisCheckTimelineData(data, rl=rowlabel, bl=barlabel, tt=tooltip, start=start, end=end)
      output <- gvisChart(type=my.type, checked.data=checked.data, options=my.options,chartid=chartid, package="timeline") 
      return(output)
    }
    
    gvisCheckTimelineData <- function(data, rl, bl, tt, start, end){
      if(any(c(rl, bl, tt, start, end) %in% ""))
            return(data)
      else  
            return(data[, c(rl, bl, tt, start, end)])
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将工具提示添加到时间线输入(必须称为 x.tooltips,其中 x 是事件或条标签向量),并将工具提示参数添加到 gVisTimeline 函数。加载 RJSONIO 包(googleVis 中的函数需要)和 googleVis 并享受您的工具提示:

    library(googleVis)
    library(RJSONIO)
    
    source("gvis_orig.R")
    source("gvis_mod_for_tooltips.R")
    
    datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
                        Name=c("Washington", "Adams", "Jefferson",
                               "Adams", "Jefferson", "Burr"),
                        start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
                                              "1801-02-03"),2)),
                        end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
                                            "1809-02-03"),2)),
                        Position.html.tooltip=c(rep("cats",6)))
    
    Timeline <- gvisTimeline(datTL, 
                             rowlabel="Name",
                             barlabel="Position",
                             start="start", 
                             end="end",
                             tooltip="Position.html.tooltip")
    plot(Timeline)
    
    Run Code Online (Sandbox Code Playgroud)

最小的例子

对于 HTML 工具提示,只需使用 HTML 代码作为 datTL 中的字符串(而不是“cats”)并添加选项行options=list(tooltip="{isHtml:'true'}")gVisTimeline调用:

    library(googleVis)
    library(RJSONIO)

    source("gvis_orig.R")
    source("gvis_mod_for_tooltips.R")

    datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),
                        Name=c("Washington", "Adams", "Jefferson",
                               "Adams", "Jefferson", "Burr"),
                        start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
                                              "1801-02-03"),2)),
                        end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
                                            "1809-02-03"),2)),
                        Position.html.tooltip=c(rep('<a href="http://www.r-project.com"><img src="http://www.r-project.org/Rlogo.jpg" alt="R logo" /></a>',6)))

    Timeline <- gvisTimeline(datTL, 
                             rowlabel="Name",
                             barlabel="Position",
                             start="start", 
                             end="end",
                             tooltip="Position.html.tooltip",
                             options=list(tooltip="{isHtml:'true'}"))
    plot(Timeline)
Run Code Online (Sandbox Code Playgroud)

HTML 工具提示

此致,

桑德罗