【发布时间】: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")
但是,我仍然无法找到一种方法来结合第三个变量来显示气球。
任何想法都将受到高度赞赏。
【问题讨论】: