【问题标题】:Custom tooltips for gvisTimeline in the R package googleVisR 包 googleVis 中 gvisTimeline 的自定义工具提示
【发布时间】:2015-08-17 20:40:14
【问题描述】:

问题/TL;DR

有没有人成功地为R包googleVis中的gvisTimeline定制工具提示内容googleVis

需求范围:

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

更新:

我对@9​​87654332@ 特别感兴趣,但在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: Tooltips for gvisPieChart 显示如何为 googlevis 包中的许多图表自定义工具提示,但不包括 gvisTimeline

检查 github (https://github.com/mages/googleVis/blob/master/R/gvis.R) 上的 gvis 文件表明,包括 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)

【问题讨论】:

    标签: r google-visualization


    【解决方案1】:

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

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

    1. 将原始包中的 gvis.R 保存并获取到您的工作文件夹(可在 https://cran.r-project.org/web/packages/googleVis/index.html 获取)
    2. 将此修改后的版本保存并源到同一文件夹(它向函数 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)])
      }
      
    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)
      

    对于 HTML 工具提示,只需在 datTL 中使用 HTML 代码作为字符串(而不是“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)
    

    最好的问候,

    桑德罗

    【讨论】:

    【解决方案2】:

    我对 Gvistimeline 需要互联网连接感到恼火,并使用 plotly 重新构建它:

    install.packages("vistime")
    library(plotly)
    library(vistime)
    
    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)),
                    color=c(rep("blue", 3), rep("red", 3)),
                    fontcolor=rep("white",6),
                    tooltip=c(rep("cats",6)))
    
    vistime(datTL, events="Position", groups="Name", title="Presidents of the USA")
    

    工具提示取自“工具提示”列。更多信息:https://github.com/shosaco/vistime

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 2011-09-28
      相关资源
      最近更新 更多