【问题标题】:Adding custom Tooltip in R amCharts在 R amCharts 中添加自定义工具提示
【发布时间】:2018-03-10 09:37:22
【问题描述】:

我在 R 中使用名为 amCharts 的 JS 库进行交互式可视化,如下所示。

在我的情节中,我想在工具提示中使用自定义 HTML 文本(即气球)。所以尝试使用以下代码:

library(rAmCharts)
Dat = data.frame(x = paste(as.character(-10:10), "%", sep = ""), y = -10:10,
                    z_Balloon = paste("<p style='font-size: 120%; font-weight: bold; margin-bottom: 15px;''></p>
                                          <table>
                                            <tr><th>People Name</th></tr>\
                                            <tr><td>", -10:10, "</td></tr></table>", sep = ""))

###However for '0' there shall not be any Balloon text
Dat[11, 3] = "<NA>"

amPlot(x = Dat$x, y = Dat$y, type = 'line', fill_alphas = .5, balloonText = Dat$z_Balloon)

但是,如您所见,我的代码无法更改工具提示。

如果有人引导我朝着正确的方向前进,我将不胜感激。

谢谢,

***** 根据 Marco 的建议更新**

我已更新 Marco 的代码,使其能够在 X = 0 时不显示气球:

library(rAmCharts)
Dat = data.frame(x = paste(as.character(-10:10), "%", sep = ""), y = -10:10)

balloonFunction <- htmlwidgets::JS(
    "function(item) {",
    "if (item.category != '0%') {",
    "return \'X: \' + item.category + \'<br>Y: \' + item.values.value;",
    "}",
    "else {",
    "return NULL",
    "}",
    "}")

p <- amPlot(x=Dat$x, y=Dat$y, type='line', fill_alphas=0.5) 
p@graphs[[1]]$balloonFunction <- balloonFunction
setBalloon(p, cornerRadius=10, color="white", fillColor="red", textAlign="left")

但是,我仍然无法找到一种方法来结合第三个变量来显示气球。

任何想法都将受到高度赞赏。

【问题讨论】:

    标签: r amcharts


    【解决方案1】:

    这是一个可能的解决方案:

    library(rAmCharts)
    library(purrr)
    set.seed(1)
    Dat = data.frame(x = paste(as.character(-10:10), "%", sep = ""), y = -10:10, z=rnorm(21))
    
    balloonFunction <- htmlwidgets::JS(
        'function(item) {',
        'if (item.category!="0%") { return \'X: \' + item.category + 
                                   \'<br>Y: \' + item.values.value +
                                   \'<br>Z: \' + item.dataContext[\'z\'];}',
        '}')
    
    p <- amSerialChart(categoryField = "x", precision = 2) %>%
      setDataProvider(dataProvider = Dat, keepNA = TRUE) %>%
      addGraph(valueField = "y", lineColor = "gray", fillAlphas=0.5, 
               bullet="round", lineThickness=4) %>%
      setChartCursor() %>%
      setBalloon(cornerRadius=10, color="white", fillColor="red", textAlign="left")
    p@graphs[[1]]$balloonFunction <- balloonFunction
    p@valueAxes <- list(list(title='y-axis name', position='left', axisAlpha=0.5))
    p@categoryAxis <- list(title='x-axis name', axisAlpha=0.5)
    p
    

    【讨论】:

    • 谢谢马可。关于如何停止 X = 0 的气球的任何想法?此外,有没有办法根据一些第三个变量(即在我的情况下为 z_Balloon)在气球中包含内容?
    • 谢谢马可。现在完美了吗。不过,只是一个简单的问题。如何为 Y 轴设置标题?我添加了 setValueAxis(title = 'y-axis name', titleColor = 'gray'),但它不起作用
    • 感谢 Marco,非常完美。
    猜你喜欢
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2018-04-07
    • 2012-10-21
    • 2018-04-23
    相关资源
    最近更新 更多